GeezerVison is showing github.com — fix: run all tasks when --continue-on-error=true · lint-staged/lint-staged@33d4502
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +'lint-staged': patch | | 3 | +--- | | 4 | + | | 5 | +Fix problems with `--continue-on-error` option, where tasks might have still been killed (`SIGINT`) when one of them failed. |
Collapse file Copy file name to clipboardExpand all lines: lib/getSpawnedTask.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -129,6 +129,7 @@ export const makeErr = (command, error, ctx) => { | 129 | 129 | * @param {Object} options | 130 | 130 | * @param {boolean} [options.color] | 131 | 131 | * @param {string} options.command — Linter task | | 132 | + * @param {string} [options.continueOnError] | 132 | 133 | * @param {string} [options.cwd] | 133 | 134 | * @param {String} options.topLevelDir - Current git repo top-level path | 134 | 135 | * @param {Boolean} options.isFn - Whether the linter task is a function | @@ -139,6 +140,7 @@ export const makeErr = (command, error, ctx) => { | 139 | 140 | export const getSpawnedTask = ({ | 140 | 141 | color, | 141 | 142 | command, | | 143 | + continueOnError = false, | 142 | 144 | cwd = process.cwd(), | 143 | 145 | files, | 144 | 146 | topLevelDir, | @@ -165,15 +167,19 @@ export const getSpawnedTask = ({ | 165 | 167 |
| 166 | 168 | try { | 167 | 169 | const subprocess = spawn(cmd, isFn ? args : args.concat(files), spawnOptions) | 168 | | - quitInterruptCheck = interruptExecutionOnError(ctx, subprocess) | | 170 | + if (!continueOnError) { | | 171 | + quitInterruptCheck = interruptExecutionOnError(ctx, subprocess) | | 172 | + } | 169 | 173 | const result = await subprocess | 170 | 174 | if (verbose) { | 171 | 175 | handleOutput(command, result, ctx) | 172 | 176 | } | 173 | 177 | } catch (error) { | 174 | 178 | throw makeErr(command, error, ctx) | 175 | 179 | } finally { | 176 | | - await quitInterruptCheck() | | 180 | + if (quitInterruptCheck) { | | 181 | + await quitInterruptCheck() | | 182 | + } | 177 | 183 | } | 178 | 184 | } | 179 | 185 | } |
Collapse file Copy file name to clipboardExpand all lines: lib/getSpawnedTasks.js +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,12 +10,21 @@ const debugLog = createDebug('lint-staged:getSpawnedTasks') | 10 | 10 | * @param {object} options | 11 | 11 | * @param {boolean} [options.color] | 12 | 12 | * @param {Array<string|Function>|string|Function} options.commands | | 13 | + * @param {string} options.continueOnError | 13 | 14 | * @param {string} options.cwd | 14 | 15 | * @param {import('./getStagedFiles.js').StagedFile[]} options.files | 15 | 16 | * @param {string} options.topLevelDir | 16 | 17 | * @param {Boolean} verbose | 17 | 18 | */ | 18 | | -export const getSpawnedTasks = async ({ color, commands, cwd, files, topLevelDir, verbose }) => { | | 19 | +export const getSpawnedTasks = async ({ | | 20 | + color, | | 21 | + commands, | | 22 | + continueOnError, | | 23 | + cwd, | | 24 | + files, | | 25 | + topLevelDir, | | 26 | + verbose, | | 27 | +}) => { | 19 | 28 | debugLog('Creating Listr tasks for commands %o', commands) | 20 | 29 | const cmdTasks = [] | 21 | 30 |
| @@ -48,6 +57,7 @@ export const getSpawnedTasks = async ({ color, commands, cwd, files, topLevelDir | 48 | 57 | const task = getSpawnedTask({ | 49 | 58 | color, | 50 | 59 | command, | | 60 | + continueOnError, | 51 | 61 | cwd, | 52 | 62 | files: filepaths, | 53 | 63 | topLevelDir, | |
Collapse file Copy file name to clipboardExpand all lines: lib/runAll.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -224,6 +224,7 @@ export const runAll = async ( | 224 | 224 | : getSpawnedTasks({ | 225 | 225 | color, | 226 | 226 | commands: task.commands, | | 227 | + continueOnError, | 227 | 228 | cwd: groupCwd, | 228 | 229 | files: task.fileList, | 229 | 230 | topLevelDir, | |
Collapse file Copy file name to clipboardExpand all lines: package-lock.json -11Lines changed: 0 additions & 11 deletions Collapse file Copy file name to clipboardExpand all lines: test/unit/runAll.spec.js +42-3Lines changed: 42 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -452,9 +452,19 @@ describe('runAll', () => { | 452 | 452 | }) | 453 | 453 |
| 454 | 454 | it('should reject after running all tasks when continueOnError is true', async ({ expect }) => { | 455 | | - getStagedFiles.mockImplementationOnce(async () => [{ filepath: 'foo.js', status: 'A' }]) | | 455 | + getStagedFiles.mockImplementationOnce(async () => [ | | 456 | + { | | 457 | + filepath: 'foo.js', | | 458 | + status: 'A', | | 459 | + }, | | 460 | + { filepath: 'bar.py', status: 'A' }, | | 461 | + ]) | | 462 | + | 456 | 463 | searchConfigs.mockImplementationOnce(async () => ({ | 457 | | - '': { '*.js': 'echo "failing command"' }, | | 464 | + '': { | | 465 | + '*.js': ['echo "success js command 1"', 'echo "success js command 2"'], | | 466 | + '*.py': 'echo "failing py command"', | | 467 | + }, | 458 | 468 | })) | 459 | 469 |
| 460 | 470 | // Mock first spawn call (git operations) to succeed | @@ -465,7 +475,15 @@ describe('runAll', () => { | 465 | 475 | }) | 466 | 476 | ) | 467 | 477 |
| 468 | | - // Mock second spawn call (the actual task) to fail | | 478 | + // Mock second spawn call (`echo "success js command 1"`) to succeed | | 479 | + spawn.mockImplementationOnce(() => | | 480 | + mockNanoSpawnReturnValue({ | | 481 | + output: 'Success', | | 482 | + nodeChildProcess: { pid: 0 }, | | 483 | + }) | | 484 | + ) | | 485 | + | | 486 | + // Mock second spawn call (`echo "failing py command"`) to fail | 469 | 487 | spawn.mockImplementationOnce(() => | 470 | 488 | mockNanoSpawnReturnValue( | 471 | 489 | Object.assign(new SubprocessError(), { | @@ -475,11 +493,32 @@ describe('runAll', () => { | 475 | 493 | ) | 476 | 494 | ) | 477 | 495 |
| | 496 | + // Mock first spawn call ('success js command 2') to succeed | | 497 | + spawn.mockImplementationOnce(() => | | 498 | + mockNanoSpawnReturnValue( | | 499 | + { | | 500 | + output: 'Success', | | 501 | + nodeChildProcess: { pid: 0 }, | | 502 | + }, | | 503 | + 1000 | | 504 | + ) | | 505 | + ) | | 506 | + | 478 | 507 | mockGitWorkflow.runTasks.mockImplementationOnce(async (ctx, task, { listrTasks }) => { | 479 | 508 | return task.newListr(listrTasks) | 480 | 509 | }) | 481 | 510 |
| 482 | 511 | // With continueOnError: true, should still reject but after running all tasks | 483 | 512 | await expect(runAll({ continueOnError: true })).rejects.toThrow('lint-staged failed') | | 513 | + | | 514 | + expect(console.printHistory()).toMatch( | | 515 | + /"data":"COMPLETED".*"title":"echo \\"success js command 1\\"/ | | 516 | + ) | | 517 | + expect(console.printHistory()).toMatch( | | 518 | + /"data":{"error":"echo \\"failing py command\\" \[FAILED\]"}/ | | 519 | + ) | | 520 | + expect(console.printHistory()).toMatch( | | 521 | + /"data":"COMPLETED".*"title":"echo \\"success js command 2\\"/ | | 522 | + ) | 484 | 523 | }) | 485 | 524 | }) |
|