Skip to content

Commit

Permalink
Revert fsu.writeFileUnique usage
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgruber committed Nov 5, 2021
1 parent 604b165 commit d12639c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
17 changes: 12 additions & 5 deletions src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ const semverRegex = /\d+\.\d+\.\d+(?:-(alpha|beta)\.\d+)?/;
* @return {Promise} Resolves with filename if successfully saved
*/
function saveFile(filename, data, overwrite) {
return overwrite
? fs.outputFile(filename, data).then(() => filename)
: fsu.writeFileUnique(filename.replace(fileExtRegex, '{_###}$&'), data, {
force: true,
});
if (overwrite) {
return fs.outputFile(filename, data).then(() => filename);
}

return new Promise((resolve, reject) => {
fsu.writeFileUnique(
filename.replace(fileExtRegex, '{_###}$&'),
data,
{ force: true },
(err, savedFile) => (err === null ? resolve(savedFile) : reject(err))
);
});
}

/**
Expand Down
16 changes: 8 additions & 8 deletions test/spec/lib/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('lib/main', () => {
it('with timestamp, boolean -> default format', () => {
opts.timestamp = true;
opts.overwrite = false;
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
return mareport.create(testData, opts).then(() => {
expect(writeFileUniqueStub.args[0][0]).to.equal(
getExpectedName(`_${cleanDateStr('isoDateTime')}`)
Expand All @@ -160,7 +160,7 @@ describe('lib/main', () => {
it('with timestamp, true string -> default format', () => {
opts.timestamp = 'true';
opts.overwrite = false;
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
return mareport.create(testData, opts).then(() => {
expect(writeFileUniqueStub.args[0][0]).to.equal(
getExpectedName(`_${cleanDateStr('isoDateTime')}`)
Expand All @@ -171,7 +171,7 @@ describe('lib/main', () => {
it('with timestamp, false string -> no timestamp', () => {
opts.timestamp = 'false';
opts.overwrite = false;
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
return mareport.create(testData, opts).then(() => {
expect(writeFileUniqueStub.args[0][0]).to.equal(getExpectedName(''));
});
Expand All @@ -180,7 +180,7 @@ describe('lib/main', () => {
it('with timestamp, empty string -> default format', () => {
opts.timestamp = '';
opts.overwrite = false;
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
return mareport.create(testData, opts).then(() => {
expect(writeFileUniqueStub.args[0][0]).to.equal(
getExpectedName(`_${cleanDateStr('isoDateTime')}`)
Expand All @@ -191,7 +191,7 @@ describe('lib/main', () => {
it('with timestamp, fullDate format', () => {
opts.timestamp = 'fullDate';
opts.overwrite = false;
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
return mareport.create(testData, opts).then(() => {
expect(writeFileUniqueStub.args[0][0]).to.equal(
getExpectedName(`_${cleanDateStr('fullDate')}`)
Expand All @@ -202,7 +202,7 @@ describe('lib/main', () => {
it('with timestamp, longTime format', () => {
opts.timestamp = 'longTime';
opts.overwrite = false;
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
return mareport.create(testData, opts).then(() => {
expect(writeFileUniqueStub.args[0][0]).to.equal(
getExpectedName(`_${cleanDateStr('longTime')}`)
Expand All @@ -217,7 +217,7 @@ describe('lib/main', () => {
reportDir: 'test',
reportFilename: 'test',
};
writeFileUniqueStub.resolves(null);
writeFileUniqueStub.yields(null);
const expectedFilename = path.resolve(
process.cwd(),
'test',
Expand Down Expand Up @@ -246,7 +246,7 @@ describe('lib/main', () => {

it('rejects when writeFileUnique throws', () => {
opts.overwrite = false;
writeFileUniqueStub.rejects(new Error('save error'));
writeFileUniqueStub.yields(new Error('save error'));
return expect(mareport.create(testData, opts)).to.be.rejectedWith(
'save error'
);
Expand Down

0 comments on commit d12639c

Please sign in to comment.