Skip to content

Commit

Permalink
fix(info): throw an error if help or version is passed as an arg (#1737)
Browse files Browse the repository at this point in the history
* fix: info throws an error if help or version is passed as an arg

* tests: use --help flag as an arg to info

* refactor: use logger

* tests: fix failing test suite
  • Loading branch information
jamesgeorge007 authored Aug 13, 2020
1 parent 1f6a8c5 commit c8ca878
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions packages/info/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { red } from 'colorette';
import envinfo from 'envinfo';
import logger from 'webpack-cli/lib/utils/logger';
import options from './options';
import WebpackCLI from 'webpack-cli';

Expand Down Expand Up @@ -36,10 +36,9 @@ export default async function info(...args): Promise<string[]> {
const infoArgs = parsedArgs.opts;
const envinfoConfig = {};

if (parsedArgs.unknownArgs.some((arg) => ['help', 'version', 'color'].includes(arg))) return;

if (parsedArgs.unknownArgs.length > 0) {
process.stderr.write(`Unknown argument: ${red(parsedArgs.unknownArgs)}\n`);
logger.error(`Unknown argument: ${parsedArgs.unknownArgs}`);
process.exit(2);
}

if (infoArgs.output) {
Expand All @@ -53,7 +52,7 @@ export default async function info(...args): Promise<string[]> {
envinfoConfig['json'] = true;
break;
default:
process.stderr.write(`${red(infoArgs.output)} is not a valid value for output\n`);
logger.error(`${infoArgs.output} is not a valid value for output\n`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/info/info-help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ const descriptionText = 'Outputs information about your system and dependencies'

describe('should print help for info command', () => {
it('shows usage information on supplying help flag', () => {
const { stdout, stderr } = runInfo(['help'], __dirname);
const { stdout, stderr } = runInfo(['--help'], __dirname);
expect(stdout).toContain(usageText);
expect(stdout).toContain(descriptionText);
expect(stderr).toHaveLength(0);
});

it('should respect the --no-color flag', () => {
const { stdout, stderr } = runInfo(['help', '--no-color'], __dirname);
const { stdout, stderr } = runInfo(['--help', '--no-color'], __dirname);
options.enabled = true;
expect(stdout).not.toContain(yellow(usageText));
expect(stdout).toContain(descriptionText);
expect(stderr).toHaveLength(0);
});

it('should output all cli flags', () => {
const { stdout, stderr } = runInfo(['help'], __dirname);
const { stdout, stderr } = runInfo(['--help'], __dirname);
infoFlags.forEach((flag) => expect(stdout).toContain(`--${flag.name}`));
expect(stderr).toHaveLength(0);
});
Expand Down
2 changes: 1 addition & 1 deletion test/info/info-unknown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ const { runInfo } = require('../utils/test-utils');
describe('should handle unknown args', () => {
it('shows an appropriate warning on supplying unknown args', () => {
const { stderr } = runInfo(['--unknown'], __dirname);
expect(stderr).toContain(`Unknown argument: ${red('--unknown')}`);
expect(stderr).toContain(`[webpack-cli] ${red('Unknown argument: --unknown')}`);
});
});

0 comments on commit c8ca878

Please sign in to comment.