@@ -499,7 +499,140 @@ func TestRun_DBs(t *testing.T) {
499499 }
500500}
501501
502- func TestRun_ParseAs (t * testing.T ) {
502+ func TestRun_ParseAsSpecific (t * testing.T ) {
503+ t .Parallel ()
504+
505+ tests := []cliTestCase {
506+ // when there is just a ":", it defaults as empty
507+ {
508+ name : "" ,
509+ args : []string {filepath .FromSlash (":./fixtures/locks-insecure/composer.lock" )},
510+ wantExitCode : 0 ,
511+ wantStdout : `
512+ Loaded the following OSV databases:
513+
514+ fixtures/locks-insecure/composer.lock: found 0 packages
515+
516+ no known vulnerabilities found
517+ ` ,
518+ wantStderr : "" ,
519+ },
520+ // ":" can be used as an escape (no test though because it's invalid on Windows)
521+ {
522+ name : "" ,
523+ args : []string {filepath .FromSlash (":./fixtures/locks-insecure/my:file" )},
524+ wantExitCode : 127 ,
525+ wantStdout : "" ,
526+ wantStderr : `
527+ Error reading ./fixtures/locks-insecure/my:file: open ./fixtures/locks-insecure/my:file: %%
528+ You must provide at least one path to either a lockfile or a directory containing at least one lockfile (see --help for usage and flags)
529+ ` ,
530+ },
531+ // when a path to a file is given, parse-as is applied to that file
532+ {
533+ name : "" ,
534+ args : []string {filepath .FromSlash ("package-lock.json:./fixtures/locks-insecure/my-package-lock.json" )},
535+ wantExitCode : 1 ,
536+ wantStdout : `
537+ Loaded the following OSV databases:
538+ npm (%% vulnerabilities, including withdrawn - last updated %%)
539+
540+ fixtures/locks-insecure/my-package-lock.json: found 1 package
541+ Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
542+
543+ [email protected] is affected by the following vulnerabilities: 544+ GHSA-whgm-jr23-g3j9: Uncontrolled Resource Consumption in ansi-html (https://github.com/advisories/GHSA-whgm-jr23-g3j9)
545+
546+ 1 known vulnerability found in fixtures/locks-insecure/my-package-lock.json
547+ ` ,
548+ wantStderr : "" ,
549+ },
550+ // when a path to a directory is given, parse-as is applied to all files in the directory
551+ {
552+ name : "" ,
553+ args : []string {filepath .FromSlash ("package-lock.json:./fixtures/locks-insecure" )},
554+ wantExitCode : 1 ,
555+ wantStdout : `
556+ Loaded the following OSV databases:
557+ npm (%% vulnerabilities, including withdrawn - last updated %%)
558+
559+ fixtures/locks-insecure/composer.lock: found 0 packages
560+
561+ no known vulnerabilities found
562+
563+ fixtures/locks-insecure/my-package-lock.json: found 1 package
564+ Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
565+
566+ [email protected] is affected by the following vulnerabilities: 567+ GHSA-whgm-jr23-g3j9: Uncontrolled Resource Consumption in ansi-html (https://github.com/advisories/GHSA-whgm-jr23-g3j9)
568+
569+ 1 known vulnerability found in fixtures/locks-insecure/my-package-lock.json
570+ ` ,
571+ wantStderr : "" ,
572+ },
573+ // files that error on parsing don't stop parsable files from being checked
574+ {
575+ name : "" ,
576+ args : []string {filepath .FromSlash ("package-lock.json:./fixtures/locks-empty" )},
577+ wantExitCode : 127 ,
578+ wantStdout : `
579+ Loaded the following OSV databases:
580+
581+
582+ fixtures/locks-empty/composer.lock: found 0 packages
583+
584+ no known vulnerabilities found
585+
586+ ` ,
587+ wantStderr : `
588+ Error, could not parse fixtures/locks-empty/Gemfile.lock: unexpected end of JSON input
589+ Error, could not parse fixtures/locks-empty/yarn.lock: invalid character '#' looking for beginning of value
590+ ` ,
591+ },
592+ // files that error on parsing don't stop parsable files from being checked
593+ {
594+ name : "" ,
595+ args : []string {filepath .FromSlash ("package-lock.json:./fixtures/locks-empty" ), filepath .FromSlash ("package-lock.json:./fixtures/locks-insecure" )},
596+ wantExitCode : 127 ,
597+ wantStdout : `
598+ Loaded the following OSV databases:
599+ npm (%% vulnerabilities, including withdrawn - last updated %%)
600+
601+
602+ fixtures/locks-empty/composer.lock: found 0 packages
603+
604+ no known vulnerabilities found
605+
606+
607+ fixtures/locks-insecure/composer.lock: found 0 packages
608+
609+ no known vulnerabilities found
610+
611+ fixtures/locks-insecure/my-package-lock.json: found 1 package
612+ Using db npm (%% vulnerabilities, including withdrawn - last updated %%)
613+
614+ [email protected] is affected by the following vulnerabilities: 615+ GHSA-whgm-jr23-g3j9: Uncontrolled Resource Consumption in ansi-html (https://github.com/advisories/GHSA-whgm-jr23-g3j9)
616+
617+ 1 known vulnerability found in fixtures/locks-insecure/my-package-lock.json
618+ ` ,
619+ wantStderr : `
620+ Error, could not parse fixtures/locks-empty/Gemfile.lock: unexpected end of JSON input
621+ Error, could not parse fixtures/locks-empty/yarn.lock: invalid character '#' looking for beginning of value
622+ ` ,
623+ },
624+ }
625+ for _ , tt := range tests {
626+ tt := tt
627+ t .Run (tt .name , func (t * testing.T ) {
628+ t .Parallel ()
629+
630+ testCli (t , tt )
631+ })
632+ }
633+ }
634+
635+ func TestRun_ParseAsGlobal (t * testing.T ) {
503636 t .Parallel ()
504637
505638 tests := []cliTestCase {
@@ -596,6 +729,41 @@ func TestRun_ParseAs(t *testing.T) {
596729 Error, could not parse fixtures/locks-empty/yarn.lock: invalid character '#' looking for beginning of value
597730 ` ,
598731 },
732+ // specific parse-as takes precedence over global parse-as
733+ {
734+ name : "" ,
735+ args : []string {"--parse-as" , "package-lock.json" , filepath .FromSlash ("Gemfile.lock:./fixtures/locks-empty" ), filepath .FromSlash ("./fixtures/locks-insecure" )},
736+ wantExitCode : 1 ,
737+ wantStdout : `
738+ Loaded the following OSV databases:
739+ npm (2971 vulnerabilities, including withdrawn - last updated %%)
740+
741+ fixtures/locks-empty/Gemfile.lock: found 0 packages
742+
743+ no known vulnerabilities found
744+
745+ fixtures/locks-empty/composer.lock: found 0 packages
746+
747+ no known vulnerabilities found
748+
749+ fixtures/locks-empty/yarn.lock: found 0 packages
750+
751+ no known vulnerabilities found
752+
753+ fixtures/locks-insecure/composer.lock: found 0 packages
754+
755+ no known vulnerabilities found
756+
757+ fixtures/locks-insecure/my-package-lock.json: found 1 package
758+ Using db npm (2971 vulnerabilities, including withdrawn - last updated %%)
759+
760+ [email protected] is affected by the following vulnerabilities: 761+ GHSA-whgm-jr23-g3j9: Uncontrolled Resource Consumption in ansi-html (https://github.com/advisories/GHSA-whgm-jr23-g3j9)
762+
763+ 1 known vulnerability found in fixtures/locks-insecure/my-package-lock.json
764+ ` ,
765+ wantStderr : "" ,
766+ },
599767 }
600768 for _ , tt := range tests {
601769 tt := tt
0 commit comments