|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { execAndCaptureError, silentExec } from '../../utils/process'; |
| 5 | +import { applyVitestBuilder } from '../../utils/vitest'; |
| 6 | +import { stripVTControlCharacters } from 'node:util'; |
| 7 | + |
| 8 | +export default async function (): Promise<void> { |
| 9 | + // This test uses `subst` to map the project directory to a virtual drive letter |
| 10 | + // to simulate running tests from a non-C drive on Windows. |
| 11 | + if (process.platform !== 'win32') { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + await applyVitestBuilder(); |
| 16 | + |
| 17 | + const originalCwd = process.cwd(); |
| 18 | + const driveLetter = 'X:'; // Pick a drive letter that is unlikely to be in use. |
| 19 | + |
| 20 | + try { |
| 21 | + // 1. Map the parent directory of the project to the virtual drive. |
| 22 | + // This avoids running the project from the root of the drive (X:\), which can cause |
| 23 | + // issues with workspace detection. |
| 24 | + const projectParentDir = path.dirname(originalCwd); |
| 25 | + const projectName = path.basename(originalCwd); |
| 26 | + |
| 27 | + await silentExec('subst', driveLetter, projectParentDir); |
| 28 | + |
| 29 | + // 2. Change the current process's working directory to the project folder on the virtual drive. |
| 30 | + const newCwd = path.join(driveLetter + '\\', projectName); |
| 31 | + process.chdir(newCwd); |
| 32 | + |
| 33 | + // Verify that the file system mapping is working as expected. |
| 34 | + assert(fs.existsSync('angular.json'), 'angular.json should exist on the subst drive'); |
| 35 | + |
| 36 | + // 3. Run `ng test`. |
| 37 | + // We expect this to fail with NG0203 in the subst environment due to dual-package hazards |
| 38 | + // (Angular loading from both X: and D:) within bazel. However, the failure proves that the |
| 39 | + // test file was discovered and loaded. |
| 40 | + const error = await execAndCaptureError('ng', ['test', '--watch=false']); |
| 41 | + const output = stripVTControlCharacters(error.message); |
| 42 | + |
| 43 | + console.log('OUTPUT:', output); |
| 44 | + |
| 45 | + // 4. Verify that Vitest found the test file and identified the tests within it. |
| 46 | + assert.match( |
| 47 | + output, |
| 48 | + /src\/app\/app\.spec\.ts \(2 tests/, |
| 49 | + 'Expected tests to be discovered and loaded, even if execution fails due to subst aliasing.', |
| 50 | + ); |
| 51 | + } finally { |
| 52 | + // 5. Teardown: Restore CWD and remove the virtual drive mapping. |
| 53 | + try { |
| 54 | + process.chdir(originalCwd); |
| 55 | + } catch (e) { |
| 56 | + console.error('Failed to restore CWD:', e); |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + await silentExec('subst', driveLetter, '/d'); |
| 61 | + } catch (e) { |
| 62 | + // Ignore errors if the drive wasn't mounted or if unmount fails (best effort) |
| 63 | + console.error(`Failed to unmount ${driveLetter}:`, e); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments