-
Notifications
You must be signed in to change notification settings - Fork 12k
/
utils.ts
43 lines (36 loc) · 1.16 KB
/
utils.ts
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
import { mkdtemp, realpath, rm } from 'fs/promises';
import { tmpdir } from 'os';
import path from 'path';
export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Promise<any> {
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;
},
);
}
export function wait(msecs: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, msecs);
});
}
export async function mktempd(prefix: string): Promise<string> {
return realpath(await mkdtemp(path.join(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 });
}
}