Skip to content

Commit

Permalink
fix: Only pass gitDir for git specific executables (#162)
Browse files Browse the repository at this point in the history
* 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
DeFuex authored and okonet committed May 17, 2017
1 parent 2c4538f commit c7283b7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/runScript.js
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
@@ -1,3 +1 @@
jest.genMockFromModule('execa')

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

0 comments on commit c7283b7

Please sign in to comment.