Copilot commented on code in PR #268: URL: https://github.com/apache/skywalking-eyes/pull/268#discussion_r3036679802
########## pkg/deps/golang_test.go: ########## @@ -0,0 +1,142 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 deps_test + +import ( + "io" + "os" + "path/filepath" + "testing" + + "golang.org/x/tools/go/packages" + + "github.com/apache/skywalking-eyes/pkg/deps" + "github.com/apache/skywalking-eyes/pkg/logger" +) + +func TestMain(m *testing.M) { + logger.Log.SetOutput(io.Discard) + os.Exit(m.Run()) +} + +const ( + validGoMod = `module example.com/foo + +go 1.21 +` + noModuleDirective = `go 1.21 +` + noGoDirective = `module example.com/foo +` +) + +func TestCanResolveGoMod(t *testing.T) { + resolver := new(deps.GoModResolver) + dir := t.TempDir() + + tests := []struct { + name string + filename string + want bool + }{ + {"go.mod", "go.mod", true}, + {"go.tool.mod", "go.tool.mod", true}, + {"custom.mod", "custom.mod", true}, + {"non-.mod extension", "Cargo.toml", false}, + {"no extension", "go", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := writeTempFile(t, dir, tt.filename, validGoMod) + if got := resolver.CanResolve(path); got != tt.want { + t.Errorf("CanResolve(%q) = %v, want %v", tt.filename, got, tt.want) + } + }) + } +} + +func TestResolveGoModInvalidFile(t *testing.T) { + resolver := new(deps.GoModResolver) + config := &deps.ConfigDeps{Threshold: 75} + + tests := []struct { + name string + content string + }{ + {"missing module directive", noModuleDirective}, + {"missing go directive", noGoDirective}, + {"non-Go content", "worker_processes auto;\n"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + path := writeTempFile(t, dir, "go.mod", tt.content) + var report deps.Report + if err := resolver.Resolve(path, config, &report); err == nil { + t.Errorf("Resolve should return an error for: %v", tt.name) + } + }) + } +} + +func TestResolvePackageLicense(t *testing.T) { + resolver := new(deps.GoModResolver) + config := &deps.ConfigDeps{Threshold: 75} + + apacheLicense, err := os.ReadFile("../../LICENSE") + if err != nil { + t.Fatalf("failed to read LICENSE fixture: %v", err) + } + + t.Run("license found in module dir", func(t *testing.T) { + dir := t.TempDir() + writeTempFile(t, dir, "LICENSE", string(apacheLicense)) + + module := &packages.Module{Path: "example.com/foo", Version: "v1.0.0", Dir: dir} + var report deps.Report + if err := resolver.ResolvePackageLicense(config, module, &report); err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(report.Resolved) != 1 { + t.Fatalf("expected 1 resolved, got %d", len(report.Resolved)) + } + if report.Resolved[0].LicenseSpdxID != npmLicenseApache20 { + t.Errorf("expected %v, got %v", npmLicenseApache20, report.Resolved[0].LicenseSpdxID) + } Review Comment: This test relies on `npmLicenseApache20`, which is declared in an unrelated NPM resolver test file. That coupling is fragile (e.g., if that file is renamed, moved, or build-tagged) and makes this test harder to understand in isolation. Prefer defining a local constant (or using a literal) in this file. ########## pkg/deps/golang.go: ########## @@ -38,27 +39,60 @@ type GoModResolver struct { Resolver } +const ( + goModFileName = "go.mod" +) + +var ( + goModuleDirective = regexp.MustCompile(`(?m)^\s*module\s+\S`) + goVersionDirective = regexp.MustCompile(`(?m)^\s*go\s+\d`) + possibleLicenseFileName = regexp.MustCompile(`(?i)^LICENSE|LICENCE(\.txt)?|COPYING(\.txt)?$`) Review Comment: `possibleLicenseFileName` is missing grouping/anchoring, so it can match unintended names (e.g., any filename containing "LICENCE" anywhere) and `^LICENSE` will also match directories like `LICENSES`. Since `ResolvePackageLicense` doesn't check `info.IsDir()`, this can lead to attempting to `ReadFile` on a directory and failing license resolution. Suggest anchoring the full pattern (e.g., `^(LICENSE|LICENCE)(\.txt)?$|^COPYING(\.txt)?$`) and skipping directories before reading. ```suggestion possibleLicenseFileName = regexp.MustCompile(`(?i)^(LICENSE|LICENCE|COPYING)(\.txt)?$`) ``` ########## pkg/deps/golang.go: ########## @@ -135,6 +167,7 @@ func (resolver *GoModResolver) ResolvePackageLicense(config *ConfigDeps, module return err } + logger.Log.Debugf("\t- Found license: %v", identifier) Review Comment: Before `os.ReadFile(licenseFilePath)` earlier in this function, ensure the directory entry isn’t a directory (`info.IsDir()`), otherwise directories like `LICENSES/` can be treated as license files and cause a hard error. (The current name regex also matches prefixes like `LICENSE*`.) ########## pkg/deps/golang.go: ########## @@ -38,27 +39,60 @@ type GoModResolver struct { Resolver } +const ( + goModFileName = "go.mod" +) + +var ( + goModuleDirective = regexp.MustCompile(`(?m)^\s*module\s+\S`) + goVersionDirective = regexp.MustCompile(`(?m)^\s*go\s+\d`) + possibleLicenseFileName = regexp.MustCompile(`(?i)^LICENSE|LICENCE(\.txt)?|COPYING(\.txt)?$`) +) + func (resolver *GoModResolver) CanResolve(file string) bool { base := filepath.Base(file) logger.Log.Debugln("Base name:", base) - return base == "go.mod" + return strings.HasSuffix(base, ".mod") +} + +func validateGoModFile(file string) error { + content, err := os.ReadFile(file) + if err != nil { + return err + } + if !goModuleDirective.Match(content) || !goVersionDirective.Match(content) { + return fmt.Errorf("%v is not a valid Go module file", file) + } Review Comment: `validateGoModFile` requires both `module` and `go` directives, but the `go` directive is optional in valid `go.mod`/`*.mod` files. This will reject legitimate modules and cause dependency resolution to fail. Consider parsing with `golang.org/x/mod/modfile` (and only requiring a valid `module` directive), or relax the validation to not require a `go` directive. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
