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: error handling for empty yarn lock files (#158) #159

Merged
merged 7 commits into from
May 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
lirantal marked this conversation as resolved.
Show resolved Hide resolved
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')
lirantal marked this conversation as resolved.
Show resolved Hide resolved
const lockfileType = 'npm'
const validators = [
{
name: 'validateHosts',
values: ['npm']
lirantal marked this conversation as resolved.
Show resolved Hide resolved
}
]

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'
candrews marked this conversation as resolved.
Show resolved Hide resolved
lirantal marked this conversation as resolved.
Show resolved Hide resolved
const validators = [
{
name: 'validateHosts',
values: ['yarn']
candrews marked this conversation as resolved.
Show resolved Hide resolved
}
]

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

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