Skip to content

Commit bb96f4c

Browse files
authored
fix: error handling for empty yarn lock files (#158) (#159)
1 parent 969ed05 commit bb96f4c

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

packages/lockfile-lint-api/src/ParseLockfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const {
2121
* @return boolean
2222
*/
2323
function checkSampleContent (lockfile, isYarnBerry) {
24+
if (Object.entries(lockfile).length < (isYarnBerry ? 2 : 1)) {
25+
return false
26+
}
2427
const [sampleKey, sampleValue] = Object.entries(lockfile)[isYarnBerry ? 1 : 0]
2528
return (
2629
sampleKey.match(/.*@.*/) &&

packages/lockfile-lint/__tests__/fixtures/empty.json

Whitespace-only changes.

packages/lockfile-lint/__tests__/main.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,48 @@ describe('Main CLI logic', () => {
164164
expect(result.validatorCount).toEqual(1)
165165
expect(result.validatorSuccesses).toEqual(1)
166166
})
167+
168+
test('should fail with an empty npm lock file', () => {
169+
const lockfilePath = path.join(__dirname, '/fixtures/empty.json')
170+
const lockfileType = 'npm'
171+
const validators = [
172+
{
173+
name: 'validateHosts',
174+
values: ['npm']
175+
}
176+
]
177+
178+
expect(() =>
179+
main
180+
.runValidators({
181+
path: lockfilePath,
182+
type: lockfileType,
183+
validators
184+
})
185+
.toThrow('Lockfile does not seem to contain a valid dependency list')
186+
)
187+
})
188+
189+
test('should fail with an empty yarn lock file', () => {
190+
const lockfilePath = path.join(__dirname, '/fixtures/empty.json')
191+
const lockfileType = 'yarn'
192+
const validators = [
193+
{
194+
name: 'validateHosts',
195+
values: ['yarn']
196+
}
197+
]
198+
199+
expect(() =>
200+
main
201+
.runValidators({
202+
path: lockfilePath,
203+
type: lockfileType,
204+
validators
205+
})
206+
.toThrow('Lockfile does not seem to contain a valid dependency list')
207+
)
208+
})
167209
})
168210

169211
describe('validateSchemes', () => {

0 commit comments

Comments
 (0)