Skip to content

Commit

Permalink
add -t to check/list/graph test dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
frapposelli committed Dec 15, 2020
1 parent dc59aff commit 37b1e1a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
11 changes: 7 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 10 additions & 8 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type dependencies struct {
nodesList map[string]bool
edges map[node][]*node
dotGraph *dot.Graph
checkTest bool
sync.RWMutex
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 37b1e1a

Please sign in to comment.