Skip to content

Commit

Permalink
make the selfLink a best effort if not explicitly true
Browse files Browse the repository at this point in the history
This stops it from always failing on Windows which is making me so
annoyed when I have to debug stuff on my windows VM.

Fix: #20
  • Loading branch information
isaacs committed Nov 4, 2023
1 parent 41657d2 commit b7c6b62
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/self-dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ const linkedAlready = (pkg: Package) => {
}

export const link = (pkg: Package, where: string) => {
if (
!pkg.name ||
pkg?.tshy?.selfLink === false ||
linkedAlready(pkg)
) {
const selfLink = pkg?.tshy?.selfLink
if (!pkg.name || selfLink === false || linkedAlready(pkg)) {
return
}
const dest = resolve(where, 'node_modules', pkg.name)
Expand All @@ -56,7 +53,14 @@ export const link = (pkg: Package, where: string) => {
symlinkSync(src, dest)
} catch {
rimrafSync(dest)
symlinkSync(src, dest)
let threw = true
try {
symlinkSync(src, dest)
threw = false
} finally {
// best effort if not set explicitly. suppress error with return.
if (threw && selfLink === undefined) return
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions tap-snapshots/test/self-dep.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,64 @@ Array [
]
`

exports[`test/self-dep.ts > TAP > throw both times, but accept if best-effort > mkdirps 1`] = `
Array [
Array [
"{CWD}/some/path/node_modules",
],
]
`

exports[`test/self-dep.ts > TAP > throw both times, but accept if best-effort > rimrafs 1`] = `
Array [
Array [
"{CWD}/some/path/node_modules/name",
],
]
`

exports[`test/self-dep.ts > TAP > throw both times, but accept if best-effort > symlinks 1`] = `
Array [
Array [
"../../..",
"{CWD}/some/path/node_modules/name",
],
Array [
"../../..",
"{CWD}/some/path/node_modules/name",
],
]
`

exports[`test/self-dep.ts > TAP > throw both times, but self-link is required > mkdirps 1`] = `
Array [
Array [
"{CWD}/some/path/node_modules",
],
]
`

exports[`test/self-dep.ts > TAP > throw both times, but self-link is required > rimrafs 1`] = `
Array [
Array [
"{CWD}/some/path/node_modules/name",
],
]
`

exports[`test/self-dep.ts > TAP > throw both times, but self-link is required > symlinks 1`] = `
Array [
Array [
"../../..",
"{CWD}/some/path/node_modules/name",
],
Array [
"../../..",
"{CWD}/some/path/node_modules/name",
],
]
`

exports[`test/self-dep.ts > TAP > try one more time if it fails > mkdirps 1`] = `
Array [
Array [
Expand Down
29 changes: 28 additions & 1 deletion test/self-dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const mkdirpCalls = t.capture(

import * as FS from 'node:fs'
let symlinkThrow: Error | undefined = undefined
let symlinkThrowAgain: Error | undefined = undefined
const fs = {
symlinkSync: () => {
if (symlinkThrow) {
try {
throw symlinkThrow
} finally {
symlinkThrow = undefined
symlinkThrow = symlinkThrowAgain
symlinkThrowAgain = undefined
}
}
},
Expand Down Expand Up @@ -75,6 +77,31 @@ t.test('try one more time if it fails', t => {
t.end()
})

t.test('throw both times, but accept if best-effort', t => {
symlinkThrow = new Error('EPERM')
symlinkThrowAgain = new Error('EPERM')
link({ name: 'name', version: '1.2.3' }, 'some/path')
t.matchSnapshot(symlinkCalls(), 'symlinks')
t.matchSnapshot(rimrafCalls(), 'rimrafs')
t.matchSnapshot(mkdirpCalls(), 'mkdirps')
t.end()
})

t.test('throw both times, but self-link is required', t => {
symlinkThrow = new Error('EPERM')
symlinkThrowAgain = new Error('EPERM')
t.throws(() =>
link(
{ name: 'name', version: '1.2.3', tshy: { selfLink: true } },
'some/path'
)
)
t.matchSnapshot(symlinkCalls(), 'symlinks')
t.matchSnapshot(rimrafCalls(), 'rimrafs')
t.matchSnapshot(mkdirpCalls(), 'mkdirps')
t.end()
})

t.test('link, but no dirs made', t => {
link({ name: 'name', version: '1.2.3' }, 'some/path')
unlink({ name: 'name', version: '1.2.3' }, 'some/path')
Expand Down

0 comments on commit b7c6b62

Please sign in to comment.