Skip to content

Commit

Permalink
fix: error handling for empty yarn lock files (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
candrews committed May 23, 2023
1 parent 42421d7 commit 0709f42
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/lockfile-lint-api/src/ParseLockfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const {
* @return boolean
*/
function checkSampleContent (lockfile, isYarnBerry) {
if (Object.entries(lockfile).length < (isYarnBerry ? 2 : 1)) {
return false
}
const [sampleKey, sampleValue] = Object.entries(lockfile)[isYarnBerry ? 1 : 0]
return (
sampleKey.match(/.*@.*/) &&
Expand Down
21 changes: 21 additions & 0 deletions packages/lockfile-lint/__tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ describe('Main CLI logic', () => {
.toThrow('Lockfile does not seem to contain a valid dependency list')
)
})

test('should fail with an empty yarn lock file', () => {
const lockfilePath = path.join(__dirname, '/fixtures/empty.json')
const lockfileType = 'yarn'
const validators = [
{
name: 'validateHosts',
values: ['npm']
}
]

expect(() =>
main
.runValidators({
path: lockfilePath,
type: lockfileType,
validators
})
.toThrow('Lockfile does not seem to contain a valid dependency list')
)
})
})

describe('validateSchemes', () => {
Expand Down

0 comments on commit 0709f42

Please sign in to comment.