Skip to content

Commit

Permalink
fix: cli parsing - handle correctly false value for validator (#146)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yoavain authored Oct 8, 2022
1 parent cb38c10 commit 8f7e4c7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
94 changes: 86 additions & 8 deletions packages/lockfile-lint/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,81 @@ describe('CLI tests', () => {
})
})

test('Linting a file that has invalid integrity hash type should return exit code 1', done => {
const process = childProcess.spawn('node', [
cliExecPath,
'--type',
'npm',
'--path',
'__tests__/fixtures/package-lock-sha1.json',
'--validate-integrity',
'--allowed-hosts',
'npm'
])

let output = ''
process.stderr.on('data', chunk => {
output += chunk
})

process.on('close', exitCode => {
expect(output.indexOf('detected invalid integrity hash type for package')).not.toBe(-1)
expect(exitCode).toBe(1)
done()
})
})

test('Linting should not run a validator when its flag is not set', done => {
const process = childProcess.spawn('node', [
cliExecPath,
'--type',
'npm',
'--path',
'__tests__/fixtures/package-lock-sha1.json',
'--allowed-hosts',
'npm'
])

let output = ''
process.stderr.on('data', chunk => {
output += chunk
})

process.on('close', exitCode => {
expect(output.indexOf('detected invalid integrity hash type for package')).toBe(-1)
expect(exitCode).toBe(0)
done()
})
})

test('Linting should not run a validator when its flag is set to "false"', done => {
const process = childProcess.spawn('node', [
cliExecPath,
'--type',
'npm',
'--path',
'__tests__/fixtures/package-lock-sha1.json',
'--validate-integrity',
'false',
'--allowed-hosts',
'npm'
])

let output = ''
process.stderr.on('data', chunk => {
output += chunk
})

process.on('close', exitCode => {
expect(output.indexOf('detected invalid integrity hash type for package')).toBe(-1)
expect(exitCode).toBe(0)
done()
})
})

test('Providing conflicting arguments should display an error', done => {
const process = childProcess.spawn(cliExecPath, [
const process = childProcess.spawn('node', [
cliExecPath,
'--type',
'yarn',
'--path',
Expand All @@ -127,7 +200,8 @@ describe('CLI tests', () => {
})

test('Allowed hosts and allowed urls flags should work together', done => {
const process = childProcess.spawn(cliExecPath, [
const process = childProcess.spawn('node', [
cliExecPath,
'--type',
'yarn',
'--path',
Expand Down Expand Up @@ -227,7 +301,7 @@ describe('CLI tests', () => {

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

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

it('command-line options take precedence', done => {
const lintProcess = childProcess.spawn(cliExecPath, ['-p', '../yarn-only-http.lock'], {
cwd: path.join(__dirname, 'fixtures/valid-config')
})
const lintProcess = childProcess.spawn(
'node',
[cliExecPath, '-p', '../yarn-only-http.lock'],
{
cwd: path.join(__dirname, 'fixtures/valid-config')
}
)

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

it('invalid config files are ignored', done => {
const lintProcess = childProcess.spawn(
cliExecPath,
['-p', '../yarn-only-https.lock', '--type', 'yarn', '--validate-https'],
'node',
[cliExecPath, '-p', '../yarn-only-https.lock', '--type', 'yarn', '--validate-https'],
{
cwd: path.join(__dirname, 'fixtures/invalid-config'),
env: Object.assign({}, process.env, {DEBUG: 'lockfile-lint'})
Expand Down
2 changes: 1 addition & 1 deletion packages/lockfile-lint/bin/lockfile-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ for (const [commandArgument, commandValue] of Object.entries(config)) {
continue
}

if (supportedValidators.has(commandArgument)) {
if (commandValue && supportedValidators.has(commandArgument)) {
const validatorItem = supportedValidators.get(commandArgument)
validators.push({
name: validatorItem,
Expand Down

0 comments on commit 8f7e4c7

Please sign in to comment.