From 6eb78833f910135ca798c0c28f8d236ef234a76c Mon Sep 17 00:00:00 2001 From: James George Date: Fri, 7 Aug 2020 10:34:14 +0530 Subject: [PATCH] fix(serve): supplying help or version as an arg should throw error (#1694) * fix: serve recognizes color flag * chore: migrate to colorette * tests: add test case to ensure that help arg recognizes subcommands * fix: throw error if help or version is supplied as an arg --- packages/serve/src/index.ts | 4 ++-- test/help/help-commands.test.js | 11 +++++++++++ test/serve/basic/serve-basic.test.js | 12 ++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts index 10d0ecfa799..0f8ffad1520 100644 --- a/packages/serve/src/index.ts +++ b/packages/serve/src/index.ts @@ -28,9 +28,9 @@ export default function serve(...args: string[]): void { parsedWebpackArgs.unknownArgs .filter((e) => e) .forEach((unknown) => { - logger.warn('Unknown argument:', unknown); + logger.error('Unknown argument:', unknown); }); - return; + process.exit(2); } cli.getCompiler(webpackArgs, core).then((compiler): void => { diff --git a/test/help/help-commands.test.js b/test/help/help-commands.test.js index 765573ad072..63f2e94fdf4 100644 --- a/test/help/help-commands.test.js +++ b/test/help/help-commands.test.js @@ -4,6 +4,17 @@ const { run } = require('../utils/test-utils'); const helpHeader = 'The build tool for modern web applications'; describe('commands help', () => { + it('throws error if supplied as an argument for subcommands', () => { + const { stderr } = run(__dirname, ['serve', 'help'], false); + expect(stderr).toContain('Unknown argument: help'); + }); + + it('shows help information with subcommands as an arg', () => { + const { stdout, stderr } = run(__dirname, ['help', 'serve'], false); + expect(stdout).toContain('webpack s | serve'); + expect(stderr).toHaveLength(0); + }); + it('throws error for invalid command with --help flag', () => { const { stderr } = run(__dirname, ['--help', 'myCommand'], false); expect(stderr).toContain(`You provided an invalid command 'myCommand'`); diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index ccb1a8e8f19..2c489313431 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -1,11 +1,15 @@ 'use strict'; +const { yellow, options } = require('colorette'); const path = require('path'); const getPort = require('get-port'); const { runServe } = require('../../utils/test-utils'); const testPath = path.resolve(__dirname); +const usageText = 'webpack s | serve'; +const descriptionText = 'Run the webpack Dev Server'; + describe('basic serve usage', () => { let port; @@ -23,6 +27,14 @@ describe('basic serve usage', () => { console.warn('TODO: fix `serve` test on windows'); }); } else { + it('should respect the --no-color flag', async () => { + const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname); + options.enabled = true; + expect(stdout).not.toContain(yellow(usageText)); + expect(stdout).toContain(descriptionText); + expect(stderr).toHaveLength(0); + }); + it('should not invoke info subcommand', async () => { const { stdout, stderr } = await runServe(['--client-log-level', 'info'], testPath); expect(stdout).toContain('main.js');