Skip to content

Commit

Permalink
fix(#26): move warnIfDirectoryIsNotEmpty to sync-fs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Feb 1, 2024
1 parent 4a5dd9a commit 3fa0392
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
14 changes: 1 addition & 13 deletions src/fn/csv-to-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const fs = require('../lib/sync-fs');
const { info, warn } = require('../lib/log');
const generateCsv = require('../lib/generate-users-csv');
const safeStringify = require('../lib/safe-stringify');
const userPrompt = require('../lib/user-prompt');

const pretty = o => JSON.stringify(o, null, 2);

Expand All @@ -32,7 +31,7 @@ const execute = () => {

const warningMsg = `There are already docs in ${jsonDir}.
New json files will be created along side these existing docs.`;
warnIfDirectoryIsNotEmpty(jsonDir, warningMsg);
fs.warnIfDirectoryIsNotEmpty(jsonDir, warningMsg);

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

Expand Down Expand Up @@ -155,16 +154,6 @@ 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 @@ -278,5 +267,4 @@ module.exports = {
setCol,
parseColumn,
removeExcludedField,
warnIfDirectoryIsNotEmpty,
};
12 changes: 12 additions & 0 deletions src/lib/sync-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const mkdirp = require('mkdirp').sync;
const os = require('os');
const path = require('path');
const trace = require('../lib/log').trace;
const userPrompt = require('../lib/user-prompt');
const warn = require('../lib/log').warn;

function read(path) {
Expand Down Expand Up @@ -84,6 +85,16 @@ function isDirectoryEmpty(dir) {
return !fs.readdirSync(dir).length;
}

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

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

module.exports = {
copy,
dirs,
Expand All @@ -103,6 +114,7 @@ module.exports = {
deleteFilesInFolder: folderPath => recurseFiles(folderPath).forEach(filePath => fs.unlinkSync(filePath)),
readdir: fs.readdirSync,
statSync: fs.statSync,
warnIfDirectoryIsNotEmpty,
withoutExtension,
write: (path, data, options = 'utf8') => fs.writeFileSync(path, data, options),
writeBinary: (path, content) => fs.writeFileSync(path, content),
Expand Down
46 changes: 0 additions & 46 deletions test/fn/csv-to-docs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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 @@ -59,50 +57,6 @@ describe('csv-to-docs', function() {

});

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

const warnSpy = sinon.spy(warn);

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;
});

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;
});

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;
52 changes: 49 additions & 3 deletions test/lib/sync-fs.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const chai = require('chai');
const { expect } = require('chai');
const fs = require('../../src/lib/sync-fs');
const sinon = require('sinon');

const { expect } = chai;
const userPrompt = require('../../src/lib/user-prompt');
const { warn } = require('../../src/lib/log');

describe('sync-fs', () => {

Expand Down Expand Up @@ -42,4 +42,50 @@ describe('sync-fs', () => {
expect(readdirSyncStub.calledWith(nonEmptyDir)).to.be.true;
});
});

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

const warnSpy = sinon.spy(warn);

it('should not trigger warning if directory is empty', () => {
const dir = 'emptyDirectory';
const warningMsg = 'This is a warning message';

sinon.stub(fs.fs, 'readdirSync').returns([]);
sinon.stub(userPrompt, 'keyInYN').returns(true);

fs.warnIfDirectoryIsNotEmpty(dir, warningMsg);

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

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.fs, 'readdirSync').returns(['file1']);
sinon.stub(userPrompt, 'keyInYN').returns(true);

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

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

it('should throw an error if user chooses not to continue', () => {
const dir = 'nonEmptyDirectory';
const warningMsg = 'This is a warning message';

sinon.stub(fs.fs, 'readdirSync').returns(['file1']);
sinon.stub(userPrompt, 'keyInYN').returns(false);

expect(() => fs.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;
});
});
});

0 comments on commit 3fa0392

Please sign in to comment.