filesystem-sandbox 1.21.0 → 1.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -29
- package/README.md +74 -74
- package/dist/filesystem-sandbox.d.ts +44 -44
- package/dist/filesystem-sandbox.js +251 -247
- package/dist/index.d.ts +2 -2
- package/dist/index.js +19 -15
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +5 -7
package/LICENSE
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020, Davyd McColl
|
|
4
|
-
All rights reserved.
|
|
5
|
-
|
|
6
|
-
Redistribution and use in source and binary forms, with or without
|
|
7
|
-
modification, are permitted provided that the following conditions are met:
|
|
8
|
-
|
|
9
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
-
list of conditions and the following disclaimer.
|
|
11
|
-
|
|
12
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
-
this list of conditions and the following disclaimer in the documentation
|
|
14
|
-
and/or other materials provided with the distribution.
|
|
15
|
-
|
|
16
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
-
contributors may be used to endorse or promote products derived from
|
|
18
|
-
this software without specific prior written permission.
|
|
19
|
-
|
|
20
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Davyd McColl
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
filesystem-sandbox
|
|
2
|
-
---
|
|
3
|
-
|
|
4
|
-

|
|
5
|
-
|
|
6
|
-

|
|
7
|
-
|
|
8
|
-
JavaScript module to provide (insecure) filesystem sandboxes for testing
|
|
9
|
-
|
|
10
|
-
## Why?
|
|
11
|
-
Because I've needed to write tests which do file I/O and I want an easy
|
|
12
|
-
way to do that and clean up afterwards.
|
|
13
|
-
|
|
14
|
-
## Usage
|
|
15
|
-
### Creation
|
|
16
|
-
```typescript
|
|
17
|
-
// create a sandbox in a folder under your repo dir:
|
|
18
|
-
const sandbox1 = await Sandbox.create();
|
|
19
|
-
|
|
20
|
-
// create a sandbox somehere specific
|
|
21
|
-
const sandbox2 = await Sandbox.create("/tmp");
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
### Cleanup
|
|
25
|
-
```typescript
|
|
26
|
-
const sandbox1 = await Sandbox.create();
|
|
27
|
-
const sandbox2 = await Sandbox.create();
|
|
28
|
-
const sandbox3 = await Sandbox.create();
|
|
29
|
-
|
|
30
|
-
// clean up an individual sandbox
|
|
31
|
-
await sandbox1.destroy();
|
|
32
|
-
|
|
33
|
-
// clean up all sandboxes seen in this session, eg in `afterEach`
|
|
34
|
-
await Sandbox.destroyAll();
|
|
35
|
-
|
|
36
|
-
// precautionary: destroy any sandboxes in the default location
|
|
37
|
-
// -> useful as a beforeAll in single test (beware using in jest
|
|
38
|
-
// with concurrent testing!) or in your jest setup to clean
|
|
39
|
-
// out remnants from prior interrupted sessions. Will not destroy
|
|
40
|
-
// sandboxes in custom locations
|
|
41
|
-
await Sandbox.destroyAny();
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Path resolution
|
|
45
|
-
```typescript
|
|
46
|
-
// get the full path to a file for operations not catered for
|
|
47
|
-
// by the sandbox:
|
|
48
|
-
const sandbox = await Sandbox.create();
|
|
49
|
-
const fullpath = sandbox.fullPathFor("some-file.txt");
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### File I/O
|
|
53
|
-
```typescript
|
|
54
|
-
const sandbox = Sandbox.create();
|
|
55
|
-
// write a binary file
|
|
56
|
-
const buf1 = Buffer.from([1,2,3]);
|
|
57
|
-
await sandbox.writeFile("binary.blob", buf1);
|
|
58
|
-
// write a text file
|
|
59
|
-
await sandbox.writeFile("hello.txt", "hello world");
|
|
60
|
-
// read a text file
|
|
61
|
-
const text = await sandbox.readTextFile("hello.txt");
|
|
62
|
-
// read a binary file
|
|
63
|
-
const data = await sandbox.readFile("binary.blob");
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## File operations
|
|
67
|
-
```typescript
|
|
68
|
-
const sandbox = Sandbox.create();
|
|
69
|
-
sandbox.writeFile("hello.txt", "hello world");
|
|
70
|
-
const exists = await sandbox.fileExists("hello.txt"); // true
|
|
71
|
-
const missing = await sandbox.folderExists("hello.txt"); // false: hello.txt is a file
|
|
72
|
-
const stat = await sandbox.stat("hello.txt"); // get fulll stat info
|
|
73
|
-
const noStats = await sandbox.stat("missing"); // you'll get null
|
|
74
|
-
```
|
|
1
|
+
filesystem-sandbox
|
|
2
|
+
---
|
|
3
|
+
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
JavaScript module to provide (insecure) filesystem sandboxes for testing
|
|
9
|
+
|
|
10
|
+
## Why?
|
|
11
|
+
Because I've needed to write tests which do file I/O and I want an easy
|
|
12
|
+
way to do that and clean up afterwards.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
### Creation
|
|
16
|
+
```typescript
|
|
17
|
+
// create a sandbox in a folder under your repo dir:
|
|
18
|
+
const sandbox1 = await Sandbox.create();
|
|
19
|
+
|
|
20
|
+
// create a sandbox somehere specific
|
|
21
|
+
const sandbox2 = await Sandbox.create("/tmp");
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Cleanup
|
|
25
|
+
```typescript
|
|
26
|
+
const sandbox1 = await Sandbox.create();
|
|
27
|
+
const sandbox2 = await Sandbox.create();
|
|
28
|
+
const sandbox3 = await Sandbox.create();
|
|
29
|
+
|
|
30
|
+
// clean up an individual sandbox
|
|
31
|
+
await sandbox1.destroy();
|
|
32
|
+
|
|
33
|
+
// clean up all sandboxes seen in this session, eg in `afterEach`
|
|
34
|
+
await Sandbox.destroyAll();
|
|
35
|
+
|
|
36
|
+
// precautionary: destroy any sandboxes in the default location
|
|
37
|
+
// -> useful as a beforeAll in single test (beware using in jest
|
|
38
|
+
// with concurrent testing!) or in your jest setup to clean
|
|
39
|
+
// out remnants from prior interrupted sessions. Will not destroy
|
|
40
|
+
// sandboxes in custom locations
|
|
41
|
+
await Sandbox.destroyAny();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Path resolution
|
|
45
|
+
```typescript
|
|
46
|
+
// get the full path to a file for operations not catered for
|
|
47
|
+
// by the sandbox:
|
|
48
|
+
const sandbox = await Sandbox.create();
|
|
49
|
+
const fullpath = sandbox.fullPathFor("some-file.txt");
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### File I/O
|
|
53
|
+
```typescript
|
|
54
|
+
const sandbox = Sandbox.create();
|
|
55
|
+
// write a binary file
|
|
56
|
+
const buf1 = Buffer.from([1,2,3]);
|
|
57
|
+
await sandbox.writeFile("binary.blob", buf1);
|
|
58
|
+
// write a text file
|
|
59
|
+
await sandbox.writeFile("hello.txt", "hello world");
|
|
60
|
+
// read a text file
|
|
61
|
+
const text = await sandbox.readTextFile("hello.txt");
|
|
62
|
+
// read a binary file
|
|
63
|
+
const data = await sandbox.readFile("binary.blob");
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## File operations
|
|
67
|
+
```typescript
|
|
68
|
+
const sandbox = Sandbox.create();
|
|
69
|
+
sandbox.writeFile("hello.txt", "hello world");
|
|
70
|
+
const exists = await sandbox.fileExists("hello.txt"); // true
|
|
71
|
+
const missing = await sandbox.folderExists("hello.txt"); // false: hello.txt is a file
|
|
72
|
+
const stat = await sandbox.stat("hello.txt"); // get fulll stat info
|
|
73
|
+
const noStats = await sandbox.stat("missing"); // you'll get null
|
|
74
|
+
```
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { StatsBase } from "fs";
|
|
3
|
-
export declare type Func<T> = () => T;
|
|
4
|
-
export declare type AsyncFunc<T> = () => Promise<T>;
|
|
5
|
-
export declare const basePrefix = "__sandboxes__";
|
|
6
|
-
export declare class Sandbox {
|
|
7
|
-
private readonly _path;
|
|
8
|
-
private readonly _base;
|
|
9
|
-
private readonly _at;
|
|
10
|
-
get path(): string;
|
|
11
|
-
constructor(at?: string);
|
|
12
|
-
destroy(): Promise<void>;
|
|
13
|
-
private tryDestroyAt;
|
|
14
|
-
private tryDestroyContainer;
|
|
15
|
-
private destroyFolderIfEmpty;
|
|
16
|
-
private tryDestroySelf;
|
|
17
|
-
writeFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
18
|
-
appendFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
19
|
-
mkdir(name: string): Promise<string>;
|
|
20
|
-
private resolveRelativePath;
|
|
21
|
-
readTextFile(at: string): Promise<string>;
|
|
22
|
-
readFile(at: string): Promise<Buffer>;
|
|
23
|
-
fullPathFor(relativePath: string, ...parts: string[]): string;
|
|
24
|
-
stat(at: string): Promise<StatsBase<any> | null>;
|
|
25
|
-
folderExists(at: string): Promise<boolean>;
|
|
26
|
-
fileExists(at: string): Promise<boolean>;
|
|
27
|
-
private runOnStat;
|
|
28
|
-
run<T>(fn: Func<T> | AsyncFunc<T>, relativePath?: string): Promise<T>;
|
|
29
|
-
private _validatePathInsideSandbox;
|
|
30
|
-
/**
|
|
31
|
-
* Destroys all sandboxes created in this process up until now
|
|
32
|
-
*/
|
|
33
|
-
static destroyAll(): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
36
|
-
* run before all tests to ensure that there are no lingering sandboxes
|
|
37
|
-
* - cannot destroy sandboxes in custom paths
|
|
38
|
-
*/
|
|
39
|
-
static destroyAny(): Promise<void>;
|
|
40
|
-
static create(at?: string): Promise<Sandbox>;
|
|
41
|
-
static setBaseTargetToCurrentWorkingDirectory(): Promise<void>;
|
|
42
|
-
static setBaseTargetToSystemTempFolder(): Promise<void>;
|
|
43
|
-
static setBaseTarget(to: string): Promise<void>;
|
|
44
|
-
}
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { StatsBase } from "fs";
|
|
3
|
+
export declare type Func<T> = () => T;
|
|
4
|
+
export declare type AsyncFunc<T> = () => Promise<T>;
|
|
5
|
+
export declare const basePrefix = "__sandboxes__";
|
|
6
|
+
export declare class Sandbox {
|
|
7
|
+
private readonly _path;
|
|
8
|
+
private readonly _base;
|
|
9
|
+
private readonly _at;
|
|
10
|
+
get path(): string;
|
|
11
|
+
constructor(at?: string);
|
|
12
|
+
destroy(): Promise<void>;
|
|
13
|
+
private tryDestroyAt;
|
|
14
|
+
private tryDestroyContainer;
|
|
15
|
+
private destroyFolderIfEmpty;
|
|
16
|
+
private tryDestroySelf;
|
|
17
|
+
writeFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
18
|
+
appendFile(at: string, contents: string | Buffer | string[]): Promise<string>;
|
|
19
|
+
mkdir(name: string): Promise<string>;
|
|
20
|
+
private resolveRelativePath;
|
|
21
|
+
readTextFile(at: string): Promise<string>;
|
|
22
|
+
readFile(at: string): Promise<Buffer>;
|
|
23
|
+
fullPathFor(relativePath: string, ...parts: string[]): string;
|
|
24
|
+
stat(at: string): Promise<StatsBase<any> | null>;
|
|
25
|
+
folderExists(at: string): Promise<boolean>;
|
|
26
|
+
fileExists(at: string): Promise<boolean>;
|
|
27
|
+
private runOnStat;
|
|
28
|
+
run<T>(fn: Func<T> | AsyncFunc<T>, relativePath?: string): Promise<T>;
|
|
29
|
+
private _validatePathInsideSandbox;
|
|
30
|
+
/**
|
|
31
|
+
* Destroys all sandboxes created in this process up until now
|
|
32
|
+
*/
|
|
33
|
+
static destroyAll(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
36
|
+
* run before all tests to ensure that there are no lingering sandboxes
|
|
37
|
+
* - cannot destroy sandboxes in custom paths
|
|
38
|
+
*/
|
|
39
|
+
static destroyAny(): Promise<void>;
|
|
40
|
+
static create(at?: string): Promise<Sandbox>;
|
|
41
|
+
static setBaseTargetToCurrentWorkingDirectory(): Promise<void>;
|
|
42
|
+
static setBaseTargetToSystemTempFolder(): Promise<void>;
|
|
43
|
+
static setBaseTarget(to: string): Promise<void>;
|
|
44
|
+
}
|
|
@@ -1,247 +1,251 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return
|
|
20
|
-
};
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
resolve(fullpath);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
async
|
|
186
|
-
at = this.resolveRelativePath(at);
|
|
187
|
-
return this.runOnStat(at, st => !!st && st.
|
|
188
|
-
}
|
|
189
|
-
async
|
|
190
|
-
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
async
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
process.chdir(
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
static async
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.Sandbox = exports.basePrefix = void 0;
|
|
30
|
+
const fs_1 = require("fs");
|
|
31
|
+
const os = __importStar(require("os"));
|
|
32
|
+
if (!fs_1.promises) {
|
|
33
|
+
throw new Error("Yer node is olde! filesystem-sandboxes requires a Node with fs.promises");
|
|
34
|
+
}
|
|
35
|
+
// require for "path" produces the cleanest js output
|
|
36
|
+
const path_1 = __importDefault(require("path"));
|
|
37
|
+
const uuid_1 = require("uuid");
|
|
38
|
+
const mkdirp_1 = require("mkdirp");
|
|
39
|
+
const rimraf_1 = require("rimraf");
|
|
40
|
+
const { writeFile, readFile, stat, appendFile } = fs_1.promises;
|
|
41
|
+
async function isFolder(p) {
|
|
42
|
+
try {
|
|
43
|
+
const st = await stat(p);
|
|
44
|
+
return st && st.isDirectory();
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function readdir(p) {
|
|
51
|
+
const exists = await isFolder(p);
|
|
52
|
+
if (!exists) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
return await fs_1.promises.readdir(p);
|
|
56
|
+
}
|
|
57
|
+
async function rimraf(p) {
|
|
58
|
+
if (!(await isFolder(p))) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
try {
|
|
63
|
+
(0, rimraf_1.sync)(p, { maxBusyTries: 5 });
|
|
64
|
+
resolve();
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
reject(e);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
exports.basePrefix = "__sandboxes__";
|
|
72
|
+
let baseTarget = os.tmpdir();
|
|
73
|
+
const sandboxes = [];
|
|
74
|
+
async function writeData(fullPath, contents, writeFunction) {
|
|
75
|
+
const options = contents instanceof Buffer
|
|
76
|
+
? undefined
|
|
77
|
+
: { encoding: "utf8" };
|
|
78
|
+
if (Array.isArray(contents)) {
|
|
79
|
+
contents = contents.join("\n");
|
|
80
|
+
}
|
|
81
|
+
await writeFunction(fullPath, contents, options);
|
|
82
|
+
return fullPath;
|
|
83
|
+
}
|
|
84
|
+
class Sandbox {
|
|
85
|
+
constructor(at) {
|
|
86
|
+
this._at = at;
|
|
87
|
+
this._base = path_1.default.join(at || baseTarget, exports.basePrefix);
|
|
88
|
+
this._path = path_1.default.join(this._base, (0, uuid_1.v4)());
|
|
89
|
+
(0, mkdirp_1.sync)(this._path);
|
|
90
|
+
sandboxes.push(this);
|
|
91
|
+
}
|
|
92
|
+
get path() {
|
|
93
|
+
return this._path;
|
|
94
|
+
}
|
|
95
|
+
async destroy() {
|
|
96
|
+
await this.tryDestroySelf();
|
|
97
|
+
await this.tryDestroyContainer();
|
|
98
|
+
await this.tryDestroyAt();
|
|
99
|
+
}
|
|
100
|
+
async tryDestroyAt() {
|
|
101
|
+
if (!this._at) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
await this.destroyFolderIfEmpty(this._at);
|
|
105
|
+
}
|
|
106
|
+
async tryDestroyContainer() {
|
|
107
|
+
await this.destroyFolderIfEmpty(this._base);
|
|
108
|
+
}
|
|
109
|
+
async destroyFolderIfEmpty(p) {
|
|
110
|
+
const contents = await readdir(p);
|
|
111
|
+
if (!contents.length) {
|
|
112
|
+
await rimraf(p);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async tryDestroySelf() {
|
|
116
|
+
const myIndex = sandboxes.indexOf(this);
|
|
117
|
+
if (myIndex > -1) {
|
|
118
|
+
sandboxes.splice(myIndex, 1);
|
|
119
|
+
}
|
|
120
|
+
await rimraf(this._path);
|
|
121
|
+
}
|
|
122
|
+
async writeFile(at, contents) {
|
|
123
|
+
try {
|
|
124
|
+
await this.mkdir(path_1.default.dirname(at));
|
|
125
|
+
return await writeData(this.fullPathFor(at), contents, writeFile);
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
throw new Error(`Unable to write file at ${at}: ${e.message || e}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async appendFile(at, contents) {
|
|
132
|
+
try {
|
|
133
|
+
await this.mkdir(path_1.default.dirname(at));
|
|
134
|
+
return await writeData(this.fullPathFor(at), contents, appendFile);
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
throw new Error(`Unable to append to file at ${at}: ${e.message || e}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async mkdir(name) {
|
|
141
|
+
name = this.resolveRelativePath(name);
|
|
142
|
+
return new Promise(async (resolve, reject) => {
|
|
143
|
+
const fullpath = path_1.default.join(this._path, name);
|
|
144
|
+
if (await isFolder(fullpath)) {
|
|
145
|
+
resolve(fullpath);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
(0, mkdirp_1.sync)(fullpath);
|
|
149
|
+
resolve(fullpath);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
reject(e);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
resolveRelativePath(at) {
|
|
157
|
+
if (!path_1.default.isAbsolute(at)) {
|
|
158
|
+
return at;
|
|
159
|
+
}
|
|
160
|
+
const result = path_1.default.relative(this._path, at);
|
|
161
|
+
if (result.startsWith("..")) {
|
|
162
|
+
throw new Error(`${at} is outside the sandbox at ${this._path}`);
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
async readTextFile(at) {
|
|
167
|
+
return readFile(this.fullPathFor(at), { encoding: "utf8" });
|
|
168
|
+
}
|
|
169
|
+
async readFile(at) {
|
|
170
|
+
return readFile(this.fullPathFor(at));
|
|
171
|
+
}
|
|
172
|
+
fullPathFor(relativePath, ...parts) {
|
|
173
|
+
relativePath = this.resolveRelativePath(relativePath);
|
|
174
|
+
return path_1.default.join(this._path, relativePath, ...parts);
|
|
175
|
+
}
|
|
176
|
+
async stat(at) {
|
|
177
|
+
at = this.resolveRelativePath(at);
|
|
178
|
+
try {
|
|
179
|
+
return await fs_1.promises.stat(this.fullPathFor(at));
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async folderExists(at) {
|
|
186
|
+
at = this.resolveRelativePath(at);
|
|
187
|
+
return this.runOnStat(at, st => !!st && st.isDirectory());
|
|
188
|
+
}
|
|
189
|
+
async fileExists(at) {
|
|
190
|
+
at = this.resolveRelativePath(at);
|
|
191
|
+
return this.runOnStat(at, st => !!st && st.isFile());
|
|
192
|
+
}
|
|
193
|
+
async runOnStat(relativePath, fn) {
|
|
194
|
+
const st = await this.stat(relativePath);
|
|
195
|
+
return fn(st);
|
|
196
|
+
}
|
|
197
|
+
async run(fn, relativePath) {
|
|
198
|
+
const start = process.cwd();
|
|
199
|
+
try {
|
|
200
|
+
const target = relativePath
|
|
201
|
+
? path_1.default.resolve(path_1.default.join(this.path, relativePath))
|
|
202
|
+
: this.path;
|
|
203
|
+
this._validatePathInsideSandbox(target);
|
|
204
|
+
process.chdir(target);
|
|
205
|
+
return await fn();
|
|
206
|
+
}
|
|
207
|
+
finally {
|
|
208
|
+
process.chdir(start);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
_validatePathInsideSandbox(t) {
|
|
212
|
+
if (t.startsWith(this.path)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
throw new Error(`${t} is not inside sandbox at ${this.path}`);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Destroys all sandboxes created in this process up until now
|
|
219
|
+
*/
|
|
220
|
+
static async destroyAll() {
|
|
221
|
+
const toDestroy = sandboxes.splice(0, sandboxes.length);
|
|
222
|
+
for (const sandbox of toDestroy) {
|
|
223
|
+
await sandbox.destroy();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Destroys the default base folder for sandboxes -- useful as a once-off
|
|
228
|
+
* run before all tests to ensure that there are no lingering sandboxes
|
|
229
|
+
* - cannot destroy sandboxes in custom paths
|
|
230
|
+
*/
|
|
231
|
+
static async destroyAny() {
|
|
232
|
+
const target = path_1.default.join(baseTarget, exports.basePrefix);
|
|
233
|
+
await rimraf(target);
|
|
234
|
+
}
|
|
235
|
+
static async create(at) {
|
|
236
|
+
return new Sandbox(at);
|
|
237
|
+
}
|
|
238
|
+
static async setBaseTargetToCurrentWorkingDirectory() {
|
|
239
|
+
await this.setBaseTarget(process.cwd());
|
|
240
|
+
}
|
|
241
|
+
static async setBaseTargetToSystemTempFolder() {
|
|
242
|
+
await this.setBaseTarget(os.tmpdir());
|
|
243
|
+
}
|
|
244
|
+
static async setBaseTarget(to) {
|
|
245
|
+
if (!(await isFolder(to))) {
|
|
246
|
+
await (0, mkdirp_1.sync)(to);
|
|
247
|
+
}
|
|
248
|
+
baseTarget = to;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
exports.Sandbox = Sandbox;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./filesystem-sandbox";
|
|
2
|
-
export * from "./index";
|
|
1
|
+
export * from "./filesystem-sandbox";
|
|
2
|
+
export * from "./index";
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// this is a generated file: do not edit
|
|
18
|
+
__exportStar(require("./filesystem-sandbox"), exports);
|
|
19
|
+
__exportStar(require("./index"), exports);
|
package/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./dist";
|
|
1
|
+
export * from "./dist";
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require("./dist");
|
|
1
|
+
module.exports = require("./dist");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "filesystem-sandbox",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.0",
|
|
4
4
|
"description": "JavaScript module to provide filesystem sandboxes for testing",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/fluffynuts/filesystem-sandbox#readme",
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@
|
|
41
|
+
"@faker-js/faker": "^6.0.0",
|
|
42
42
|
"@types/jest": "^25.2.3",
|
|
43
43
|
"@types/lodash": "^4.14.153",
|
|
44
44
|
"@types/mkdirp": "^1.0.0",
|
|
@@ -48,14 +48,12 @@
|
|
|
48
48
|
"eslint": "^7.1.0",
|
|
49
49
|
"expect-even-more-jest": "^1.0.0",
|
|
50
50
|
"expect-more-jest": "^5.1.0",
|
|
51
|
-
"
|
|
52
|
-
"jest": "^26.0.1",
|
|
51
|
+
"jest": "^27.5.1",
|
|
53
52
|
"lodash": "^4.17.15",
|
|
54
53
|
"nodemon": "^2.0.4",
|
|
55
|
-
"
|
|
56
|
-
"ts-jest": "^26.0.0",
|
|
54
|
+
"ts-jest": "^27.1.3",
|
|
57
55
|
"tslint": "^6.1.2",
|
|
58
|
-
"typescript": "^
|
|
56
|
+
"typescript": "^4.6.2",
|
|
59
57
|
"zarro": "^1.99.0"
|
|
60
58
|
},
|
|
61
59
|
"dependencies": {
|