diff --git a/lib/file.js b/lib/file.js index a81084366..68339b3c7 100644 --- a/lib/file.js +++ b/lib/file.js @@ -20,30 +20,40 @@ const exists = async filename => { /** * Read contents of a file to buffer * @param {String} filename - * @param {Boolean} [rejectENOENT=false] — Whether to throw if the file doesn't exist + * @param {Boolean} [ignoreENOENT=true] — Whether to throw if the file doesn't exist * @returns {Promise} */ -const readFile = async (filename, rejectENOENT = false) => { +const readFile = async (filename, ignoreENOENT = true) => { debug('Reading file `%s`', filename) try { return await fs.readFile(filename) } catch (error) { - if (!rejectENOENT && error.code === 'ENOENT') { + if (ignoreENOENT && error.code === 'ENOENT') { debug("File `%s` doesn't exist, ignoring...", filename) return null // no-op file doesn't exist + } else { + throw error } - throw error } } /** * Unlink a file if it exists * @param {String} filename + * @param {Boolean} [ignoreENOENT=true] — Whether to throw if the file doesn't exist */ -const unlink = async filename => { +const unlink = async (filename, ignoreENOENT = true) => { if (filename) { - await fs.access(filename) - await fs.unlink(filename) + debug('Unlinking file `%s`', filename) + try { + await fs.unlink(filename) + } catch (error) { + if (ignoreENOENT && error.code === 'ENOENT') { + debug("File `%s` doesn't exist, ignoring...", filename) + } else { + throw error + } + } } } diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index 749764cb0..df4a6f813 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -140,7 +140,7 @@ class GitWorkflow { .split('\n') .filter(Boolean) .map(file => path.resolve(this.gitDir, file)) - await Promise.all(untrackedFiles.map(unlink)) + await Promise.all(untrackedFiles.map(file => unlink(file))) // Get a diff of unstaged changes by diffing the saved stash against what's left on disk. await this.execGit([ @@ -236,7 +236,7 @@ class GitWorkflow { } // If stashing resurrected deleted files, clean them out - await Promise.all(this.deletedFiles.map(unlink)) + await Promise.all(this.deletedFiles.map(file => unlink(file))) } /** @@ -251,7 +251,7 @@ class GitWorkflow { debug('Done restoring original state!') // If stashing resurrected deleted files, clean them out - await Promise.all(this.deletedFiles.map(unlink)) + await Promise.all(this.deletedFiles.map(file => unlink(file))) // Restore meta information about ongoing git merge await this.restoreMergeStatus() diff --git a/test/file.spec.js b/test/file.spec.js new file mode 100644 index 000000000..48badfaab --- /dev/null +++ b/test/file.spec.js @@ -0,0 +1,17 @@ +import { unlink, readFile } from '../lib/file' + +describe('unlink', () => { + it('should throw when second argument is false and file is not found', async () => { + await expect(unlink('example', false)).rejects.toThrowErrorMatchingInlineSnapshot( + `"ENOENT: no such file or directory, unlink 'example'"` + ) + }) +}) + +describe('readFile', () => { + it('should throw when second argument is false and file is not found', async () => { + await expect(readFile('example', false)).rejects.toThrowErrorMatchingInlineSnapshot( + `"ENOENT: no such file or directory, open 'example'"` + ) + }) +}) diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js index 5b4c109e7..092e585a1 100644 --- a/test/runAll.unmocked.spec.js +++ b/test/runAll.unmocked.spec.js @@ -609,7 +609,14 @@ describe('runAll', () => { await fs.remove(readmeFile) // Remove file from previous commit await appendFile('test.js', testJsFilePretty) await execGit(['add', 'test.js']) - await runAll({ cwd, config: { '*.{js,md}': 'prettier --list-different' } }) + + try { + await runAll({ cwd, config: { '*.{js,md}': 'prettier --list-different' } }) + } catch (error) { + globalConsoleTemp.warn(error) + globalConsoleTemp.error(console.printHistory()) + } + const exists = await fs.exists(readmeFile) expect(exists).toEqual(false) })