Skip to content

Commit

Permalink
fix: add check for changed lint-staged files before stashing changes
Browse files Browse the repository at this point in the history
- add test to check if stashing is skipped if no lint-staged files are changed

Fixes lint-staged#570
  • Loading branch information
silbinarywolf committed Jan 24, 2019
1 parent 406a0c0 commit 04aa58f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/runAll.js
Expand Up @@ -54,15 +54,21 @@ module.exports = function runAll(config) {
return `No staged files match ${task.pattern}`
}
return false
}
},
hasStagedFiles: () => task.fileList.length > 0
}))

const listrBaseOptions = {
dateFormat: false,
renderer
}

if (tasks.length) {
let hasStagedTaskFiles = false
tasks.forEach(task => {
hasStagedTaskFiles = hasStagedTaskFiles || task.hasStagedFiles()
})

if (hasStagedTaskFiles) {
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})

Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/runAll.spec.js.snap
Expand Up @@ -83,6 +83,8 @@ LOG Running tasks for *.js [completed]
LOG Running linters... [completed]"
`;

exports[`runAll should skip stashing changes if no lint-staged files are changed 1`] = `""`;

exports[`runAll should skip updating stash if there are errors during linting 1`] = `
"
LOG Stashing changes... [started]
Expand Down
27 changes: 27 additions & 0 deletions test/runAll.spec.js
Expand Up @@ -161,4 +161,31 @@ describe('runAll', () => {
expect(err).toEqual('test')
}
})

it.only('should skip stashing changes if no lint-staged files are changed', async () => {
expect.assertions(4)
hasPartiallyStagedFiles.mockImplementationOnce(() => Promise.resolve(true))
sgfMock.mockImplementationOnce((params, callback) => {
callback(null, [{ filename: 'sample.java', status: 'Modified' }])
})
execa.mockImplementationOnce(() =>
Promise.resolve({
stdout: '',
stderr: 'Linter finished with error',
code: 1,
failed: true,
cmd: 'mock cmd'
})
)

try {
await runAll(getConfig({ linters: { '*.js': ['echo "sample"'] } }))
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
expect(gitStashSave).toHaveBeenCalledTimes(0)
expect(updateStash).toHaveBeenCalledTimes(0)
expect(gitStashPop).toHaveBeenCalledTimes(0)
})
})

0 comments on commit 04aa58f

Please sign in to comment.