8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 471fe71 commit 7e10f95Copy full SHA for 7e10f95
test/common/fs.js
@@ -0,0 +1,49 @@
1
+'use strict';
2
+
3
+const { mustNotMutateObjectDeep } = require('.');
4
+const { readdirSync } = require('node:fs');
5
+const { join } = require('node:path');
6
+const assert = require('node:assert');
7
+const tmpdir = require('./tmpdir.js');
8
9
+let dirc = 0;
10
+function nextdir(dirname) {
11
+ return tmpdir.resolve(dirname || `copy_%${++dirc}`);
12
+}
13
14
+function assertDirEquivalent(dir1, dir2) {
15
+ const dir1Entries = [];
16
+ collectEntries(dir1, dir1Entries);
17
+ const dir2Entries = [];
18
+ collectEntries(dir2, dir2Entries);
19
+ assert.strictEqual(dir1Entries.length, dir2Entries.length);
20
+ for (const entry1 of dir1Entries) {
21
+ const entry2 = dir2Entries.find((entry) => {
22
+ return entry.name === entry1.name;
23
+ });
24
+ assert(entry2, `entry ${entry2.name} not copied`);
25
+ if (entry1.isFile()) {
26
+ assert(entry2.isFile(), `${entry2.name} was not file`);
27
+ } else if (entry1.isDirectory()) {
28
+ assert(entry2.isDirectory(), `${entry2.name} was not directory`);
29
+ } else if (entry1.isSymbolicLink()) {
30
+ assert(entry2.isSymbolicLink(), `${entry2.name} was not symlink`);
31
+ }
32
33
34
35
+function collectEntries(dir, dirEntries) {
36
+ const newEntries = readdirSync(dir, mustNotMutateObjectDeep({ withFileTypes: true }));
37
+ for (const entry of newEntries) {
38
+ if (entry.isDirectory()) {
39
+ collectEntries(join(dir, entry.name), dirEntries);
40
41
42
+ dirEntries.push(...newEntries);
43
44
45
+module.exports = {
46
+ nextdir,
47
+ assertDirEquivalent,
48
+ collectEntries,
49
+};
test/parallel/parallel.status
@@ -24,8 +24,14 @@ test-snapshot-incompatible: SKIP
test-async-context-frame: PASS, FLAKY
# https://github.com/nodejs/node/issues/54534
test-runner-run-watch: PASS, FLAKY
-# https://github.com/nodejs/node/issues/56794
-test-fs-cp: PASS, FLAKY
+# https://github.com/nodejs/node/pull/59408#issuecomment-3170650933
+test-fs-cp-sync-error-on-exist: PASS, FLAKY
+test-fs-cp-sync-copy-symlink-not-pointing-to-folder: PASS, FLAKY
+test-fs-cp-sync-symlink-points-to-dest-error: PASS, FLAKY
+test-fs-cp-sync-resolve-relative-symlinks-false: PASS, FLAKY
+test-fs-cp-async-symlink-points-to-dest: PASS, FLAKY
+test-fs-cp-sync-unicode-folder-names: PASS, FLAKY
+test-fs-cp-sync-resolve-relative-symlinks-default: PASS, FLAKY
# https://github.com/nodejs/node/issues/56751
test-without-async-context-frame: PASS, FLAKY
test/parallel/test-fs-cp-async-async-filter-function.mjs
@@ -0,0 +1,32 @@
+// This tests that cp() supports async filter function.
+import { mustCall } from '../common/index.mjs';
+import { nextdir, collectEntries } from '../common/fs.js';
+import assert from 'node:assert';
+import { cp, statSync } from 'node:fs';
+import { setTimeout } from 'node:timers/promises';
+import tmpdir from '../common/tmpdir.js';
+import fixtures from '../common/fixtures.js';
+tmpdir.refresh();
+const src = fixtures.path('copy/kitchen-sink');
+const dest = nextdir();
+cp(src, dest, {
+ filter: async (path) => {
+ await setTimeout(5, 'done');
+ const pathStat = statSync(path);
+ return pathStat.isDirectory() || path.endsWith('.js');
+ },
+ dereference: true,
+ recursive: true,
+}, mustCall((err) => {
+ assert.strictEqual(err, null);
+ const destEntries = [];
+ collectEntries(dest, destEntries);
+ for (const entry of destEntries) {
+ assert.strictEqual(
+ entry.isDirectory() || entry.name.endsWith('.js'),
+ true
+ );
+}));
test/parallel/test-fs-cp-async-copy-non-directory-symlink.mjs
@@ -0,0 +1,22 @@
+// This tests that cp() copies link if it does not point to folder in src.
+import { mustCall, mustNotMutateObjectDeep } from '../common/index.mjs';
+import { nextdir } from '../common/fs.js';
+import { cp, mkdirSync, readlinkSync, symlinkSync } from 'node:fs';
+import { join } from 'node:path';
+const src = nextdir();
+mkdirSync(join(src, 'a', 'b'), mustNotMutateObjectDeep({ recursive: true }));
+symlinkSync(src, join(src, 'a', 'c'));
+mkdirSync(join(dest, 'a'), mustNotMutateObjectDeep({ recursive: true }));
+symlinkSync(dest, join(dest, 'a', 'c'));
+cp(src, dest, mustNotMutateObjectDeep({ recursive: true }), mustCall((err) => {
+ const link = readlinkSync(join(dest, 'a', 'c'));
+ assert.strictEqual(link, src);
test/parallel/test-fs-cp-async-dereference-force-false-silent-fail.mjs
@@ -0,0 +1,25 @@
+// This tests that it does not fail if the same directory is copied to dest
+// twice, when dereference is true, and force is false (fails silently).
+import { cp, cpSync, lstatSync } from 'node:fs';
+const destFile = join(dest, 'a/b/README2.md');
+cpSync(src, dest, mustNotMutateObjectDeep({ dereference: true, recursive: true }));
+ recursive: true
+ const stat = lstatSync(destFile);
+ assert(stat.isFile());
test/parallel/test-fs-cp-async-dereference-symlink.mjs
@@ -0,0 +1,27 @@
+// This tests that cp() copies file itself, rather than symlink, when dereference is true.
+import { cp, lstatSync, mkdirSync, symlinkSync, writeFileSync } from 'node:fs';
+mkdirSync(src, mustNotMutateObjectDeep({ recursive: true }));
+writeFileSync(join(src, 'foo.js'), 'foo', 'utf8');
+symlinkSync(join(src, 'foo.js'), join(src, 'bar.js'));
+mkdirSync(dest, mustNotMutateObjectDeep({ recursive: true }));
+const destFile = join(dest, 'foo.js');
+cp(join(src, 'bar.js'), destFile, mustNotMutateObjectDeep({ dereference: true }),
+ mustCall((err) => {
+ })
+);
test/parallel/test-fs-cp-async-dest-symlink-points-to-src-error.mjs
@@ -0,0 +1,21 @@
+// This tests that cp() returns error if parent directory of symlink in dest points to src.
+import { cp, mkdirSync, symlinkSync } from 'node:fs';
+mkdirSync(join(src, 'a'), mustNotMutateObjectDeep({ recursive: true }));
+// Create symlink in dest pointing to src.
+const destLink = join(dest, 'b');
+symlinkSync(src, destLink);
+cp(src, join(dest, 'b', 'c'), mustCall((err) => {
+ assert.strictEqual(err.code, 'ERR_FS_CP_EINVAL');
test/parallel/test-fs-cp-async-dir-to-file.mjs
@@ -0,0 +1,17 @@
+// This tests that cp() returns error if attempt is made to copy directory to file.
+import { cp, mkdirSync } from 'node:fs';
+const dest = fixtures.path('copy/kitchen-sink/README.md');
+cp(src, dest, mustCall((err) => {
+ assert.strictEqual(err.code, 'ERR_FS_CP_DIR_TO_NON_DIR');
test/parallel/test-fs-cp-async-error-on-exist.mjs
+// This tests that cp() returns error if errorOnExist is true, force is false, and file or folder copied over.
+import { cp, cpSync } from 'node:fs';
+cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
+ errorOnExist: true,
+ force: false,
+ assert.strictEqual(err.code, 'ERR_FS_CP_EEXIST');
test/parallel/test-fs-cp-async-file-to-dir.mjs
+// This tests that cp() returns error if attempt is made to copy file to directory.
+const src = fixtures.path('copy/kitchen-sink/README.md');
+ assert.strictEqual(err.code, 'ERR_FS_CP_NON_DIR_TO_DIR');