Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Only pass gitDir for git specific executables (#162)
* change npm to git since gitDir is only used for git executables * fix(gitDir): `gitDir` should only be applied when using `git` binary Inverses logic of checking for "not equal to `npm`", instead check that binary to run is equal to `git` (and accounting for absolute paths). This means that commands besides `npm` will also run in CWD instead of `gitDir`. Closes #158
- Loading branch information
Showing
4 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
jest.genMockFromModule('execa') | ||
|
||
module.exports = jest.fn() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// This is in a separate test file because I was unable to get `jest.mock` working in a test block | ||
// `jest.mock` gets hoisted, but even with `jest.doMock` it wasn't working | ||
/* eslint no-underscore-dangle: 0 */ | ||
|
||
import expect from 'expect' | ||
import isPromise from 'is-promise' | ||
import mockFn from 'execa' | ||
import runScript from '../src/runScript' | ||
|
||
jest.mock('execa') | ||
|
||
// Mock findBin to return an absolute path | ||
jest.mock('../src/findBin', () => (commands, paths) => { | ||
const [ | ||
bin, | ||
...otherArgs | ||
] = commands.split(' ') | ||
|
||
return ({ | ||
bin: `/usr/local/bin/${ bin }`, | ||
args: otherArgs.concat(['--']).concat(paths) | ||
}) | ||
}, { virtual: true }) | ||
|
||
expect.extend({ | ||
toBeAPromise() { | ||
expect.assert( | ||
isPromise(this.actual), | ||
'expected %s to be a Promise', | ||
this.actual | ||
) | ||
return this | ||
} | ||
}) | ||
|
||
const packageJSON = { | ||
scripts: { | ||
test: 'noop', | ||
test2: 'noop' | ||
}, | ||
'lint-staged': {} | ||
} | ||
|
||
describe.only('runScript with absolute paths', () => { | ||
beforeEach(() => { | ||
mockFn.mockReset() | ||
mockFn.mockImplementation(() => new Promise(() => {})) | ||
}) | ||
|
||
it('can pass `gitDir` as `cwd` to `execa()` when git is called via absolute path', () => { | ||
const res = runScript( | ||
['git add'], | ||
'test.js', | ||
packageJSON, | ||
{ gitDir: '../' } | ||
) | ||
|
||
expect(res[0].task()).toBeAPromise() | ||
expect(mockFn.mock.calls.length).toEqual(1) | ||
expect(mockFn.mock.calls[0][0]).toMatch('/usr/local/bin/git') | ||
expect(mockFn.mock.calls[0][1]).toEqual(['add', '--', 'test.js']) | ||
expect(mockFn.mock.calls[0][2]).toEqual({ cwd: '../' }) | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters