-
Notifications
You must be signed in to change notification settings - Fork 2
test(lockfile): add invariant — LocationRole must be set when BlockLocation is valid #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package lockfile_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/DataDog/datadog-sbom-generator/internal/utility/fileposition" | ||
| "github.com/DataDog/datadog-sbom-generator/pkg/lockfile" | ||
| "github.com/DataDog/datadog-sbom-generator/pkg/lockfile/internal/testutil" | ||
| _ "github.com/DataDog/datadog-sbom-generator/pkg/lockfile/parsers" // Register all extractors | ||
| ) | ||
|
|
||
| // TestLocationRoleSetWhenBlockLocationIsValid verifies that every extractor sets | ||
| // LocationRole whenever it also sets a valid BlockLocation. | ||
| // | ||
| // This is an invariant that the reporter relies on: vulnerability_result.go uses | ||
| // LocationRole to populate the "role" field in the emitted PackageLocation. If an | ||
| // extractor populates BlockLocation but leaves LocationRole empty, the role will be | ||
| // blank in the SBOM output even though the location is meaningful. | ||
| // | ||
| // The test walks pkg/lockfile/fixtures/, runs the registered extractor for each | ||
| // fixture file, and fails if any returned PackageDetails has a valid BlockLocation | ||
| // but an empty LocationRole. | ||
| func TestLocationRoleSetWhenBlockLocationIsValid(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| fixturesRoot := "fixtures" | ||
| context := testutil.GetTestContext() | ||
|
|
||
| // Enable all registered parsers so FindExtractor can match fixture files. | ||
| // An empty map disables every parser (FindExtractor checks enabledParsers[name] == true). | ||
| allParsers := make(map[string]bool) | ||
| for name := range lockfile.ListSupportedExtractors() { | ||
| allParsers[name] = true | ||
| } | ||
|
|
||
| err := filepath.WalkDir(fixturesRoot, func(path string, d os.DirEntry, err error) error { | ||
| if err != nil || d.IsDir() { | ||
| return err | ||
| } | ||
|
|
||
| extractor, _ := lockfile.FindExtractor(path, allParsers) | ||
| if extractor == nil { | ||
| return nil | ||
| } | ||
|
|
||
| f, err := lockfile.OpenLocalDepFile(path) | ||
| if err != nil { | ||
| return nil // skip unreadable files | ||
| } | ||
| defer f.Close() | ||
|
|
||
| packages, err := extractor.Extract(f, context) | ||
| if err != nil { | ||
| return nil // skip files that fail to parse (invalid fixtures are intentional) | ||
| } | ||
|
|
||
| for _, pkg := range packages { | ||
| if fileposition.IsFilePositionExtractedSuccessfully(pkg.BlockLocation) && pkg.LocationRole == "" { | ||
| t.Errorf( | ||
| "extractor for %q returned package %s@%s with a valid BlockLocation but empty LocationRole", | ||
| path, pkg.Name, pkg.Version, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("error walking fixtures: %v", err) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For extractors that add source-file locations via matchers, this check is bypassed because it calls
extractor.Extractdirectly. Production extraction then runs anyExtractorWithMatchermatchers (seeExtractFromFileWithContext), and those matchers are exactly where several manifestBlockLocation/LocationRolefields are populated. If a matcher regresses and starts setting a validBlockLocationwithoutLocationRole, this invariant still passes, so use the same extraction path that applies matchers for each fixture.Useful? React with 👍 / 👎.