@jsenv/snapshot 2.2.7 → 2.2.8
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/package.json +1 -1
- package/src/function_snapshot.js +32 -19
package/package.json
CHANGED
package/src/function_snapshot.js
CHANGED
|
@@ -4,7 +4,11 @@ import {
|
|
|
4
4
|
removeFileSync,
|
|
5
5
|
writeFileSync,
|
|
6
6
|
} from "@jsenv/filesystem";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
ensurePathnameTrailingSlash,
|
|
9
|
+
urlToFilename,
|
|
10
|
+
urlToRelativeUrl,
|
|
11
|
+
} from "@jsenv/urls";
|
|
8
12
|
import { takeDirectorySnapshot } from "./filesystem_snapshot.js";
|
|
9
13
|
import { replaceFluctuatingValues } from "./replace_fluctuating_values.js";
|
|
10
14
|
|
|
@@ -13,15 +17,18 @@ const consoleSpySymbol = Symbol.for("console_spy_for_jsenv_snapshot");
|
|
|
13
17
|
export const snapshotFunctionSideEffects = (
|
|
14
18
|
fn,
|
|
15
19
|
fnFileUrl,
|
|
16
|
-
sideEffectDirectoryRelativeUrl,
|
|
20
|
+
sideEffectDirectoryRelativeUrl = "./",
|
|
17
21
|
{
|
|
18
22
|
rootDirectoryUrl = new URL("./", fnFileUrl),
|
|
19
23
|
captureConsole = true,
|
|
20
24
|
filesystemEffects,
|
|
21
|
-
|
|
25
|
+
filesystemEffectsDirectory,
|
|
22
26
|
restoreFilesystem = true,
|
|
23
27
|
} = {},
|
|
24
28
|
) => {
|
|
29
|
+
if (filesystemEffectsDirectory === true) {
|
|
30
|
+
filesystemEffectsDirectory = "./fs/";
|
|
31
|
+
}
|
|
25
32
|
const sideEffectDirectoryUrl = new URL(
|
|
26
33
|
sideEffectDirectoryRelativeUrl,
|
|
27
34
|
fnFileUrl,
|
|
@@ -33,15 +40,15 @@ export const snapshotFunctionSideEffects = (
|
|
|
33
40
|
const sideEffectFileUrl = new URL(sideEffectFilename, sideEffectDirectoryUrl);
|
|
34
41
|
const sideEffects = [];
|
|
35
42
|
const finallyCallbackSet = new Set();
|
|
36
|
-
const onError = (e) => {
|
|
43
|
+
const onError = (e, isAsync) => {
|
|
37
44
|
sideEffects.push({
|
|
38
|
-
type: "throw",
|
|
45
|
+
type: isAsync ? "reject" : "throw",
|
|
39
46
|
value: e,
|
|
40
47
|
});
|
|
41
48
|
};
|
|
42
|
-
const onResult = (result) => {
|
|
49
|
+
const onResult = (result, isAsync) => {
|
|
43
50
|
sideEffects.push({
|
|
44
|
-
type: "return",
|
|
51
|
+
type: isAsync ? "resolve" : "return",
|
|
45
52
|
value: result,
|
|
46
53
|
});
|
|
47
54
|
};
|
|
@@ -53,7 +60,7 @@ export const snapshotFunctionSideEffects = (
|
|
|
53
60
|
sideEffectFileUrl,
|
|
54
61
|
stringifySideEffects(sideEffects, {
|
|
55
62
|
rootDirectoryUrl,
|
|
56
|
-
|
|
63
|
+
filesystemEffectsDirectory,
|
|
57
64
|
}),
|
|
58
65
|
);
|
|
59
66
|
sideEffectDirectorySnapshot.compare();
|
|
@@ -82,7 +89,13 @@ export const snapshotFunctionSideEffects = (
|
|
|
82
89
|
installConsoleSpy("log");
|
|
83
90
|
}
|
|
84
91
|
if (filesystemEffects) {
|
|
85
|
-
const fsSideEffectDirectoryUrl =
|
|
92
|
+
const fsSideEffectDirectoryUrl = ensurePathnameTrailingSlash(
|
|
93
|
+
new URL(filesystemEffectsDirectory, sideEffectDirectoryUrl),
|
|
94
|
+
);
|
|
95
|
+
const fsSideEffectsDirectoryRelativeUrl = urlToRelativeUrl(
|
|
96
|
+
fsSideEffectDirectoryUrl,
|
|
97
|
+
sideEffectFileUrl,
|
|
98
|
+
);
|
|
86
99
|
for (const filesystemEffect of filesystemEffects) {
|
|
87
100
|
const from = new URL(filesystemEffect, fnFileUrl);
|
|
88
101
|
const relativeUrl = urlToRelativeUrl(from, fnFileUrl);
|
|
@@ -114,15 +127,15 @@ export const snapshotFunctionSideEffects = (
|
|
|
114
127
|
atStartState.content !== nowState.content ||
|
|
115
128
|
atStartState.mtimeMs !== nowState.mtimeMs
|
|
116
129
|
) {
|
|
117
|
-
if (
|
|
130
|
+
if (filesystemEffectsDirectory) {
|
|
131
|
+
writeFileSync(toUrl, nowState.content);
|
|
118
132
|
onFileSystemSideEffect({
|
|
119
|
-
type: `write file "${relativeUrl}"`,
|
|
133
|
+
type: `write file "${relativeUrl}" (see ./${fsSideEffectsDirectoryRelativeUrl}${relativeUrl})`,
|
|
120
134
|
value: nowState.content,
|
|
121
135
|
});
|
|
122
136
|
} else {
|
|
123
|
-
writeFileSync(toUrl, nowState.content);
|
|
124
137
|
onFileSystemSideEffect({
|
|
125
|
-
type: `write file "${relativeUrl}"
|
|
138
|
+
type: `write file "${relativeUrl}"`,
|
|
126
139
|
value: nowState.content,
|
|
127
140
|
});
|
|
128
141
|
}
|
|
@@ -148,11 +161,11 @@ export const snapshotFunctionSideEffects = (
|
|
|
148
161
|
if (returnValue && returnValue.then) {
|
|
149
162
|
returnedPromise = returnValue.then(
|
|
150
163
|
(value) => {
|
|
151
|
-
onResult(value);
|
|
164
|
+
onResult(value, true);
|
|
152
165
|
onFinally();
|
|
153
166
|
},
|
|
154
167
|
(e) => {
|
|
155
|
-
onError(e);
|
|
168
|
+
onError(e, true);
|
|
156
169
|
onFinally();
|
|
157
170
|
},
|
|
158
171
|
);
|
|
@@ -173,7 +186,7 @@ export const snapshotFunctionSideEffects = (
|
|
|
173
186
|
|
|
174
187
|
const stringifySideEffects = (
|
|
175
188
|
sideEffects,
|
|
176
|
-
{ rootDirectoryUrl,
|
|
189
|
+
{ rootDirectoryUrl, filesystemEffectsDirectory },
|
|
177
190
|
) => {
|
|
178
191
|
let string = "";
|
|
179
192
|
let index = 0;
|
|
@@ -194,17 +207,17 @@ const stringifySideEffects = (
|
|
|
194
207
|
sideEffect.type.startsWith("remove file") ||
|
|
195
208
|
sideEffect.type.startsWith("write file")
|
|
196
209
|
) {
|
|
197
|
-
if (
|
|
210
|
+
if (!filesystemEffectsDirectory) {
|
|
198
211
|
string += "\n";
|
|
199
212
|
string += value;
|
|
200
213
|
}
|
|
201
|
-
} else if (sideEffect.type === "throw") {
|
|
214
|
+
} else if (sideEffect.type === "throw" || sideEffect.type === "reject") {
|
|
202
215
|
value = replaceFluctuatingValues(value.stack, {
|
|
203
216
|
stringType: "error",
|
|
204
217
|
});
|
|
205
218
|
string += "\n";
|
|
206
219
|
string += value;
|
|
207
|
-
} else if (sideEffect.type === "return") {
|
|
220
|
+
} else if (sideEffect.type === "return" || sideEffect.type === "resolve") {
|
|
208
221
|
value =
|
|
209
222
|
value === undefined
|
|
210
223
|
? undefined
|