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
candrews authored May 23, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 969ed05 commit bb96f4c
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
@@ -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(/.*@.*/) &&
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
@@ -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', () => {

0 comments on commit bb96f4c

Please sign in to comment.