@jsenv/snapshot 2.4.0 → 2.4.2
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
CHANGED
|
@@ -28,6 +28,7 @@ export const snapshotFunctionSideEffects = (
|
|
|
28
28
|
fnFileUrl,
|
|
29
29
|
sideEffectDirectoryRelativeUrl = "./",
|
|
30
30
|
{
|
|
31
|
+
sideEffectbasename,
|
|
31
32
|
rootDirectoryUrl = new URL("./", fnFileUrl),
|
|
32
33
|
consoleEffects = true,
|
|
33
34
|
filesystemEffects = true,
|
|
@@ -46,7 +47,10 @@ export const snapshotFunctionSideEffects = (
|
|
|
46
47
|
const sideEffectDirectorySnapshot = takeDirectorySnapshot(
|
|
47
48
|
sideEffectDirectoryUrl,
|
|
48
49
|
);
|
|
49
|
-
|
|
50
|
+
if (sideEffectbasename === undefined) {
|
|
51
|
+
sideEffectbasename = `${urlToFilename(sideEffectDirectoryUrl)}_side_effects`;
|
|
52
|
+
}
|
|
53
|
+
const sideEffectFilename = `${sideEffectbasename}.md`;
|
|
50
54
|
const sideEffectFileUrl = new URL(sideEffectFilename, sideEffectDirectoryUrl);
|
|
51
55
|
const callbackSet = new Set();
|
|
52
56
|
const sideEffectDetectors = [
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
// https://github.com/antfu/fs-spy/blob/main/src/index.ts
|
|
2
2
|
// https://github.com/tschaub/mock-fs/tree/main
|
|
3
|
+
// https://github.com/tschaub/mock-fs/blob/6e84d5bb320022624c7d770432e3322323ce043e/lib/binding.js#L353
|
|
4
|
+
// https://github.com/tschaub/mock-fs/issues/348
|
|
3
5
|
|
|
4
6
|
import { removeDirectorySync, removeFileSync } from "@jsenv/filesystem";
|
|
5
7
|
import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
6
8
|
import { pathToFileURL } from "node:url";
|
|
7
|
-
import { spyMethod } from "./spy_method.js";
|
|
9
|
+
import { disableSpiesWhileCalling, spyMethod } from "./spy_method.js";
|
|
8
10
|
|
|
9
11
|
export const spyFilesystemCalls = (
|
|
10
12
|
{
|
|
13
|
+
readFile = () => {}, // TODO
|
|
11
14
|
writeFile = () => {},
|
|
12
15
|
writeDirectory = () => {},
|
|
13
|
-
removeFile = () => {},
|
|
16
|
+
removeFile = () => {}, // TODO
|
|
14
17
|
// removeDirectory = () => {},
|
|
15
18
|
},
|
|
16
19
|
{ undoFilesystemSideEffects } = {},
|
|
@@ -61,6 +64,14 @@ export const spyFilesystemCalls = (
|
|
|
61
64
|
writeDirectory(directoryUrl);
|
|
62
65
|
};
|
|
63
66
|
const restoreCallbackSet = new Set();
|
|
67
|
+
|
|
68
|
+
const getFileStateWithinSpy = (fileUrl) => {
|
|
69
|
+
return disableSpiesWhileCalling(
|
|
70
|
+
() => getFileState(fileUrl),
|
|
71
|
+
[openSpy, closeSpy],
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
64
75
|
const mkdirSpy = spyMethod(
|
|
65
76
|
_internalFs,
|
|
66
77
|
"mkdir",
|
|
@@ -90,54 +101,118 @@ export const spyFilesystemCalls = (
|
|
|
90
101
|
_internalFs,
|
|
91
102
|
"open",
|
|
92
103
|
(filePath, flags, mode, callback) => {
|
|
104
|
+
const stateBefore = getFileStateWithinSpy(filePath);
|
|
105
|
+
filesystemStateInfoMap.set(filePath, stateBefore);
|
|
93
106
|
if (callback) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
107
|
+
if (callback.context) {
|
|
108
|
+
const original = callback.context.callback;
|
|
109
|
+
callback.context.callback = function (...args) {
|
|
110
|
+
callback.context.callback = original;
|
|
111
|
+
const [error, fd] = args;
|
|
112
|
+
if (error) {
|
|
113
|
+
original.call(this, ...args);
|
|
114
|
+
} else {
|
|
115
|
+
if (typeof fd === "number") {
|
|
116
|
+
fileDescriptorPathMap.set(fd, filePath);
|
|
117
|
+
} else {
|
|
118
|
+
// it's a buffer (happens for readFile)
|
|
119
|
+
}
|
|
120
|
+
original.call(this, ...args);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
openSpy.callOriginal();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const original = callback.oncomplete;
|
|
127
|
+
callback.oncomplete = function (...args) {
|
|
128
|
+
callback.oncomplete = original;
|
|
129
|
+
const [error, fd] = args;
|
|
98
130
|
if (error) {
|
|
99
|
-
|
|
131
|
+
original.call(this, ...args);
|
|
100
132
|
} else {
|
|
101
133
|
fileDescriptorPathMap.set(fd, filePath);
|
|
102
|
-
|
|
134
|
+
original.call(this, ...args);
|
|
103
135
|
}
|
|
104
136
|
};
|
|
105
|
-
|
|
137
|
+
openSpy.callOriginal();
|
|
138
|
+
return;
|
|
106
139
|
}
|
|
107
|
-
const stateBefore = getFileState(filePath);
|
|
108
|
-
filesystemStateInfoMap.set(filePath, stateBefore);
|
|
109
140
|
const fd = openSpy.callOriginal();
|
|
110
|
-
|
|
111
|
-
|
|
141
|
+
if (typeof fd === "number") {
|
|
142
|
+
fileDescriptorPathMap.set(fd, filePath);
|
|
143
|
+
} else {
|
|
144
|
+
// it's a buffer (happens for readFile)
|
|
145
|
+
}
|
|
112
146
|
},
|
|
113
147
|
);
|
|
114
|
-
const closeSpy = spyMethod(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
148
|
+
const closeSpy = spyMethod(
|
|
149
|
+
_internalFs,
|
|
150
|
+
"close",
|
|
151
|
+
(fileDescriptor, callback) => {
|
|
152
|
+
const filePath = fileDescriptorPathMap.get(fileDescriptor);
|
|
153
|
+
if (!filePath) {
|
|
154
|
+
closeSpy.callOriginal();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const stateBefore = filesystemStateInfoMap.get(filePath);
|
|
158
|
+
if (!stateBefore) {
|
|
159
|
+
closeSpy.callOriginal();
|
|
160
|
+
fileDescriptorPathMap.delete(fileDescriptor);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const fileUrl = pathToFileURL(filePath);
|
|
164
|
+
if (callback) {
|
|
165
|
+
if (callback.context) {
|
|
166
|
+
const original = callback.context.callback;
|
|
167
|
+
callback.context.callback = function (...args) {
|
|
168
|
+
callback.context.callback = original;
|
|
169
|
+
const [error] = args;
|
|
170
|
+
if (error) {
|
|
171
|
+
original.call(this, ...args);
|
|
172
|
+
} else {
|
|
173
|
+
original.call(this, ...args);
|
|
174
|
+
fileDescriptorPathMap.delete(fileDescriptor);
|
|
175
|
+
filesystemStateInfoMap.delete(filePath);
|
|
176
|
+
const stateAfter = getFileStateWithinSpy(fileUrl);
|
|
177
|
+
readFile(fileUrl);
|
|
178
|
+
onWriteFileDone(fileUrl, stateBefore, stateAfter);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
closeSpy.callOriginal();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const original = callback.oncomplete;
|
|
185
|
+
callback.oncomplete = function (...args) {
|
|
186
|
+
callback.oncomplete = original;
|
|
187
|
+
const [error] = args;
|
|
188
|
+
if (error) {
|
|
189
|
+
original.call(this, ...args);
|
|
190
|
+
} else {
|
|
191
|
+
original.call(this, ...args);
|
|
192
|
+
fileDescriptorPathMap.delete(fileDescriptor);
|
|
193
|
+
filesystemStateInfoMap.delete(filePath);
|
|
194
|
+
const stateAfter = getFileStateWithinSpy(fileUrl);
|
|
195
|
+
onWriteFileDone(fileUrl, stateBefore, stateAfter);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
closeSpy.callOriginal();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
closeSpy.callOriginal();
|
|
122
202
|
fileDescriptorPathMap.delete(fileDescriptor);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const returnValue = closeSpy.callOriginal();
|
|
129
|
-
fileDescriptorPathMap.delete(fileDescriptor);
|
|
130
|
-
filesystemStateInfoMap.delete(filePath);
|
|
131
|
-
return returnValue;
|
|
132
|
-
});
|
|
203
|
+
filesystemStateInfoMap.delete(filePath);
|
|
204
|
+
const stateAfter = getFileStateWithinSpy(fileUrl);
|
|
205
|
+
onWriteFileDone(fileUrl, stateBefore, stateAfter);
|
|
206
|
+
},
|
|
207
|
+
);
|
|
133
208
|
const writeFileUtf8Spy = spyMethod(
|
|
134
209
|
_internalFs,
|
|
135
210
|
"writeFileUtf8",
|
|
136
211
|
(filePath) => {
|
|
137
212
|
const fileUrl = pathToFileURL(filePath);
|
|
138
|
-
const stateBefore =
|
|
213
|
+
const stateBefore = getFileStateWithinSpy(fileUrl);
|
|
139
214
|
writeFileUtf8Spy.callOriginal();
|
|
140
|
-
const stateAfter =
|
|
215
|
+
const stateAfter = getFileStateWithinSpy(fileUrl);
|
|
141
216
|
onWriteFileDone(fileUrl, stateBefore, stateAfter);
|
|
142
217
|
},
|
|
143
218
|
);
|
|
@@ -16,6 +16,7 @@ export const spyMethod = (object, method, spyCallback) => {
|
|
|
16
16
|
preventOriginalCall = jsenvSpySymbolValue.preventOriginalCall;
|
|
17
17
|
} else {
|
|
18
18
|
const original = current;
|
|
19
|
+
let currentThis;
|
|
19
20
|
let currentArgs;
|
|
20
21
|
let originalCalled = false;
|
|
21
22
|
let originalReturnValue;
|
|
@@ -32,33 +33,40 @@ export const spyMethod = (object, method, spyCallback) => {
|
|
|
32
33
|
return originalReturnValue;
|
|
33
34
|
}
|
|
34
35
|
someSpyUsedCallOriginal = true;
|
|
35
|
-
onOriginalCall(original(...currentArgs));
|
|
36
|
+
onOriginalCall(original.call(currentThis, ...currentArgs));
|
|
36
37
|
return originalReturnValue;
|
|
37
38
|
};
|
|
38
39
|
preventOriginalCall = () => {
|
|
39
40
|
preventOriginalCallCalled = true;
|
|
40
41
|
};
|
|
41
42
|
const spyCallbackSet = new Set();
|
|
42
|
-
const spy = (...args)
|
|
43
|
+
const spy = function (...args) {
|
|
43
44
|
if (spyExecuting) {
|
|
44
45
|
// when a spy is executing
|
|
45
46
|
// if it calls the method himself
|
|
46
47
|
// then we want this call to go trough
|
|
47
48
|
// and others spy should not know about it
|
|
48
|
-
onOriginalCall(original(...args));
|
|
49
|
+
onOriginalCall(original.call(this, ...args));
|
|
49
50
|
return originalReturnValue;
|
|
50
51
|
}
|
|
51
52
|
spyExecuting = true;
|
|
52
53
|
originalCalled = false;
|
|
54
|
+
currentThis = this;
|
|
53
55
|
currentArgs = args;
|
|
54
56
|
someSpyUsedCallOriginal = false;
|
|
55
|
-
allSpyUsedPreventOriginalCall =
|
|
57
|
+
allSpyUsedPreventOriginalCall = undefined;
|
|
56
58
|
for (const spyCallback of spyCallbackSet) {
|
|
59
|
+
if (spyCallback.disabled) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
57
62
|
try {
|
|
58
63
|
spyCallback(...args);
|
|
59
64
|
} finally {
|
|
60
65
|
if (preventOriginalCallCalled) {
|
|
61
66
|
preventOriginalCallCalled = false;
|
|
67
|
+
if (allSpyUsedPreventOriginalCall === undefined) {
|
|
68
|
+
allSpyUsedPreventOriginalCall = true;
|
|
69
|
+
}
|
|
62
70
|
} else {
|
|
63
71
|
allSpyUsedPreventOriginalCall = false;
|
|
64
72
|
}
|
|
@@ -68,6 +76,7 @@ export const spyMethod = (object, method, spyCallback) => {
|
|
|
68
76
|
if (!someSpyUsedCallOriginal && !allSpyUsedPreventOriginalCall) {
|
|
69
77
|
callOriginal();
|
|
70
78
|
}
|
|
79
|
+
currentThis = null;
|
|
71
80
|
currentArgs = null;
|
|
72
81
|
if (originalCalled) {
|
|
73
82
|
originalCalled = false;
|
|
@@ -103,9 +112,28 @@ export const spyMethod = (object, method, spyCallback) => {
|
|
|
103
112
|
const spyHooks = {
|
|
104
113
|
callOriginal,
|
|
105
114
|
preventOriginalCall,
|
|
115
|
+
disable: () => {
|
|
116
|
+
spyCallback.disabled = true;
|
|
117
|
+
},
|
|
118
|
+
enable: () => {
|
|
119
|
+
spyCallback.disabled = false;
|
|
120
|
+
},
|
|
106
121
|
remove: () => {
|
|
107
122
|
removeCallback(spyCallback);
|
|
108
123
|
},
|
|
109
124
|
};
|
|
110
125
|
return spyHooks;
|
|
111
126
|
};
|
|
127
|
+
|
|
128
|
+
export const disableSpiesWhileCalling = (fn, spyToDisableArray) => {
|
|
129
|
+
for (const spyToDisable of spyToDisableArray) {
|
|
130
|
+
spyToDisable.disable();
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
return fn();
|
|
134
|
+
} finally {
|
|
135
|
+
for (const spyToEnable of spyToDisableArray) {
|
|
136
|
+
spyToEnable.enable();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|