From d12639ca4c70c7478445fc6c7418a2e7891ef23f Mon Sep 17 00:00:00 2001 From: Adam Gruber Date: Fri, 5 Nov 2021 10:59:55 -0400 Subject: [PATCH] Revert fsu.writeFileUnique usage --- src/lib/main.js | 17 ++++++++++++----- test/spec/lib/main.test.js | 16 ++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/lib/main.js b/src/lib/main.js index 2412729..0b6f3b2 100644 --- a/src/lib/main.js +++ b/src/lib/main.js @@ -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)) + ); + }); } /** diff --git a/test/spec/lib/main.test.js b/test/spec/lib/main.test.js index 087c0d9..b595be6 100644 --- a/test/spec/lib/main.test.js +++ b/test/spec/lib/main.test.js @@ -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')}`) @@ -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')}`) @@ -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('')); }); @@ -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')}`) @@ -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')}`) @@ -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')}`) @@ -217,7 +217,7 @@ describe('lib/main', () => { reportDir: 'test', reportFilename: 'test', }; - writeFileUniqueStub.resolves(null); + writeFileUniqueStub.yields(null); const expectedFilename = path.resolve( process.cwd(), 'test', @@ -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' );