UNPKG

5.68 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2018,2020. All Rights Reserved.
3// Node module: @loopback/testlab
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.TestSandbox = void 0;
8const fs_extra_1 = require("fs-extra");
9const path_1 = require("path");
10/**
11 * TestSandbox class provides a convenient way to get a reference to a
12 * sandbox folder in which you can perform operations for testing purposes.
13 */
14class TestSandbox {
15 /**
16 * Will create a directory if it doesn't already exist. If it exists, you
17 * still get an instance of the TestSandbox.
18 *
19 * @example
20 * ```ts
21 * // Create a sandbox as a unique temporary subdirectory under the rootPath
22 * const sandbox = new TestSandbox(rootPath);
23 * const sandbox = new TestSandbox(rootPath, {subdir: true});
24 *
25 * // Create a sandbox in the root path directly
26 * // This is same as the old behavior
27 * const sandbox = new TestSandbox(rootPath, {subdir: false});
28 *
29 * // Create a sandbox in the `test1` subdirectory of the root path
30 * const sandbox = new TestSandbox(rootPath, {subdir: 'test1'});
31 * ```
32 *
33 * @param rootPath - Root path of the TestSandbox. If relative it will be
34 * resolved against the current directory.
35 * @param options - Options to control if/how the sandbox creates a
36 * subdirectory for the sandbox. If not provided, the sandbox
37 * will automatically creates a unique temporary subdirectory. This allows
38 * sandboxes with the same root path can be used in parallel during testing.
39 */
40 constructor(rootPath, options) {
41 rootPath = (0, path_1.resolve)(rootPath);
42 (0, fs_extra_1.ensureDirSync)(rootPath);
43 options = { subdir: true, ...options };
44 const subdir = typeof options.subdir === 'string' ? options.subdir : '.';
45 if (options.subdir !== true) {
46 this._path = (0, path_1.resolve)(rootPath, subdir);
47 }
48 else {
49 // Create a unique temporary directory under the root path
50 // See https://nodejs.org/api/fs.html#fs_fs_mkdtempsync_prefix_options
51 this._path = (0, fs_extra_1.mkdtempSync)((0, path_1.join)(rootPath, `/${process.pid}`));
52 }
53 }
54 get path() {
55 if (!this._path) {
56 throw new Error(`TestSandbox instance was deleted. Create a new instance.`);
57 }
58 return this._path;
59 }
60 /**
61 * Resets the TestSandbox. (Remove all files in it).
62 */
63 async reset() {
64 // Decache files from require's cache so future tests aren't affected incase
65 // a file is recreated in sandbox with the same file name but different
66 // contents after resetting the sandbox.
67 for (const key in require.cache) {
68 if (key.startsWith(this.path)) {
69 delete require.cache[key];
70 }
71 }
72 await (0, fs_extra_1.emptyDir)(this.path);
73 }
74 /**
75 * Deletes the TestSandbox.
76 */
77 async delete() {
78 await (0, fs_extra_1.remove)(this.path);
79 delete this._path;
80 }
81 /**
82 * Makes a directory in the TestSandbox
83 *
84 * @param dir - Name of directory to create (relative to TestSandbox path)
85 */
86 async mkdir(dir) {
87 await (0, fs_extra_1.ensureDir)((0, path_1.resolve)(this.path, dir));
88 }
89 /**
90 * Copies a file from src to the TestSandbox. If copying a `.js` file which
91 * has an accompanying `.js.map` file in the src file location, the dest file
92 * will have its sourceMappingURL updated to point to the original file as
93 * an absolute path so you don't need to copy the map file.
94 *
95 * @param src - Absolute path of file to be copied to the TestSandbox
96 * @param dest - Optional. Destination filename of the copy operation
97 * (relative to TestSandbox). Original filename used if not specified.
98 * @param transform - Optional. A function to transform the file content.
99 */
100 async copyFile(src, dest, transform) {
101 dest = dest
102 ? (0, path_1.resolve)(this.path, dest)
103 : (0, path_1.resolve)(this.path, (0, path_1.parse)(src).base);
104 if (transform == null) {
105 await (0, fs_extra_1.copy)(src, dest);
106 }
107 else {
108 let content = await (0, fs_extra_1.readFile)(src, 'utf-8');
109 content = transform(content);
110 await (0, fs_extra_1.outputFile)(dest, content, { encoding: 'utf-8' });
111 }
112 if ((0, path_1.parse)(src).ext === '.js' && (await (0, fs_extra_1.pathExists)(src + '.map'))) {
113 const srcMap = src + '.map';
114 await (0, fs_extra_1.appendFile)(dest, `\n//# sourceMappingURL=${srcMap}`);
115 }
116 }
117 /**
118 * Creates a new file and writes the given data serialized as JSON.
119 *
120 * @param dest - Destination filename, optionally including a relative path.
121 * @param data - The data to write.
122 */
123 async writeJsonFile(dest, data) {
124 dest = (0, path_1.resolve)(this.path, dest);
125 return (0, fs_extra_1.outputJson)(dest, data, { spaces: 2 });
126 }
127 /**
128 * Creates a new file and writes the given data as a UTF-8-encoded text.
129 *
130 * @param dest - Destination filename, optionally including a relative path.
131 * @param data - The text to write.
132 */
133 async writeTextFile(dest, data) {
134 dest = (0, path_1.resolve)(this.path, dest);
135 return (0, fs_extra_1.outputFile)(dest, data, 'utf-8');
136 }
137}
138exports.TestSandbox = TestSandbox;
139//# sourceMappingURL=test-sandbox.js.map
\No newline at end of file