|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { window as vscodeWindow } from 'vscode'; |
| 3 | + |
| 4 | +import { GitExecutor } from '../../common/git/gitExecutor'; |
| 5 | +import { PrCloneInPlaceService } from '../../services/prCloneInPlaceService'; |
| 6 | +import { mockLogService } from '../e2e/helpers/mockLogService'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Regression tests for issue #207: "Cherry-pick continue-vs-skip decided by workdir |
| 10 | + * dirtiness — silently drops commits". `cherryPickNext(true)` must decide continue vs. |
| 11 | + * skip vs. prompt from real cherry-pick state (`getUnresolvedConflicts()` + |
| 12 | + * `isWorkdirHasChanges()`), never silently skip a legitimately-empty resolution. |
| 13 | + */ |
| 14 | +describe('PrCloneInPlaceService.cherryPickNext(true) conflict-state decision', () => { |
| 15 | + interface GitCalls { |
| 16 | + getUnresolvedConflicts: number; |
| 17 | + isWorkdirHasChanges: number; |
| 18 | + cherryPickContinue: number; |
| 19 | + cherryPickSkip: number; |
| 20 | + commitAllowEmpty: number; |
| 21 | + } |
| 22 | + |
| 23 | + const createGitStub = (opts: { unresolvedConflicts?: string[]; workdirHasChanges?: boolean }) => { |
| 24 | + const calls: GitCalls = { |
| 25 | + getUnresolvedConflicts: 0, |
| 26 | + isWorkdirHasChanges: 0, |
| 27 | + cherryPickContinue: 0, |
| 28 | + cherryPickSkip: 0, |
| 29 | + commitAllowEmpty: 0, |
| 30 | + }; |
| 31 | + |
| 32 | + const gitStub = { |
| 33 | + hasConflicts: async () => false, |
| 34 | + getUnresolvedConflicts: async () => { |
| 35 | + calls.getUnresolvedConflicts += 1; |
| 36 | + return opts.unresolvedConflicts ?? []; |
| 37 | + }, |
| 38 | + isWorkdirHasChanges: async () => { |
| 39 | + calls.isWorkdirHasChanges += 1; |
| 40 | + return opts.workdirHasChanges ?? false; |
| 41 | + }, |
| 42 | + cherryPickContinue: async () => { |
| 43 | + calls.cherryPickContinue += 1; |
| 44 | + }, |
| 45 | + cherryPickSkip: async () => { |
| 46 | + calls.cherryPickSkip += 1; |
| 47 | + }, |
| 48 | + commitAllowEmpty: async () => { |
| 49 | + calls.commitAllowEmpty += 1; |
| 50 | + }, |
| 51 | + // Reached only after the decision under test; make it fail predictably so the |
| 52 | + // remainder of cherryPickNext's "commit landed" flow short-circuits via the |
| 53 | + // existing try/catch, without needing to mock the full push/PR-creation chain. |
| 54 | + pushBranchToGitHub: async () => { |
| 55 | + throw new Error('stop-after-decision (expected in this test)'); |
| 56 | + }, |
| 57 | + } as unknown as GitExecutor; |
| 58 | + |
| 59 | + return { gitStub, calls }; |
| 60 | + }; |
| 61 | + |
| 62 | + // A fake commit generator that is immediately "done" — cherryPickNext's post-decision |
| 63 | + // code path (push/PR creation) is not what these tests exercise; see pushBranchToGitHub |
| 64 | + // stub above for how that path is short-circuited. |
| 65 | + const fakeDoneGenerator = () => |
| 66 | + (async function* () { |
| 67 | + // no commits to yield |
| 68 | + })(); |
| 69 | + |
| 70 | + const createService = (gitStub: GitExecutor) => { |
| 71 | + const service = new PrCloneInPlaceService(gitStub, {} as any, mockLogService); |
| 72 | + (service as any).commitGenerator = fakeDoneGenerator(); |
| 73 | + return service; |
| 74 | + }; |
| 75 | + |
| 76 | + let originalShowWarningMessage: typeof vscodeWindow.showWarningMessage; |
| 77 | + let originalShowQuickPick: typeof vscodeWindow.showQuickPick; |
| 78 | + let originalShowErrorMessage: typeof vscodeWindow.showErrorMessage; |
| 79 | + |
| 80 | + const stubWindow = () => { |
| 81 | + const warnings: string[] = []; |
| 82 | + let quickPickResponse: string | undefined; |
| 83 | + |
| 84 | + originalShowWarningMessage = vscodeWindow.showWarningMessage; |
| 85 | + originalShowQuickPick = vscodeWindow.showQuickPick; |
| 86 | + originalShowErrorMessage = vscodeWindow.showErrorMessage; |
| 87 | + |
| 88 | + (vscodeWindow as any).showWarningMessage = async (message: string) => { |
| 89 | + warnings.push(message); |
| 90 | + return undefined; |
| 91 | + }; |
| 92 | + let quickPickCallCount = 0; |
| 93 | + (vscodeWindow as any).showQuickPick = async () => { |
| 94 | + quickPickCallCount += 1; |
| 95 | + return quickPickResponse; |
| 96 | + }; |
| 97 | + (vscodeWindow as any).showErrorMessage = async () => undefined; |
| 98 | + |
| 99 | + return { |
| 100 | + warnings, |
| 101 | + setQuickPickResponse: (value: string | undefined) => { |
| 102 | + quickPickResponse = value; |
| 103 | + }, |
| 104 | + getQuickPickCallCount: () => quickPickCallCount, |
| 105 | + }; |
| 106 | + }; |
| 107 | + |
| 108 | + const restoreWindow = () => { |
| 109 | + (vscodeWindow as any).showWarningMessage = originalShowWarningMessage; |
| 110 | + (vscodeWindow as any).showQuickPick = originalShowQuickPick; |
| 111 | + (vscodeWindow as any).showErrorMessage = originalShowErrorMessage; |
| 112 | + }; |
| 113 | + |
| 114 | + it('unresolved conflicts remain: warns with the file list, never continues or skips', async () => { |
| 115 | + const { gitStub, calls } = createGitStub({ unresolvedConflicts: ['src/foo.ts', 'src/bar.ts'] }); |
| 116 | + const service = createService(gitStub); |
| 117 | + const { warnings } = stubWindow(); |
| 118 | + |
| 119 | + try { |
| 120 | + await service.cherryPickNext(true); |
| 121 | + } finally { |
| 122 | + restoreWindow(); |
| 123 | + } |
| 124 | + |
| 125 | + assert.strictEqual(calls.getUnresolvedConflicts, 1); |
| 126 | + assert.strictEqual(calls.cherryPickContinue, 0, 'must not continue with unresolved conflicts'); |
| 127 | + assert.strictEqual(calls.cherryPickSkip, 0, 'must not skip with unresolved conflicts'); |
| 128 | + assert.strictEqual(calls.commitAllowEmpty, 0); |
| 129 | + assert.strictEqual(calls.isWorkdirHasChanges, 0, 'must not even consult workdir dirtiness'); |
| 130 | + assert.strictEqual(warnings.length, 1); |
| 131 | + assert.match(warnings[0], /src\/foo\.ts/); |
| 132 | + assert.match(warnings[0], /src\/bar\.ts/); |
| 133 | + }); |
| 134 | + |
| 135 | + it('resolved, non-empty result: continues the cherry-pick, no prompt shown', async () => { |
| 136 | + const { gitStub, calls } = createGitStub({ unresolvedConflicts: [], workdirHasChanges: true }); |
| 137 | + const service = createService(gitStub); |
| 138 | + const { getQuickPickCallCount } = stubWindow(); |
| 139 | + |
| 140 | + try { |
| 141 | + await service.cherryPickNext(true); |
| 142 | + } catch { |
| 143 | + // expected: the post-decision push/PR flow short-circuits via the stubbed |
| 144 | + // pushBranchToGitHub rejection — irrelevant to this test. |
| 145 | + } finally { |
| 146 | + restoreWindow(); |
| 147 | + } |
| 148 | + |
| 149 | + assert.strictEqual(calls.cherryPickContinue, 1, 'must continue when the result is non-empty'); |
| 150 | + assert.strictEqual(calls.cherryPickSkip, 0); |
| 151 | + assert.strictEqual(calls.commitAllowEmpty, 0); |
| 152 | + assert.strictEqual(getQuickPickCallCount(), 0, 'must not prompt when the result is non-empty'); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('resolved, empty result (the issue #207 failure scenario)', () => { |
| 156 | + it('never silently skips — prompts, and "Keep as empty commit" commits an empty commit', async () => { |
| 157 | + const { gitStub, calls } = createGitStub({ unresolvedConflicts: [], workdirHasChanges: false }); |
| 158 | + const service = createService(gitStub); |
| 159 | + const { setQuickPickResponse, getQuickPickCallCount } = stubWindow(); |
| 160 | + setQuickPickResponse('Keep as empty commit'); |
| 161 | + |
| 162 | + try { |
| 163 | + await service.cherryPickNext(true); |
| 164 | + } catch { |
| 165 | + // expected short-circuit past the decision under test; see stub comment above. |
| 166 | + } finally { |
| 167 | + restoreWindow(); |
| 168 | + } |
| 169 | + |
| 170 | + assert.strictEqual(getQuickPickCallCount(), 1, 'must prompt instead of silently skipping'); |
| 171 | + assert.strictEqual(calls.commitAllowEmpty, 1); |
| 172 | + assert.strictEqual(calls.cherryPickSkip, 0); |
| 173 | + assert.strictEqual(calls.cherryPickContinue, 0); |
| 174 | + }); |
| 175 | + |
| 176 | + it('"Skip this commit" explicitly chosen skips the commit', async () => { |
| 177 | + const { gitStub, calls } = createGitStub({ unresolvedConflicts: [], workdirHasChanges: false }); |
| 178 | + const service = createService(gitStub); |
| 179 | + const { setQuickPickResponse } = stubWindow(); |
| 180 | + setQuickPickResponse('Skip this commit'); |
| 181 | + |
| 182 | + try { |
| 183 | + await service.cherryPickNext(true); |
| 184 | + } catch { |
| 185 | + // expected short-circuit past the decision under test; see stub comment above. |
| 186 | + } finally { |
| 187 | + restoreWindow(); |
| 188 | + } |
| 189 | + |
| 190 | + assert.strictEqual(calls.cherryPickSkip, 1); |
| 191 | + assert.strictEqual(calls.commitAllowEmpty, 0); |
| 192 | + assert.strictEqual(calls.cherryPickContinue, 0); |
| 193 | + }); |
| 194 | + |
| 195 | + it('dismissing the prompt (Escape) aborts the clone rather than skipping or continuing', async () => { |
| 196 | + const { gitStub, calls } = createGitStub({ unresolvedConflicts: [], workdirHasChanges: false }); |
| 197 | + const service = createService(gitStub); |
| 198 | + const { setQuickPickResponse } = stubWindow(); |
| 199 | + setQuickPickResponse(undefined); |
| 200 | + |
| 201 | + try { |
| 202 | + await service.cherryPickNext(true); |
| 203 | + } finally { |
| 204 | + restoreWindow(); |
| 205 | + } |
| 206 | + |
| 207 | + assert.strictEqual(calls.cherryPickSkip, 0); |
| 208 | + assert.strictEqual(calls.commitAllowEmpty, 0); |
| 209 | + assert.strictEqual(calls.cherryPickContinue, 0); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments