github.com/jenkins-x/jx/v2@v2.1.155/cmd/codegen/util/go_test.go (about) 1 // +build unit 2 3 package util 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func Test_ensure_gopath_set(t *testing.T) { 16 tmpGoDir, err := ioutil.TempDir("", "jx-codegen-tests") 17 if err != nil { 18 assert.Fail(t, "unable to create test directory") 19 } 20 defer os.RemoveAll(tmpGoDir) 21 err = os.Setenv(gopath, tmpGoDir) 22 if err != nil { 23 assert.Fail(t, "unable to set env variable") 24 } 25 26 err = EnsureGoPath() 27 assert.NoError(t, err, "GOPATH should be set") 28 } 29 30 func Test_ensure_gopath_unset(t *testing.T) { 31 err := os.Setenv(gopath, "") 32 if err != nil { 33 assert.Fail(t, "unable to set env variable") 34 } 35 36 err = EnsureGoPath() 37 assert.Error(t, err, "GOPATH should not be set") 38 assert.Equal(t, "GOPATH needs to be set", err.Error()) 39 } 40 41 func Test_ensure_gopath_does_not_exist(t *testing.T) { 42 err := os.Setenv(gopath, "snafu") 43 if err != nil { 44 assert.Fail(t, "unable to set env variable") 45 } 46 47 err = EnsureGoPath() 48 assert.Error(t, err, "GOPATH should not be set") 49 assert.Equal(t, "the GOPATH directory snafu does not exist", err.Error()) 50 } 51 52 func Test_get_gopath(t *testing.T) { 53 tmpGoDir, err := ioutil.TempDir("", "jx-codegen-tests") 54 if err != nil { 55 assert.Fail(t, "unable to create test directory") 56 } 57 defer os.RemoveAll(tmpGoDir) 58 err = os.Setenv(gopath, tmpGoDir) 59 if err != nil { 60 assert.Fail(t, "unable to set env variable") 61 } 62 63 goPath := GoPath() 64 assert.Equal(t, tmpGoDir, goPath) 65 } 66 67 func Test_get_gopath_unset_env(t *testing.T) { 68 err := os.Setenv(gopath, "") 69 if err != nil { 70 assert.Fail(t, "unable to set env variable") 71 } 72 73 goPath := GoPath() 74 assert.Equal(t, "", goPath) 75 } 76 77 func Test_get_gopath_multiple_elements(t *testing.T) { 78 tmpGoDir, err := ioutil.TempDir("", "jx-codegen-tests") 79 if err != nil { 80 assert.Fail(t, "unable to create test directory") 81 } 82 defer os.RemoveAll(tmpGoDir) 83 err = os.Setenv(gopath, fmt.Sprintf("%s%sfoo", tmpGoDir, string(os.PathListSeparator))) 84 if err != nil { 85 assert.Fail(t, "unable to set env variable") 86 } 87 88 goPath := GoPath() 89 assert.Equal(t, tmpGoDir, goPath) 90 } 91 92 func Test_get_gopath_src(t *testing.T) { 93 tmpGoDir, err := ioutil.TempDir("", "jx-codegen-tests") 94 if err != nil { 95 assert.Fail(t, "unable to create test directory") 96 } 97 defer os.RemoveAll(tmpGoDir) 98 err = os.Setenv(gopath, tmpGoDir) 99 if err != nil { 100 assert.Fail(t, "unable to set env variable") 101 } 102 103 goPathSrc := GoPathSrc(tmpGoDir) 104 assert.Equal(t, filepath.Join(tmpGoDir, "src"), goPathSrc) 105 } 106 107 func Test_get_gopath_bin(t *testing.T) { 108 tmpGoDir, err := ioutil.TempDir("", "jx-codegen-tests") 109 if err != nil { 110 assert.Fail(t, "unable to create test directory") 111 } 112 defer os.RemoveAll(tmpGoDir) 113 err = os.Setenv(gopath, tmpGoDir) 114 if err != nil { 115 assert.Fail(t, "unable to set env variable") 116 } 117 118 goPathBin := GoPathBin(tmpGoDir) 119 assert.Equal(t, filepath.Join(tmpGoDir, "bin"), goPathBin) 120 }