Skip to content

Commit f936425

Browse files
authored
test: use a different name for the default config file and ensure all vulns are ignored (#2254)
This should make it a lot easier to author and maintain cmd tests without disappointing tools like scorecard and dependabot, as it means going forward we can just drop a `osv-scanner.toml` configured to ignore everything into any `testdata` fixture that has actual vulns without it impacting our tests. This is even more useful in the context of #2216 which has us report ignored vulns that weren't found, which currently happens a lot in our test suite due to how artificial everything is 😅 Previously we'd discussed making this read from an env variable which I think could be useful for others, but this starts by just making our currently-constant "config file name" string an internally public variable so that we can change it as part of the setup of our tests
1 parent ddacd76 commit f936425

File tree

29 files changed

+130
-227
lines changed

29 files changed

+130
-227
lines changed

cmd/osv-scanner/__snapshots__/main_test.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ Warning: `scan` exists as both a subcommand of OSV-Scanner and as a file on the
5050
[Test_run_SubCommands/with_no_subcommand - 1]
5151
Scanning dir ./testdata/locks-many/composer.lock
5252
Scanned <rootdir>/testdata/locks-many/composer.lock file and found 1 package
53-
Loaded filter from: <rootdir>/testdata/locks-many/osv-scanner.toml
5453
No issues found
5554

5655
---
@@ -62,7 +61,6 @@ No issues found
6261
[Test_run_SubCommands/with_scan_subcommand - 1]
6362
Scanning dir ./testdata/locks-many/composer.lock
6463
Scanned <rootdir>/testdata/locks-many/composer.lock file and found 1 package
65-
Loaded filter from: <rootdir>/testdata/locks-many/osv-scanner.toml
6664
No issues found
6765

6866
---
Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,2 @@
11
[[PackageOverrides]]
2-
name = "chownr"
3-
ecosystem = "npm"
42
ignore = true
5-
reason = "This is an intentionally vulnerable test project"
6-
7-
[[PackageOverrides]]
8-
name = "concat-stream"
9-
ecosystem = "npm"
10-
ignore = true
11-
reason = "This is an intentionally vulnerable test project"
12-
13-
[[PackageOverrides]]
14-
name = "hosted-git-info"
15-
ecosystem = "npm"
16-
ignore = true
17-
reason = "This is an intentionally vulnerable test project"
18-
19-
[[PackageOverrides]]
20-
name = "request"
21-
ecosystem = "npm"
22-
ignore = true
23-
reason = "This is an intentionally vulnerable test project"
24-
25-
[[PackageOverrides]]
26-
name = "semver"
27-
ecosystem = "npm"
28-
ignore = true
29-
reason = "This is an intentionally vulnerable test project"
30-
31-
[[PackageOverrides]]
32-
name = "tough-cookie"
33-
ecosystem = "npm"
34-
ignore = true
35-
reason = "This is an intentionally vulnerable test project"
Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,2 @@
11
[[PackageOverrides]]
2-
name = "commons-io:commons-io"
3-
ecosystem = "Maven"
42
ignore = true
5-
reason = "This is an intentionally vulnerable test project"
6-
7-
[[PackageOverrides]]
8-
name = "org.apache.httpcomponents:httpclient"
9-
ecosystem = "Maven"
10-
ignore = true
11-
reason = "This is an intentionally vulnerable test project"
12-
13-
[[PackageOverrides]]
14-
name = "org.codehaus.plexus:plexus-utils"
15-
ecosystem = "Maven"
16-
ignore = true
17-
reason = "This is an intentionally vulnerable test project"
18-
19-
[[PackageOverrides]]
20-
name = "org.jsoup:jsoup"
21-
ecosystem = "Maven"
22-
ignore = true
23-
reason = "This is an intentionally vulnerable test project"

cmd/osv-scanner/fix/testmain_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"github.com/google/osv-scanner/v2/cmd/osv-scanner/fix"
88
"github.com/google/osv-scanner/v2/cmd/osv-scanner/internal/cmd"
99
"github.com/google/osv-scanner/v2/cmd/osv-scanner/internal/testcmd"
10+
"github.com/google/osv-scanner/v2/internal/config"
1011
"github.com/google/osv-scanner/v2/internal/testlogger"
1112
"github.com/google/osv-scanner/v2/internal/testutility"
1213
)
1314

