Skip to content

Commit

Permalink
fix: don't panic on empty files
Browse files Browse the repository at this point in the history
Copied from G-Rath/osv-detector#191

Co-authored-by: Gareth Jones <Jones258@Gmail.com>

fixes: #364
  • Loading branch information
robotdana committed May 5, 2023
1 parent a8e90c9 commit 6f850b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
8 changes: 1 addition & 7 deletions pkg/lockfile/fixtures/pnpm/empty.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
lockfileVersion: 5.3

specifiers:

dependencies:

packages:
# this is an empty file!
7 changes: 7 additions & 0 deletions pkg/lockfile/fixtures/pnpm/no-packages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
lockfileVersion: 5.3

specifiers:

dependencies:

packages:
5 changes: 5 additions & 0 deletions pkg/lockfile/parse-pnpm-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,10 @@ func ParsePnpmLock(pathToLockfile string) ([]PackageDetails, error) {
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
}

// this will happen if the file is empty
if parsedLockfile == nil {
parsedLockfile = &PnpmLockfile{}
}

return parsePnpmLock(*parsedLockfile), nil
}
14 changes: 13 additions & 1 deletion pkg/lockfile/parse-pnpm-lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestParsePnpmLock_InvalidYaml(t *testing.T) {
expectPackages(t, packages, []lockfile.PackageDetails{})
}

func TestParsePnpmLock_NoPackages(t *testing.T) {
func TestParsePnpmLock_Empty(t *testing.T) {
t.Parallel()

packages, err := lockfile.ParsePnpmLock("fixtures/pnpm/empty.yaml")
Expand All @@ -35,6 +35,18 @@ func TestParsePnpmLock_NoPackages(t *testing.T) {
expectPackages(t, packages, []lockfile.PackageDetails{})
}

func TestParsePnpmLock_NoPackages(t *testing.T) {
t.Parallel()

packages, err := lockfile.ParsePnpmLock("fixtures/pnpm/no-packages.yaml")

if err != nil {
t.Errorf("Got unexpected error: %v", err)
}

expectPackages(t, packages, []lockfile.PackageDetails{})
}

func TestParsePnpmLock_OnePackage(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 6f850b3

Please sign in to comment.