From d453c12742e992d672fcf3e61b7a5ed35391c4b0 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sun, 19 Feb 2023 00:43:26 +0200 Subject: Replace deprecated ioutil with io and os https://pkg.go.dev/io/ioutil is deprecated since Go 1.16. --- deploy/deploy.go | 3 +-- deploy/deploy_test.go | 69 ++++++++++++++------------------------------------- 2 files changed, 19 insertions(+), 53 deletions(-) (limited to 'deploy') diff --git a/deploy/deploy.go b/deploy/deploy.go index f0a4e0178..5675d7aa0 100644 --- a/deploy/deploy.go +++ b/deploy/deploy.go @@ -24,7 +24,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "mime" "os" "path/filepath" @@ -403,7 +402,7 @@ func (lf *localFile) Reader() (io.ReadCloser, error) { // We've got the gzipped contents cached in gzipped. // Note: we can't use lf.gzipped directly as a Reader, since we it discards // data after it is read, and we may read it more than once. - return ioutil.NopCloser(bytes.NewReader(lf.gzipped.Bytes())), nil + return io.NopCloser(bytes.NewReader(lf.gzipped.Bytes())), nil } // Not expected to fail since we did it successfully earlier in newLocalFile, // but could happen due to changes in the underlying filesystem. diff --git a/deploy/deploy_test.go b/deploy/deploy_test.go index a92649b74..5c436abf2 100644 --- a/deploy/deploy_test.go +++ b/deploy/deploy_test.go @@ -23,7 +23,6 @@ import ( "crypto/md5" "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -407,7 +406,7 @@ func TestLocalFile(t *testing.T) { if err != nil { t.Fatal(err) } - gotContent, err := ioutil.ReadAll(r) + gotContent, err := io.ReadAll(r) if err != nil { t.Fatal(err) } @@ -420,7 +419,7 @@ func TestLocalFile(t *testing.T) { if err != nil { t.Fatal(err) } - gotContent, err = ioutil.ReadAll(r) + gotContent, err = io.ReadAll(r) if err != nil { t.Fatal(err) } @@ -520,47 +519,35 @@ type fsTest struct { // 1. An in-memory afero.Fs paired with an in-memory Go CDK bucket. // 2. A filesystem-based afero.Fs paired with an filesystem-based Go CDK bucket. // It returns the pair of tests and a cleanup function. -func initFsTests() ([]*fsTest, func(), error) { - tmpfsdir, err := ioutil.TempDir("", "fs") - if err != nil { - return nil, nil, err - } - tmpbucketdir, err := ioutil.TempDir("", "bucket") - if err != nil { - return nil, nil, err - } +func initFsTests(t *testing.T) []*fsTest { + t.Helper() + + tmpfsdir := t.TempDir() + tmpbucketdir := t.TempDir() memfs := afero.NewMemMapFs() membucket := memblob.OpenBucket(nil) + t.Cleanup(func() { membucket.Close() }) filefs := afero.NewBasePathFs(afero.NewOsFs(), tmpfsdir) filebucket, err := fileblob.OpenBucket(tmpbucketdir, nil) if err != nil { - return nil, nil, err + t.Fatal(err) } + t.Cleanup(func() { filebucket.Close() }) tests := []*fsTest{ {"mem", memfs, membucket}, {"file", filefs, filebucket}, } - cleanup := func() { - membucket.Close() - filebucket.Close() - os.RemoveAll(tmpfsdir) - os.RemoveAll(tmpbucketdir) - } - return tests, cleanup, nil + return tests } // TestEndToEndSync verifies that basic adds, updates, and deletes are working // correctly. func TestEndToEndSync(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { local, err := initLocalFs(ctx, test.fs) @@ -643,11 +630,7 @@ func TestEndToEndSync(t *testing.T) { // TestMaxDeletes verifies that the "maxDeletes" flag is working correctly. func TestMaxDeletes(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { local, err := initLocalFs(ctx, test.fs) @@ -772,14 +755,10 @@ func TestIncludeExclude(t *testing.T) { } for _, test := range tests { t.Run(fmt.Sprintf("include %q exclude %q", test.Include, test.Exclude), func(t *testing.T) { - fsTests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + fsTests := initFsTests(t) fsTest := fsTests[1] // just do file-based test - _, err = initLocalFs(ctx, fsTest.fs) + _, err := initLocalFs(ctx, fsTest.fs) if err != nil { t.Fatal(err) } @@ -841,11 +820,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) { } for _, test := range tests { t.Run(fmt.Sprintf("include %q exclude %q", test.Include, test.Exclude), func(t *testing.T) { - fsTests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + fsTests := initFsTests(t) fsTest := fsTests[1] // just do file-based test local, err := initLocalFs(ctx, fsTest.fs) @@ -897,11 +872,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) { func TestCompression(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { local, err := initLocalFs(ctx, test.fs) @@ -956,11 +927,7 @@ func TestCompression(t *testing.T) { // attribute for matcher works. func TestMatching(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { _, err := initLocalFs(ctx, test.fs) -- cgit v1.2.3