UNPKG

3.68 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2018. 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 });
7const fs_extra_1 = require("fs-extra");
8const path_1 = require("path");
9/**
10 * TestSandbox class provides a convenient way to get a reference to a
11 * sandbox folder in which you can perform operations for testing purposes.
12 */
13class TestSandbox {
14 /**
15 * Will create a directory if it doesn't already exist. If it exists, you
16 * still get an instance of the TestSandbox.
17 *
18 * @param path - Path of the TestSandbox. If relative (it will be resolved relative to cwd()).
19 */
20 constructor(path) {
21 // resolve ensures path is absolute / makes it absolute (relative to cwd())
22 this._path = path_1.resolve(path);
23 fs_extra_1.ensureDirSync(this.path);
24 }
25 get path() {
26 if (!this._path) {
27 throw new Error(`TestSandbox instance was deleted. Create a new instance.`);
28 }
29 return this._path;
30 }
31 /**
32 * Returns the path of the TestSandbox
33 */
34 getPath() {
35 return this.path;
36 }
37 /**
38 * Resets the TestSandbox. (Remove all files in it).
39 */
40 async reset() {
41 // Decache files from require's cache so future tests aren't affected incase
42 // a file is recreated in sandbox with the same file name but different
43 // contents after resetting the sandbox.
44 for (const key in require.cache) {
45 if (key.startsWith(this.path)) {
46 delete require.cache[key];
47 }
48 }
49 await fs_extra_1.emptyDir(this.path);
50 }
51 /**
52 * Deletes the TestSandbox.
53 */
54 async delete() {
55 await fs_extra_1.remove(this.path);
56 delete this._path;
57 }
58 /**
59 * Makes a directory in the TestSandbox
60 *
61 * @param dir - Name of directory to create (relative to TestSandbox path)
62 */
63 async mkdir(dir) {
64 await fs_extra_1.ensureDir(path_1.resolve(this.path, dir));
65 }
66 /**
67 * Copies a file from src to the TestSandbox. If copying a `.js` file which
68 * has an accompanying `.js.map` file in the src file location, the dest file
69 * will have its sourceMappingURL updated to point to the original file as
70 * an absolute path so you don't need to copy the map file.
71 *
72 * @param src - Absolute path of file to be copied to the TestSandbox
73 * @param dest - Optional. Destination filename of the copy operation
74 * (relative to TestSandbox). Original filename used if not specified.
75 */
76 async copyFile(src, dest) {
77 dest = dest
78 ? path_1.resolve(this.path, dest)
79 : path_1.resolve(this.path, path_1.parse(src).base);
80 await fs_extra_1.copy(src, dest);
81 if (path_1.parse(src).ext === '.js' && fs_extra_1.pathExists(src + '.map')) {
82 const srcMap = src + '.map';
83 await fs_extra_1.appendFile(dest, `\n//# sourceMappingURL=${srcMap}`);
84 }
85 }
86 /**
87 * Creates a new file and writes the given data serialized as JSON.
88 *
89 * @param dest - Destination filename, optionally including a relative path.
90 * @param data - The data to write.
91 */
92 async writeJsonFile(dest, data) {
93 dest = path_1.resolve(this.path, dest);
94 const destDir = path_1.parse(dest).dir;
95 await fs_extra_1.ensureDir(destDir);
96 return fs_extra_1.writeJson(dest, data, { spaces: 2 });
97 }
98}
99exports.TestSandbox = TestSandbox;
100//# sourceMappingURL=test-sandbox.js.map
\No newline at end of file