1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

Fail when specified config is wrong or json is specified

This commit is contained in:
Bogdan Chadkin
2021-02-22 23:48:29 +03:00
parent aa8e0bd3f6
commit a855b40ec5
3 changed files with 12 additions and 13 deletions

View File

@ -9,11 +9,6 @@ const {
} = require('./svgo.js');
const importConfig = async configFile => {
try {
await fs.promises.access(configFile);
} catch {
return null;
}
const config = require(configFile);
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
throw Error(`Invalid config file "${configFile}"`);

View File

@ -34,7 +34,7 @@ module.exports = function makeProgram(program) {
.option('-f, --folder <FOLDER>', 'Input folder, optimize and rewrite all *.svg files')
.option('-o, --output <OUTPUT...>', 'Output file or folder (by default the same as the input), "-" for STDOUT')
.option('-p, --precision <INTEGER>', 'Set number of digits in the fractional part, overrides plugins params')
.option('--config <CONFIG>', 'Config file or JSON string to extend or replace default')
.option('--config <CONFIG>', 'Custom config file, only .js is supported')
.option('--datauri <FORMAT>', 'Output as Data URI string (base64), URI encoded (enc) or unencoded (unenc)')
.option('--multipass', 'Pass over SVGs multiple times to ensure all optimizations are applied')
.option('--pretty', 'Make SVG pretty printed')

View File

@ -206,16 +206,20 @@ describe('config', function() {
);
expect(config).to.deep.equal({ plugins: [] });
});
it('gives null module does not exist', async () => {
const absoluteConfig = await loadConfig(
path.join(process.cwd(), './test/config/fixtures/config.js'),
);
expect(absoluteConfig).to.equal(null);
const searchedConfig = await loadConfig(
it('gives null when config is not found', async () => {
const config = await loadConfig(
null,
path.join(process.cwd(), './test/config'),
);
expect(searchedConfig).to.equal(null);
expect(config).to.equal(null);
});
it('is failed when specified config does not exist', async () => {
try {
await loadConfig("{}");
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).to.match(/Cannot find module/);
}
});
it('is failed to load when module exports not an object', async () => {
try {