Skip to content

Commit

Permalink
test: update some tests for new behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Iiro Jäppinen committed Jun 19, 2019
1 parent 8e79a9e commit 6b4074c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 151 deletions.
44 changes: 14 additions & 30 deletions test/__snapshots__/runAll.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [completed]
LOG Running tasks for *.js [completed]
LOG Running linters... [completed]
LOG Updating stash... [started]
LOG Updating stash... [completed]
LOG Restoring local changes... [started]
LOG Restoring local changes... [completed]"
LOG Clearing temporary stashed changed... [started]
LOG Clearing temporary stashed changed... [completed]"
`;

exports[`runAll should not skip tasks if there are files 1`] = `
"
LOG Stashing changes... [started]
LOG Stashing changes... [skipped]
LOG → No partially staged files found...
LOG Stashing changes... [completed]
LOG Running linters... [started]
LOG Running tasks for *.js [started]
LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [completed]
LOG Running tasks for *.js [completed]
LOG Running linters... [completed]"
LOG Running linters... [completed]
LOG Clearing temporary stashed changed... [started]
LOG Clearing temporary stashed changed... [completed]"
`;

exports[`runAll should resolve the promise with no files 1`] = `
Expand All @@ -46,36 +45,22 @@ LOG →
LOG Running tasks for *.js [failed]
LOG →
LOG Running linters... [failed]
LOG Updating stash... [started]
LOG Updating stash... [skipped]
LOG → Skipping stash update since some tasks exited with errors
LOG Restoring local changes... [started]
LOG Restoring local changes... [completed]
LOG Clearing temporary stashed changed... [started]
LOG Clearing temporary stashed changed... [completed]
LOG {
name: 'ListrError',
errors: [
{
privateMsg: '\\\\n\\\\n\\\\n‼ echo \\"sample\\" was terminated with SIGINT',
context: {hasStash: true, hasErrors: true}
context: {hasErrors: true}
}
],
context: {hasStash: true, hasErrors: true}
context: {hasErrors: true}
}"
`;

exports[`runAll should skip stashing and restoring if there are no partially staged files 1`] = `
"
LOG Stashing changes... [started]
LOG Stashing changes... [skipped]
LOG → No partially staged files found...
LOG Running linters... [started]
LOG Running tasks for *.js [started]
LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [completed]
LOG Running tasks for *.js [completed]
LOG Running linters... [completed]"
`;

exports[`runAll should skip stashing changes if no lint-staged files are changed 1`] = `
"
LOG No staged files match any of provided globs."
Expand All @@ -93,20 +78,19 @@ LOG →
LOG Running tasks for *.js [failed]
LOG →
LOG Running linters... [failed]
LOG Updating stash... [started]
LOG Updating stash... [skipped]
LOG → Skipping stash update since some tasks exited with errors
LOG Restoring local changes... [started]
LOG Restoring local changes... [completed]
LOG Clearing temporary stashed changed... [started]
LOG Clearing temporary stashed changed... [completed]
LOG {
name: 'ListrError',
errors: [
{
privateMsg: '\\\\n\\\\n\\\\n× echo \\"sample\\" found some errors. Please fix them and try committing again.\\\\n\\\\nLinter finished with error',
context: {hasStash: true, hasErrors: true}
context: {hasErrors: true}
}
],
context: {hasStash: true, hasErrors: true}
context: {hasErrors: true}
}"
`;

Expand Down
135 changes: 50 additions & 85 deletions test/gitStash.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const execa = require('execa')
const path = require('path')
const tmp = require('tmp')
const gitflow = require('../src/gitWorkflow')
Expand Down Expand Up @@ -68,42 +67,7 @@ describe('git', () => {
wcDir.removeCallback()
})

describe('hasPartiallyStagedFiles', () => {
it('should return false if files are not staged', async () => {
const res = await gitflow.hasPartiallyStagedFiles(gitOpts)
expect(res).toEqual(false)
})

it('should return false if there are no modified files exist', async () => {
await gitflow.execGit(['checkout', '.'], gitOpts)
const res = await gitflow.hasPartiallyStagedFiles(gitOpts)
expect(res).toEqual(false)
})

it('should return false if changes are already in the index', async () => {
await gitflow.execGit(['checkout', 'test.css'], gitOpts)
await gitflow.execGit(['add', 'test.js'], gitOpts)
const res = await gitflow.hasPartiallyStagedFiles(gitOpts)
expect(res).toEqual(false)
})

it('should return false if there are untracked files', async () => {
const touch = process.platform === 'win32' ? 'echo.>' : 'touch'
await execa(touch, ['untracked.file'], gitOpts)
const res = await gitflow.hasPartiallyStagedFiles(gitOpts)
expect(res).toEqual(false)
})

it('should return true if files are modified and in the index', async () => {
await gitflow.execGit(['checkout', 'test.css'], gitOpts)
await gitflow.execGit(['add', 'test.js'], gitOpts)
await fsp.writeFile(path.join(wcDirPath, 'test.js'), '')
const res = await gitflow.hasPartiallyStagedFiles(gitOpts)
expect(res).toEqual(true)
})
})

describe('gitStashSave/gitStashPop', () => {
describe('saveStagedFiles/restoreStagedFiles', () => {
it('should stash and restore WC state without a commit', async () => {
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
Expand All @@ -118,11 +82,14 @@ M test.js"
`)

