From 9771a3f21b79a142b0e40df3c0ee995c66f8cf09 Mon Sep 17 00:00:00 2001 From: Tim Ramlot <42113979+inteon@users.noreply.github.com> Date: Fri, 2 May 2025 18:16:17 +0200 Subject: [PATCH] remove filePath function argument which can be obtained from the Library struct directly instead Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> --- licenses/library.go | 4 ++-- licenses/library_test.go | 20 +++++++------------- report.go | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/licenses/library.go b/licenses/library.go index fa59120f..cfe165d3 100644 --- a/licenses/library.go +++ b/licenses/library.go @@ -353,7 +353,7 @@ func (l *Library) String() string { // FileURL attempts to determine the URL for a file in this library using // go module name and version. -func (l *Library) FileURL(ctx context.Context, cl *source.Client, filePath string) (string, error) { +func (l *Library) FileURL(ctx context.Context, cl *source.Client) (string, error) { if l == nil { return "", fmt.Errorf("library is nil") } @@ -393,7 +393,7 @@ func (l *Library) FileURL(ctx context.Context, cl *source.Client, filePath strin remote.SetCommit("HEAD") klog.Warningf("module %s has empty version, defaults to HEAD. The license URL may be incorrect. Please verify!", m.Path) } - relativePath, err := filepath.Rel(m.Dir, filePath) + relativePath, err := filepath.Rel(m.Dir, l.LicenseFile) if err != nil { return "", wrap(err) } diff --git a/licenses/library_test.go b/licenses/library_test.go index da00e8ce..8076f2f9 100644 --- a/licenses/library_test.go +++ b/licenses/library_test.go @@ -221,7 +221,6 @@ func TestLibraryFileURL(t *testing.T) { for _, test := range []struct { desc string lib *Library - path string wantURL string wantErr bool }{ @@ -232,14 +231,13 @@ func TestLibraryFileURL(t *testing.T) { "github.com/google/trillian", "github.com/google/trillian/crypto", }, - LicenseFile: "/go/src/github.com/google/trillian/LICENSE", + LicenseFile: "/go/src/github.com/google/trillian/foo/README.md", module: &Module{ Path: "github.com/google/trillian", Dir: "/go/src/github.com/google/trillian", Version: "v1.2.3", }, }, - path: "/go/src/github.com/google/trillian/foo/README.md", wantURL: "https://github.com/google/trillian/blob/v1.2.3/foo/README.md", }, { @@ -249,14 +247,13 @@ func TestLibraryFileURL(t *testing.T) { "bitbucket.org/user/project/pkg", "bitbucket.org/user/project/pkg2", }, - LicenseFile: "/foo/bar/bitbucket.org/user/project/LICENSE", + LicenseFile: "/foo/bar/bitbucket.org/user/project/foo/README.md", module: &Module{ Path: "bitbucket.org/user/project", Dir: "/foo/bar/bitbucket.org/user/project", Version: "v1.2.3", }, }, - path: "/foo/bar/bitbucket.org/user/project/foo/README.md", wantURL: "https://bitbucket.org/user/project/src/v1.2.3/foo/README.md", }, { @@ -266,14 +263,13 @@ func TestLibraryFileURL(t *testing.T) { "example.com/user/project/pkg", "example.com/user/project/pkg2", }, - LicenseFile: "/foo/bar/example.com/user/project/LICENSE", + LicenseFile: "/foo/bar/example.com/user/project/foo/README.md", module: &Module{ Path: "example.com/user/project", Dir: "/foo/bar/example.com/user/project", Version: "v1.2.3", }, }, - path: "/foo/bar/example.com/user/project/foo/README.md", wantURL: "https://example.com/user/project/blob/v1.2.3/foo/README.md", }, { @@ -283,13 +279,12 @@ func TestLibraryFileURL(t *testing.T) { "github.com/google/trillian", "github.com/google/trillian/crypto", }, - LicenseFile: "/go/src/github.com/google/trillian/LICENSE", + LicenseFile: "/go/src/github.com/google/trillian/foo/README.md", module: &Module{ Path: "github.com/google/trillian", Dir: "/go/src/github.com/google/trillian", }, }, - path: "/go/src/github.com/google/trillian/foo/README.md", wantURL: "https://github.com/google/trillian/blob/HEAD/foo/README.md", }, { @@ -305,20 +300,19 @@ func TestLibraryFileURL(t *testing.T) { Version: "v0.23.1", }, }, - path: "/go/modcache/k8s.io/api/LICENSE", wantURL: "https://github.com/kubernetes/api/blob/v0.23.1/LICENSE", }, } { t.Run(test.desc, func(t *testing.T) { client := source.NewClient(time.Second * 20) - fileURL, err := test.lib.FileURL(context.Background(), client, test.path) + fileURL, err := test.lib.FileURL(context.Background(), client) if gotErr := err != nil; gotErr != test.wantErr { - t.Fatalf("FileURL(%q) = (_, %q), want err? %t", test.path, err, test.wantErr) + t.Fatalf("FileURL() = (_, %q), want err? %t", err, test.wantErr) } else if gotErr { return } if got, want := fileURL, test.wantURL; got != want { - t.Fatalf("FileURL(%q) = %q, want %q", test.path, got, want) + t.Fatalf("FileURL() = %q, want %q", got, want) } }) } diff --git a/report.go b/report.go index b145178b..f5dec16f 100644 --- a/report.go +++ b/report.go @@ -121,7 +121,7 @@ func reportMain(_ *cobra.Command, args []string) error { if lib.LicenseFile != "" { group.Go(func() error { - url, err := lib.FileURL(gctx, client, lib.LicenseFile) + url, err := lib.FileURL(gctx, client) if err == nil { reportData[idx].LicenseURL = url } else {