@jsenv/snapshot 2.2.2 → 2.2.3
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 +58 -18
package/package.json
CHANGED
package/src/function_snapshot.js
CHANGED
|
@@ -1,20 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
readEntryStatSync,
|
|
3
|
+
readFileSync,
|
|
4
|
+
removeFileSync,
|
|
5
|
+
writeFileSync,
|
|
6
|
+
} from "@jsenv/filesystem";
|
|
2
7
|
import { urlToRelativeUrl } from "@jsenv/urls/src/url_to_relative_url.js";
|
|
3
|
-
import {
|
|
8
|
+
import { takeDirectorySnapshot } from "./filesystem_snapshot.js";
|
|
4
9
|
import { replaceFluctuatingValues } from "./replace_fluctuating_values.js";
|
|
5
10
|
|
|
6
11
|
export const snapshotFunctionSideEffects = (
|
|
7
12
|
fn,
|
|
8
13
|
fnFileUrl,
|
|
9
|
-
|
|
14
|
+
sideEffectDirectoryRelativeUrl,
|
|
10
15
|
{
|
|
11
16
|
rootDirectoryUrl = new URL("./", fnFileUrl),
|
|
12
17
|
captureConsole = true,
|
|
13
18
|
filesystemEffects,
|
|
19
|
+
restoreFilesystem,
|
|
14
20
|
} = {},
|
|
15
21
|
) => {
|
|
16
|
-
const
|
|
17
|
-
|
|
22
|
+
const sideEffectDirectoryUrl = new URL(
|
|
23
|
+
sideEffectDirectoryRelativeUrl,
|
|
24
|
+
fnFileUrl,
|
|
25
|
+
);
|
|
26
|
+
const sideEffectDirectorySnapshot = takeDirectorySnapshot(
|
|
27
|
+
sideEffectDirectoryUrl,
|
|
28
|
+
);
|
|
29
|
+
const sideEffectFileUrl = new URL(
|
|
30
|
+
"./side_effect.txt",
|
|
31
|
+
sideEffectDirectoryUrl,
|
|
32
|
+
);
|
|
18
33
|
const sideEffects = [];
|
|
19
34
|
const finallyCallbackSet = new Set();
|
|
20
35
|
const onError = (e) => {
|
|
@@ -33,10 +48,11 @@ export const snapshotFunctionSideEffects = (
|
|
|
33
48
|
for (const finallyCallback of finallyCallbackSet) {
|
|
34
49
|
finallyCallback();
|
|
35
50
|
}
|
|
36
|
-
|
|
51
|
+
writeFileSync(
|
|
52
|
+
sideEffectFileUrl,
|
|
37
53
|
stringifySideEffects(sideEffects, { rootDirectoryUrl }),
|
|
38
54
|
);
|
|
39
|
-
|
|
55
|
+
sideEffectDirectorySnapshot.compare();
|
|
40
56
|
};
|
|
41
57
|
if (captureConsole) {
|
|
42
58
|
const installConsoleSpy = (methodName) => {
|
|
@@ -57,9 +73,11 @@ export const snapshotFunctionSideEffects = (
|
|
|
57
73
|
installConsoleSpy("log");
|
|
58
74
|
}
|
|
59
75
|
if (filesystemEffects) {
|
|
76
|
+
const fsSideEffectDirectoryUrl = new URL("./fs/", sideEffectDirectoryUrl);
|
|
60
77
|
for (const filesystemEffect of filesystemEffects) {
|
|
61
78
|
const from = new URL(filesystemEffect, fnFileUrl);
|
|
62
79
|
const relativeUrl = urlToRelativeUrl(from, fnFileUrl);
|
|
80
|
+
const toUrl = new URL(relativeUrl, fsSideEffectDirectoryUrl);
|
|
63
81
|
const atStartState = getFileState(from);
|
|
64
82
|
const onFileSystemSideEffect = (fsSideEffect) => {
|
|
65
83
|
const last = sideEffects.pop();
|
|
@@ -71,8 +89,10 @@ export const snapshotFunctionSideEffects = (
|
|
|
71
89
|
if (atStartState.found && !nowState.found) {
|
|
72
90
|
onFileSystemSideEffect({
|
|
73
91
|
type: `remove file "${relativeUrl}"`,
|
|
74
|
-
value: atStartState.content,
|
|
75
92
|
});
|
|
93
|
+
if (restoreFilesystem) {
|
|
94
|
+
writeFileSync(from, atStartState.content);
|
|
95
|
+
}
|
|
76
96
|
return;
|
|
77
97
|
}
|
|
78
98
|
// we use same type because we don't want to differentiate between
|
|
@@ -84,10 +104,19 @@ export const snapshotFunctionSideEffects = (
|
|
|
84
104
|
atStartState.content !== nowState.content ||
|
|
85
105
|
atStartState.mtimeMs !== nowState.mtimeMs
|
|
86
106
|
) {
|
|
107
|
+
writeFileSync(toUrl, nowState.content);
|
|
87
108
|
onFileSystemSideEffect({
|
|
88
|
-
type: `write file "
|
|
89
|
-
value: nowState.content,
|
|
109
|
+
type: `write file "./fs/${relativeUrl}"`,
|
|
90
110
|
});
|
|
111
|
+
if (restoreFilesystem) {
|
|
112
|
+
if (atStartState.found) {
|
|
113
|
+
if (atStartState.content !== nowState.content) {
|
|
114
|
+
writeFileSync(from, atStartState.content);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
removeFileSync(from);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
91
120
|
return;
|
|
92
121
|
}
|
|
93
122
|
// file is exactly the same
|
|
@@ -131,12 +160,24 @@ const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
|
|
|
131
160
|
string += "\n\n";
|
|
132
161
|
}
|
|
133
162
|
string += `${index + 1}. ${sideEffect.type}`;
|
|
134
|
-
string += "\n";
|
|
135
163
|
let value = sideEffect.value;
|
|
136
|
-
if (sideEffect.type
|
|
164
|
+
if (sideEffect.type.startsWith("console.")) {
|
|
165
|
+
value = replaceFluctuatingValues(value, {
|
|
166
|
+
stringType: "console",
|
|
167
|
+
rootDirectoryUrl,
|
|
168
|
+
});
|
|
169
|
+
string += "\n";
|
|
170
|
+
string += value;
|
|
171
|
+
} else if (
|
|
172
|
+
sideEffect.type.startsWith("remove file") ||
|
|
173
|
+
sideEffect.type.startsWith("write file")
|
|
174
|
+
) {
|
|
175
|
+
} else if (sideEffect.type === "throw") {
|
|
137
176
|
value = replaceFluctuatingValues(value.stack, {
|
|
138
177
|
stringType: "error",
|
|
139
178
|
});
|
|
179
|
+
string += "\n";
|
|
180
|
+
string += value;
|
|
140
181
|
} else if (sideEffect.type === "return") {
|
|
141
182
|
value =
|
|
142
183
|
value === undefined
|
|
@@ -145,13 +186,12 @@ const stringifySideEffects = (sideEffects, { rootDirectoryUrl }) => {
|
|
|
145
186
|
stringType: "json",
|
|
146
187
|
rootDirectoryUrl,
|
|
147
188
|
});
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
189
|
+
string += "\n";
|
|
190
|
+
string += value;
|
|
191
|
+
} else {
|
|
192
|
+
string += "\n";
|
|
193
|
+
string += value;
|
|
153
194
|
}
|
|
154
|
-
string += value;
|
|
155
195
|
index++;
|
|
156
196
|
}
|
|
157
197
|
return string;
|