Skip to content

Commit 8f7e4c7

Browse files
authored
fix: cli parsing - handle correctly false value for validator (#146)
* Rename command-line argument from `--validate-integrity-sha512` to `--validate-integrity` * Add validator only if commandValue is not falsy * Added tests to make sure validator does not run when flag is missing, or flag is set to "false" * Added another cli test for the integrity validator * Refactor: Add validator only if commandValue is not falsy
1 parent cb38c10 commit 8f7e4c7

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

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

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,81 @@ describe('CLI tests', () => {
104104
})
105105
})
106106

107+
test('Linting a file that has invalid integrity hash type should return exit code 1', done => {
108+
const process = childProcess.spawn('node', [
109+
cliExecPath,
110+
'--type',
111+
'npm',
112+
'--path',
113+
'__tests__/fixtures/package-lock-sha1.json',
114+
'--validate-integrity',
115+
'--allowed-hosts',
116+
'npm'
117+
])
118+
119+
let output = ''
120+
process.stderr.on('data', chunk => {
121+
output += chunk
122+
})
123+
124+
process.on('close', exitCode => {
125+
expect(output.indexOf('detected invalid integrity hash type for package')).not.toBe(-1)
126+
expect(exitCode).toBe(1)
127+
done()
128+
})
129+
})
130+
131+
test('Linting should not run a validator when its flag is not set', done => {
132+
const process = childProcess.spawn('node', [
133+
cliExecPath,
134+
'--type',
135+
'npm',
136+
'--path',
137+
'__tests__/fixtures/package-lock-sha1.json',
138+
'--allowed-hosts',
139+
'npm'
140+
])
141+
142+
let output = ''
143+
process.stderr.on('data', chunk => {
144+
output += chunk
145+
})
146+
147+
process.on('close', exitCode => {
148+
expect(output.indexOf('detected invalid integrity hash type for package')).toBe(-1)
149+
expect(exitCode).toBe(0)
150+
done()
151+
})
152+
})
153+
154+
test('Linting should not run a validator when its flag is set to "false"', done => {
155+
const process = childProcess.spawn('node', [
156+
cliExecPath,
157+
'--type',
158+
'npm',
159+
'--path',
160+
'__tests__/fixtures/package-lock-sha1.json',
161+
'--validate-integrity',
162+
'false',
163+
'--allowed-hosts',
164+
'npm'
165+
])
166+
167+
let output = ''
168+
process.stderr.on('data', chunk => {
169+
output += chunk
170+
})
171+
172+
process.on('close', exitCode => {
173+
expect(output.indexOf('detected invalid integrity hash type for package')).toBe(-1)
174+
expect(exitCode).toBe(0)
175+
done()
176+
})
177+
})
178+
107179
test('Providing conflicting arguments should display an error', done => {
108-
const process = childProcess.spawn(cliExecPath, [
180+
const process = childProcess.spawn('node', [
181+
cliExecPath,
109182
'--type',
110183
'yarn',
111184
'--path',
@@ -127,7 +200,8 @@ describe('CLI tests', () => {
127200
})
128201

129202
test('Allowed hosts and allowed urls flags should work together', done => {
130-
const process = childProcess.spawn(cliExecPath, [
203+
const process = childProcess.spawn('node', [
204+
cliExecPath,
131205
'--type',
132206
'yarn',
133207
'--path',
@@ -227,7 +301,7 @@ describe('CLI tests', () => {
227301

228302
describe('cosmiconfig integration', () => {
229303
it('options are loaded from cosmiconfig files', done => {
230-
const lintProcess = childProcess.spawn(cliExecPath, [], {
304+
const lintProcess = childProcess.spawn('node', [cliExecPath], {
231305
cwd: path.join(__dirname, 'fixtures/valid-config')
232306
})
233307

@@ -244,9 +318,13 @@ describe('CLI tests', () => {
244318
})
245319

246320
it('command-line options take precedence', done => {
247-
const lintProcess = childProcess.spawn(cliExecPath, ['-p', '../yarn-only-http.lock'], {
248-
cwd: path.join(__dirname, 'fixtures/valid-config')
249-
})
321+
const lintProcess = childProcess.spawn(
322+
'node',
323+
[cliExecPath, '-p', '../yarn-only-http.lock'],
324+
{
325+
cwd: path.join(__dirname, 'fixtures/valid-config')
326+
}
327+
)
250328

251329
lintProcess.on('close', exitCode => {
252330
expect(exitCode).toBe(1)
@@ -256,8 +334,8 @@ describe('CLI tests', () => {
256334

257335
it('invalid config files are ignored', done => {
258336
const lintProcess = childProcess.spawn(
259-
cliExecPath,
260-
['-p', '../yarn-only-https.lock', '--type', 'yarn', '--validate-https'],
337+
'node',
338+
[cliExecPath, '-p', '../yarn-only-https.lock', '--type', 'yarn', '--validate-https'],
261339
{
262340
cwd: path.join(__dirname, 'fixtures/invalid-config'),
263341
env: Object.assign({}, process.env, {DEBUG: 'lockfile-lint'})

packages/lockfile-lint/bin/lockfile-lint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ for (const [commandArgument, commandValue] of Object.entries(config)) {
6161
continue
6262
}
6363

64-
if (supportedValidators.has(commandArgument)) {
64+
if (commandValue && supportedValidators.has(commandArgument)) {
6565
const validatorItem = supportedValidators.get(commandArgument)
6666
validators.push({
6767
name: validatorItem,

0 commit comments

Comments
 (0)