From 7f50948bb984821449277d6b5632b98a695eb029 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 15 Jul 2021 09:31:52 +0530 Subject: [PATCH] fix: show default value in help output if available (#2814) * fix: show default value in help output if available * test: add --- packages/webpack-cli/lib/webpack-cli.js | 2 +- test/api/CLI.test.js | 30 ++++++++++++++++++ .../__snapshots__/CLI.test.js.snap.webpack4 | 31 +++++++++++++++++++ .../__snapshots__/CLI.test.js.snap.webpack5 | 31 +++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/api/__snapshots__/CLI.test.js.snap.webpack4 create mode 100644 test/api/__snapshots__/CLI.test.js.snap.webpack5 diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index e8f7b0a3146..14d2294638e 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1357,7 +1357,7 @@ class WebpackCLI { this.logger.raw(`${bold("Description:")} ${option.description}`); } - if (!option.negate && options.defaultValue) { + if (!option.negate && option.defaultValue) { this.logger.raw( `${bold("Default value:")} ${JSON.stringify(option.defaultValue)}`, ); diff --git a/test/api/CLI.test.js b/test/api/CLI.test.js index b785fa4f90a..0896c989deb 100644 --- a/test/api/CLI.test.js +++ b/test/api/CLI.test.js @@ -1699,4 +1699,34 @@ describe("CLI API", () => { expect(command.helpInformation()).toContain("--no-boolean Negated description"); }); }); + + describe("custom help output", () => { + let consoleSpy; + let exitSpy; + + beforeEach(async () => { + consoleSpy = jest.spyOn(global.console, "log"); + exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {}); + + cli.program.option("--color [value]", "any color", "blue"); + await new Promise((resolve, reject) => { + try { + cli.run(["help", "--color"], { from: "user" }); + resolve(); + } catch (error) { + reject(error); + } + }); + }); + + afterEach(async () => { + consoleSpy.mockRestore(); + exitSpy.mockRestore(); + }); + + it("should display help information", () => { + expect(exitSpy).toHaveBeenCalledWith(0); + expect(consoleSpy.mock.calls).toMatchSnapshot(); + }); + }); }); diff --git a/test/api/__snapshots__/CLI.test.js.snap.webpack4 b/test/api/__snapshots__/CLI.test.js.snap.webpack4 new file mode 100644 index 00000000000..50607abaa35 --- /dev/null +++ b/test/api/__snapshots__/CLI.test.js.snap.webpack4 @@ -0,0 +1,31 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`CLI API custom help output should display help information 1`] = ` +Array [ + Array [ + "Usage: webpack --color [value]", + ], + Array [ + "Description: any color", + ], + Array [ + "Default value: \\"blue\\"", + ], + Array [ + "", + ], + Array [ + "To see list of all supported commands and options run 'webpack --help=verbose'. +", + ], + Array [ + "Webpack documentation: https://webpack.js.org/.", + ], + Array [ + "CLI documentation: https://webpack.js.org/api/cli/.", + ], + Array [ + "Made with ♥ by the webpack team.", + ], +] +`; diff --git a/test/api/__snapshots__/CLI.test.js.snap.webpack5 b/test/api/__snapshots__/CLI.test.js.snap.webpack5 new file mode 100644 index 00000000000..50607abaa35 --- /dev/null +++ b/test/api/__snapshots__/CLI.test.js.snap.webpack5 @@ -0,0 +1,31 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`CLI API custom help output should display help information 1`] = ` +Array [ + Array [ + "Usage: webpack --color [value]", + ], + Array [ + "Description: any color", + ], + Array [ + "Default value: \\"blue\\"", + ], + Array [ + "", + ], + Array [ + "To see list of all supported commands and options run 'webpack --help=verbose'. +", + ], + Array [ + "Webpack documentation: https://webpack.js.org/.", + ], + Array [ + "CLI documentation: https://webpack.js.org/api/cli/.", + ], + Array [ + "Made with ♥ by the webpack team.", + ], +] +`;