Skip to content

Commit

Permalink
Fail when specified config is wrong or json is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Feb 22, 2021
1 parent aa8e0bd commit a855b40
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
5 changes: 0 additions & 5 deletions lib/svgo-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);
Expand Down
2 changes: 1 addition & 1 deletion lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
18 changes: 11 additions & 7 deletions test/config/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a855b40

Please sign in to comment.