From 05a1602cd006085b87f8a095e7c444949bd48af4 Mon Sep 17 00:00:00 2001 From: CHIKAMATSU Naohiro Date: Sun, 18 Sep 2022 16:52:31 +0900 Subject: [PATCH] Fixed unit tests failing in Windows environment --- cmd/check_test.go | 42 ++++++++++++++++++++++++++++++++++-------- cmd/remove_test.go | 16 +++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/cmd/check_test.go b/cmd/check_test.go index f189ba7..4c0beec 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -27,6 +27,39 @@ func Test_check(t *testing.T) { stdout []string }{ { + name: "not go install command in $GOBIN", + gobin: "./testdata/check_fail", + args: args{}, + want: 1, + stdout: []string{}, + }, + } + + if runtime.GOOS == "windows" { + tests = append(tests, struct { + name string + gobin string + args args + want int + stdout []string + }{ + name: "detect old version command", + gobin: "./testdata/check_success_for_windows", + args: args{}, + want: 0, + stdout: []string{ + " $ gup update gal posixer ", + "", + }, + }) + } else { + tests = append(tests, struct { + name string + gobin string + args args + want int + stdout []string + }{ name: "detect old version command", gobin: "./testdata/check_success", args: args{}, @@ -35,14 +68,7 @@ func Test_check(t *testing.T) { " $ gup update subaru gal posixer ", "", }, - }, - { - name: "not go install command in $GOBIN", - gobin: "./testdata/check_fail", - args: args{}, - want: 1, - stdout: []string{}, - }, + }) } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/cmd/remove_test.go b/cmd/remove_test.go index bb07a6a..b7e2ca3 100644 --- a/cmd/remove_test.go +++ b/cmd/remove_test.go @@ -250,12 +250,22 @@ func Test_removeLoop(t *testing.T) { t.Fatal(err) } - newFile, err := os.Create("./testdata/delete/posixer") + src := "" + dest := "" + if runtime.GOOS == "windows" { + src = "./testdata/check_success_for_windows/posixer.exe" + dest = "./testdata/delete/posixer.exe" + } else { + src = "./testdata/check_success/posixer" + dest = "./testdata/delete/posixer" + + } + newFile, err := os.Create(dest) if err != nil { t.Fatal(err) } - oldFile, err := os.Open("./testdata/check_success/posixer") + oldFile, err := os.Open(src) if err != nil { t.Fatal(err) } @@ -265,7 +275,7 @@ func Test_removeLoop(t *testing.T) { t.Fatal(err) } defer func() { - os.Remove("./testdata/delete/posixer") + os.Remove(dest) }() funcDefer, err := mockStdin(t, tt.input)