Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't panic on empty files #367

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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