Skip to content

Commit

Permalink
Test coverage in bin
Browse files Browse the repository at this point in the history
  • Loading branch information
ddamato committed Jun 26, 2020
1 parent 8e346d9 commit 20758ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 1 addition & 5 deletions bin/create-symbols.js
Expand Up @@ -20,11 +20,7 @@ createSymbols(args.input).then(async (symbols) => {
throw new Error('Cannot overwrite input directory, please provide another directory for output.');
}

try {
await fs.ensureDir(args.output);
} catch (err) {
throw new Error(`Output directory "${args.output}" does not exist.`);
}
await fs.ensureDir(args.output);

await writeSymbolSvgFiles(symbols, { outputDir: args.output });
await writeSymbolReferenceFile(symbols, { outputDir: args.output, type: args.type });
Expand Down
10 changes: 7 additions & 3 deletions src/createSymbols.js
Expand Up @@ -17,11 +17,15 @@ export default async function createSymbols(pathOrObject) {
try {
const files = await fs.promises.readdir(pathOrObject);
const sources = await Promise.all(files.map(async (file) => {
const filepath = path.resolve(pathOrObject, file);
if (!fs.statSync(filepath).isFile()) {
return;
}
const fileName = path.parse(file).name;
const source = await fs.promises.readFile(path.resolve(pathOrObject, file));
const source = await fs.promises.readFile(filepath);
return { [fileName]: source.toString() };
}));
const flattened = sources.reduce(( acc, source ) => Object.assign(acc, source), {});
}))
const flattened = sources.filter(Boolean).reduce(( acc, source ) => Object.assign(acc, source), {});
return createSymbols(flattened);
} catch (err) {
throw new Error(err);
Expand Down
18 changes: 17 additions & 1 deletion test/bin/create-symbols.test.js
Expand Up @@ -54,5 +54,21 @@ describe('create-symbols.js', async function () {
const { x } = esmImport(esmPath);
expect(exists).to.be.true;
expect(x).to.be.a('string');
})
});

it('should throw if no symbols are found in directory', async function () {
try {
await execPromise(`${createSymbolsProcess} -i test/bin/fixtures/none -o test/bin/output`);
} catch (err) {
expect(err).to.exist;
}
});

it('should throw if attempting to overwrite in input directory', async function () {
try {
await execPromise(`${createSymbolsProcess} -i test/bin/fixtures -o test/bin/fixtures`);
} catch (err) {
expect(err).to.exist;
}
});
});

0 comments on commit 20758ef

Please sign in to comment.