test: add golden tests for AnalyzeLibrary#2475
Closed
Conversation
Add golden tests that snapshot the output of AnalyzeLibrary() for all 35 lockfile fixtures in integration/data/lockfile/. This establishes a regression baseline before refactoring the Trivy fanal integration. - Covers all supported languages: npm, yarn, pnpm, bun, pip, pipenv, poetry, uv, bundler, cargo, composer, go mod/sum/binary, pom.xml, gradle, JAR/WAR, NuGet, conan, pubspec, mix, cocoapods, swift - Results are sorted by name+version for deterministic comparison - Run with -update flag to regenerate: go test ./scanner/... -args -update Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds golden (“snapshot”) tests for AnalyzeLibrary() to establish a regression baseline across the repository’s lockfile fixtures (spanning many ecosystems) prior to refactoring the Trivy fanal integration.
Changes:
- Introduces
TestAnalyzeLibrary_Goldenwith deterministic normalization (sorting) to stabilize output comparisons. - Adds a set of golden JSON snapshots for each lockfile fixture’s
AnalyzeLibrary()output. - Adds an
-updateflag to regenerate golden files when intended output changes.
Reviewed changes
Copilot reviewed 32 out of 35 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| scanner/analyze_golden_test.go | Golden test harness: loads fixtures, runs AnalyzeLibrary, normalizes output, and compares/updates snapshots. |
| scanner/testdata/golden/Cargo.lock.golden.json | Golden snapshot for Cargo lockfile analysis. |
| scanner/testdata/golden/Directory.Packages.props.golden.json | Golden snapshot for .NET Directory.Packages.props analysis. |
| scanner/testdata/golden/Gemfile.lock.golden.json | Golden snapshot for Bundler lockfile analysis. |
| scanner/testdata/golden/Package.resolved.golden.json | Golden snapshot for Swift Package Manager resolution file analysis. |
| scanner/testdata/golden/Pipfile.lock.golden.json | Golden snapshot for Pipenv lockfile analysis. |
| scanner/testdata/golden/Podfile.lock.golden.json | Golden snapshot for CocoaPods lockfile analysis. |
| scanner/testdata/golden/bun.lock.golden.json | Golden snapshot for Bun lockfile analysis. |
| scanner/testdata/golden/composer.lock.golden.json | Golden snapshot for Composer lockfile analysis. |
| scanner/testdata/golden/conan-v1_conan.lock.golden.json | Golden snapshot for Conan v1 lockfile analysis. |
| scanner/testdata/golden/conan-v2_conan.lock.golden.json | Golden snapshot for Conan v2 lockfile analysis. |
| scanner/testdata/golden/datacollector.deps.json.golden.json | Golden snapshot for .NET deps.json analysis. |
| scanner/testdata/golden/go.mod.golden.json | Golden snapshot for Go module (go.mod) analysis. |
| scanner/testdata/golden/go.sum.golden.json | Golden snapshot for Go sums (go.sum) analysis. |
| scanner/testdata/golden/gobinary.golden.json | Golden snapshot for Go binary analysis. |
| scanner/testdata/golden/gradle.lockfile.golden.json | Golden snapshot for Gradle lockfile analysis. |
| scanner/testdata/golden/hello-rust.golden.json | Golden snapshot for Rust binary analysis. |
| scanner/testdata/golden/juddiv3-war-3.3.5.war.golden.json | Golden snapshot for WAR/JAR contents analysis. |
| scanner/testdata/golden/log4j-core-2.13.0.jar.golden.json | Golden snapshot for JAR analysis. |
| scanner/testdata/golden/mix.lock.golden.json | Golden snapshot for Elixir mix.lock analysis. |
| scanner/testdata/golden/npm-v1_package-lock.json.golden.json | Golden snapshot for npm v1 package-lock analysis. |
| scanner/testdata/golden/npm-v2_package-lock.json.golden.json | Golden snapshot for npm v2 package-lock analysis. |
| scanner/testdata/golden/npm-v3_package-lock.json.golden.json | Golden snapshot for npm v3 package-lock analysis. |
| scanner/testdata/golden/packages.config.golden.json | Golden snapshot for NuGet packages.config analysis. |
| scanner/testdata/golden/packages.lock.json.golden.json | Golden snapshot for NuGet packages.lock.json analysis. |
| scanner/testdata/golden/pnpm-v9_pnpm-lock.yaml.golden.json | Golden snapshot for pnpm v9 lockfile analysis. |
| scanner/testdata/golden/pnpm_pnpm-lock.yaml.golden.json | Golden snapshot for pnpm lockfile analysis. |
| scanner/testdata/golden/poetry-v1_poetry.lock.golden.json | Golden snapshot for Poetry v1 lockfile analysis. |
| scanner/testdata/golden/poetry-v2_poetry.lock.golden.json | Golden snapshot for Poetry v2 lockfile analysis. |
| scanner/testdata/golden/pom.xml.golden.json | Golden snapshot for Maven pom.xml analysis. |
| scanner/testdata/golden/pubspec.lock.golden.json | Golden snapshot for Dart pubspec.lock analysis. |
| scanner/testdata/golden/requirements.txt.golden.json | Golden snapshot for pip requirements.txt analysis. |
| scanner/testdata/golden/uv.lock.golden.json | Golden snapshot for uv.lock analysis. |
| scanner/testdata/golden/wrong-name-log4j-core.jar.golden.json | Golden snapshot for “wrong-name” JAR heuristic/analysis case. |
| scanner/testdata/golden/yarn.lock.golden.json | Golden snapshot for Yarn lockfile analysis. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+118
to
+128
| func TestAnalyzeLibrary_Golden(t *testing.T) { | ||
| integrationDir := filepath.Join("..", "integration", "data", "lockfile") | ||
| goldenDir := filepath.Join("testdata", "golden") | ||
|
|
||
| for _, lf := range lockfiles { | ||
| t.Run(lf.path, func(t *testing.T) { | ||
| srcPath := filepath.Join(integrationDir, lf.path) | ||
| contents, err := os.ReadFile(srcPath) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read %s: %v", srcPath, err) | ||
| } |
| // goldenFileName converts a lockfile path to a golden file name. | ||
| // e.g. "npm-v3/package-lock.json" -> "npm-v3_package-lock.json" | ||
| func goldenFileName(lockfilePath string) string { | ||
| return strings.ReplaceAll(lockfilePath, string(os.PathSeparator), "_") + ".golden.json" |
Member
Author
|
Closed in favor of #2476 which includes these golden tests along with the fixture copy for CI compatibility and the fanal removal itself. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AnalyzeLibrary()for all 35 lockfile fixtures inintegration/data/lockfile/Coverage
All supported languages: npm (v1/v2/v3), yarn, pnpm (v8/v9), bun, pip, pipenv, poetry (v1/v2), uv, bundler, cargo, rust binary, composer, go mod/sum/binary, pom.xml, gradle, JAR/WAR, NuGet, packages.config, deps.json, Directory.Packages.props, conan (v1/v2), pubspec, mix, cocoapods, swift
Usage
Test plan
installed.jsonis not matched by any analyzer)🤖 Generated with Claude Code