| 1 |
const { mkdirSync } = require('node:fs') |
| 2 |
const localeCompare = require('@isaacs/string-locale-compare')('en') |
| 3 |
const { unlinkSync, symlinkSync, readFileSync, writeFileSync } = require('node:fs') |
| 4 |
const { relative, resolve, dirname } = require('node:path') |
| 5 |
const mkdirp = (p) => mkdirSync(p, { recursive: true }) |
| 6 |
|
| 7 |
const fixtures = __dirname |
| 8 |
|
| 9 |
const roots = [ |
| 10 |
'bundle', |
| 11 |
'deepmixedloop', |
| 12 |
'deeproot/root', |
| 13 |
'devloop', |
| 14 |
'linkedroot', |
| 15 |
'links-all-over', |
| 16 |
'mixedloop', |
| 17 |
'mixedmidway', |
| 18 |
'noname', |
| 19 |
'optionalloop', |
| 20 |
'optofdev', |
| 21 |
'other', |
| 22 |
'root', |
| 23 |
// This test flakes out on Apple Silicon |
| 24 |
// https://github.com/npm/cli/pull/7411 |
| 25 |
process.platform === 'darwin' && process.arch === 'arm64' && process.env.TAP_SNAPSHOT !== '1' |
| 26 |
? null |
| 27 |
: 'selflink', |
| 28 |
'symlinked-node-modules/example', |
| 29 |
'workspace', |
| 30 |
'workspace2', |
| 31 |
'workspace3', |
| 32 |
'workspaces-simple', |
| 33 |
'install-types', |
| 34 |
'pnpm', |
| 35 |
'external-dep/root', |
| 36 |
'external-link/root', |
| 37 |
'yarn-lock-mkdirp-file-dep', |
| 38 |
'link-dep-cycle', |
| 39 |
'link-dep-nested', |
| 40 |
'link-dep-nested/root', |
| 41 |
'external-link-cached-dummy-dep/root', |
| 42 |
].filter(Boolean) |
| 43 |
|
| 44 |
const symlinks = { |
| 45 |
'selflink/node_modules/@scope/z/node_modules/glob': |
| 46 |
'../../../foo/node_modules/glob', |
| 47 |
'selflink/node_modules/foo/node_modules/selflink': |
| 48 |
'../../..', |
| 49 |
'other/node_modules/glob': |
| 50 |
'../../root/node_modules/@scope/x/node_modules/glob', |
| 51 |
linkedroot: 'root', |
| 52 |
'deep/root': '../root', |
| 53 |
deeproot: 'deep', |
| 54 |
'badlink/node_modules/foo': 'foo', |
| 55 |
'badlink/node_modules/bar': 'baz', |
| 56 |
'testing-peer-deps-link': 'testing-peer-deps', |
| 57 |
|
| 58 |
'workspace/node_modules/a': '../packages/a', |
| 59 |
'workspace/node_modules/b': '../packages/b', |
| 60 |
'workspace/node_modules/c': '../packages/c', |
| 61 |
'workspace/packages/a/node_modules/b': '../../../packages/b', |
| 62 |
'workspace/packages/a/node_modules/c': '../../../packages/c', |
| 63 |
'workspace/packages/b/node_modules/a': '../../../packages/a', |
| 64 |
'workspace/packages/b/node_modules/c': '../../../packages/c', |
| 65 |
'workspace/packages/c/node_modules/a': '../../../packages/a', |
| 66 |
'workspace/packages/c/node_modules/b': '../../../packages/b', |
| 67 |
|
| 68 |
'workspace2/node_modules/c/node_modules/d': '../../b/node_modules/d', |
| 69 |
'workspace2/node_modules/c/node_modules/x': '../../../x', |
| 70 |
|
| 71 |
'workspace3/node_modules/app': '../app', |
| 72 |
'workspace3/node_modules/a': '../packages/a', |
| 73 |
'workspace3/node_modules/b': '../packages/b', |
| 74 |
'workspace3/node_modules/c': '../packages/c', |
| 75 |
'workspaces-simple/node_modules/a': '../a', |
| 76 |
'workspaces-simple/node_modules/b': '../b', |
| 77 |
|
| 78 |
'links-all-over/node_modules/link-outside-nest': |
| 79 |
'../real', |
| 80 |
'links-all-over/node_modules/link-deep': |
| 81 |
'nest/node_modules/a/node_modules/b/node_modules/c/node_modules/d/node_modules/deep', |
| 82 |
'links-all-over/node_modules/link-link': 'link-deep', |
| 83 |
'links-all-over/node_modules/nest/node_modules/link-in-nest': |
| 84 |
'../../../real', |
| 85 |
|
| 86 |
'symlinked-node-modules/example/node_modules': |
| 87 |
'../linked-node-modules/', |
| 88 |
'symlinked-node-modules/linked-node-modules/bar': '../bar', |
| 89 |
|
| 90 |
'install-types/node_modules/symlink': '../abbrev-link-target', |
| 91 |
|
| 92 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/@scope/x/1.0.0/node_modules/a': |
| 93 |
'../../../../a/1.0.0/node_modules/a', |
| 94 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/@scope/x/1.0.0/node_modules/b': |
| 95 |
'../../../../b/1.0.0/node_modules/b', |
| 96 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/@scope/x/1.0.0/node_modules/c': |
| 97 |
'../../../../c/1.0.0/node_modules/c', |
| 98 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/a/1.0.0/node_modules/b': |
| 99 |
'../../../b/1.0.0/node_modules/b', |
| 100 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/a/1.0.0/node_modules/c': |
| 101 |
'../../../c/1.0.0/node_modules/c', |
| 102 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/b/1.0.0/node_modules/c': |
| 103 |
'../../../c/1.0.0/node_modules/c', |
| 104 |
'pnpm/node_modules/.pnpm/registry.npmjs.org/c/1.0.0/node_modules/@scope/x': |
| 105 |
'../../../../@scope/x/1.0.0/node_modules/@scope/x', |
| 106 |
'pnpm/node_modules/@scope/x': |
| 107 |
'../.pnpm/registry.npmjs.org/@scope/x/1.0.0/node_modules/@scope/x', |
| 108 |
'pnpm/node_modules/a': '.pnpm/registry.npmjs.org/a/1.0.0/node_modules/a', |
| 109 |
|
| 110 |
'global-style/lib-link': './lib', |
| 111 |
|
| 112 |
'external-link/root/node_modules/o': '../../m/node_modules/n/o', |
| 113 |
'external-link/root/node_modules/o2': '../../m/node_modules/n/o2', |
| 114 |
'external-link/root/node_modules/j': '../../i/j', |
| 115 |
'external-link/root/node_modules/x/node_modules/b': '../../../../a/node_modules/b', |
| 116 |
|
| 117 |
'link-dep-cycle/node_modules/a': '../a', |
| 118 |
'link-dep-cycle/node_modules/b': '../b', |
| 119 |
'link-dep-cycle/a/node_modules/b': '../../b', |
| 120 |
'link-dep-cycle/b/node_modules/a': '../../a', |
| 121 |
|
| 122 |
'link-dep-nested/node_modules/once': '../once', |
| 123 |
'link-dep-nested/node_modules/foo': '../once', |
| 124 |
'link-dep-nested/root/node_modules/bork': '../..', |
| 125 |
|
| 126 |
'external-link-cached-dummy-dep/root/node_modules/z': '../../a/t/u/v/w/x/y/z', |
| 127 |
'external-link-cached-dummy-dep/root/node_modules/x': '../../a/node_modules/b/node_modules/x', |
| 128 |
'cli-750/node_modules/app': '../app', |
| 129 |
'cli-750/node_modules/lib': '../lib', |
| 130 |
'link-dep/node_modules/linked-dep': '../target', |
| 131 |
'link-dev-dep/node_modules/linked-dev-dep': '../target', |
| 132 |
'link-meta-deps/node_modules/@isaacs/testing-link-dep/node_modules/linked-dep': |
| 133 |
'../target', |
| 134 |
'prune-dev-bins/node_modules/.bin/yes': '../yes/yes.js', |
| 135 |
'old-package-lock-with-bins/node_modules/.bin/ruy': |
| 136 |
'../ruy/bin/index.js', |
| 137 |
|
| 138 |
'workspaces-add-new-dep/node_modules/pkg-a': '../a', |
| 139 |
'workspaces-add-new-dep/node_modules/a': '../a', |
| 140 |
|
| 141 |
'workspaces-non-simplistic/node_modules/pkg-a': '../a', |
| 142 |
|
| 143 |
'testing-bundledeps-link': './testing-bundledeps-2', |
| 144 |
} |
| 145 |
|
| 146 |
const cleanup = () => Object.keys(symlinks).forEach(s => { |
| 147 |
try { |
| 148 |
unlinkSync(resolve(__dirname, s)) |
| 149 |
} catch { |
| 150 |
// ok if cleanup fails |
| 151 |
} |
| 152 |
}) |
| 153 |
|
| 154 |
const setup = () => { |
| 155 |
const links = [] |
| 156 |
let didSomething = false |
| 157 |
Object.keys(symlinks).forEach(s => { |
| 158 |
const p = resolve(__dirname, s) |
| 159 |
mkdirp(dirname(p)) |
| 160 |
const rel = relative(resolve(__dirname), p) |
| 161 |
links.push('/' + rel.replace(/\\/g, '/')) |
| 162 |
|
| 163 |
try { |
| 164 |
symlinkSync(symlinks[s], p, 'junction') |
| 165 |
didSomething = true |
| 166 |
} catch { |
| 167 |
// it's fine for this to throw, since it typically means |
| 168 |
// that the links already exist, and that's fine. |
| 169 |
} |
| 170 |
}) |
| 171 |
if (didSomething) { |
| 172 |
const start = '### BEGIN IGNORED SYMLINKS ###' |
| 173 |
const end = '### END IGNORED SYMLINKS ###' |
| 174 |
const gifile = resolve(__dirname, './.gitignore') |
| 175 |
const gitignore = readFileSync(gifile, 'utf8') |
| 176 |
.replace(new RegExp(`${start}[\\s\\S]*${end}`), [ |
| 177 |
start, |
| 178 |
'### this list is generated automatically, do not edit directly', |
| 179 |
'### update it by running `node test/fixtures/index.js`', |
| 180 |
...links.sort(localeCompare), |
| 181 |
end, |
| 182 |
].join('\n')) |
| 183 |
writeFileSync(gifile, gitignore) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
const doCleanup = process.argv[2] === 'cleanup' && require.main === module || |
| 188 |
process.env.ARBORIST_FIXTURE_CLEANUP === '1' |
| 189 |
|
| 190 |
if (doCleanup) { |
| 191 |
cleanup() |
| 192 |
} else { |
| 193 |
setup() |
| 194 |
} |
| 195 |
|
| 196 |
module.exports = { |
| 197 |
roots, |
| 198 |
symlinks, |
| 199 |
fixtures, |
| 200 |
} |