Skip to content

Commit 109b510

Browse files
committed
fix: resolve package binaries correctly (fixes all scripts)
The previous `resolveBin` modification actually broke all scripts (including `validate` which is used on CI, which caused false positives for everything.
1 parent 7a35bef commit 109b510

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/__tests__/utils.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,35 @@ test('resolveHoverScripts resolves to "hover-scripts" if not in the @hover/javas
4141
expect(require('../utils').resolveHoverScripts()).toBe('hover-scripts')
4242
})
4343

44-
test(`resolveBin resolves to the full path when it's not in $PATH`, () => {
45-
expect(require('../utils').resolveBin('cross-env')).toBe(
46-
require.resolve('cross-env/src/bin/cross-env').replace(process.cwd(), '.'),
47-
)
48-
})
44+
describe('resolveBin', () => {
45+
beforeAll(() => {
46+
jest.unmock('read-pkg-up')
47+
})
4948

50-
test(`resolveBin resolves to the binary if it's in $PATH`, () => {
51-
whichSyncMock.mockImplementationOnce(() =>
52-
require.resolve('cross-env/src/bin/cross-env').replace(process.cwd(), '.'),
53-
)
54-
expect(require('../utils').resolveBin('cross-env')).toBe('cross-env')
55-
expect(whichSyncMock).toHaveBeenCalledTimes(1)
56-
expect(whichSyncMock).toHaveBeenCalledWith('cross-env')
49+
afterAll(() => {
50+
jest.mock('read-pkg-up', () => ({
51+
sync: jest.fn(() => ({packageJson: {}, path: '/blah/package.json'})),
52+
}))
53+
})
54+
55+
test(`resolveBin resolves to the full path when it's not in $PATH`, () => {
56+
expect(require('../utils').resolveBin('cross-env')).toBe(
57+
require
58+
.resolve('cross-env/src/bin/cross-env')
59+
.replace(process.cwd(), '.'),
60+
)
61+
})
62+
63+
test(`resolveBin resolves to the binary if it's in $PATH`, () => {
64+
whichSyncMock.mockImplementationOnce(() =>
65+
require
66+
.resolve('cross-env/src/bin/cross-env')
67+
.replace(process.cwd(), '.'),
68+
)
69+
expect(require('../utils').resolveBin('cross-env')).toBe('cross-env')
70+
expect(whichSyncMock).toHaveBeenCalledTimes(1)
71+
expect(whichSyncMock).toHaveBeenCalledWith('cross-env')
72+
})
5773
})
5874

5975
describe('for windows', () => {

src/utils.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ function resolveBin(modName, {executable = modName, cwd = process.cwd()} = {}) {
3030
// ignore _error
3131
}
3232
try {
33-
const { packageJson: binPackage, path: modPkgDir } = readPkgUp.sync({
33+
const {packageJson: binPackage, path: binPackagePath} = readPkgUp.sync({
3434
cwd: path.dirname(require.resolve(modName)),
3535
})
36-
37-
const {bin} = binPackage;
38-
36+
37+
const modPkgDir = path.dirname(binPackagePath)
38+
const {bin} = binPackage
39+
3940
const binPath = typeof bin === 'string' ? bin : bin[executable]
4041
const fullPathToBin = path.join(modPkgDir, binPath)
4142
if (fullPathToBin === pathFromWhich) {

0 commit comments

Comments
 (0)