Skip to content

Commit d56cb61

Browse files
authored
fix: ensure absolute paths can be provided on Windows (#354)
* test: add cases for absolute file paths * fix: explicitly check for absolute paths on Windows
1 parent dda78b9 commit d56cb61

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

__snapshots__/main_test.snap

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

662662

663+
---
664+
665+
[TestRun_Lockfile_AbsolutePath/#00 - 1]
666+
Loaded the following OSV databases:
667+
npm (%% vulnerabilities, including withdrawn - last updated %%)
668+
669+
<rootdir>/testdata/locks-one/yarn.lock: found 1 package
670+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
671+
672+
no known vulnerabilities found
673+
674+
---
675+
676+
[TestRun_Lockfile_AbsolutePath/#00 - 2]
677+
678+
---
679+
680+
[TestRun_Lockfile_AbsolutePath/#01 - 1]
681+
Loaded the following OSV databases:
682+
RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
683+
Packagist (%% vulnerabilities, including withdrawn - last updated %%)
684+
npm (%% vulnerabilities, including withdrawn - last updated %%)
685+
686+
<rootdir>/testdata/locks-many/Gemfile.lock: found 1 package
687+
Using db RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
688+
689+
no known vulnerabilities found
690+
691+
<rootdir>/testdata/locks-many/composer.lock: found 1 package
692+
Using db Packagist (%% vulnerabilities, including withdrawn - last updated %%)
693+
694+
no known vulnerabilities found
695+
696+
<rootdir>/testdata/locks-many/yarn.lock: found 1 package
697+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
698+
699+
no known vulnerabilities found
700+
701+
---
702+
703+
[TestRun_Lockfile_AbsolutePath/#01 - 2]
704+
705+
---
706+
707+
[TestRun_Lockfile_AbsolutePath/#02 - 1]
708+
Loaded the following OSV databases:
709+
710+
<rootdir>/testdata/locks-empty/Gemfile.lock: found 0 packages
711+
712+
no known vulnerabilities found
713+
714+
<rootdir>/testdata/locks-empty/composer.lock: found 0 packages
715+
716+
no known vulnerabilities found
717+
718+
<rootdir>/testdata/locks-empty/yarn.lock: found 0 packages
719+
720+
no known vulnerabilities found
721+
722+
---
723+
724+
[TestRun_Lockfile_AbsolutePath/#02 - 2]
725+
726+
---
727+
728+
[TestRun_Lockfile_AbsolutePath/#03 - 1]
729+
Loaded the following OSV databases:
730+
npm (%% vulnerabilities, including withdrawn - last updated %%)
731+
732+
<rootdir>/testdata/locks-insecure/my-package-lock.json: found 1 package
733+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
734+
735+
[email protected] is affected by the following vulnerabilities:
736+
GHSA-whgm-jr23-g3j9: Uncontrolled Resource Consumption in ansi-html (https://github.com/advisories/GHSA-whgm-jr23-g3j9)
737+
738+
1 known vulnerability found in <rootdir>/testdata/locks-insecure/my-package-lock.json
739+
740+
---
741+
742+
[TestRun_Lockfile_AbsolutePath/#03 - 2]
743+
744+
---
745+
746+
[TestRun_Lockfile_AbsolutePath/#04 - 1]
747+
{"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":[]}]}]}
748+
---
749+
750+
[TestRun_Lockfile_AbsolutePath/#04 - 2]
751+
Loaded the following OSV databases:
752+
npm (%% vulnerabilities, including withdrawn - last updated %%)
753+
754+
<rootdir>/testdata/locks-one/yarn.lock: found 1 package
755+
Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
756+
757+
663758
---
664759

665760
[TestRun_ParseAsGlobal/#00 - 1]

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9+
"runtime"
910
"slices"
1011
"strings"
1112

@@ -405,6 +406,10 @@ func (files lockfileAndConfigOrErrs) adjustExtraDatabases(
405406
}
406407

407408
func parseLockfilePathWithParseAs(lockfilePathWithParseAs string) (string, string) {
409+
if runtime.GOOS == "windows" && filepath.IsAbs(lockfilePathWithParseAs) {
410+
return "", lockfilePathWithParseAs
411+
}
412+
408413
if !strings.Contains(lockfilePathWithParseAs, ":") {
409414
return "", lockfilePathWithParseAs
410415
}

main_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,53 @@ func TestRun_Lockfile(t *testing.T) {
231231
}
232232
}
233233

234+
func TestRun_Lockfile_AbsolutePath(t *testing.T) {
235+
t.Parallel()
236+
237+
testdataDir, err := filepath.Abs("./testdata")
238+
239+
if err != nil {
240+
t.Fatal(err)
241+
}
242+
243+
tests := []cliTestCase{
244+
{
245+
name: "",
246+
args: []string{filepath.Join(testdataDir, "locks-one")},
247+
exit: 0,
248+
},
249+
{
250+
name: "",
251+
args: []string{filepath.Join(testdataDir, "locks-many")},
252+
exit: 0,
253+
},
254+
{
255+
name: "",
256+
args: []string{filepath.Join(testdataDir, "locks-empty")},
257+
exit: 0,
258+
},
259+
// parse-as + known vulnerability exits with error code 1
260+
{
261+
name: "",
262+
args: []string{"--parse-as", "package-lock.json", filepath.Join(testdataDir, "locks-insecure/my-package-lock.json")},
263+
exit: 1,
264+
},
265+
// json results in non-json output going to stderr
266+
{
267+
name: "",
268+
args: []string{"--json", filepath.Join(testdataDir, "locks-one")},
269+
exit: 0,
270+
},
271+
}
272+
for _, tt := range tests {
273+
t.Run(tt.name, func(t *testing.T) {
274+
t.Parallel()
275+
276+
testCli(t, tt)
277+
})
278+
}
279+
}
280+
234281
func TestRun_DBs(t *testing.T) {
235282
t.Parallel()
236283

0 commit comments

Comments
 (0)