Skip to content

Commit

Permalink
fix: linting (npm#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored Aug 11, 2022
1 parent df2d8e8 commit 163f021
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 45 deletions.
2 changes: 2 additions & 0 deletions lib/link-gently.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const linkGently = async ({ path, to, from, absFrom, force }) => {
if (target.indexOf(path) === 0 || force) {
return rm(to).then(() => CLOBBER)
}
// neither skip nor clobber
return false
})
} else {
// doesn't exist, dir might not either
Expand Down
5 changes: 3 additions & 2 deletions lib/shim-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,20 @@ const shimBin = ({ path, to, from, absFrom, force }) => {
}

if (force) {
return
return false
}

return Promise.all(shims.map((s, i) => [s, stats[i]]).map(([s, st]) => {
if (!st) {
return
return false
}
return readCmdShim(s)
.then(target => {
target = resolve(dirname(to), target)
if (target.indexOf(resolve(path)) !== 0) {
return failEEXIST({ from, to, path })
}
return false
}, er => handleReadCmdShimError({ er, from, to }))
}))
})
Expand Down
72 changes: 34 additions & 38 deletions test/fix-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ const umask = process.umask()
const fs = require('fs')
const { readFileSync, statSync, chmodSync } = fs

t.test('fix windows hashbang', t => {
t.test('fix windows hashbang', async t => {
const dir = t.testdir({
whb: `#!/usr/bin/env node\r\nconsole.log('hello')\r\n`,
})
chmodSync(`${dir}/whb`, 0o644)
return fixBin(`${dir}/whb`).then(() => {
t.equal((statSync(`${dir}/whb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/whb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})
await fixBin(`${dir}/whb`)
t.equal((statSync(`${dir}/whb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/whb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})

t.test('dont fix non-windows hashbang file', t => {
t.test('dont fix non-windows hashbang file', async t => {
const dir = t.testdir({
goodhb: `#!/usr/bin/env node\nconsole.log('hello')\r\n`,
})
chmodSync(`${dir}/goodhb`, 0o644)
return fixBin(`${dir}/goodhb`).then(() => {
t.equal((statSync(`${dir}/goodhb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/goodhb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})
await fixBin(`${dir}/goodhb`)
t.equal((statSync(`${dir}/goodhb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/goodhb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})

t.test('failure to read means not a windows hash bang file', t => {
t.test('failure to read means not a windows hash bang file', async t => {
const fsMock = {
...fs,
read: (a, b, c, d, e, cb) => {
Expand All @@ -45,15 +43,14 @@ t.test('failure to read means not a windows hash bang file', t => {
whb: `#!/usr/bin/env node\r\nconsole.log('hello')\r\n`,
})
chmodSync(`${dir}/whb`, 0o644)
return mockedFixBin(`${dir}/whb`).then(() => {
t.equal((statSync(`${dir}/whb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/whb`, 'utf8'),
/* eslint-disable-next-line max-len */
`#!/usr/bin/env node\r\nconsole.log('hello')\r\n`, 'did not fix \\r on hashbang line (failed read)')
})
await mockedFixBin(`${dir}/whb`)
t.equal((statSync(`${dir}/whb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/whb`, 'utf8'),
/* eslint-disable-next-line max-len */
`#!/usr/bin/env node\r\nconsole.log('hello')\r\n`, 'did not fix \\r on hashbang line (failed read)')
})

t.test('failure to close is ignored', t => {
t.test('failure to close is ignored', async t => {
const fsMock = {
...fs,
close: (fd, cb) => {
Expand All @@ -69,34 +66,33 @@ t.test('failure to close is ignored', t => {
whb: `#!/usr/bin/env node\r\nconsole.log('hello')\r\n`,
})
chmodSync(`${dir}/whb`, 0o644)
return mockedFixBin(`${dir}/whb`).then(() => {
t.equal((statSync(`${dir}/whb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/whb`, 'utf8'),
/* eslint-disable-next-line max-len */
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line (ignored failed close)')
})
await mockedFixBin(`${dir}/whb`)
t.equal((statSync(`${dir}/whb`).mode & 0o777), 0o777 & (~umask), 'has exec perms')
t.equal(
readFileSync(`${dir}/whb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`,
'fixed \\r on hashbang line (ignored failed close)'
)
})

t.test('custom exec mode', t => {
t.test('custom exec mode', async t => {
const dir = t.testdir({
goodhb: `#!/usr/bin/env node\nconsole.log('hello')\r\n`,
})
chmodSync(`${dir}/goodhb`, 0o644)
return fixBin(`${dir}/goodhb`, 0o755).then(() => {
t.equal((statSync(`${dir}/goodhb`).mode & 0o755), 0o755 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/goodhb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})
await fixBin(`${dir}/goodhb`, 0o755)
t.equal((statSync(`${dir}/goodhb`).mode & 0o755), 0o755 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/goodhb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})

t.test('custom exec mode in windows', t => {
t.test('custom exec mode in windows', async t => {
const dir = t.testdir({
goodhb: `#!/usr/bin/env node\r\nconsole.log('hello')\r\n`,
})
chmodSync(`${dir}/goodhb`, 0o644)
return fixBin(`${dir}/goodhb`, 0o755).then(() => {
t.equal((statSync(`${dir}/goodhb`).mode & 0o755), 0o755 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/goodhb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})
await fixBin(`${dir}/goodhb`, 0o755)
t.equal((statSync(`${dir}/goodhb`).mode & 0o755), 0o755 & (~umask), 'has exec perms')
t.equal(readFileSync(`${dir}/goodhb`, 'utf8'),
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})
9 changes: 4 additions & 5 deletions test/link-gently.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ t.test('racey race', async t => {
existingLink: t.fixture('symlink', './pkg/hello.js'),
existingFile: 'hello',
})
return Promise.all([
await Promise.all([
mockedLinkGently({
path: `${dir}/pkg`,
from: `./pkg/hello.js`,
Expand All @@ -125,8 +125,7 @@ t.test('racey race', async t => {
force: true,
}),
new Promise((res) => fs.symlink(__filename, `${dir}/racecar`, 'file', res)),
]).then(() => {
const target = fs.readlinkSync(`${dir}/racecar`)
t.match(target, /^\.\/(other)?pkg\/hello\.js$/, 'should link to one of them')
})
])
const target = fs.readlinkSync(`${dir}/racecar`)
t.match(target, /^\.\/(other)?pkg\/hello\.js$/, 'should link to one of them')
})

0 comments on commit 163f021

Please sign in to comment.