Skip to content

Commit

Permalink
fix(#26): refactor the checking if directory exists and raising warning
Browse files Browse the repository at this point in the history
into a separate function and write unit tests for it
  • Loading branch information
sugat009 committed Jan 25, 2024
1 parent f722e20 commit 75966a7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
23 changes: 15 additions & 8 deletions src/fn/csv-to-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ const execute = () => {
const jsonDir = `${environment.pathToProject}/json_docs`;
fs.mkdir(jsonDir);

if (!fs.isDirectoryEmpty(jsonDir)) {
warn(`There are already docs in ${jsonDir}.
New json files will be created along side these existing docs.`);
if (!userPrompt.keyInYN('Are you sure you want to continue?')) {
throw new Error('User aborted execution.');
}
}
const warningMsg = `There are already docs in ${jsonDir}.
New json files will be created along side these existing docs.`;
warnIfDirectoryIsNotEmpty(jsonDir, warningMsg);

const saveJsonDoc = doc => fs.write(`${jsonDir}/${doc._id}.doc.json`, safeStringify(doc) + '\n');

Expand Down Expand Up @@ -159,6 +155,16 @@ const execute = () => {
}
};

function warnIfDirectoryIsNotEmpty(dir, warningMsg) {
if (!fs.isDirectoryEmpty(dir)) {
warn(warningMsg);

if (!userPrompt.keyInYN('Are you sure you want to continue?')) {
throw new Error('User aborted execution.');
}
}
}

function setCol(doc, col, val) {
const colParts = col.split('.');

Expand Down Expand Up @@ -271,5 +277,6 @@ module.exports = {
int,
setCol,
parseColumn,
removeExcludedField
removeExcludedField,
warnIfDirectoryIsNotEmpty,
};
55 changes: 52 additions & 3 deletions test/fn/csv-to-docs.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const { assert } = require('chai');
const { expect } = require('chai');
const sinon = require('sinon');

const csvToDocs = require('../../src/fn/csv-to-docs');
const environment = require('../../src/lib/environment');
const fs = require('../../src/lib/sync-fs');
const userPrompt = require('../../src/lib/user-prompt');
const { warn } = require('../../src/lib/log');

let clock;

Expand Down Expand Up @@ -36,7 +38,7 @@ describe('csv-to-docs', function() {
const expectedDocsDir = `${dir}/expected-json_docs`;

// then
assert.equal(countFilesInDir(generatedDocsDir),
expect(countFilesInDir(generatedDocsDir)).to.equal(
countFilesInDir(expectedDocsDir ),
`Different number of files in ${generatedDocsDir} and ${expectedDocsDir}.`);

Expand All @@ -47,7 +49,7 @@ describe('csv-to-docs', function() {
const generated = fs.read(`${generatedDocsDir}/${file}`);

// and
assert.equal(generated, expected, `Different contents for "${file}"`);
expect(generated).to.equal(expected, `Different contents for "${file}"`);
});
})
.then(done)
Expand All @@ -57,6 +59,53 @@ describe('csv-to-docs', function() {

});

describe('#warnIfDirectoryIsNotEmpty()', () => {
afterEach(() => {
sinon.restore();
});

const warnSpy = sinon.spy(warn);

// Test case 1: Directory is empty, no warning should be triggered
it('should not trigger warning if directory is empty', () => {
const dir = 'emptyDirectory';
const warningMsg = 'This is a warning message';
sinon.stub(fs, 'isDirectoryEmpty').returns(true);
sinon.stub(userPrompt, 'keyInYN').returns(true);

csvToDocs.warnIfDirectoryIsNotEmpty(dir, warningMsg);

expect(userPrompt.keyInYN.called).to.be.false;
});

// Test case 2: Directory is not empty, warning and user prompt should be triggered
it('should trigger warning and user prompt if directory is not empty', () => {
const dir = 'nonEmptyDirectory';
const warningMsg = 'This is a warning message';

sinon.stub(fs, 'isDirectoryEmpty').returns(false);
sinon.stub(userPrompt, 'keyInYN').returns(true);

expect(() => csvToDocs.warnIfDirectoryIsNotEmpty(dir, warningMsg)).to.not.throw();

expect(warnSpy.calledWith(warningMsg));
expect(userPrompt.keyInYN.calledWith('Are you sure you want to continue?')).to.be.true;
});

// Test case 3: User chooses not to continue, an error should be thrown
it('should throw an error if user chooses not to continue', () => {
const dir = 'nonEmptyDirectory';
const warningMsg = 'This is a warning message';

sinon.stub(fs, 'isDirectoryEmpty').returns(false);
sinon.stub(userPrompt, 'keyInYN').returns(false);

expect(() => csvToDocs.warnIfDirectoryIsNotEmpty(dir, warningMsg)).to.throw('User aborted execution.');

expect(warnSpy.calledWith(warningMsg));
expect(userPrompt.keyInYN.calledWith('Are you sure you want to continue?')).to.be.true;
});
});
});

const countFilesInDir = path => fs.fs.readdirSync(path).length;
4 changes: 1 addition & 3 deletions test/lib/sync-fs.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const assert = require('chai').assert;

const chai = require('chai');
const fs = require('../../src/lib/sync-fs');
// NOTE: be careful with the names, this one is the original fs module
Expand All @@ -19,7 +17,7 @@ describe('sync-fs', () => {
].forEach(([input, expected]) => {

it(`should convert ${input} to ${expected}`, () => {
assert.equal(fs.withoutExtension(input), expected);
expect(fs.withoutExtension(input), expected);
});
});
});
Expand Down

0 comments on commit 75966a7

Please sign in to comment.