From 04aa58f43d7dcbbede444586d03abf5fde0d2392 Mon Sep 17 00:00:00 2001 From: Jake Bentvelzen Date: Thu, 24 Jan 2019 15:45:12 +1100 Subject: [PATCH] fix: add check for changed lint-staged files before stashing changes - add test to check if stashing is skipped if no lint-staged files are changed Fixes #570 --- src/runAll.js | 10 ++++++++-- test/__snapshots__/runAll.spec.js.snap | 2 ++ test/runAll.spec.js | 27 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/runAll.js b/src/runAll.js index ab3ba8747..06ac2aa68 100644 --- a/src/runAll.js +++ b/src/runAll.js @@ -54,7 +54,8 @@ module.exports = function runAll(config) { return `No staged files match ${task.pattern}` } return false - } + }, + hasStagedFiles: () => task.fileList.length > 0 })) const listrBaseOptions = { @@ -62,7 +63,12 @@ module.exports = function runAll(config) { 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', () => {}) diff --git a/test/__snapshots__/runAll.spec.js.snap b/test/__snapshots__/runAll.spec.js.snap index 6f20b5ec5..2277b352d 100644 --- a/test/__snapshots__/runAll.spec.js.snap +++ b/test/__snapshots__/runAll.spec.js.snap @@ -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] diff --git a/test/runAll.spec.js b/test/runAll.spec.js index 375f4426f..f1bf96103 100644 --- a/test/runAll.spec.js +++ b/test/runAll.spec.js @@ -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) + }) })