Skip to content

Commit

Permalink
tests: Small helper function rename/refactor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vancluever committed Jan 8, 2019
1 parent 1ebd7bf commit 6d796ed
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
8 changes: 6 additions & 2 deletions client_option_progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"
)
Expand Down Expand Up @@ -35,15 +37,17 @@ 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)
}
}

{ // 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)
}
Expand Down
8 changes: 4 additions & 4 deletions get_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions get_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions get_hg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions get_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions get_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
32 changes: 16 additions & 16 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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")

Expand All @@ -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"

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)}
Expand Down Expand Up @@ -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)}
Expand Down
2 changes: 1 addition & 1 deletion module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 6d796ed

Please sign in to comment.