2018-07-19 17:42:25 +01:00
|
|
|
package gosec_test
|
2016-11-18 04:18:31 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-25 14:11:31 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
. "github.com/onsi/ginkgo"
|
2019-04-25 14:11:31 +01:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/securego/gosec"
|
2016-11-18 04:18:31 +00:00
|
|
|
)
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
var _ = Describe("Helpers", func() {
|
2019-04-25 14:11:31 +01:00
|
|
|
Context("when listing pacakge paths", func() {
|
|
|
|
var dir string
|
|
|
|
JustBeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
dir, err = ioutil.TempDir("", "gosec")
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, err = ioutil.TempFile(dir, "test*.go")
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
JustAfterEach(func() {
|
|
|
|
err := os.RemoveAll(dir)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
It("should return the root directory as package path", func() {
|
|
|
|
paths, err := gosec.PackagePaths(dir, nil)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
Expect(paths).Should(Equal([]string{dir}))
|
|
|
|
})
|
|
|
|
It("should return the package package path", func() {
|
|
|
|
paths, err := gosec.PackagePaths(dir+"/...", nil)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
Expect(paths).Should(Equal([]string{dir}))
|
|
|
|
})
|
|
|
|
It("should exclude folder", func() {
|
|
|
|
nested := dir + "/vendor"
|
|
|
|
err := os.Mkdir(nested, 0755)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, err = os.Create(nested + "/test.go")
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
exclude, err := regexp.Compile(`([\\/])?vendor([\\/])?`)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
paths, err := gosec.PackagePaths(dir+"/...", exclude)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
Expect(paths).Should(Equal([]string{dir}))
|
|
|
|
})
|
|
|
|
It("should be empty when folder does not exist", func() {
|
|
|
|
nested := dir + "/test"
|
|
|
|
paths, err := gosec.PackagePaths(nested+"/...", nil)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
Expect(paths).Should(BeEmpty())
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|