Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid stashing if no lint-staged files are changed (#570) #573

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure I understand how this is working. Mind explaining in code comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the logic of this slightly in my next commit but also added a comment as you requested.

})

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 () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove only to enable all tests again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! I've fixed this in my next commit :)

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)
})
})