From 37b1e1a89a9d464fb1a618fa77b401741752ea95 Mon Sep 17 00:00:00 2001 From: Fabio Rapposelli Date: Tue, 15 Dec 2020 12:17:03 +0100 Subject: [PATCH] add -t to check/list/graph test dependencies --- cli.go | 11 +++++++---- walker.go | 18 ++++++++++-------- walker_test.go | 4 ++-- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cli.go b/cli.go index 78139d9..80bb510 100644 --- a/cli.go +++ b/cli.go @@ -23,16 +23,19 @@ type cliOpts struct { type List struct { NoColor bool `long:"no-color" description:"disable colored output"` CoverageThreshold float64 `short:"c" long:"coverage" description:"coverage threshold is the minimum percentage of the file that must contain license text" default:"75"` + CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"` } type Check struct { File string `short:"f" long:"file" description:"input file" default:".wwhrd.yml"` NoColor bool `long:"no-color" description:"disable colored output"` CoverageThreshold float64 `short:"c" long:"coverage" description:"coverage threshold is the minimum percentage of the file that must contain license text" default:"75"` + CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"` } type Graph struct { - File string `short:"o" long:"output" description:"output file" default:"wwhrd-graph.dot"` + File string `short:"o" long:"output" description:"output file" default:"wwhrd-graph.dot"` + CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"` } const VersionHelp flags.ErrorType = 1961 @@ -78,7 +81,7 @@ func (g *Graph) Execute(args []string) error { log.Infof("Generating DOT graph") - dotGraph, err := GraphImports(root) + dotGraph, err := GraphImports(root, g.CheckTestFiles) if err != nil { log.Fatal(err) } @@ -125,7 +128,7 @@ func (l *List) Execute(args []string) error { return err } - pkgs, err := WalkImports(root) + pkgs, err := WalkImports(root, l.CheckTestFiles) if err != nil { return err } @@ -160,7 +163,7 @@ func (c *Check) Execute(args []string) error { return err } - pkgs, err := WalkImports(root) + pkgs, err := WalkImports(root, c.CheckTestFiles) if err != nil { return err } diff --git a/walker.go b/walker.go index 622f625..faa245e 100644 --- a/walker.go +++ b/walker.go @@ -81,6 +81,7 @@ type dependencies struct { nodesList map[string]bool edges map[node][]*node dotGraph *dot.Graph + checkTest bool sync.RWMutex } @@ -90,9 +91,10 @@ type node struct { vendor string } -func newGraph() *dependencies { +func newGraph(checkTest bool) *dependencies { var g dependencies g.nodesList = make(map[string]bool) + g.checkTest = checkTest return &g } @@ -205,7 +207,7 @@ func (g *dependencies) WalkNode(n *node) { log.Debugf("walking %q", path) // check if we need to skip this - if ok, err := shouldSkip(path, info); ok { + if ok, err := shouldSkip(path, info, g.checkTest); ok { return err } @@ -244,9 +246,9 @@ func (g *dependencies) WalkNode(n *node) { } -func WalkImports(root string) (map[string]bool, error) { +func WalkImports(root string, checkTest bool) (map[string]bool, error) { - graph := newGraph() + graph := newGraph(checkTest) rootNode := node{pkg: "root", dir: root, vendor: root} if err := graph.addNode(&rootNode); err != nil { log.Debug(err.Error()) @@ -258,9 +260,9 @@ func WalkImports(root string) (map[string]bool, error) { return graph.nodesList, nil } -func GraphImports(root string) (string, error) { +func GraphImports(root string, checkTest bool) (string, error) { - graph := newGraph() + graph := newGraph(checkTest) rootNode := node{pkg: "root", dir: root, vendor: root} if err := graph.addNode(&rootNode); err != nil { log.Debug(err.Error()) @@ -361,7 +363,7 @@ func scanDir(checker *licensecheck.Scanner, fpath string, threshold float64) str return license } -func shouldSkip(path string, info os.FileInfo) (bool, error) { +func shouldSkip(path string, info os.FileInfo, checkTest bool) (bool, error) { if info.IsDir() { name := info.Name() // check if directory is in the blocklist @@ -377,7 +379,7 @@ func shouldSkip(path string, info os.FileInfo) (bool, error) { return true, nil } // if it's a test file, skip - if strings.HasSuffix(path, "_test.go") { + if strings.HasSuffix(path, "_test.go") && !checkTest { log.Debugf("skipping %q: test file", path) return true, nil } diff --git a/walker_test.go b/walker_test.go index 85112c4..0cd4175 100644 --- a/walker_test.go +++ b/walker_test.go @@ -10,7 +10,7 @@ func TestWalkImports(t *testing.T) { dir, rm := mockGoPackageDir(t, "TestWalkImports") defer rm() - pkgs, err := WalkImports(dir) + pkgs, err := WalkImports(dir, false) assert.NoError(t, err) res := make(map[string]bool) @@ -29,7 +29,7 @@ func TestGetLicenses(t *testing.T) { dir, rm := mockGoPackageDir(t, "TestGetLicenses") defer rm() - pkgs, err := WalkImports(dir) + pkgs, err := WalkImports(dir, false) assert.NoError(t, err) lics := GetLicenses(dir, pkgs, 75)