Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/fix-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
chmod,
open,
readFile,
stat,
} = require('fs/promises')

const execMode = 0o777 & (~process.umask())
Expand Down Expand Up @@ -35,7 +36,8 @@ const dos2Unix = file =>
readFile(file, 'utf8').then(content =>
writeFileAtomic(file, content.replace(/^(#![^\n]+)\r\n/, '$1\n')))

const fixBin = (file, mode = execMode) => chmod(file, mode)
const fixBin = (file, mode = execMode) => stat(file)
.then(st => (st.mode & mode) === mode ? null : chmod(file, mode))
.then(() => isWindowsHashbangFile(file))
.then(isWHB => isWHB ? dos2Unix(file) : null)

Expand Down
20 changes: 20 additions & 0 deletions test/fix-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ t.test('custom exec mode', async t => {
`#!/usr/bin/env node\nconsole.log('hello')\r\n`, 'fixed \\r on hashbang line')
})

t.test('skip chmod if file already has exec permissions', async t => {
const fsMock = {
...fs.promises,
chmod: async () => {
throw new Error('chmod should not be called')
},
}
const mockedFixBin = requireInject('../lib/fix-bin.js', {
'fs/promises': fsMock,
})

const dir = t.testdir({
execfile: `#!/usr/bin/env node\nconsole.log('hello')\n`,
})
chmodSync(`${dir}/execfile`, 0o755)
// should not throw even though chmod is mocked to throw
await mockedFixBin(`${dir}/execfile`, 0o755)
t.equal((statSync(`${dir}/execfile`).mode & 0o755), 0o755 & (~umask), 'still has exec perms')
})

t.test('custom exec mode in windows', async t => {
const dir = t.testdir({
goodhb: `#!/usr/bin/env node\r\nconsole.log('hello')\r\n`,
Expand Down
Loading