Skip to content

Commit

Permalink
fix: Windows platform test failure
Browse files Browse the repository at this point in the history
- Avoid to check OS dependent error message (wrap them with custom message).
- Avoid use of OS dependent directory separator.
  • Loading branch information
KEINOS committed Sep 19, 2022
1 parent 797b4bd commit 8476fb6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gen2brain/beeep v0.0.0-20210529141713-5586760f0cc1
github.com/google/go-cmp v0.5.9
github.com/mattn/go-colorable v0.1.13
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peK
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
Expand Down
22 changes: 19 additions & 3 deletions internal/goutil/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -68,11 +70,21 @@ func ExampleGetPackageInformation() {
log.Fatal("example GetPackageInformation failed. The returned package information is nil")
}

// Expected package information on Linux and macOS
want := []string{
"gal",
"github.com/nao1215/gal/cmd/gal",
"github.com/nao1215/gal",
}

// On Windows, paths are missing
if runtime.GOOS == "windows" {
want = []string{
"gal", "", "",
}
}

// Actual package information
got := []string{
pkgInfo[0].Name,
pkgInfo[0].ImportPath,
Expand All @@ -82,7 +94,7 @@ func ExampleGetPackageInformation() {
if cmp.Equal(got, want) {
fmt.Println("Example GetPackageInformation: OK")
} else {
log.Fatalf("example GetPackageInformation failed. got: %v, want: %v", got, want)
log.Fatalf("example GetPackageInformation failed. got: %#v, want: %#v", got, want)
}
// Output: Example GetPackageInformation: OK
}
Expand Down Expand Up @@ -125,7 +137,12 @@ func ExampleGoBin() {
func ExampleGoVersionWithOptionM() {
// GoVersionWithOptionM returns the embedded module version information of
// the executable. `gal` in this case.
modInfo, err := goutil.GoVersionWithOptionM("../../cmd/testdata/check_success/gal")
pathFileBin := filepath.Join("..", "..", "cmd", "testdata", "check_success", "gal")
if runtime.GOOS == "windows" {
pathFileBin = filepath.Join("..", "..", "cmd", "testdata", "check_success_for_windows", "gal.exe")
}

modInfo, err := goutil.GoVersionWithOptionM(pathFileBin)
if err != nil {
log.Fatal(err)
}
Expand All @@ -138,7 +155,6 @@ func ExampleGoVersionWithOptionM() {
break
}
}

// Output: Example GoVersionWithOptionM: OK
}

Expand Down
30 changes: 24 additions & 6 deletions internal/goutil/goutil.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goutil

import (
"errors"
"fmt"
"go/build"
"os"
Expand All @@ -12,6 +11,7 @@ import (

"github.com/fatih/color"
"github.com/nao1215/gup/internal/print"
"github.com/pkg/errors"
)

// Internal variables to mock/monkey-patch behaviors in tests.
Expand Down Expand Up @@ -119,11 +119,20 @@ func (gp *GoPaths) StartDryRunMode() error {

if gp.GOBIN != "" {
if err := os.Setenv(keyGoBin, tmpDir); err != nil {
return err
// Wrap error to avoid OS dependent error message during testing.
return errors.Wrapf(
err,
"failed to set GOBIN to env variable. key: %v, value: %v",
keyGoBin, tmpDir,
)
}
} else if gp.GOPATH != "" {
if err := os.Setenv(keyGoPath, tmpDir); err != nil {
return err
return errors.Wrapf(
err,
"failed to set GOPATH to env variable. key: %v, value: %v",
keyGoPath, tmpDir,
)
}
} else {
return errors.New("$GOPATH and $GOBIN is not set")
Expand All @@ -135,18 +144,27 @@ func (gp *GoPaths) StartDryRunMode() error {
func (gp *GoPaths) EndDryRunMode() error {
if gp.GOBIN != "" {
if err := os.Setenv(keyGoBin, gp.GOBIN); err != nil {
return err
// Wrap error to avoid OS dependent error message during testing.
return errors.Wrapf(
err,
"failed to set GOBIN to env variable. key: %v, value: %v",
keyGoBin, gp.GOBIN,
)
}
} else if gp.GOPATH != "" {
if err := os.Setenv(keyGoPath, gp.GOPATH); err != nil {
return err
return errors.Wrapf(
err,
"failed to set GOPATH to env variable. key: %v, value: %v",
keyGoPath, gp.GOPATH,
)
}
} else {
return errors.New("$GOPATH and $GOBIN is not set")
}

if err := gp.removeTmpDir(); err != nil {
return fmt.Errorf("%s: %w", "temporary directory for dry run remains", err)
return errors.Wrap(err, "temporary directory for dry run remains")
}
return nil
}
Expand Down
16 changes: 7 additions & 9 deletions internal/goutil/goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// ============================================================================

func TestBinaryPathList_non_existing_path(t *testing.T) {
dummyPath := filepath.Join("/non", "existing", "path")
dummyPath := filepath.Join("non", "existing", "path")
list, err := BinaryPathList(dummyPath)

// Require to be error
Expand Down Expand Up @@ -195,7 +195,6 @@ func TestGoBin_golden(t *testing.T) {
func Test_goPath_get_from_build_default_gopath(t *testing.T) {
// Backup and defer restore
oldKeyGoPath := keyGoPath

defer func() {
keyGoPath = oldKeyGoPath
}()
Expand All @@ -205,9 +204,8 @@ func Test_goPath_get_from_build_default_gopath(t *testing.T) {
keyGoPath = t.Name()

// Assert to be equal
want := os.Getenv("HOME") + "/go"
want := build.Default.GOPATH
got := goPath()

if want != got {
t.Errorf("goPath() should return default GOPATH. got: %v, want: %v", got, want)
}
Expand Down Expand Up @@ -333,13 +331,13 @@ func TestGoPaths_EndDryRunMode_fail_if_key_not_set(t *testing.T) {
},
{
name: "case GOBIN is not empty",
expectErrMsg: "invalid argument",
expectErrMsg: "failed to set GOBIN to env variable",
reasonErr: "it should be error if field GOBIN is not empty but env key is not set",
tmpGOBIN: "dummy", tmpGOPATH: "",
},
{
name: "case GOPATH is not empty",
expectErrMsg: "invalid argument",
expectErrMsg: "failed to set GOPATH to env variable",
reasonErr: "it should be error if field GOPATH is not empty but env key is not set",
tmpGOBIN: "", tmpGOPATH: "dummy",
},
Expand Down Expand Up @@ -382,7 +380,7 @@ func TestGoPaths_EndDryRunMode_fail_to_remove_temp_dir(t *testing.T) {
}()

gp := GoPaths{
GOBIN: "dummy/",
GOBIN: "dummy",
GOPATH: "",
TmpPath: ".", // os.RemoveAll(".") will fail
}
Expand Down Expand Up @@ -464,13 +462,13 @@ func TestGoPaths_StartDryRunMode_fail_if_key_not_set(t *testing.T) {
},
{
name: "case GOBIN is not empty",
expectErrMsg: "invalid argument",
expectErrMsg: "failed to set GOBIN to env variable",
reasonErr: "it should be error if field GOBIN is not empty but env key is not set",
tmpGOBIN: "dummy", tmpGOPATH: "",
},
{
name: "case GOPATH is not empty",
expectErrMsg: "invalid argument",
expectErrMsg: "failed to set GOPATH to env variable",
reasonErr: "it should be error if field GOPATH is not empty but env key is not set",
tmpGOBIN: "", tmpGOPATH: "dummy",
},
Expand Down

0 comments on commit 8476fb6

Please sign in to comment.