Skip to content

Commit

Permalink
Fixed unit tests and minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Nov 12, 2019
1 parent ef87422 commit 3b4bc2e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 23 deletions.
4 changes: 3 additions & 1 deletion lib/utils/promisified-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const util = require('util');
'readdir',
'readFile',
'writeFile',
'mkdir'
'mkdir',
'rmdir',
'unlink'

].forEach(method => {
fs[method] = util.promisify(fs[method]);
Expand Down
2 changes: 0 additions & 2 deletions lib/yml-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class YmlBuilder {
// If the directory not exists
return false;
}

}

async _validateOutputPath(filePath) {
Expand Down Expand Up @@ -95,7 +94,6 @@ class YmlBuilder {
} catch(err) {
throw new YmlBuilderError(`An error ocurred while reading source directory: ${err.message}`, YmlBuilderError.codes.READ_FILES_ERROR);
}

}


Expand Down
119 changes: 99 additions & 20 deletions tests/yml-builder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const assert = require('assert');
const sandbox = require('sinon').createSandbox();
const path = require('path');
const yaml = require('yaml');
const recursive = require('recursive-readdir');
const fs = require('../lib/utils/promisified-fs');

const YmlBuilder = require('../lib/yml-builder');
Expand All @@ -17,9 +16,9 @@ describe('YmlBuilder', () => {
const basePath = path.join('tests', 'resources');
const outputPath = path.join(basePath, 'output.yml');

const getParentDir = dirPath => path.parse(dirPath).dir;

const getPath = targetPath => path.join(basePath, targetPath);
const makeYmlBuilder = (input, output) => new YmlBuilder(getPath(input), output || outputPath);
const getOutputFile = async () => fs.readFile(outputPath, 'utf8');

afterEach(() => {
sandbox.restore();
Expand All @@ -29,11 +28,11 @@ describe('YmlBuilder', () => {

it('Should build an array yaml when the received files are array ymls', async () => {

const ymlBuilder = new YmlBuilder(getPath('valid-array'), outputPath);
const ymlBuilder = makeYmlBuilder('valid-array');

await ymlBuilder.execute();

const output = await fs.readFile(outputPath, 'utf8');
const output = await getOutputFile();

assert.deepStrictEqual(yaml.parse(output), [
{ 'new-item': 'new-property' },
Expand All @@ -44,11 +43,11 @@ describe('YmlBuilder', () => {

it('Should build an object yaml when the received files are object ymls', async () => {

const ymlBuilder = new YmlBuilder(getPath('valid-object'), outputPath);
const ymlBuilder = makeYmlBuilder('valid-object');

await ymlBuilder.execute();

const output = await fs.readFile(outputPath, 'utf8');
const output = await getOutputFile();

assert.deepStrictEqual(yaml.parse(output), {
'some-item': {
Expand All @@ -64,52 +63,131 @@ describe('YmlBuilder', () => {
});
});

it('Should create the necessary sub directories specified in output file path', async () => {

const recursiveOutputPath = path.join(basePath, 'output-dir', 'output.yml');

const ymlBuilder = makeYmlBuilder('valid-array', recursiveOutputPath);

const mkdirMock = sandbox.mock(fs)
.expects('mkdir')
.withExactArgs(
path.join(process.cwd(), getPath('output-dir')),
{ recursive: true }
)
.returns();

const writeFileMock = sandbox.mock(fs)
.expects('writeFile')
.returns();

await ymlBuilder.execute();

mkdirMock.verify();
sandbox.assert.calledWithMatch(writeFileMock, recursiveOutputPath, sandbox.match.string, { recursive: true });
});

it('Should build an empty output file when the received files are empty', async () => {

const ymlBuilder = new YmlBuilder(getPath('valid-empty'), outputPath);
const ymlBuilder = makeYmlBuilder('valid-empty');

await ymlBuilder.execute();

const output = await fs.readFile(outputPath, 'utf8');
const output = await getOutputFile();

assert.deepStrictEqual(yaml.parse(output), null);
});

it('Should build an empty output file when the received input dir not exists', async () => {

const ymlBuilder = new YmlBuilder(getPath('fake-dir'), outputPath);
const ymlBuilder = makeYmlBuilder('fake-dir');

await ymlBuilder.execute();

const output = await fs.readFile(outputPath, 'utf8');
const output = await getOutputFile();

assert.deepStrictEqual(yaml.parse(output), null);
});

it('Should build an empty output file when the received input dir is empty', async () => {

const ymlBuilder = new YmlBuilder(getPath('empty-input'), outputPath);
const ymlBuilder = makeYmlBuilder('empty-input');

await ymlBuilder.execute();

const output = await fs.readFile(outputPath, 'utf8');
const output = await getOutputFile();

assert.deepStrictEqual(yaml.parse(output), null);
});

it('Should reject when the received files includes array and object ymls mixed', async () => {
it('Should not reject when there are unsupported file extensions in input directory then ignore them', async () => {

const ymlBuilder = makeYmlBuilder('unsupported-exts');

await ymlBuilder.execute();

const output = await getOutputFile();

assert.deepStrictEqual(yaml.parse(output), {
'some-item': {
property: 'some-property'
}
});
});

it('Should reject when the specified output file is not a yml file', async () => {

const ymlBuilder = makeYmlBuilder('valid-array', 'output.txt');

await assert.rejects(ymlBuilder.execute(), {
name: 'YmlBuilderError',
code: YmlBuilderError.codes.INVALID_OUTPUT_FILE
});
});

it('Should reject when can\'t create the output directory (if not exists)', async () => {

const recursiveOutputPath = path.join(basePath, 'output-dir', 'output.yml');

const ymlBuilder = makeYmlBuilder('valid-array', recursiveOutputPath);

const mkdirMock = sandbox.mock(fs)
.expects('mkdir')
.withExactArgs(
path.join(process.cwd(), getPath('output-dir')),
{ recursive: true }
)
.throws();

await assert.rejects(ymlBuilder.execute(), {
name: 'YmlBuilderError',
code: YmlBuilderError.codes.INVALID_OUTPUT_PATH
});

mkdirMock.verify();
});

it('Should reject when the received files includes array and object ymls to combine', async () => {

const ymlBuilder = new YmlBuilder(getPath('invalid'), outputPath);
const ymlBuilder = makeYmlBuilder('invalid');

await assert.rejects(ymlBuilder.execute(), {
name: 'YmlBuilderError',
code: YmlBuilderError.codes.YML_BUILD_ERROR
});
});

it('Should reject when the received files includes object and array ymls to combine', async () => {

const ymlBuilder = makeYmlBuilder('invalid');

const files = await fs.readdir(getPath('invalid'));

const readdirMock = sandbox.mock(fs)
.expects('readdir')
.withArgs(
path.join(process.cwd(), getPath('invalid'))
)
.yields(null, files.reverse());

await assert.rejects(ymlBuilder.execute(), {
Expand All @@ -118,12 +196,11 @@ describe('YmlBuilder', () => {
});

readdirMock.verify();
readdirMock.calledWithExactly(path.join(process.cwd(), getPath('invalid')));
});

it('Should reject when can\'t write the output file', async () => {

const ymlBuilder = new YmlBuilder(getPath('valid-array'), outputPath);
const ymlBuilder = makeYmlBuilder('valid-array');

const writeFileMock = sandbox.mock(fs)
.expects('writeFile')
Expand All @@ -135,15 +212,18 @@ describe('YmlBuilder', () => {
});

writeFileMock.verify();
writeFileMock.calledWithMatch(outputPath, sandbox.match.string, { recursive: true });
sandbox.assert.calledWithMatch(writeFileMock, outputPath, sandbox.match.string, { recursive: true });
});

it('Should reject when can\'t read the input directory files', async () => {

const ymlBuilder = new YmlBuilder(getPath('valid-array'), outputPath);
const ymlBuilder = makeYmlBuilder('valid-array');

const readdirMock = sandbox.mock(fs)
.expects('readdir')
.withArgs(
path.join(process.cwd(), getPath('valid-array'))
)
.throws();

await assert.rejects(ymlBuilder.execute(), {
Expand All @@ -152,7 +232,6 @@ describe('YmlBuilder', () => {
});

readdirMock.verify();
readdirMock.calledWithExactly(path.join(process.cwd(), getPath('valid-array')));
});
});
});

0 comments on commit 3b4bc2e

Please sign in to comment.