From bc00b30cd9220af9f23b648af691ed021c7aa1c9 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 16 Oct 2020 13:23:15 +0530 Subject: [PATCH] tests: add more tests for env --- packages/webpack-cli/lib/utils/arg-parser.js | 2 +- .../function-with-env.test.js | 28 +++++++++++++++++++ .../function-with-env/webpack.env.config.js | 19 ++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/webpack-cli/lib/utils/arg-parser.js b/packages/webpack-cli/lib/utils/arg-parser.js index 62e6bbefccf..068a00986dc 100644 --- a/packages/webpack-cli/lib/utils/arg-parser.js +++ b/packages/webpack-cli/lib/utils/arg-parser.js @@ -97,7 +97,7 @@ const argParser = (options, args, argsOnly = false, name = '') => { const multiArg = (value, previous = {}) => { // this ensures we're only splitting by the first `=` const [allKeys, val] = value.split(/=(.+)/, 2); - const splitKeys = allKeys.split('.'); + const splitKeys = allKeys.split(/\.(?!$)/); let prevRef = previous; splitKeys.forEach((someKey, index) => { if (!prevRef[someKey]) prevRef[someKey] = {}; diff --git a/test/config/type/function-with-env/function-with-env.test.js b/test/config/type/function-with-env/function-with-env.test.js index d2ee018ee7e..bbb878377ee 100644 --- a/test/config/type/function-with-env/function-with-env.test.js +++ b/test/config/type/function-with-env/function-with-env.test.js @@ -53,6 +53,34 @@ describe('function configuration', () => { // Should generate the appropriate files expect(existsSync(resolve(__dirname, './bin/Atsumu.js'))).toBeTruthy(); }); + it('Supports multiple equal in a string', () => { + const { stderr, stdout } = run(__dirname, [ + '--env', + 'file=name=is=Eren', + '--env', + 'environment=multipleq', + '-c', + 'webpack.env.config.js', + ]); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + // Should generate the appropriate files + expect(existsSync(resolve(__dirname, './bin/name=is=Eren.js'))).toBeTruthy(); + }); + it('Supports dot at the end', () => { + const { stderr, stdout } = run(__dirname, ['--env', 'name.=Hisoka', '--env', 'environment=dot', '-c', 'webpack.env.config.js']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + // Should generate the appropriate files + expect(existsSync(resolve(__dirname, './bin/Hisoka.js'))).toBeTruthy(); + }); + it('Supports dot at the end', () => { + const { stderr, stdout } = run(__dirname, ['--env', 'name.', '--env', 'environment=dot', '-c', 'webpack.env.config.js']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + // Should generate the appropriate files + expect(existsSync(resolve(__dirname, './bin/true.js'))).toBeTruthy(); + }); it('is able to understand multiple env flags', (done) => { const { stderr, stdout } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']); expect(stderr).toBeFalsy(); diff --git a/test/config/type/function-with-env/webpack.env.config.js b/test/config/type/function-with-env/webpack.env.config.js index 33878ce4b0f..b131cf9866d 100644 --- a/test/config/type/function-with-env/webpack.env.config.js +++ b/test/config/type/function-with-env/webpack.env.config.js @@ -2,7 +2,6 @@ module.exports = (env) => { const { environment, app, file } = env; const customName = file && file.name && file.name.is && file.name.is.this; const appTitle = app && app.title; - console.log(env); if (environment === 'production') { return { entry: './a.js', @@ -11,5 +10,23 @@ module.exports = (env) => { }, }; } + if (environment === 'multipleq') { + const { file } = env; + return { + entry: './a.js', + output: { + filename: `${file}.js`, + }, + }; + } + if (environment === 'dot') { + const file = env['name.']; + return { + entry: './a.js', + output: { + filename: `${file}.js`, + }, + }; + } return {}; };