Skip to content

Commit

Permalink
feat(v2): Add include_tests flag to analyse deps in tests (google#62)
Browse files Browse the repository at this point in the history
Signed-off-by: blanet <[email protected]>
  • Loading branch information
blanet committed Apr 12, 2022
1 parent 8e88f7d commit 5cef818
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func checkMain(_ *cobra.Command, args []string) error {
return err
}

libs, err := licenses.Libraries(context.Background(), classifier, args...)
libs, err := licenses.Libraries(context.Background(), classifier, includeTests, args...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func csvMain(_ *cobra.Command, args []string) error {
return err
}

libs, err := licenses.Libraries(context.Background(), classifier, args...)
libs, err := licenses.Libraries(context.Background(), classifier, includeTests, args...)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion licenses/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func (e PackagesError) Error() string {
// A library is a collection of one or more packages covered by the same license file.
// Packages not covered by a license will be returned as individual libraries.
// Standard library packages will be ignored.
func Libraries(ctx context.Context, classifier Classifier, importPaths ...string) ([]*Library, error) {
func Libraries(ctx context.Context, classifier Classifier, includeTests bool, importPaths ...string) ([]*Library, error) {
cfg := &packages.Config{
Context: ctx,
Mode: packages.NeedImports | packages.NeedDeps | packages.NeedFiles | packages.NeedName | packages.NeedModule,
Tests: includeTests,
}

rootPkgs, err := packages.Load(cfg, importPaths...)
Expand Down
28 changes: 23 additions & 5 deletions licenses/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func TestLibraries(t *testing.T) {
}

for _, test := range []struct {
desc string
importPath string
goflags string
wantLibs []string
desc string
importPath string
goflags string
includeTests bool
wantLibs []string
}{
{
desc: "Detects direct dependency",
Expand All @@ -60,6 +61,23 @@ func TestLibraries(t *testing.T) {
"github.com/google/go-licenses/licenses/testdata/indirect",
},
},
{
desc: "Dependency in tests detected if includeTests set true",
importPath: "github.com/google/go-licenses/licenses/testdata/testlib",
includeTests: true,
wantLibs: []string{
"github.com/google/go-licenses/licenses/testdata/testlib",
"github.com/google/go-licenses/licenses/testdata/testlib.test",
"github.com/google/go-licenses/licenses/testdata/indirect",
},
},
{
desc: "Dependency in tests not detected if includeTests set false",
importPath: "github.com/google/go-licenses/licenses/testdata/testlib",
wantLibs: []string{
"github.com/google/go-licenses/licenses/testdata/testlib",
},
},
{
desc: "Build tagged package",
importPath: "github.com/google/go-licenses/licenses/testdata/tags",
Expand All @@ -75,7 +93,7 @@ func TestLibraries(t *testing.T) {
os.Setenv("GOFLAGS", test.goflags)
defer os.Unsetenv("GOFLAGS")
}
gotLibs, err := Libraries(context.Background(), classifier, test.importPath)
gotLibs, err := Libraries(context.Background(), classifier, test.includeTests, test.importPath)
if err != nil {
t.Fatalf("Libraries(_, %q) = (_, %q), want (_, nil)", test.importPath, err)
}
Expand Down
15 changes: 15 additions & 0 deletions licenses/testdata/testlib/testlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testlib
20 changes: 20 additions & 0 deletions licenses/testdata/testlib/testlib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testlib

import (
// This import should be detected if includeTests set true
_ "github.com/google/go-licenses/licenses/testdata/indirect"
)
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Prerequisites:

// Flags shared between subcommands
confidenceThreshold float64
includeTests bool
packageHelp = `
Typically, specify the Go package that builds your Go binary.
Expand All @@ -63,6 +64,7 @@ func init() {
os.Exit(1)
}
rootCmd.PersistentFlags().Float64Var(&confidenceThreshold, "confidence_threshold", 0.9, "Minimum confidence required in order to positively identify a license.")
rootCmd.PersistentFlags().BoolVar(&includeTests, "include_tests", false, "Set true to include go tests while analysing dependencies.")
}

func main() {
Expand Down
2 changes: 1 addition & 1 deletion save.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func saveMain(_ *cobra.Command, args []string) error {
return err
}

libs, err := licenses.Libraries(context.Background(), classifier, args...)
libs, err := licenses.Libraries(context.Background(), classifier, includeTests, args...)
if err != nil {
return err
}
Expand Down

0 comments on commit 5cef818

Please sign in to comment.