/
resolveGitRepo.spec.js
74 lines (64 loc) · 2.84 KB
/
resolveGitRepo.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import normalize from 'normalize-path'
import path from 'path'
import resolveGitRepo, { determineGitDir } from '../lib/resolveGitRepo'
/**
* resolveGitRepo runs execa, so the mock needs to be disabled for these tests
*/
jest.unmock('execa')
describe('resolveGitRepo', () => {
it('should resolve to current working dir when .git is in the same dir', async () => {
const cwd = normalize(process.cwd())
const { gitDir } = await resolveGitRepo()
expect(gitDir).toEqual(cwd)
})
it('should resolve to the parent dir when .git is in the parent dir', async () => {
const expected = normalize(path.dirname(__dirname))
const processCwdBkp = process.cwd
process.cwd = () => __dirname
const { gitDir } = await resolveGitRepo()
expect(gitDir).toEqual(expected)
process.cwd = processCwdBkp
})
it('should resolve to the parent dir when .git is in the parent dir even when the GIT_DIR environment variable is set', async () => {
const expected = normalize(path.dirname(__dirname))
const processCwdBkp = process.cwd
process.cwd = () => __dirname
process.env.GIT_DIR = 'wrong/path/.git' // refer to https://github.com/DonJayamanne/gitHistoryVSCode/issues/233#issuecomment-375769718
const { gitDir } = await resolveGitRepo()
expect(gitDir).toEqual(expected)
process.cwd = processCwdBkp
})
it('should resolve to the parent dir when .git is in the parent dir even when the GIT_WORK_TREE environment variable is set', async () => {
const expected = normalize(path.dirname(__dirname))
const processCwdBkp = process.cwd
process.cwd = () => __dirname
process.env.GIT_WORK_TREE = './wrong/path/'
const { gitDir } = await resolveGitRepo()
expect(gitDir).toEqual(expected)
process.cwd = processCwdBkp
})
it('should return null when not in a git directory', async () => {
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()))
})
})
})