Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gitDir Bug for git specific executables #162

Merged
merged 9 commits into from
May 17, 2017
5 changes: 3 additions & 2 deletions src/runScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ module.exports = function runScript(commands, pathsToLint, packageJson, options)
task: () => {
try {
const res = findBin(linter, pathsToLint, packageJson, options)
// Only use gitDir as CWD if we are using the git binary
// e.g `npm` should run tasks in the actual CWD
const execaOptions =
res.bin !== 'npm' && options && options.gitDir ? { cwd: options.gitDir } : {}
res.bin.endsWith('git') && options && options.gitDir ? { cwd: options.gitDir } : {}
return new Promise((resolve, reject) => {
execa(res.bin, res.args, execaOptions)
.then(() => {
Expand All @@ -29,4 +31,3 @@ ${ err.stdout }`))
}
}))
}

2 changes: 0 additions & 2 deletions test/__mocks__/execa.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
jest.genMockFromModule('execa')

module.exports = jest.fn()
65 changes: 65 additions & 0 deletions test/runScript-mock-findBin.spec.js
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: '../' })
})
})

16 changes: 15 additions & 1 deletion test/runScript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('runScript', () => {

beforeEach(() => {
mockFn.mockReset()
mockFn.mockImplementation(() => new Promise(() => {}))
})

it('should return an array', () => {
Expand Down Expand Up @@ -105,6 +106,20 @@ describe('runScript', () => {
expect(mockFn.mock.calls[1][2]).toEqual({ cwd: '../' })
})

it('should not pass `gitDir` as `cwd` to `execa()` if a non-git binary is called', () => {
const res = runScript(
['jest'],
'test.js',
packageJSON,
{ gitDir: '../' }
)
expect(res[0].task()).toBeAPromise()
expect(mockFn.mock.calls.length).toEqual(1)
expect(mockFn.mock.calls[0]).toEqual(
['jest', ['--', 'test.js'], {}]
)
})

it('should use --silent in non-verbose mode', () => {
const res = runScript(
'test',
Expand Down Expand Up @@ -133,4 +148,3 @@ describe('runScript', () => {
)
})
})