1415
func TestMain(m *testing.M) {
16+
config.OSVScannerConfigName = "osv-scanner-test.toml"
17+
1518
slog.SetDefault(slog.New(testlogger.New()))
1619
testcmd.CommandsUnderTest = []cmd.CommandBuilder{fix.Command}
1720
m.Run()

cmd/osv-scanner/scan/__snapshots__/command_test.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ No issues found
1414
[TestCommand_SubCommands/with_no_subcommand - 1]
1515
Scanning dir ./testdata/locks-many/composer.lock
1616
Scanned <rootdir>/testdata/locks-many/composer.lock file and found 1 package
17-
Loaded filter from: <rootdir>/testdata/locks-many/osv-scanner.toml
1817
No issues found
1918

2019
---
@@ -26,7 +25,6 @@ No issues found
2625
[TestCommand_SubCommands/with_scan_subcommand - 1]
2726
Scanning dir ./testdata/locks-many/composer.lock
2827
Scanned <rootdir>/testdata/locks-many/composer.lock file and found 1 package
29-
Loaded filter from: <rootdir>/testdata/locks-many/osv-scanner.toml
3028
No issues found
3129

3230
---
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[PackageOverrides]]
2+
ignore = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[PackageOverrides]]
2+
ignore = true

cmd/osv-scanner/scan/image/testmain_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"github.com/google/osv-scanner/v2/cmd/osv-scanner/internal/cmd"
88
"github.com/google/osv-scanner/v2/cmd/osv-scanner/internal/testcmd"
99
"github.com/google/osv-scanner/v2/cmd/osv-scanner/scan/image"
10+
"github.com/google/osv-scanner/v2/internal/config"
1011
"github.com/google/osv-scanner/v2/internal/testlogger"
1112
"github.com/google/osv-scanner/v2/internal/testutility"
1213
)
1314

