|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import { createHash } from 'crypto'; |
| 8 | +import { describe, it } from 'mocha'; |
| 9 | +import { posix, win32 } from 'path'; |
| 10 | +import { getVSCodeTestIsolate } from '../../.scripts/vscodeTestPath'; |
| 11 | + |
| 12 | +const posixScriptDirectory = '/worktrees/agent/Extension/.scripts'; |
| 13 | +const windowsScriptDirectory = 'C:\\worktrees\\agent\\Extension\\.scripts'; |
| 14 | + |
| 15 | +function getWorktreeHash(scriptDirectory: string): string { |
| 16 | + return createHash('sha256').update(scriptDirectory).digest('hex').substring(0, 6); |
| 17 | +} |
| 18 | + |
| 19 | +describe('VS Code test isolate path', () => { |
| 20 | + it('uses XDG_CACHE_HOME on Linux', () => { |
| 21 | + assert.strictEqual( |
| 22 | + getVSCodeTestIsolate(posixScriptDirectory, 'linux', { XDG_CACHE_HOME: '/cache' }, '/home/developer'), |
| 23 | + posix.resolve('/cache', 'vscode-cpptools', 'vscode-test', getWorktreeHash(posixScriptDirectory))); |
| 24 | + }); |
| 25 | + |
| 26 | + it('falls back to the user cache directory on Linux', () => { |
| 27 | + const expected = posix.resolve('/home/developer', '.cache', 'vscode-cpptools', 'vscode-test', getWorktreeHash(posixScriptDirectory)); |
| 28 | + |
| 29 | + assert.strictEqual( |
| 30 | + getVSCodeTestIsolate(posixScriptDirectory, 'linux', {}, '/home/developer'), |
| 31 | + expected); |
| 32 | + assert.strictEqual( |
| 33 | + getVSCodeTestIsolate(posixScriptDirectory, 'linux', { XDG_CACHE_HOME: 'relative-cache' }, '/home/developer'), |
| 34 | + expected); |
| 35 | + }); |
| 36 | + |
| 37 | + it('uses the user cache directory on macOS', () => { |
| 38 | + assert.strictEqual( |
| 39 | + getVSCodeTestIsolate(posixScriptDirectory, 'darwin', {}, '/Users/developer'), |
| 40 | + posix.resolve('/Users/developer', 'Library', 'Caches', 'vscode-cpptools', 'vscode-test', getWorktreeHash(posixScriptDirectory))); |
| 41 | + }); |
| 42 | + |
| 43 | + it('uses LOCALAPPDATA on Windows', () => { |
| 44 | + assert.strictEqual( |
| 45 | + getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { LOCALAPPDATA: 'D:\\LocalAppData' }, 'C:\\Users\\developer'), |
| 46 | + win32.resolve('D:\\LocalAppData', 'Microsoft', 'vscode-cpptools', 'vscode-test', getWorktreeHash(windowsScriptDirectory))); |
| 47 | + }); |
| 48 | + |
| 49 | + it('falls back to the user profile on Windows', () => { |
| 50 | + const expected = win32.resolve('C:\\Users\\developer', 'AppData', 'Local', 'Microsoft', 'vscode-cpptools', 'vscode-test', getWorktreeHash(windowsScriptDirectory)); |
| 51 | + |
| 52 | + assert.strictEqual( |
| 53 | + getVSCodeTestIsolate(windowsScriptDirectory, 'win32', {}, 'C:\\Users\\developer'), |
| 54 | + expected); |
| 55 | + assert.strictEqual( |
| 56 | + getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { LOCALAPPDATA: 'relative-cache' }, 'C:\\Users\\developer'), |
| 57 | + expected); |
| 58 | + assert.strictEqual( |
| 59 | + getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { LOCALAPPDATA: '\\relative-cache' }, 'C:\\Users\\developer'), |
| 60 | + expected); |
| 61 | + }); |
| 62 | + |
| 63 | + it('honors CPPTOOLS_VSCODE_TEST_ROOT without sharing worktree isolates', () => { |
| 64 | + const environment = { CPPTOOLS_VSCODE_TEST_ROOT: '/test-root', XDG_CACHE_HOME: '/cache' }; |
| 65 | + const first = getVSCodeTestIsolate('/worktrees/first/Extension/.scripts', 'linux', environment, '/home/developer'); |
| 66 | + const firstAgain = getVSCodeTestIsolate('/worktrees/first/Extension/.scripts', 'linux', environment, '/home/developer'); |
| 67 | + const second = getVSCodeTestIsolate('/worktrees/second/Extension/.scripts', 'linux', environment, '/home/developer'); |
| 68 | + |
| 69 | + assert.strictEqual(first, firstAgain); |
| 70 | + assert.match(posix.basename(first), /^[0-9a-f]{6}$/); |
| 71 | + assert.notStrictEqual(first, second); |
| 72 | + assert.strictEqual(posix.dirname(first), '/test-root'); |
| 73 | + assert.strictEqual(posix.dirname(second), '/test-root'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('accepts fully qualified Windows CPPTOOLS_VSCODE_TEST_ROOT values', () => { |
| 77 | + const driveRoot = 'D:\\test-root'; |
| 78 | + const uncRoot = '\\\\server\\share\\test-root'; |
| 79 | + |
| 80 | + assert.strictEqual( |
| 81 | + getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { CPPTOOLS_VSCODE_TEST_ROOT: driveRoot }, 'C:\\Users\\developer'), |
| 82 | + win32.resolve(driveRoot, getWorktreeHash(windowsScriptDirectory))); |
| 83 | + assert.strictEqual( |
| 84 | + getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { CPPTOOLS_VSCODE_TEST_ROOT: uncRoot }, 'C:\\Users\\developer'), |
| 85 | + win32.resolve(uncRoot, getWorktreeHash(windowsScriptDirectory))); |
| 86 | + }); |
| 87 | + |
| 88 | + it('rejects non-fully-qualified CPPTOOLS_VSCODE_TEST_ROOT values', () => { |
| 89 | + assert.throws( |
| 90 | + () => getVSCodeTestIsolate(posixScriptDirectory, 'linux', { CPPTOOLS_VSCODE_TEST_ROOT: 'test-root' }, '/home/developer'), |
| 91 | + /CPPTOOLS_VSCODE_TEST_ROOT must be a fully qualified absolute path/); |
| 92 | + assert.throws( |
| 93 | + () => getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { CPPTOOLS_VSCODE_TEST_ROOT: 'C:' }, 'C:\\Users\\developer'), |
| 94 | + /CPPTOOLS_VSCODE_TEST_ROOT must be a fully qualified absolute path/); |
| 95 | + assert.throws( |
| 96 | + () => getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { CPPTOOLS_VSCODE_TEST_ROOT: '\\test-root' }, 'C:\\Users\\developer'), |
| 97 | + /CPPTOOLS_VSCODE_TEST_ROOT must be a fully qualified absolute path/); |
| 98 | + assert.throws( |
| 99 | + () => getVSCodeTestIsolate(windowsScriptDirectory, 'win32', { CPPTOOLS_VSCODE_TEST_ROOT: '/test-root' }, 'C:\\Users\\developer'), |
| 100 | + /CPPTOOLS_VSCODE_TEST_ROOT must be a fully qualified absolute path/); |
| 101 | + }); |
| 102 | +}); |
0 commit comments