-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_tracker_test.go
54 lines (51 loc) · 1.35 KB
/
import_tracker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package gosec_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/securego/gosec/v2"
"github.com/securego/gosec/v2/testutils"
)
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": {"fm"}}))
})
})
})