Skip to content

Commit

Permalink
feat: configtest validate default configuration (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jan 15, 2021
1 parent 38869d2 commit 487691a
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ test/typescript/webpack.config.ts
test/config/error-commonjs/syntax-error.js
test/config/error-mjs/syntax-error.mjs
test/config/error-array/webpack.config.js
test/configtest/syntax-error.config.js
test/configtest/with-config-path/syntax-error.config.js
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ test/config/error-mjs/syntax-error.mjs
packages/webpack-cli/__tests__/test-assets/.yo-rc.json
test/build-errors/stats.json
packages/**/lib
test/configtest/syntax-error.config.js
test/configtest/with-config-path/syntax-error.config.js
27 changes: 23 additions & 4 deletions packages/configtest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@ class ConfigTestCommand {

await cli.makeCommand(
{
name: 'configtest <config-path>',
name: 'configtest [config-path]',
alias: 't',
description: 'Tests webpack configuration against validation errors.',
usage: '<config-path>',
pkg: '@webpack-cli/configtest',
},
[],
async (configPath: string): Promise<void> => {
const config = await cli.resolveConfig({ config: [configPath] });
async (configPath: string | undefined): Promise<void> => {
const config = await cli.resolveConfig(configPath ? { config: [configPath] } : {});
const configPaths = new Set<string>();

if (Array.isArray(config.options)) {
config.options.forEach((options) => {
if (config.path.get(options)) {
configPaths.add(config.path.get(options));
}
});
} else {
if (config.path.get(config.options)) {
configPaths.add(config.path.get(config.options));
}
}

if (configPaths.size === 0) {
logger.error('No configuration found.');
process.exit(2);
}

logger.info(`Validate '${Array.from(configPaths).join(' ,')}'.`);

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class WebpackCLI {
pkg: '@webpack-cli/migrate',
},
{
name: 'configtest',
name: 'configtest [config-path]',
alias: 't',
pkg: '@webpack-cli/configtest',
},
Expand Down Expand Up @@ -362,7 +362,7 @@ class WebpackCLI {
} else {
const builtInExternalCommandInfo = externalBuiltInCommandsInfo.find(
(externalBuiltInCommandInfo) =>
externalBuiltInCommandInfo.name === commandName ||
getCommandName(externalBuiltInCommandInfo.name) === commandName ||
(typeof Array.isArray(externalBuiltInCommandInfo.alias)
? externalBuiltInCommandInfo.alias.includes(commandName)
: externalBuiltInCommandInfo.alias === commandName),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

const { run } = require('../utils/test-utils');
const path = require('path');

describe('basic info usage', () => {
const { run } = require('../../utils/test-utils');

describe("'configtest' command with the configuration path option", () => {
it('should validate webpack config successfully', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './webpack.config.js'], false);
const { exitCode, stderr, stdout } = run(__dirname, ['configtest', './basic.config.js'], false);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'basic.config.js')}'.`);
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
});

Expand All @@ -17,7 +20,7 @@ describe('basic info usage', () => {
expect(exitCode).toBe(2);
expect(stderr).toContain('Invalid configuration object.');
expect(stderr).toContain('configuration.mode should be one of these:');
expect(stdout).toBeFalsy();
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'error.config.js')}'.`);
});

it('should throw syntax error', () => {
Expand All @@ -34,7 +37,7 @@ describe('basic info usage', () => {
expect(exitCode).toBe(2);
expect(stderr).toContain('Invalid configuration object.');
expect(stderr).toContain('configuration.mode should be one of these:');
expect(stdout).toBeFalsy();
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'error.config.js')}'.`);
});

it('should throw error if configuration does not exist', () => {
Expand All @@ -44,12 +47,4 @@ describe('basic info usage', () => {
expect(stderr).toContain(`The specified config file doesn't exist`);
expect(stdout).toBeFalsy();
});

it('should throw error if no configuration was provided', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);

expect(exitCode).toBe(2);
expect(stderr).toContain(`error: missing required argument 'config-path'`);
expect(stdout).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"mode": "development",
"target": "node",
"stats": "verbose"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const path = require('path');

const { run } = require('../../utils/test-utils');

describe("'configtest' command without the configuration path option", () => {
it.only('should validate default configuration', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.json')}'.`);
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
});
});
5 changes: 5 additions & 0 deletions test/configtest/without-config-path-error/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
mode: 'invalid',
target: 'node',
stats: 'verbose',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const path = require('path');

const { run } = require('../../utils/test-utils');

describe("'configtest' command without the configuration path option", () => {
it.only('should validate default configuration', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);

expect(exitCode).toBe(2);
expect(stderr).toContain('Invalid configuration object.');
expect(stderr).toContain('configuration.mode should be one of these:');
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.js')}'.`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = [
{
mode: 'development',
target: 'node',
stats: 'verbose',
},
{
mode: 'development',
target: 'node',
stats: 'verbose',
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const path = require('path');

const { run } = require('../../utils/test-utils');

describe("'configtest' command without the configuration path option", () => {
it.only('should validate default configuration', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.js')}'.`);
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const { run } = require('../../utils/test-utils');

describe("'configtest' command without the configuration path option", () => {
it.only('should validate default configuration', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);

expect(exitCode).toBe(2);
expect(stderr).toContain('No configuration found.');
expect(stdout).toBeFalsy();
});
});
5 changes: 5 additions & 0 deletions test/configtest/without-config-path/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
mode: 'development',
target: 'node',
stats: 'verbose',
};
16 changes: 16 additions & 0 deletions test/configtest/without-config-path/without-config-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const path = require('path');

const { run } = require('../../utils/test-utils');

describe("'configtest' command without the configuration path option", () => {
it.only('should validate default configuration', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['configtest'], false);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toContain(`Validate '${path.resolve(__dirname, 'webpack.config.js')}'.`);
expect(stdout).toContain('There are no validation errors in the given webpack configuration.');
});
});

0 comments on commit 487691a

Please sign in to comment.