Skip to content

Commit

Permalink
fix: error handling for empty yarn lock files (#158) (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
candrews authored May 23, 2023
1 parent 969ed05 commit bb96f4c
Show file tree
Hide file tree
Showing 3 changed files with 45 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
Empty file.
42 changes: 42 additions & 0 deletions packages/lockfile-lint/__tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ describe('Main CLI logic', () => {
expect(result.validatorCount).toEqual(1)
expect(result.validatorSuccesses).toEqual(1)
})

test('should fail with an empty npm lock file', () => {
const lockfilePath = path.join(__dirname, '/fixtures/empty.json')
const lockfileType = 'npm'
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')
)
})

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: ['yarn']
}
]

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 bb96f4c

Please sign in to comment.