Skip to content

Commit

Permalink
tests: add more tests for env
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Oct 16, 2020
1 parent 0187b6c commit bc00b30
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/utils/arg-parser.js
Expand Up @@ -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] = {};
Expand Down
28 changes: 28 additions & 0 deletions test/config/type/function-with-env/function-with-env.test.js
Expand Up @@ -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();
Expand Down
19 changes: 18 additions & 1 deletion test/config/type/function-with-env/webpack.env.config.js
Expand Up @@ -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',
Expand All @@ -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 {};
};

0 comments on commit bc00b30

Please sign in to comment.