|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | 6 | import { strict as assert } from 'assert'; |
| 7 | +import { mkdtempSync, realpathSync, rmdirSync, symlinkSync, unlinkSync } from 'fs'; |
7 | 8 | import { tmpdir } from 'os'; |
8 | 9 | import * as path from 'path'; |
9 | 10 | import { Uri } from 'vscode'; |
@@ -411,6 +412,36 @@ describe('Update pull request stack branches', function () { |
411 | 412 | }); |
412 | 413 |
|
413 | 414 | describe('orphaned temporary worktrees', function () { |
| 415 | + /** Points the temporary directory at an alias of the real one, as macOS does with /var and /private/var. */ |
| 416 | + async function withTemporaryDirectoryAlias(run: (canonical: string) => Promise<void>): Promise<void> { |
| 417 | + const canonical = realpathSync.native(tmpdir()); |
| 418 | + const directory = mkdtempSync(path.join(canonical, 'stack-path-alias-')); |
| 419 | + const alias = path.join(directory, 'temporary'); |
| 420 | + const variables = ['TMPDIR', 'TMP', 'TEMP']; |
| 421 | + const saved = variables.map(name => process.env[name]); |
| 422 | + try { |
| 423 | + symlinkSync(canonical, alias, 'junction'); |
| 424 | + try { |
| 425 | + for (const name of variables) { |
| 426 | + process.env[name] = alias; |
| 427 | + } |
| 428 | + assert.equal(tmpdir(), alias); |
| 429 | + await run(canonical); |
| 430 | + } finally { |
| 431 | + variables.forEach((name, index) => { |
| 432 | + if (saved[index] === undefined) { |
| 433 | + delete process.env[name]; |
| 434 | + } else { |
| 435 | + process.env[name] = saved[index]; |
| 436 | + } |
| 437 | + }); |
| 438 | + unlinkSync(alias); |
| 439 | + } |
| 440 | + } finally { |
| 441 | + rmdirSync(directory); |
| 442 | + } |
| 443 | + } |
| 444 | + |
414 | 445 | const temporaryWorktree = (name: string, directory = tmpdir()) => ({ |
415 | 446 | // Git reports worktree paths with forward slashes on Windows. |
416 | 447 | name, path: path.join(directory, name).replace(/\\/g, '/'), ref: '', main: false, detached: true, |
@@ -439,6 +470,33 @@ describe('Update pull request stack branches', function () { |
439 | 470 | assert.deepEqual(repository.deletedWorktrees, [exited.path]); |
440 | 471 | }); |
441 | 472 |
|
| 473 | + it('recognizes an orphan when Git reports the canonical temporary directory', async function () { |
| 474 | + await withTemporaryDirectoryAlias(async canonical => { |
| 475 | + const exited = temporaryWorktree('vscode-pr-stack-111-0b9f2c4e-1a2b-4c3d-8e9f-0a1b2c3d4e5f', canonical); |
| 476 | + repository.state.worktrees!.push( |
| 477 | + exited, |
| 478 | + temporaryWorktree('vscode-pr-stack-222-5e4d3c2b-1a0f-4e9d-8c7b-6a5f4e3d2c1b', canonical), |
| 479 | + { ...exited, main: true }, |
| 480 | + temporaryWorktree('vscode-pr-stack-111-0b9f2c4e-1a2b-4c3d-8e9f-0a1b2c3d4e5f', path.join(canonical, 'nested')), |
| 481 | + ); |
| 482 | + await removeOrphanedStackWorktrees(repository, pid => pid === 222); |
| 483 | + assert.deepEqual(repository.deletedWorktrees, [exited.path]); |
| 484 | + }); |
| 485 | + }); |
| 486 | + |
| 487 | + it('keeps an active worktree when Git reports the canonical temporary directory', async function () { |
| 488 | + await withTemporaryDirectoryAlias(async canonical => { |
| 489 | + temporary.conflictingStops = 1; |
| 490 | + await updateStackBranches(repository, open, 'origin', branches, 2, () => undefined, async worktree => { |
| 491 | + repository.state.worktrees!.push(temporaryWorktree(path.basename(repository.createdWorktrees[0]), canonical)); |
| 492 | + await removeOrphanedStackWorktrees(repository); |
| 493 | + assert.deepEqual(repository.deletedWorktrees, []); |
| 494 | + await worktree.rebaseContinue!(); |
| 495 | + return true; |
| 496 | + }); |
| 497 | + }); |
| 498 | + }); |
| 499 | + |
442 | 500 | it('keeps the worktree of an update that is waiting for conflicts to be resolved', async function () { |
443 | 501 | temporary.conflictingStops = 1; |
444 | 502 | let deletedWhileResolving: string[] | undefined; |
|
0 commit comments