// Stashing files
await gitflow.gitStashSave(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
await gitflow.saveStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
Expand All @@ -139,11 +106,14 @@ D test.js"
`)

// Stashing files
await gitflow.gitStashSave(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`"D test.js"`)
await gitflow.saveStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
D test.js"
`)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
D test.js"
Expand All @@ -160,11 +130,14 @@ D test.js"
`)

// Stashing files
await gitflow.gitStashSave(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`"R test.js -> test-renamed.js"`)
await gitflow.saveStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
"R test.js -> test-renamed.js
M test.css"
`)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
"R test.js -> test-renamed.js
M test.css"
Expand All @@ -183,13 +156,18 @@ D test.js"
`)

// Stashing files
await gitflow.gitStashSave(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`"?? test-renamed.js"`)
await gitflow.saveStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
D test.js
?? test-renamed.js"
`)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
D test.js
?? test-renamed.js"
`)
})
Expand All @@ -213,10 +191,8 @@ M test.js"
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)
await gitflow.saveStagedFiles(gitOpts)

// Only index should remain
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Do additional edits (imitate eslint --fix)
Expand All @@ -229,12 +205,15 @@ M test.js"
await fsp.writeFile(path.join(wcDirPath, 'test.js'), eslintContent)

// Expect both indexed and modified state on one file
expect(await gitStatus()).toMatchInlineSnapshot(`"MM test.js"`)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js"
`)
// and index isn't modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)
// Expect stashed files to be back
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
Expand Down Expand Up @@ -272,10 +251,7 @@ MM test.js"
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
await gitflow.saveStagedFiles(gitOpts)

// Do additional edits (imitate eslint --fix)
await fsp.writeFile(
Expand All @@ -285,13 +261,16 @@ MM test.js"
};`
)

// Expect both indexed and modified state on one file
expect(await gitStatus()).toMatchInlineSnapshot(`"MM test.js"`)
// Expect both indexed and modified state
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js"
`)
// and index isn't modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -321,10 +300,7 @@ M test.js"
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
await gitflow.saveStagedFiles(gitOpts)

// Do additional edits (imitate eslint --fix)
const newContent = `module.exports = {
Expand All @@ -333,18 +309,16 @@ M test.js"
await fsp.writeFile(path.join(wcDirPath, 'test.js'), newContent)
// and add to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
await gitflow.updateStash(gitOpts)
const newIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Expect only index changes
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)
// and index is modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).not.toEqual(initialIndex)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(newIndex)

// Restoring state
await gitflow.gitStashPop(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
Expand Down Expand Up @@ -382,10 +356,8 @@ MM test.js"
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)
await gitflow.saveStagedFiles(gitOpts)

// Only index should remain
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Do additional edits (imitate eslint --fix)
Expand All @@ -397,25 +369,21 @@ MM test.js"
)
// and add to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
await gitflow.updateStash(gitOpts)
const newIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Expect index is modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).not.toEqual(initialIndex)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(newIndex)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js"
`)
// and content is back to user modifications
expect(await readFile('test.js')).toEqual(userContent)
// Expect formatting changes in the index
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(newIndex)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)
})

it('should add hooks fixes to index and working copy on partially staged files', async () => {
Expand Down Expand Up @@ -462,10 +430,8 @@ MM test.js"
const initialIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Stashing state
await gitflow.gitStashSave(gitOpts)
await gitflow.saveStagedFiles(gitOpts)

// Only index should remain
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Imitate running prettier on the version from the index
Expand All @@ -483,11 +449,10 @@ MM test.js"
};`
)
await gitflow.execGit(['add', 'test.js'], gitOpts)
await gitflow.updateStash(gitOpts)
const indexAfterEslint = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Restoring state
await gitflow.gitStashPop(gitOpts)
await gitflow.restoreStagedFiles(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toMatchInlineSnapshot(`"MM test.js"`)
Expand Down

0 comments on commit 6b4074c

Please sign in to comment.