Skip to content

Commit 1529cb0

Browse files
committed
test(@angular/cli): use TEST_TMPDIR instead of os.tmpdir() in unit tests
Use process.env['TEST_TMPDIR'] with an assertion in unit tests to ensure hermetic execution within the Bazel sandbox.
1 parent cf88074 commit 1529cb0

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

‎packages/angular/cli/src/commands/mcp/host_spec.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import assert from 'node:assert';
910
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
10-
import { tmpdir } from 'node:os';
1111
import { join } from 'node:path';
1212
import { LocalWorkspaceHost, createRootRestrictedHost } from './host';
1313

@@ -17,9 +17,11 @@ describe('createRootRestrictedHost', () => {
1717
let outsideDir: string;
1818

1919
beforeEach(() => {
20-
root1 = mkdtempSync(join(tmpdir(), 'angular-cli-mcp-root1-'));
21-
root2 = mkdtempSync(join(tmpdir(), 'angular-cli-mcp-root2-'));
22-
outsideDir = mkdtempSync(join(tmpdir(), 'angular-cli-mcp-outside-'));
20+
const baseTmpDir = process.env['TEST_TMPDIR'];
21+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
22+
root1 = mkdtempSync(join(baseTmpDir, 'angular-cli-mcp-root1-'));
23+
root2 = mkdtempSync(join(baseTmpDir, 'angular-cli-mcp-root2-'));
24+
outsideDir = mkdtempSync(join(baseTmpDir, 'angular-cli-mcp-outside-'));
2325

2426
writeFileSync(join(root1, 'file1.txt'), 'root 1 content');
2527
writeFileSync(join(root2, 'file2.txt'), 'root 2 content');

‎packages/angular/cli/src/commands/mcp/tools/projects_spec.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
import { workspaces } from '@angular-devkit/core';
10+
import assert from 'node:assert';
1011
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
11-
import { tmpdir } from 'node:os';
1212
import { join } from 'node:path';
1313
import { pathToFileURL } from 'node:url';
1414
import { AngularWorkspace } from '../../../utilities/config';
@@ -23,7 +23,9 @@ describe('List Projects Tool', () => {
2323
let workspaceDir: string;
2424

2525
beforeEach(() => {
26-
tempDir = mkdtempSync(join(tmpdir(), 'mcp-projects-tool-'));
26+
const baseTmpDir = process.env['TEST_TMPDIR'];
27+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
28+
tempDir = mkdtempSync(join(baseTmpDir, 'mcp-projects-tool-'));
2729
allowedRoot = join(tempDir, 'allowed-root');
2830
workspaceDir = join(allowedRoot, 'workspace');
2931
mkdirSync(workspaceDir, { recursive: true });

‎packages/angular/cli/src/commands/mcp/workspace-utils_spec.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
import { workspaces } from '@angular-devkit/core';
10+
import assert from 'node:assert';
1011
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
11-
import { tmpdir } from 'node:os';
1212
import { join } from 'node:path';
1313
import { pathToFileURL } from 'node:url';
1414
import { AngularWorkspace } from '../../utilities/config';
@@ -114,7 +114,9 @@ describe('MCP Workspace Utils', () => {
114114
beforeEach(() => {
115115
mockHost = createMockHost();
116116
spyOn(process, 'cwd').and.returnValue(cwd);
117-
tempDir = mkdtempSync(join(tmpdir(), 'mcp-workspace-utils-'));
117+
const baseTmpDir = process.env['TEST_TMPDIR'];
118+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
119+
tempDir = mkdtempSync(join(baseTmpDir, 'mcp-workspace-utils-'));
118120
allowedRoot = join(tempDir, 'allowed-root');
119121
allowedWorkspace = join(allowedRoot, 'workspace');
120122
outsideWorkspace = join(tempDir, 'outside-workspace');

‎packages/angular/cli/src/commands/update/update-resolver_spec.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
import { logging } from '@angular-devkit/core';
10+
import assert from 'node:assert';
1011
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
11-
import { tmpdir } from 'node:os';
1212
import * as path from 'node:path';
1313
import * as semver from 'semver';
1414
import type { PackageManager, PackageManifest, PackageMetadata } from '../../package-managers';
@@ -43,7 +43,9 @@ describe('UpdateResolver', () => {
4343
const logger = new logging.NullLogger();
4444

4545
beforeEach(() => {
46-
tempRoot = mkdtempSync(path.join(tmpdir(), 'angular-cli-update-resolver-test-'));
46+
const baseTmpDir = process.env['TEST_TMPDIR'];
47+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
48+
tempRoot = mkdtempSync(path.join(baseTmpDir, 'angular-cli-update-resolver-test-'));
4749
});
4850

4951
afterEach(() => {

0 commit comments

Comments
 (0)