Skip to content

Commit 3ee3023

Browse files
authored
Set scm running context to false if command fails (#3727)
1 parent 9d39870 commit 3ee3023

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

extension/src/cli/dvc/executor.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { join } from 'path'
22
import { EventEmitter } from 'vscode'
33
import { Disposable, Disposer } from '@hediet/std/disposable'
4-
import { Flag, GcPreserveFlag } from './constants'
4+
import { Flag, GcPreserveFlag, UNEXPECTED_ERROR_CODE } from './constants'
55
import { DvcExecutor } from './executor'
66
import { CliResult, CliStarted } from '..'
77
import { createProcess } from '../../process/execution'
88
import { flushPromises, getMockedProcess } from '../../test/util/jest'
99
import { getProcessEnv } from '../../env'
1010
import { Config } from '../../config'
1111
import { ContextKey, setContextValue } from '../../vscode/context'
12+
import { MaybeConsoleError } from '../error'
1213

1314
jest.mock('vscode')
1415
jest.mock('@hediet/std/disposable')
@@ -92,6 +93,37 @@ describe('CliExecutor', () => {
9293
executable: 'dvc'
9394
})
9495
})
96+
97+
it('should set the correct context value if the command fails', async () => {
98+
const cwd = __dirname
99+
const relPath = join('data', 'MNIST', 'raw')
100+
const unexpectedError = new Error(
101+
'unexpected error - something something'
102+
)
103+
const unexpectedStderr = 'This is very unexpected'
104+
;(unexpectedError as MaybeConsoleError).exitCode = UNEXPECTED_ERROR_CODE
105+
;(unexpectedError as MaybeConsoleError).stderr = unexpectedStderr
106+
mockedCreateProcess.mockImplementationOnce(() => {
107+
throw unexpectedError
108+
})
109+
110+
let error
111+
112+
try {
113+
await dvcExecutor.add(cwd, relPath)
114+
} catch (thrownError) {
115+
error = thrownError
116+
}
117+
118+
expect((error as MaybeConsoleError).stderr).toStrictEqual(
119+
unexpectedStderr
120+
)
121+
122+
expect(mockedSetContextValue).toHaveBeenLastCalledWith(
123+
ContextKey.SCM_RUNNING,
124+
false
125+
)
126+
})
95127
})
96128

97129
describe('checkout', () => {

extension/src/cli/dvc/executor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,14 @@ export class DvcExecutor extends DvcCli {
193193

194194
private async blockAndExecuteProcess(cwd: string, ...args: Args) {
195195
this.setRunning(true)
196-
const output = await this.executeDvcProcess(cwd, ...args)
197-
this.setRunning(false)
198-
return output
196+
try {
197+
const output = await this.executeDvcProcess(cwd, ...args)
198+
this.setRunning(false)
199+
return output
200+
} catch (error) {
201+
this.setRunning(false)
202+
throw error
203+
}
199204
}
200205

201206
private setRunning(running: boolean) {

0 commit comments

Comments
 (0)