Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kalmár committed Oct 9, 2021
1 parent 7b7e15e commit 23def5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/resolveGitRepo.js
Expand Up @@ -27,15 +27,15 @@ const resolveGitConfigDir = async (gitDir) => {

const determineGitDir = (cwd, relativeDir) => {
// if relative dir and cwd have different endings normalize it
if (relativeDir.endsWith(path.sep) && !cwd.endsWith(path.sep)) {
// this happens under windows, where normalize is unable to normalize git's output
if (relativeDir && relativeDir.endsWith(path.sep)) {
relativeDir = relativeDir.slice(0, -1)
}
if (relativeDir) {
// the current working dir is inside the git top-level directory
return normalize(cwd.substring(0, cwd.lastIndexOf(relativeDir)))
} else {
// the current working dir is the top-level git directory

return normalize(cwd)
}
}
Expand Down Expand Up @@ -70,3 +70,6 @@ const resolveGitRepo = async (cwd = process.cwd()) => {
}

module.exports = resolveGitRepo

// exported for test
module.exports.determineGitDir = determineGitDir
25 changes: 24 additions & 1 deletion test/resolveGitRepo.spec.js
@@ -1,7 +1,7 @@
import normalize from 'normalize-path'
import path from 'path'

import resolveGitRepo from '../lib/resolveGitRepo'
import resolveGitRepo, { determineGitDir } from '../lib/resolveGitRepo'

/**
* resolveGitRepo runs execa, so the mock needs to be disabled for these tests
Expand Down Expand Up @@ -48,4 +48,27 @@ describe('resolveGitRepo', () => {
const { gitDir } = await resolveGitRepo({ cwd: '/' }) // assume root is not a git directory
expect(gitDir).toEqual(null)
})

describe('determineGitDir', () => {
it('should resolve to current working dir when relative dir is empty', () => {
const cwd = process.cwd()
const relativeDir = undefined
const rootDir = determineGitDir(cwd, relativeDir)
expect(rootDir).toEqual(normalize(cwd))
})

it('should resolve to parent dir when relative dir is child', () => {
const relativeDir = 'bar'
const cwd = process.cwd() + path.sep + 'bar'
const rootDir = determineGitDir(cwd, relativeDir)
expect(rootDir).toEqual(normalize(process.cwd()))
})

it('should resolve to parent dir when relative dir is child and child has trailing dir separator', () => {
const relativeDir = 'bar' + path.sep
const cwd = process.cwd() + path.sep + 'bar'
const rootDir = determineGitDir(cwd, relativeDir)
expect(rootDir).toEqual(normalize(process.cwd()))
})
})
})

0 comments on commit 23def5d

Please sign in to comment.