Skip to content

Commit

Permalink
refactor: no need to create absolute paths since cwd is already absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj authored and okonet committed Aug 17, 2019
1 parent eb3fa83 commit 94dbeda
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
8 changes: 1 addition & 7 deletions src/execGit.js
Expand Up @@ -2,19 +2,13 @@

const debug = require('debug')('lint-staged:git')
const execa = require('execa')
const path = require('path')

function getAbsolutePath(dir) {
return path.isAbsolute(dir) ? dir : path.resolve(dir)
}

module.exports = async function execGit(cmd, options = {}) {
const cwd = options.cwd || process.cwd()
debug('Running git command', cmd)
try {
const { stdout } = await execa('git', [].concat(cmd), {
...options,
cwd: getAbsolutePath(cwd)
cwd: options.cwd || process.cwd()
})
return stdout
} catch (err) {
Expand Down
21 changes: 5 additions & 16 deletions test/gitWorkflow.spec.js
Expand Up @@ -10,26 +10,15 @@ tmp.setGracefulCleanup()
describe('gitWorkflow', () => {
describe('execGit', () => {
it('should execute git in process.cwd if working copy is not specified', async () => {
const cwd = process.cwd()
await execGit(['init', 'param'])
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], {
cwd: path.resolve(process.cwd())
})
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], { cwd })
})

it('should execute git in a given working copy', async () => {
await execGit(['init', 'param'], { cwd: 'test/__fixtures__' })
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], {
cwd: path.resolve(process.cwd(), 'test', '__fixtures__')
})
})

it('should work with relative paths', async () => {
await execGit(['init', 'param'], {
cwd: 'test/__fixtures__'
})
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], {
cwd: path.resolve(process.cwd(), 'test', '__fixtures__')
})
const cwd = path.join(process.cwd(), 'test', '__fixtures__')
await execGit(['init', 'param'], { cwd })
expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], { cwd })
})
})

Expand Down

0 comments on commit 94dbeda

Please sign in to comment.