From 25b5a1a1ce9c499926dd611ea79c7633654a4e48 Mon Sep 17 00:00:00 2001 From: Cosmin Cojocar Date: Mon, 29 Apr 2019 18:32:39 +0200 Subject: [PATCH] Add tests to cover the import tracker from file Signed-off-by: Cosmin Cojocar --- call_list_test.go | 2 +- import_tracker_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++ testutils/pkg.go | 9 ++++- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 import_tracker_test.go diff --git a/call_list_test.go b/call_list_test.go index 2f1cf60..4f1c72b 100644 --- a/call_list_test.go +++ b/call_list_test.go @@ -9,7 +9,7 @@ import ( "github.com/securego/gosec/testutils" ) -var _ = Describe("call list", func() { +var _ = Describe("Call List", func() { var ( calls gosec.CallList ) diff --git a/import_tracker_test.go b/import_tracker_test.go new file mode 100644 index 0000000..3bc08a2 --- /dev/null +++ b/import_tracker_test.go @@ -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"})) + }) + }) +}) diff --git a/testutils/pkg.go b/testutils/pkg.go index 7c7112d..0842b05 100644 --- a/testutils/pkg.go +++ b/testutils/pkg.go @@ -90,7 +90,6 @@ func (p *TestPackage) Build() error { Tests: false, } pkgs, err := packages.Load(conf, packageFiles...) - if err != nil { return err } @@ -140,3 +139,11 @@ func (p *TestPackage) Close() { } } } + +// Pkgs returns the current built packages +func (p *TestPackage) Pkgs() []*packages.Package { + if p.build != nil { + return p.build.pkgs + } + return []*packages.Package{} +}