-
Notifications
You must be signed in to change notification settings - Fork 87
/
sandbox.ts
98 lines (90 loc) · 3.14 KB
/
sandbox.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2021 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import * as del from 'del';
import * as fs from 'fs';
import * as p from 'path';
import {URL, pathToFileURL} from 'url';
import {PromiseOr} from 'sass';
/**
* Runs `test` within a sandbox directory. This directory is made available via
* the `dir` parameter, which provides various utilities for working with it.
*
* Handles all buildup and teardown. Returns a promise that resolves when `test`
* finishes running.
*/
export async function sandbox(
test: (dir: SandboxDirectory) => PromiseOr<void, 'async'>,
options?: {
// Directories to put in the SASS_PATH env variable before running test.
sassPathDirs?: string[];
}
): Promise<void> {
const testDir = p.resolve(
p.join('spec', 'sandbox', `${Math.random()}`.slice(2))
);
fs.mkdirSync(testDir, {recursive: true});
if (options?.sassPathDirs) {
process.env.SASS_PATH = options.sassPathDirs.join(
process.platform === 'win32' ? ';' : ':'
);
}
try {
await test(
Object.assign((...paths: string[]) => p.join(testDir, ...paths), {
root: testDir,
url: (...paths: string[]) => pathToFileURL(p.join(testDir, ...paths)),
relativeUrl: (...paths: string[]) => {
const path = p.relative(process.cwd(), p.join(testDir, ...paths));
const components =
p.sep === '\\' ? path.split(/[/\\]/) : path.split('/');
return components.map(encodeURIComponent).join('/');
},
write: (paths: {[path: string]: string}) => {
for (const [path, contents] of Object.entries(paths)) {
const fullPath = p.join(testDir, path);
fs.mkdirSync(p.dirname(fullPath), {recursive: true});
fs.writeFileSync(fullPath, contents);
}
},
chdir: (callback: () => unknown) => {
const oldPath = process.cwd();
process.chdir(testDir);
try {
return callback();
} finally {
process.chdir(oldPath);
}
},
})
);
} finally {
if (options?.sassPathDirs) process.env.SASS_PATH = undefined;
// TODO(awjin): Change this to rmSync once we drop support for Node 12.
del.sync(p.join(testDir, '**'), {force: true});
}
}
/** The directory object passed to `sandbox`'s callback. */
interface SandboxDirectory {
/** The root of the sandbox. */
readonly root: string;
/** Like `p.join()`, but prefixes the path with `root`. */
(...paths: string[]): string;
/**
* Joins `paths` underneath `root` and converts the result to a `file:` URL.
*/
url(...paths: string[]): URL;
/**
* Joins `paths` underneath `root` and converts the result to a relative
* `file:`-style URL.
*/
relativeUrl(...paths: string[]): string;
/**
* Writes `paths` to disk within this directory.
*
* Each key is a filename, and each value is the file's contents.
*/
write(paths: {[path: string]: string}): void;
/** Runs `callback` with `root` as the current directory. */
chdir<T>(callback: () => T): void;
}