1415
func TestMain(m *testing.M) {
16+
config.OSVScannerConfigName = "osv-scanner-test.toml"
17+
1518
slog.SetDefault(slog.New(testlogger.New()))
1619
testcmd.CommandsUnderTest = []cmd.CommandBuilder{image.Command}
1720
m.Run()

cmd/osv-scanner/scan/source/__snapshots__/command_test.snap

Lines changed: 50 additions & 48 deletions
Large diffs are not rendered by default.

cmd/osv-scanner/scan/source/command_test.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,63 +29,63 @@ func TestCommand(t *testing.T) {
2929
// one specific supported sbom with vulns
3030
{
3131
Name: "folder of supported sbom with vulns",
32-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/sbom-insecure/"},
32+
Args: []string{"", "source", "./testdata/sbom-insecure/"},
3333
Exit: 1,
3434
},
3535
// one specific supported sbom with only unimportant
3636
{
3737
Name: "folder of supported sbom with only unimportant",
38-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/sbom-insecure/only-unimportant.spdx.json"},
38+
Args: []string{"", "source", "./testdata/sbom-insecure/only-unimportant.spdx.json"},
3939
Exit: 0,
4040
},
4141
// one specific supported sbom with only unimportant but with --all-vulns
4242
{
4343
Name: "folder of supported sbom with only unimportant",
44-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--all-vulns", "./testdata/sbom-insecure/only-unimportant.spdx.json"},
44+
Args: []string{"", "source", "--all-vulns", "./testdata/sbom-insecure/only-unimportant.spdx.json"},
4545
Exit: 1,
4646
},
4747
// one specific supported sbom with vulns
4848
{
4949
Name: "one specific supported sbom with vulns",
50-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--sbom", "./testdata/sbom-insecure/alpine.cdx.xml"},
50+
Args: []string{"", "source", "--sbom", "./testdata/sbom-insecure/alpine.cdx.xml"},
5151
Exit: 1,
5252
},
5353
{
5454
Name: "one specific supported sbom with vulns using -L flag",
55-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "./testdata/sbom-insecure/alpine.cdx.xml"},
55+
Args: []string{"", "source", "-L", "./testdata/sbom-insecure/alpine.cdx.xml"},
5656
Exit: 1,
5757
},
5858
// one specific supported sbom with vulns and invalid PURLs
5959
{
6060
Name: "one specific supported sbom with invalid PURLs",
61-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--sbom", "./testdata/sbom-insecure/bad-purls.cdx.xml"},
61+
Args: []string{"", "source", "--sbom", "./testdata/sbom-insecure/bad-purls.cdx.xml"},
6262
Exit: 0,
6363
},
6464
{
6565
Name: "one specific supported sbom with invalid PURLs using -L flag",
66-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "./testdata/sbom-insecure/bad-purls.cdx.xml"},
66+
Args: []string{"", "source", "-L", "./testdata/sbom-insecure/bad-purls.cdx.xml"},
6767
Exit: 0,
6868
},
6969
// one specific supported sbom with duplicate PURLs
7070
{
7171
Name: "one specific supported sbom with duplicate PURLs",
72-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--sbom", "./testdata/sbom-insecure/with-duplicates.cdx.xml"},
72+
Args: []string{"", "source", "--sbom", "./testdata/sbom-insecure/with-duplicates.cdx.xml"},
7373
Exit: 1,
7474
},
7575
{
7676
Name: "one specific supported sbom with duplicate PURLs using -L flag",
77-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "./testdata/sbom-insecure/with-duplicates.cdx.xml"},
77+
Args: []string{"", "source", "-L", "./testdata/sbom-insecure/with-duplicates.cdx.xml"},
7878
Exit: 1,
7979
},
8080
// one file that does not match the supported sbom file names
8181
{
8282
Name: "one file that does not match the supported sbom file names",
83-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--sbom", "./testdata/locks-many/composer.lock"},
83+
Args: []string{"", "source", "--sbom", "./testdata/locks-many/composer.lock"},
8484
Exit: 127,
8585
},
8686
{
8787
Name: "one file that does not match the supported sbom file names using -L flag",
88-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "spdx:./testdata/locks-many/composer.lock"},
88+
Args: []string{"", "source", "-L", "spdx:./testdata/locks-many/composer.lock"},
8989
Exit: 127,
9090
},
9191
// one specific unsupported lockfile
@@ -257,12 +257,12 @@ func TestCommand(t *testing.T) {
257257
},
258258
{
259259
Name: "PURL SBOM case sensitivity (api)",
260-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--format", "table", "./testdata/sbom-insecure/alpine.cdx.xml"},
260+
Args: []string{"", "source", "--format", "table", "./testdata/sbom-insecure/alpine.cdx.xml"},
261261
Exit: 1,
262262
},
263263
{
264264
Name: "PURL SBOM case sensitivity (local)",
265-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--offline", "--download-offline-databases", "--format", "table", "./testdata/sbom-insecure/alpine.cdx.xml"},
265+
Args: []string{"", "source", "--offline", "--download-offline-databases", "--format", "table", "./testdata/sbom-insecure/alpine.cdx.xml"},
266266
Exit: 1,
267267
},
268268
// Go project with an overridden go version
@@ -310,12 +310,12 @@ func TestCommand(t *testing.T) {
310310
// a bunch of requirements.txt files with different names
311311
{
312312
Name: "requirements.txt can have all kinds of names",
313-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/locks-requirements"},
313+
Args: []string{"", "source", "./testdata/locks-requirements"},
314314
Exit: 1,
315315
},
316316
{
317317
Name: "go_packages_in_osv-scanner.json_format",
318-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "osv-scanner:./testdata/locks-insecure/osv-scanner.json"},
318+
Args: []string{"", "source", "-L", "osv-scanner:./testdata/locks-insecure/osv-scanner.json"},
319319
Exit: 1,
320320
},
321321
}
@@ -723,7 +723,6 @@ func TestCommand_LockfileWithExplicitParseAs(t *testing.T) {
723723
Args: []string{
724724
"",
725725
"source",
726-
"--config=./testdata/osv-scanner-empty-config.toml",
727726
"-L",
728727
"package-lock.json:" + filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
729728
filepath.FromSlash("./testdata/locks-insecure"),
@@ -735,7 +734,6 @@ func TestCommand_LockfileWithExplicitParseAs(t *testing.T) {
735734
Args: []string{
736735
"",
737736
"source",
738-
"--config=./testdata/osv-scanner-empty-config.toml",
739737
"-L", "package-lock.json:" + filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
740738
"-L", "yarn.lock:" + filepath.FromSlash("./testdata/locks-insecure/my-yarn.lock"),
741739
filepath.FromSlash("./testdata/locks-insecure"),
@@ -747,7 +745,6 @@ func TestCommand_LockfileWithExplicitParseAs(t *testing.T) {
747745
Args: []string{
748746
"",
749747
"source",
750-
"--config=./testdata/osv-scanner-empty-config.toml",
751748
"-L", "yarn.lock:" + filepath.FromSlash("./testdata/locks-insecure/my-yarn.lock"),
752749
"-L", "package-lock.json:" + filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
753750
filepath.FromSlash("./testdata/locks-insecure"),
@@ -844,12 +841,12 @@ func TestCommand_GithubActions(t *testing.T) {
844841
tests := []testcmd.Case{
845842
{
846843
Name: "scanning osv-scanner custom format",
847-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "osv-scanner:./testdata/locks-insecure/osv-scanner-flutter-deps.json"},
844+
Args: []string{"", "source", "-L", "osv-scanner:./testdata/locks-insecure/osv-scanner-flutter-deps.json"},
848845
Exit: 1,
849846
},
850847
{
851848
Name: "scanning osv-scanner custom format output json",
852-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "osv-scanner:./testdata/locks-insecure/osv-scanner-flutter-deps.json", "--format=sarif"},
849+
Args: []string{"", "source", "-L", "osv-scanner:./testdata/locks-insecure/osv-scanner-flutter-deps.json", "--format=sarif"},
853850
Exit: 1,
854851
},
855852
}
@@ -872,7 +869,7 @@ func TestCommand_LocalDatabases(t *testing.T) {
872869
},
873870
{
874871
Name: "one specific supported sbom with vulns",
875-
Args: []string{"", "source", "--offline", "--download-offline-databases", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/sbom-insecure/postgres-stretch.cdx.xml"},
872+
Args: []string{"", "source", "--offline", "--download-offline-databases", "./testdata/sbom-insecure/postgres-stretch.cdx.xml"},
876873
Exit: 1,
877874
},
878875
{
@@ -951,7 +948,7 @@ func TestCommand_LocalDatabases_AlwaysOffline(t *testing.T) {
951948
tests := []testcmd.Case{
952949
{
953950
Name: "a bunch of different lockfiles and ecosystem",
954-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--offline", "./testdata/locks-requirements", "./testdata/locks-many"},
951+
Args: []string{"", "source", "--offline", "./testdata/locks-requirements", "./testdata/locks-many"},
955952
Exit: 127,
956953
},
957954
}
@@ -1078,12 +1075,12 @@ func TestCommand_Licenses(t *testing.T) {
10781075
},
10791076
{
10801077
Name: "When offline licenses summary cannot be printed",
1081-
Args: []string{"", "source", "--offline", "--licenses", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/locks-many/package-lock.json"},
1078+
Args: []string{"", "source", "--offline", "--licenses", "./testdata/locks-many/package-lock.json"},
10821079
Exit: 127,
10831080
},
10841081
{
10851082
Name: "When offline licenses cannot be checked",
1086-
Args: []string{"", "source", "--offline", "--licenses=MIT", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/locks-many/package-lock.json"},
1083+
Args: []string{"", "source", "--offline", "--licenses=MIT", "./testdata/locks-many/package-lock.json"},
10871084
Exit: 127,
10881085
},
10891086
{
@@ -1107,69 +1104,69 @@ func TestCommand_Transitive(t *testing.T) {
11071104
tests := []testcmd.Case{
11081105
{
11091106
Name: "scans transitive dependencies for pom.xml by default",
1110-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/maven-transitive/pom.xml"},
1107+
Args: []string{"", "source", "./testdata/maven-transitive/pom.xml"},
11111108
Exit: 1,
11121109
},
11131110
{
11141111
Name: "scans transitive dependencies by specifying pom.xml",
1115-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "pom.xml:./testdata/maven-transitive/abc.xml"},
1112+
Args: []string{"", "source", "-L", "pom.xml:./testdata/maven-transitive/abc.xml"},
11161113
Exit: 1,
11171114
},
11181115
{
11191116
Name: "scans pom.xml with non UTF-8 encoding",
1120-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "pom.xml:./testdata/maven-transitive/encoding.xml"},
1117+
Args: []string{"", "source", "-L", "pom.xml:./testdata/maven-transitive/encoding.xml"},
11211118
Exit: 1,
11221119
},
11231120
{
11241121
// Direct dependencies do not have any vulnerability.
11251122
Name: "does not scan transitive dependencies for pom.xml with offline mode",
1126-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--offline", "--download-offline-databases", "./testdata/maven-transitive/pom.xml"},
1123+
Args: []string{"", "source", "--offline", "--download-offline-databases", "./testdata/maven-transitive/pom.xml"},
11271124
Exit: 0,
11281125
},
11291126
{
11301127
// Direct dependencies do not have any vulnerability.
11311128
Name: "does not scan transitive dependencies for pom.xml with no-resolve",
1132-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--no-resolve", "./testdata/maven-transitive/pom.xml"},
1129+
Args: []string{"", "source", "--no-resolve", "./testdata/maven-transitive/pom.xml"},
11331130
Exit: 0,
11341131
},
11351132
{
11361133
Name: "scans dependencies from multiple registries",
1137-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "-L", "pom.xml:./testdata/maven-transitive/registry.xml"},
1134+
Args: []string{"", "source", "-L", "pom.xml:./testdata/maven-transitive/registry.xml"},
11381135
Exit: 1,
11391136
},
11401137
{
11411138
Name: "resolves transitive dependencies with native data source",
1142-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--data-source=native", "-L", "pom.xml:./testdata/maven-transitive/registry.xml"},
1139+
Args: []string{"", "source", "--data-source=native", "-L", "pom.xml:./testdata/maven-transitive/registry.xml"},
11431140
Exit: 1,
11441141
},
11451142
{
11461143
Name: "uses native data source for requirements.txt",
1147-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/locks-requirements/requirements.txt"},
1144+
Args: []string{"", "source", "./testdata/locks-requirements/requirements.txt"},
11481145
Exit: 1,
11491146
},
11501147
{
11511148
Name: "fall back to the offline extractor if resolution failed",
1152-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "./testdata/locks-requirements/unresolvable-requirements.txt"},
1149+
Args: []string{"", "source", "./testdata/locks-requirements/unresolvable-requirements.txt"},
11531150
Exit: 1,
11541151
},
11551152
{
11561153
Name: "does not scan transitive dependencies for requirements.txt with no-resolve",
1157-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--no-resolve", "./testdata/locks-requirements/requirements.txt"},
1154+
Args: []string{"", "source", "--no-resolve", "./testdata/locks-requirements/requirements.txt"},
11581155
Exit: 1,
11591156
},
11601157
{
11611158
Name: "does not scan transitive dependencies for requirements.txt with offline mode",
1162-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--offline", "--download-offline-databases", "./testdata/locks-requirements/requirements.txt"},
1159+
Args: []string{"", "source", "--offline", "--download-offline-databases", "./testdata/locks-requirements/requirements.txt"},
11631160
Exit: 1,
11641161
},
11651162
{
11661163
Name: "errors_with_invalid_data_source",
1167-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--data-source=github", "-L", "pom.xml:./testdata/maven-transitive/registry.xml"},
1164+
Args: []string{"", "source", "--data-source=github", "-L", "pom.xml:./testdata/maven-transitive/registry.xml"},
11681165
Exit: 127,
11691166
},
11701167
{
11711168
Name: "scan local disk transitive dependencies",
1172-
Args: []string{"", "source", "--config=./testdata/osv-scanner-empty-config.toml", "--no-resolve", "./testdata/locks-requirements/requirements-transitive.txt"},
1169+
Args: []string{"", "source", "--no-resolve", "./testdata/locks-requirements/requirements-transitive.txt"},
11731170
Exit: 1,
11741171
},
11751172
}

0 commit comments

Comments
 (0)