From 6d796edce00cfc56eb60a60acd726cccd2b33b04 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Tue, 8 Jan 2019 11:01:40 -0800 Subject: [PATCH] tests: Small helper function rename/refactor This renames the internal test tempFile function to tempTestFile. It also fixes some of the cleanup calls that were added - rather than deleting the static file created only, it deletes the directory, which is actually the specific temporary data created. --- client_option_progress_test.go | 8 ++++++-- get_file_test.go | 8 ++++---- get_git_test.go | 4 ++-- get_hg_test.go | 4 ++-- get_http_test.go | 4 ++-- get_s3_test.go | 12 ++++++------ get_test.go | 32 ++++++++++++++++---------------- module_test.go | 2 +- 8 files changed, 39 insertions(+), 35 deletions(-) diff --git a/client_option_progress_test.go b/client_option_progress_test.go index da3256738..a578fed67 100644 --- a/client_option_progress_test.go +++ b/client_option_progress_test.go @@ -4,6 +4,8 @@ import ( "io" "net/http" "net/http/httptest" + "os" + "path/filepath" "sync" "testing" ) @@ -35,7 +37,8 @@ func TestGet_progress(t *testing.T) { defer s.Close() { // dl without tracking - dst := tempFile(t) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) if err := GetFile(dst, s.URL+"/file?thig=this&that"); err != nil { t.Fatalf("download failed: %v", err) } @@ -43,7 +46,8 @@ func TestGet_progress(t *testing.T) { { // tracking p := &MockProgressTracking{} - dst := tempFile(t) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) if err := GetFile(dst, s.URL+"/file?thig=this&that", WithProgress(p)); err != nil { t.Fatalf("download failed: %v", err) } diff --git a/get_file_test.go b/get_file_test.go index 54cf1d06c..94ab3c1c1 100644 --- a/get_file_test.go +++ b/get_file_test.go @@ -105,8 +105,8 @@ func TestFileGetter_dirSymlink(t *testing.T) { func TestFileGetter_GetFile(t *testing.T) { g := new(FileGetter) - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) // With a dir that doesn't exist if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil { @@ -130,8 +130,8 @@ func TestFileGetter_GetFile_Copy(t *testing.T) { g := new(FileGetter) g.Copy = true - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) // With a dir that doesn't exist if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil { diff --git a/get_git_test.go b/get_git_test.go index 0ccb6912a..ab44c8b1b 100644 --- a/get_git_test.go +++ b/get_git_test.go @@ -176,8 +176,8 @@ func TestGitGetter_GetFile(t *testing.T) { } g := new(GitGetter) - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) repo := testGitRepo(t, "file") repo.commitFile("file.txt", "hello") diff --git a/get_hg_test.go b/get_hg_test.go index 1227278c0..ee1657945 100644 --- a/get_hg_test.go +++ b/get_hg_test.go @@ -83,8 +83,8 @@ func TestHgGetter_GetFile(t *testing.T) { } g := new(HgGetter) - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) // Download if err := g.GetFile(dst, testModuleURL("basic-hg/foo.txt")); err != nil { diff --git a/get_http_test.go b/get_http_test.go index 8d01ff678..1ba60a0f8 100644 --- a/get_http_test.go +++ b/get_http_test.go @@ -210,8 +210,8 @@ func TestHttpGetter_file(t *testing.T) { defer ln.Close() g := new(HttpGetter) - dst := tempFile(t) - defer os.RemoveAll(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) var u url.URL u.Scheme = "http" diff --git a/get_s3_test.go b/get_s3_test.go index 5af4838a5..e2233a28b 100644 --- a/get_s3_test.go +++ b/get_s3_test.go @@ -63,8 +63,8 @@ func TestS3Getter_subdir(t *testing.T) { func TestS3Getter_GetFile(t *testing.T) { g := new(S3Getter) - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) // Download err := g.GetFile( @@ -82,8 +82,8 @@ func TestS3Getter_GetFile(t *testing.T) { func TestS3Getter_GetFile_badParams(t *testing.T) { g := new(S3Getter) - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) // Download err := g.GetFile( @@ -100,8 +100,8 @@ func TestS3Getter_GetFile_badParams(t *testing.T) { func TestS3Getter_GetFile_notfound(t *testing.T) { g := new(S3Getter) - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) // Download err := g.GetFile( diff --git a/get_test.go b/get_test.go index 97f562bc1..c385fb003 100644 --- a/get_test.go +++ b/get_test.go @@ -224,8 +224,8 @@ func TestGetAny_dir(t *testing.T) { } func TestGetFile(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) u := testModule("basic-file/foo.txt") if err := GetFile(dst, u); err != nil { @@ -237,8 +237,8 @@ func TestGetFile(t *testing.T) { } func TestGetFile_archive(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) u := testModule("basic-file-archive/archive.tar.gz") if err := GetFile(dst, u); err != nil { @@ -250,8 +250,8 @@ func TestGetFile_archive(t *testing.T) { } func TestGetFile_archiveChecksum(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) u := testModule( "basic-file-archive/archive.tar.gz?checksum=md5:fbd90037dacc4b1ab40811d610dde2f0") @@ -264,8 +264,8 @@ func TestGetFile_archiveChecksum(t *testing.T) { } func TestGetFile_archiveNoUnarchive(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) u := testModule("basic-file-archive/archive.tar.gz") u += "?archive=false" @@ -352,8 +352,8 @@ func TestGetFile_checksum(t *testing.T) { u := testModule("basic-file/foo.txt") + tc.Append func() { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) if err := GetFile(dst, u); (err != nil) != tc.Err { t.Fatalf("append: %s\n\nerr: %s", tc.Append, err) } @@ -432,8 +432,8 @@ func TestGetFile_checksum_from_file(t *testing.T) { for _, tc := range cases { u := checksums + "/content.txt" + tc.Append t.Run(tc.Append, func(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) if err := GetFile(dst, u); (err != nil) != tc.WantErr { t.Fatalf("append: %s\n\nerr: %s", tc.Append, err) } @@ -447,8 +447,8 @@ func TestGetFile_checksum_from_file(t *testing.T) { } func TestGetFile_checksumURL(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3" getter := &MockGetter{Proxy: new(FileGetter)} @@ -487,8 +487,8 @@ func TestGetFile_filename(t *testing.T) { } func TestGetFile_checksumSkip(t *testing.T) { - dst := tempFile(t) - defer os.Remove(dst) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3" getter := &MockGetter{Proxy: new(FileGetter)} diff --git a/module_test.go b/module_test.go index 8a2f970a1..d5e628628 100644 --- a/module_test.go +++ b/module_test.go @@ -27,7 +27,7 @@ func tempDir(t *testing.T) string { return dir } -func tempFile(t *testing.T) string { +func tempTestFile(t *testing.T) string { dir := tempDir(t) return filepath.Join(dir, "foo") }