@jsenv/snapshot 2.10.1 → 2.10.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/snapshot",
3
- "version": "2.10.1",
3
+ "version": "2.10.2",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -379,24 +379,22 @@ ${extraUrls.join("\n")}`);
379
379
  const relativeUrl = urlToRelativeUrl(directoryItemUrl, directoryUrl);
380
380
  if (directoryItemStat.isDirectory()) {
381
381
  ensurePathnameTrailingSlash(directoryItemUrl);
382
- if (!shouldVisitDirectory(directoryItemUrl)) {
382
+ if (!shouldVisitDirectory(directoryItemUrl.href)) {
383
383
  continue;
384
384
  }
385
- contentSnapshotNaturalOrder[relativeUrl] = createDirectorySnapshot(
386
- directoryItemUrl,
387
- {
388
- shouldVisitDirectory,
389
- shouldIncludeFile,
390
- shouldCompareFileContent,
391
- clean,
392
- },
393
- );
394
- if (clean) {
385
+ const subdirSnapshot = createDirectorySnapshot(directoryItemUrl, {
386
+ shouldVisitDirectory,
387
+ shouldIncludeFile,
388
+ shouldCompareFileContent,
389
+ clean,
390
+ });
391
+ contentSnapshotNaturalOrder[relativeUrl] = subdirSnapshot;
392
+ if (clean && subdirSnapshot) {
395
393
  removeDirectorySync(directoryItemUrl);
396
394
  }
397
395
  continue;
398
396
  }
399
- if (!shouldIncludeFile(directoryItemUrl)) {
397
+ if (!shouldIncludeFile(directoryItemUrl.href)) {
400
398
  continue;
401
399
  }
402
400
  contentSnapshotNaturalOrder[relativeUrl] =
@@ -4,13 +4,14 @@
4
4
  // https://github.com/tschaub/mock-fs/issues/348
5
5
 
6
6
  import {
7
+ comparePathnames,
7
8
  removeDirectorySync,
8
9
  removeFileSync,
9
10
  writeFileSync,
10
11
  } from "@jsenv/filesystem";
11
12
  import { URL_META } from "@jsenv/url-meta";
12
13
  import { ensurePathnameTrailingSlash, yieldAncestorUrls } from "@jsenv/urls";
13
- import { readFileSync, statSync } from "node:fs";
14
+ import { readdirSync, readFileSync, statSync } from "node:fs";
14
15
  import { pathToFileURL } from "node:url";
15
16
  import {
16
17
  disableHooksWhileCalling,
@@ -47,6 +48,7 @@ export const spyFilesystemCalls = (
47
48
  const filesystemStateInfoMap = new Map();
48
49
  const fileDescriptorPathMap = new Map();
49
50
  const fileRestoreMap = new Map();
51
+ const dirRestoreMap = new Map();
50
52
  const onWriteFileDone = (fileUrl, stateBefore, stateAfter) => {
51
53
  if (!stateAfter.found) {
52
54
  // seems to be possible somehow
@@ -99,12 +101,16 @@ export const spyFilesystemCalls = (
99
101
  action === "compare_presence_only" ||
100
102
  action === true;
101
103
  if (action === "undo" || shouldCompare) {
102
- if (undoFilesystemSideEffects && !fileRestoreMap.has(directoryUrl)) {
103
- fileRestoreMap.set(directoryUrl, () => {
104
- removeDirectorySync(directoryUrl, {
105
- allowUseless: true,
106
- recursive: true,
107
- });
104
+ if (undoFilesystemSideEffects && !dirRestoreMap.has(directoryUrl)) {
105
+ dirRestoreMap.set(directoryUrl, () => {
106
+ try {
107
+ const isEmpty = readdirSync(new URL(directoryUrl)).length === 0;
108
+ if (isEmpty) {
109
+ removeDirectorySync(directoryUrl, {
110
+ allowUseless: true,
111
+ });
112
+ }
113
+ } catch {}
108
114
  });
109
115
  }
110
116
  }
@@ -253,6 +259,18 @@ export const spyFilesystemCalls = (
253
259
  restore();
254
260
  }
255
261
  fileRestoreMap.clear();
262
+ const dirUrls = Array.from(dirRestoreMap.keys());
263
+ dirUrls.sort((left, right) => {
264
+ return comparePathnames(
265
+ new URL(left).pathname,
266
+ new URL(right).pathname,
267
+ );
268
+ });
269
+ for (const dirUrl of dirUrls) {
270
+ const restore = dirRestoreMap.get(dirUrl);
271
+ restore();
272
+ }
273
+ dirRestoreMap.clear();
256
274
  },
257
275
  };
258
276
  };
@@ -31,10 +31,7 @@ export const snapshotTests = async (
31
31
  fnRegisteringTest,
32
32
  {
33
33
  outFilePattern = "./_[source_filename]/[filename]",
34
- filesystemActions = {
35
- "**": "compare",
36
- // "**/*.svg": "compare_presence_only",
37
- },
34
+ filesystemActions,
38
35
  rootDirectoryUrl,
39
36
  generatedBy = true,
40
37
  executionEffects,
@@ -44,6 +41,7 @@ export const snapshotTests = async (
44
41
  } = {},
45
42
  ) => {
46
43
  filesystemActions = {
44
+ "**": "compare",
47
45
  ...filesystemActions,
48
46
  "**/*.svg": "compare_presence_only",
49
47
  };