Skip to content

Commit 25b5a1a

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Add tests to cover the import tracker from file
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 5ef2bee commit 25b5a1a

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

call_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/securego/gosec/testutils"
1010
)
1111

12-
var _ = Describe("call list", func() {
12+
var _ = Describe("Call List", func() {
1313
var (
1414
calls gosec.CallList
1515
)

import_tracker_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package gosec_test
2+
3+
import (
4+
"github.com/securego/gosec"
5+
"github.com/securego/gosec/testutils"
6+
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
)
10+
11+
var _ = Describe("Import Tracker", func() {
12+
Context("when tracking a file", func() {
13+
It("should parse the imports from file", func() {
14+
tracker := gosec.NewImportTracker()
15+
pkg := testutils.NewTestPackage()
16+
defer pkg.Close()
17+
pkg.AddFile("foo.go", `
18+
package foo
19+
import "fmt"
20+
func foo() {
21+
fmt.Println()
22+
}
23+
`)
24+
err := pkg.Build()
25+
Expect(err).ShouldNot(HaveOccurred())
26+
pkgs := pkg.Pkgs()
27+
Expect(pkgs).Should(HaveLen(1))
28+
files := pkgs[0].Syntax
29+
Expect(files).Should(HaveLen(1))
30+
tracker.TrackFile(files[0])
31+
Expect(tracker.Imported).Should(Equal(map[string]string{"fmt": "fmt"}))
32+
})
33+
It("should parse the named imports from file", func() {
34+
tracker := gosec.NewImportTracker()
35+
pkg := testutils.NewTestPackage()
36+
defer pkg.Close()
37+
pkg.AddFile("foo.go", `
38+
package foo
39+
import fm "fmt"
40+
func foo() {
41+
fm.Println()
42+
}
43+
`)
44+
err := pkg.Build()
45+
Expect(err).ShouldNot(HaveOccurred())
46+
pkgs := pkg.Pkgs()
47+
Expect(pkgs).Should(HaveLen(1))
48+
files := pkgs[0].Syntax
49+
Expect(files).Should(HaveLen(1))
50+
tracker.TrackFile(files[0])
51+
Expect(tracker.Imported).Should(Equal(map[string]string{"fmt": "fmt"}))
52+
})
53+
It("should not parse the init imports from file", func() {
54+
tracker := gosec.NewImportTracker()
55+
pkg := testutils.NewTestPackage()
56+
defer pkg.Close()
57+
pkg.AddFile("foo.go", `
58+
package foo
59+
import (
60+
"fmt"
61+
_ "os"
62+
)
63+
func foo() {
64+
fmt.Println()
65+
}
66+
`)
67+
err := pkg.Build()
68+
Expect(err).ShouldNot(HaveOccurred())
69+
pkgs := pkg.Pkgs()
70+
Expect(pkgs).Should(HaveLen(1))
71+
files := pkgs[0].Syntax
72+
Expect(files).Should(HaveLen(1))
73+
tracker.TrackFile(files[1])
74+
Expect(tracker.Imported).Should(Equal(map[string]string{"fmt": "fmt"}))
75+
})
76+
})
77+
})

testutils/pkg.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ func (p *TestPackage) Build() error {
9090
Tests: false,
9191
}
9292
pkgs, err := packages.Load(conf, packageFiles...)
93-
9493
if err != nil {
9594
return err
9695
}
@@ -140,3 +139,11 @@ func (p *TestPackage) Close() {
140139
}
141140
}
142141
}
142+
143+
// Pkgs returns the current built packages
144+
func (p *TestPackage) Pkgs() []*packages.Package {
145+
if p.build != nil {
146+
return p.build.pkgs
147+
}
148+
return []*packages.Package{}
149+
}

0 commit comments

Comments
 (0)