forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmock-host.ts
More file actions
76 lines (63 loc) · 2.29 KB
/
mock-host.ts
File metadata and controls
76 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Stats } from 'node:fs';
import { Host } from '../host';
/**
* A mock `Host` implementation for testing.
* This class allows for simulating a file system in memory.
*/
export class MockHost implements Host {
private readonly fs = new Map<string, string[] | true>();
constructor(files: Record<string, string[] | true> = {}) {
// Normalize paths to use forward slashes for consistency in tests.
for (const [path, content] of Object.entries(files)) {
const normalizedPath = path.replace(/\\/g, '/');
this.fs.set(normalizedPath, content);
// If the content is an array (directory listing), create entries for the files in it.
if (Array.isArray(content)) {
for (const file of content) {
const filePath = normalizedPath === '/' ? `/${file}` : `${normalizedPath}/${file}`;
this.fs.set(filePath, []); // Use empty array to represent a file (not `true` which is a dir)
}
}
}
}
mkdir(path: string, options?: { recursive?: boolean }): Promise<string | undefined> {
throw new Error('Method not implemented.');
}
stat(path: string): Promise<Stats> {
const content = this.fs.get(path.replace(/\\/g, '/'));
if (content === undefined) {
return Promise.reject(new Error(`File not found: ${path}`));
}
// A `true` value signifies a directory in our mock file system.
// Anything else is considered a file for the purpose of this mock.
return Promise.resolve({
isDirectory: () => content === true,
isFile: () => content !== true,
} as Stats);
}
runCommand(): Promise<{ stdout: string; stderr: string }> {
throw new Error('Method not implemented.');
}
createTempDirectory(): Promise<string> {
throw new Error('Method not implemented.');
}
deleteDirectory(): Promise<void> {
throw new Error('Method not implemented.');
}
writeFile(): Promise<void> {
throw new Error('Method not implemented.');
}
readFile(): Promise<string> {
throw new Error('Method not implemented.');
}
copyFile(): Promise<void> {
throw new Error('Method not implemented.');
}
}