mirror of
https://github.com/securego/gosec.git
synced 2025-12-01 22:41:54 +02:00
Add tests to cover the import tracker from file
Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
committed by
Cosmin Cojocar
parent
5ef2beeaa6
commit
25b5a1a1ce
77
import_tracker_test.go
Normal file
77
import_tracker_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package gosec_test
|
||||
|
||||
import (
|
||||
"github.com/securego/gosec"
|
||||
"github.com/securego/gosec/testutils"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Import Tracker", func() {
|
||||
Context("when tracking a file", func() {
|
||||
It("should parse the imports from file", func() {
|
||||
tracker := gosec.NewImportTracker()
|
||||
pkg := testutils.NewTestPackage()
|
||||
defer pkg.Close()
|
||||
pkg.AddFile("foo.go", `
|
||||
package foo
|
||||
import "fmt"
|
||||
func foo() {
|
||||
fmt.Println()
|
||||
}
|
||||
`)
|
||||
err := pkg.Build()
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
pkgs := pkg.Pkgs()
|
||||
Expect(pkgs).Should(HaveLen(1))
|
||||
files := pkgs[0].Syntax
|
||||
Expect(files).Should(HaveLen(1))
|
||||
tracker.TrackFile(files[0])
|
||||
Expect(tracker.Imported).Should(Equal(map[string]string{"fmt": "fmt"}))
|
||||
})
|
||||
It("should parse the named imports from file", func() {
|
||||
tracker := gosec.NewImportTracker()
|
||||
pkg := testutils.NewTestPackage()
|
||||
defer pkg.Close()
|
||||
pkg.AddFile("foo.go", `
|
||||
package foo
|
||||
import fm "fmt"
|
||||
func foo() {
|
||||
fm.Println()
|
||||
}
|
||||
`)
|
||||
err := pkg.Build()
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
pkgs := pkg.Pkgs()
|
||||
Expect(pkgs).Should(HaveLen(1))
|
||||
files := pkgs[0].Syntax
|
||||
Expect(files).Should(HaveLen(1))
|
||||
tracker.TrackFile(files[0])
|
||||
Expect(tracker.Imported).Should(Equal(map[string]string{"fmt": "fmt"}))
|
||||
})
|
||||
It("should not parse the init imports from file", func() {
|
||||
tracker := gosec.NewImportTracker()
|
||||
pkg := testutils.NewTestPackage()
|
||||
defer pkg.Close()
|
||||
pkg.AddFile("foo.go", `
|
||||
package foo
|
||||
import (
|
||||
"fmt"
|
||||
_ "os"
|
||||
)
|
||||
func foo() {
|
||||
fmt.Println()
|
||||
}
|
||||
`)
|
||||
err := pkg.Build()
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
pkgs := pkg.Pkgs()
|
||||
Expect(pkgs).Should(HaveLen(1))
|
||||
files := pkgs[0].Syntax
|
||||
Expect(files).Should(HaveLen(1))
|
||||
tracker.TrackFile(files[1])
|
||||
Expect(tracker.Imported).Should(Equal(map[string]string{"fmt": "fmt"}))
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user