github.com/dvyukov/gometalinter@v2.0.12-0.20181028185006-9777a28a8438+incompatible/regressiontests/gosec_test.go (about) 1 package regressiontests 2 3 import ( 4 "fmt" 5 "go/build" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "gotest.tools/fs" 13 ) 14 15 const projPath = "src/test-gosec" 16 17 func TestGosec(t *testing.T) { 18 t.Parallel() 19 20 dir := fs.NewDir(t, "test-gosec", 21 fs.WithDir("src", 22 fs.WithDir("test-gosec", 23 fs.WithFile("file.go", gosecFileErrorUnhandled("root")), 24 fs.WithDir("sub", 25 fs.WithFile("file.go", gosecFileErrorUnhandled("sub")))))) 26 defer dir.Remove() 27 28 gopath, err := filepath.EvalSymlinks(dir.Path()) 29 assert.NoError(t, err) 30 err = updateGopath(gopath) 31 assert.NoError(t, err, "should update GOPATH with temp dir path") 32 defer cleanGopath(gopath) 33 34 expected := Issues{ 35 {Linter: "gosec", Severity: "warning", Path: "file.go", Line: 3, Col: 0, Message: "Errors unhandled.,LOW,HIGH"}, 36 {Linter: "gosec", Severity: "warning", Path: "sub/file.go", Line: 3, Col: 0, Message: "Errors unhandled.,LOW,HIGH"}, 37 } 38 39 actual := RunLinter(t, "gosec", filepath.Join(gopath, projPath)) 40 assert.Equal(t, expected, actual) 41 } 42 43 func gosecFileErrorUnhandled(pkg string) string { 44 return fmt.Sprintf(`package %s 45 func badFunction() string { 46 u, _ := ErrorHandle() 47 return u 48 } 49 50 func ErrorHandle() (u string, err error) { 51 return u 52 } 53 `, pkg) 54 } 55 56 func updateGopath(dir string) error { 57 gopath := os.Getenv("GOPATH") 58 if gopath == "" { 59 gopath = build.Default.GOPATH 60 } 61 gopath += ":" + dir 62 return os.Setenv("GOPATH", gopath) 63 } 64 65 func cleanGopath(dir string) error { 66 gopath := os.Getenv("GOPATH") 67 gopath = strings.TrimSuffix(gopath, ":"+dir) 68 return os.Setenv("GOPATH", gopath) 69 }