From 75966a779699883ab7aff56d427baa3c0789a664 Mon Sep 17 00:00:00 2001 From: Sugat Bajracharya Date: Thu, 25 Jan 2024 12:04:53 +0545 Subject: [PATCH] fix(#26): refactor the checking if directory exists and raising warning into a separate function and write unit tests for it --- src/fn/csv-to-docs.js | 23 ++++++++++------ test/fn/csv-to-docs.spec.js | 55 +++++++++++++++++++++++++++++++++++-- test/lib/sync-fs.spec.js | 4 +-- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/fn/csv-to-docs.js b/src/fn/csv-to-docs.js index 3b1106926..66b7a4b4d 100644 --- a/src/fn/csv-to-docs.js +++ b/src/fn/csv-to-docs.js @@ -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'); @@ -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('.'); @@ -271,5 +277,6 @@ module.exports = { int, setCol, parseColumn, - removeExcludedField + removeExcludedField, + warnIfDirectoryIsNotEmpty, }; diff --git a/test/fn/csv-to-docs.spec.js b/test/fn/csv-to-docs.spec.js index 0f9f10f9d..a21580335 100644 --- a/test/fn/csv-to-docs.spec.js +++ b/test/fn/csv-to-docs.spec.js @@ -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; @@ -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}.`); @@ -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) @@ -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; diff --git a/test/lib/sync-fs.spec.js b/test/lib/sync-fs.spec.js index d74c9de73..436fb9159 100644 --- a/test/lib/sync-fs.spec.js +++ b/test/lib/sync-fs.spec.js @@ -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 @@ -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); }); }); });