Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
46 lines (39 loc) · 1.5 KB
/
Copy pathutils.ts
File metadata and controls
46 lines (39 loc) · 1.5 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
import assert from 'node:assert';
import { mkdtemp, realpath, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Promise<Error> {
return fn().then(
() => {
const functionSource = fn.name || (<any>fn).source || fn.toString();
const errorDetails = errorMessage ? `\n\tDetails:\n\t${errorMessage}` : '';
throw new Error(
`Function ${functionSource} was expected to fail, but succeeded.${errorDetails}`,
);
},
(err) => {
return err instanceof Error ? err : new Error(err);
},
);
}
export async function mktempd(prefix: string, tempRoot?: string): Promise<string> {
return realpath(await mkdtemp(path.join(tempRoot ?? tmpdir(), prefix)));
}
export async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await mktempd('angular-cli-e2e-home-');
const oldHome = process.env.HOME;
process.env.HOME = tempHome;
try {
await cb(tempHome);
} finally {
process.env.HOME = oldHome;
await rm(tempHome, { recursive: true, force: true });
}
}
export function assertIsError(value: unknown): asserts value is Error & { code?: string } {
const isError =
value instanceof Error ||
// The following is needing to identify errors coming from RxJs.
(typeof value === 'object' && value && 'name' in value && 'message' in value);
assert(isError, 'catch clause variable is not an Error instance');
}