Skip to content

Commit

Permalink
test: add test for complex additions and deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 28, 2020
1 parent 217dbc0 commit 892d706
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
8 changes: 2 additions & 6 deletions test/file.spec.js
Expand Up @@ -2,16 +2,12 @@ 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'"`
)
await expect(unlink('example', false)).rejects.toThrowError()
})
})

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'"`
)
await expect(readFile('example', false)).rejects.toThrowError()
})
})
38 changes: 30 additions & 8 deletions test/runAll.unmocked.spec.js
Expand Up @@ -609,18 +609,40 @@ describe('runAll', () => {
await fs.remove(readmeFile) // Remove file from previous commit
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])

try {
await runAll({ cwd, config: { '*.{js,md}': 'prettier --list-different' } })
} catch (error) {
globalConsoleTemp.warn(error)
globalConsoleTemp.error(console.printHistory())
}

await runAll({ cwd, config: { '*.{js,md}': 'prettier --list-different' } })
const exists = await fs.exists(readmeFile)
expect(exists).toEqual(false)
})

it('should not resurrect removed files in complex case', async () => {
// Add file to index, and remove it from disk
await appendFile('test.js', testJsFilePretty)
await execGit(['add', 'test.js'])
const testFile = path.resolve(cwd, 'test.js')
await fs.remove(testFile)

// Rename file in index, and remove it from disk
const readmeFile = path.resolve(cwd, 'README.md')
const readme = await readFile(readmeFile)
await fs.remove(readmeFile)
await execGit(['add', readmeFile])
const newReadmeFile = path.resolve(cwd, 'README_NEW.md')
await appendFile(newReadmeFile, readme)
await execGit(['add', newReadmeFile])
await fs.remove(newReadmeFile)

const status = await execGit(['status', '--porcelain'])
expect(status).toMatchInlineSnapshot(`
"RD README.md -> README_NEW.md
AD test.js"
`)

await runAll({ cwd, config: { '*.{js,md}': 'prettier --list-different' } })
expect(await fs.exists(testFile)).toEqual(false)
expect(await fs.exists(newReadmeFile)).toEqual(false)
expect(await execGit(['status', '--porcelain'])).toEqual(status)
})

it('should not resurrect removed files due to git bug when tasks fail', async () => {
const readmeFile = path.resolve(cwd, 'README.md')
await fs.remove(readmeFile) // Remove file from previous commit
Expand Down

0 comments on commit 892d706

Please sign in to comment.