|
| 1 | +// This tests that cpSync dereferences symlinks found inside the copied tree, |
| 2 | +// not only a symlink passed as src. |
| 3 | +import { mustNotMutateObjectDeep } from '../common/index.mjs'; |
| 4 | +import { nextdir } from '../common/fs.js'; |
| 5 | +import assert from 'node:assert'; |
| 6 | +import { cpSync, lstatSync, mkdirSync, readFileSync, symlinkSync, writeFileSync } from 'node:fs'; |
| 7 | +import { basename, join } from 'node:path'; |
| 8 | +import tmpdir from '../common/tmpdir.js'; |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +const src = nextdir(); |
| 13 | +const target = nextdir(); |
| 14 | +const dest = nextdir(); |
| 15 | + |
| 16 | +mkdirSync(src, { recursive: true }); |
| 17 | +mkdirSync(join(target, 'dir'), { recursive: true }); |
| 18 | +writeFileSync(join(target, 'file.txt'), 'file', 'utf8'); |
| 19 | +writeFileSync(join(target, 'dir', 'nested.txt'), 'nested', 'utf8'); |
| 20 | +// Relative, as in the report: the link is resolved against its own directory. |
| 21 | +symlinkSync(join('..', basename(target), 'file.txt'), join(src, 'link-to-file')); |
| 22 | +symlinkSync(join(target, 'dir'), join(src, 'link-to-dir'), 'dir'); |
| 23 | + |
| 24 | +cpSync(src, dest, mustNotMutateObjectDeep({ dereference: true, recursive: true })); |
| 25 | + |
| 26 | +assert(!lstatSync(join(dest, 'link-to-file')).isSymbolicLink()); |
| 27 | +assert.strictEqual(readFileSync(join(dest, 'link-to-file'), 'utf8'), 'file'); |
| 28 | + |
| 29 | +assert(!lstatSync(join(dest, 'link-to-dir')).isSymbolicLink()); |
| 30 | +assert.strictEqual(readFileSync(join(dest, 'link-to-dir', 'nested.txt'), 'utf8'), 'nested'); |
| 31 | + |
| 32 | +// A dangling link has no target to copy. |
| 33 | +const dangling = nextdir(); |
| 34 | +mkdirSync(dangling, { recursive: true }); |
| 35 | +symlinkSync(join(target, 'missing.txt'), join(dangling, 'link')); |
| 36 | +assert.throws( |
| 37 | + () => cpSync(dangling, nextdir(), |
| 38 | + mustNotMutateObjectDeep({ dereference: true, recursive: true })), |
| 39 | + { code: 'ENOENT' }, |
| 40 | +); |
| 41 | + |
| 42 | +// A symlink cycle fails with ELOOP instead of recursing indefinitely. |
| 43 | +const looping = nextdir(); |
| 44 | +mkdirSync(looping, { recursive: true }); |
| 45 | +symlinkSync(looping, join(looping, 'loop'), 'dir'); |
| 46 | +assert.throws( |
| 47 | + () => cpSync(looping, nextdir(), |
| 48 | + mustNotMutateObjectDeep({ dereference: true, recursive: true })), |
| 49 | + { code: 'ELOOP' }, |
| 50 | +); |
| 51 | + |
| 52 | +// Under force, an existing destination link is replaced rather than written |
| 53 | +// through. Whether replacement happens at all still follows force and |
| 54 | +// errorOnExist. |
| 55 | +function withDestLink() { |
| 56 | + const outside = nextdir(); |
| 57 | + const from = nextdir(); |
| 58 | + const to = nextdir(); |
| 59 | + mkdirSync(outside, { recursive: true }); |
| 60 | + mkdirSync(from, { recursive: true }); |
| 61 | + mkdirSync(to, { recursive: true }); |
| 62 | + writeFileSync(join(outside, 'untouched.txt'), 'untouched', 'utf8'); |
| 63 | + symlinkSync(join(target, 'file.txt'), join(from, 'entry')); |
| 64 | + symlinkSync(join(outside, 'untouched.txt'), join(to, 'entry')); |
| 65 | + return { outside, from, to }; |
| 66 | +} |
| 67 | + |
| 68 | +{ |
| 69 | + const { outside, from, to } = withDestLink(); |
| 70 | + cpSync(from, to, mustNotMutateObjectDeep({ dereference: true, recursive: true })); |
| 71 | + assert(!lstatSync(join(to, 'entry')).isSymbolicLink()); |
| 72 | + assert.strictEqual(readFileSync(join(to, 'entry'), 'utf8'), 'file'); |
| 73 | + assert.strictEqual(readFileSync(join(outside, 'untouched.txt'), 'utf8'), 'untouched'); |
| 74 | +} |
| 75 | + |
| 76 | +{ |
| 77 | + const { outside, from, to } = withDestLink(); |
| 78 | + cpSync(from, to, mustNotMutateObjectDeep({ |
| 79 | + dereference: true, recursive: true, force: false, |
| 80 | + })); |
| 81 | + assert(lstatSync(join(to, 'entry')).isSymbolicLink()); |
| 82 | + assert.strictEqual(readFileSync(join(outside, 'untouched.txt'), 'utf8'), 'untouched'); |
| 83 | +} |
| 84 | + |
| 85 | +{ |
| 86 | + const { outside, from, to } = withDestLink(); |
| 87 | + assert.throws( |
| 88 | + () => cpSync(from, to, mustNotMutateObjectDeep({ |
| 89 | + dereference: true, recursive: true, force: false, errorOnExist: true, |
| 90 | + })), |
| 91 | + { code: 'ERR_FS_CP_EEXIST' }, |
| 92 | + ); |
| 93 | + assert(lstatSync(join(to, 'entry')).isSymbolicLink()); |
| 94 | + assert.strictEqual(readFileSync(join(outside, 'untouched.txt'), 'utf8'), 'untouched'); |
| 95 | +} |
| 96 | + |
| 97 | +// A link resolving to a directory descends into whatever already occupies the |
| 98 | +// destination path: a file there fails the way copying into it fails, and a |
| 99 | +// link to a directory is followed and merged into. |
| 100 | +{ |
| 101 | + const from = nextdir(); |
| 102 | + const to = nextdir(); |
| 103 | + mkdirSync(from, { recursive: true }); |
| 104 | + mkdirSync(to, { recursive: true }); |
| 105 | + symlinkSync(join(target, 'dir'), join(from, 'entry'), 'dir'); |
| 106 | + writeFileSync(join(to, 'entry'), 'occupied', 'utf8'); |
| 107 | + assert.throws( |
| 108 | + () => cpSync(from, to, |
| 109 | + mustNotMutateObjectDeep({ dereference: true, recursive: true })), |
| 110 | + { code: 'ENOTDIR' }, |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +{ |
| 115 | + const existing = nextdir(); |
| 116 | + const from = nextdir(); |
| 117 | + const to = nextdir(); |
| 118 | + mkdirSync(existing, { recursive: true }); |
| 119 | + mkdirSync(from, { recursive: true }); |
| 120 | + mkdirSync(to, { recursive: true }); |
| 121 | + writeFileSync(join(existing, 'kept.txt'), 'kept', 'utf8'); |
| 122 | + symlinkSync(join(target, 'dir'), join(from, 'entry'), 'dir'); |
| 123 | + symlinkSync(existing, join(to, 'entry'), 'dir'); |
| 124 | + |
| 125 | + cpSync(from, to, mustNotMutateObjectDeep({ dereference: true, recursive: true })); |
| 126 | + |
| 127 | + assert(lstatSync(join(to, 'entry')).isSymbolicLink()); |
| 128 | + assert.strictEqual(readFileSync(join(existing, 'kept.txt'), 'utf8'), 'kept'); |
| 129 | + assert.strictEqual(readFileSync(join(existing, 'nested.txt'), 'utf8'), 'nested'); |
| 130 | +} |
| 131 | + |
| 132 | +// A directory occupying the destination path is an occupied destination like |
| 133 | +// any other, so the same force and errorOnExist rules decide its fate. |
| 134 | +function withDestDir() { |
| 135 | + const from = nextdir(); |
| 136 | + const to = nextdir(); |
| 137 | + mkdirSync(from, { recursive: true }); |
| 138 | + mkdirSync(join(to, 'entry'), { recursive: true }); |
| 139 | + symlinkSync(join(target, 'file.txt'), join(from, 'entry')); |
| 140 | + return { from, to }; |
| 141 | +} |
| 142 | + |
| 143 | +{ |
| 144 | + const { from, to } = withDestDir(); |
| 145 | + cpSync(from, to, mustNotMutateObjectDeep({ dereference: true, recursive: true })); |
| 146 | + assert(lstatSync(join(to, 'entry')).isFile()); |
| 147 | + assert.strictEqual(readFileSync(join(to, 'entry'), 'utf8'), 'file'); |
| 148 | +} |
| 149 | + |
| 150 | +{ |
| 151 | + const { from, to } = withDestDir(); |
| 152 | + cpSync(from, to, mustNotMutateObjectDeep({ |
| 153 | + dereference: true, recursive: true, force: false, |
| 154 | + })); |
| 155 | + assert(lstatSync(join(to, 'entry')).isDirectory()); |
| 156 | +} |
| 157 | + |
| 158 | +{ |
| 159 | + const { from, to } = withDestDir(); |
| 160 | + assert.throws( |
| 161 | + () => cpSync(from, to, mustNotMutateObjectDeep({ |
| 162 | + dereference: true, recursive: true, force: false, errorOnExist: true, |
| 163 | + })), |
| 164 | + { code: 'ERR_FS_CP_EEXIST' }, |
| 165 | + ); |
| 166 | + assert(lstatSync(join(to, 'entry')).isDirectory()); |
| 167 | +} |
0 commit comments