From a855b40ec5d53f3ce3b45213fd4c65bc4dc05347 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 22 Feb 2021 23:48:29 +0300 Subject: [PATCH] Fail when specified config is wrong or json is specified --- lib/svgo-node.js | 5 ----- lib/svgo/coa.js | 2 +- test/config/_index.js | 18 +++++++++++------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/svgo-node.js b/lib/svgo-node.js index a56c3214..71f973b4 100644 --- a/lib/svgo-node.js +++ b/lib/svgo-node.js @@ -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}"`); diff --git a/lib/svgo/coa.js b/lib/svgo/coa.js index 8da66299..4f605e8b 100644 --- a/lib/svgo/coa.js +++ b/lib/svgo/coa.js @@ -34,7 +34,7 @@ module.exports = function makeProgram(program) { .option('-f, --folder ', 'Input folder, optimize and rewrite all *.svg files') .option('-o, --output ', 'Output file or folder (by default the same as the input), "-" for STDOUT') .option('-p, --precision ', 'Set number of digits in the fractional part, overrides plugins params') - .option('--config ', 'Config file or JSON string to extend or replace default') + .option('--config ', 'Custom config file, only .js is supported') .option('--datauri ', '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') diff --git a/test/config/_index.js b/test/config/_index.js index 37dc291c..44dcc50e 100644 --- a/test/config/_index.js +++ b/test/config/_index.js @@ -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 {