Skip to content

Commit a4d63d0

Browse files
committed
test: make sure absolute paths work
1 parent 820dcce commit a4d63d0

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

__snapshots__/main_test.snap

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,101 @@ testdata/locks-one/yarn.lock: found 1 package
698698
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
699699

700700

701+
---
702+
703+
[TestRun_Lockfile_AbsolutePath/#00 - 1]
704+
Loaded the following OSV databases:
705+
npm (%% vulnerabilities, including withdrawn - last updated %%)
706+
707+
<rootdir>/testdata/locks-one/yarn.lock: found 1 package
708+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
709+
710+
no known vulnerabilities found
711+
712+
---
713+
714+
[TestRun_Lockfile_AbsolutePath/#00 - 2]
715+
716+
---
717+
718+
[TestRun_Lockfile_AbsolutePath/#01 - 1]
719+
Loaded the following OSV databases:
720+
RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
721+
Packagist (%% vulnerabilities, including withdrawn - last updated %%)
722+
npm (%% vulnerabilities, including withdrawn - last updated %%)
723+
724+
<rootdir>/testdata/locks-many/Gemfile.lock: found 1 package
725+
Using db RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
726+
727+
no known vulnerabilities found
728+
729+
<rootdir>/testdata/locks-many/composer.lock: found 1 package
730+
Using db Packagist (%% vulnerabilities, including withdrawn - last updated %%)
731+
732+
no known vulnerabilities found
733+
734+
<rootdir>/testdata/locks-many/yarn.lock: found 1 package
735+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
736+
737+
no known vulnerabilities found
738+
739+
---
740+
741+
[TestRun_Lockfile_AbsolutePath/#01 - 2]
742+
743+
---
744+
745+
[TestRun_Lockfile_AbsolutePath/#02 - 1]
746+
Loaded the following OSV databases:
747+
748+
<rootdir>/testdata/locks-empty/Gemfile.lock: found 0 packages
749+
750+
no known vulnerabilities found
751+
752+
<rootdir>/testdata/locks-empty/composer.lock: found 0 packages
753+
754+
no known vulnerabilities found
755+
756+
<rootdir>/testdata/locks-empty/yarn.lock: found 0 packages
757+
758+
no known vulnerabilities found
759+
760+
---
761+
762+
[TestRun_Lockfile_AbsolutePath/#02 - 2]
763+
764+
---
765+
766+
[TestRun_Lockfile_AbsolutePath/#03 - 1]
767+
Loaded the following OSV databases:
768+
npm (%% vulnerabilities, including withdrawn - last updated %%)
769+
770+
<rootdir>/testdata/locks-insecure/my-package-lock.json: found 1 package
771+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
772+
773+
ansi-html@0.0.1 is affected by the following vulnerabilities:
774+
GHSA-whgm-jr23-g3j9: Uncontrolled Resource Consumption in ansi-html (https://github.com/advisories/GHSA-whgm-jr23-g3j9)
775+
776+
1 known vulnerability found in <rootdir>/testdata/locks-insecure/my-package-lock.json
777+
778+
---
779+
780+
[TestRun_Lockfile_AbsolutePath/#03 - 2]
781+
782+
---
783+
784+
[TestRun_Lockfile_AbsolutePath/#04 - 1]
785+
{"results":[{"filePath":"<rootdir>/testdata/locks-one/yarn.lock","parsedAs":"yarn.lock","packages":[{"name":"balanced-match","version":"1.0.2","ecosystem":"npm","compareAs":"npm","vulnerabilities":[],"ignored":[]}]}]}
786+
---
787+
788+
[TestRun_Lockfile_AbsolutePath/#04 - 2]
789+
Loaded the following OSV databases:
790+
npm (%% vulnerabilities, including withdrawn - last updated %%)
791+
792+
<rootdir>/testdata/locks-one/yarn.lock: found 1 package
793+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
794+
795+
701796
---
702797

703798
[TestRun_ParseAsGlobal/#00 - 1]

main_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"regexp"
11+
"runtime"
1112
"strings"
1213
"testing"
1314

@@ -84,6 +85,33 @@ func normalizeLocalhostCalls(str string) string {
8485
return re.ReplaceAllString(str, "://localhost:9999")
8586
}
8687

88+
func pathWithoutRoot(str string) string {
89+
// Replace versions without the root as well
90+
var root string
91+
if runtime.GOOS == "windows" {
92+
root = filepath.VolumeName(str) + "\\"
93+
}
94+
95+
if strings.HasPrefix(str, "/") {
96+
root = "/"
97+
}
98+
99+
return str[len(root):]
100+
}
101+
102+
// normalizeLocalhostCalls attempts to replace uses of localhost that come from httptest.NewServer,
103+
// which will have a random port
104+
func normalizeRootDirectory(str string, cwd string) string {
105+
// file uris with Windows end up with three slashes, so we normalize that too
106+
str = strings.ReplaceAll(str, "file:///"+cwd, "file://<rootdir>")
107+
str = strings.ReplaceAll(str, cwd, "<rootdir>")
108+
109+
// Replace versions without the root as well
110+
str = strings.ReplaceAll(str, pathWithoutRoot(cwd), "<rootdir>")
111+
112+
return str
113+
}
114+
87115
func testCli(t *testing.T, tc cliTestCase) {
88116
t.Helper()
89117

@@ -107,6 +135,14 @@ func testCli(t *testing.T, tc cliTestCase) {
107135
stdout = normalizeTempDirectories(stdout, t.Name())
108136
stderr = normalizeTempDirectories(stderr, t.Name())
109137

138+
cwd, err := os.Getwd()
139+
if err != nil {
140+
t.Fatalf("failed to get current directory: %v", err)
141+
}
142+
143+
stdout = normalizeRootDirectory(stdout, cwd)
144+
stderr = normalizeRootDirectory(stderr, cwd)
145+
110146
if ec != tc.exit {
111147
t.Errorf("cli exited with code %d, not %d", ec, tc.exit)
112148
}
@@ -290,6 +326,53 @@ func TestRun_Lockfile(t *testing.T) {
290326
}
291327
}
292328

329+
func TestRun_Lockfile_AbsolutePath(t *testing.T) {
330+
t.Parallel()
331+
332+
testdataDir, err := filepath.Abs("./testdata")
333+
334+
if err != nil {
335+
t.Fatal(err)
336+
}
337+
338+
tests := []cliTestCase{
339+
{
340+
name: "",
341+
args: []string{filepath.Join(testdataDir, "locks-one")},
342+
exit: 0,
343+
},
344+
{
345+
name: "",
346+
args: []string{filepath.Join(testdataDir, "locks-many")},
347+
exit: 0,
348+
},
349+
{
350+
name: "",
351+
args: []string{filepath.Join(testdataDir, "locks-empty")},
352+
exit: 0,
353+
},
354+
// parse-as + known vulnerability exits with error code 1
355+
{
356+
name: "",
357+
args: []string{"--parse-as", "package-lock.json", filepath.Join(testdataDir, "locks-insecure/my-package-lock.json")},
358+
exit: 1,
359+
},
360+
// json results in non-json output going to stderr
361+
{
362+
name: "",
363+
args: []string{"--json", filepath.Join(testdataDir, "locks-one")},
364+
exit: 0,
365+
},
366+
}
367+
for _, tt := range tests {
368+
t.Run(tt.name, func(t *testing.T) {
369+
t.Parallel()
370+
371+
testCli(t, tt)
372+
})
373+
}
374+
}
375+
293376
func TestRun_DBs(t *testing.T) {
294377
t.Parallel()
295378

0 commit comments

Comments
 (0)