Skip to content

Commit

Permalink
fix: correct file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 28, 2020
1 parent 7a6dc7a commit a6ece6f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
24 changes: 17 additions & 7 deletions lib/file.js
Expand Up @@ -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<Buffer>}
*/
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
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/gitWorkflow.js
Expand Up @@ -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([
Expand Down Expand Up @@ -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)))
}

/**
Expand All @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions 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'"`
)
})
})
9 changes: 8 additions & 1 deletion test/runAll.unmocked.spec.js
Expand Up @@ -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)
})
Expand Down

0 comments on commit a6ece6f

Please sign in to comment.