This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathremoteNameAndBranch.test.ts
More file actions
85 lines (71 loc) · 3.4 KB
/
remoteNameAndBranch.test.ts
File metadata and controls
85 lines (71 loc) · 3.4 KB
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
75
76
77
78
79
80
81
82
83
84
85
import assert from 'assert'
import { GitHelpers } from './helpers'
import { gitRemoteNameAndBranch } from './remoteNameAndBranch'
describe('git', () => {
function createMockGitHelpers(
remotes: string[],
branch: string,
upstreamAndBranch: string
): Pick<GitHelpers, 'branch' | 'remotes' | 'upstreamAndBranch'> {
return {
remotes: () => Promise.resolve(remotes),
branch: () => Promise.resolve(branch),
upstreamAndBranch: () => {
if (!upstreamAndBranch) {
throw new Error(`fatal: no upstream configured for branch ${branch}`)
}
return Promise.resolve(upstreamAndBranch)
},
}
}
describe('gitRemoteNameAndBranch()', () => {
it('handles simple upstream and branch names', async () => {
const { remoteName, branch } = await gitRemoteNameAndBranch(
'',
createMockGitHelpers(['origin'], 'feature', 'origin/feature')
)
assert.strictEqual(remoteName, 'origin', 'incorrect remote name')
assert.strictEqual(branch, 'feature', 'incorrect branch name')
})
it('handles branch names with slashes', async () => {
const { remoteName, branch } = await gitRemoteNameAndBranch(
'',
createMockGitHelpers(['origin'], 'author/feature', 'origin/author/feature')
)
assert.strictEqual(remoteName, 'origin', 'incorrect remote name')
assert.strictEqual(branch, 'author/feature', 'incorrect branch name')
})
it('handles remote and branch names with slashes', async () => {
const { remoteName, branch } = await gitRemoteNameAndBranch(
'',
createMockGitHelpers(['remote/one', 'remote/two'], 'feature', 'remote/two/feature')
)
assert.strictEqual(remoteName, 'remote/two', 'incorrect remote name')
assert.strictEqual(branch, 'feature', 'incorrect branch name')
})
it('falls back to first remote when no upstream is configured', async () => {
const { remoteName, branch } = await gitRemoteNameAndBranch(
'',
createMockGitHelpers(['remote-one', 'remote-two', 'remote-three'], 'feature', '')
)
assert.strictEqual(remoteName, 'remote-one', 'incorrect remote name')
assert.strictEqual(branch, 'feature', 'incorrect branch name')
})
it('falls back to first remote and upstream branch when branch does not match upstream', async () => {
const { remoteName, branch } = await gitRemoteNameAndBranch(
'',
createMockGitHelpers(['origin'], 'feature', 'origin/master')
)
assert.strictEqual(remoteName, 'origin', 'incorrect remote name')
assert.strictEqual(branch, 'master', 'incorrect branch name')
})
it('falls back to first remote and branch when remote does not match upstream', async () => {
const { remoteName, branch } = await gitRemoteNameAndBranch(
'',
createMockGitHelpers(['origin'], 'feature', 'upstream/master')
)
assert.strictEqual(remoteName, 'origin', 'incorrect remote name')
assert.strictEqual(branch, 'feature', 'incorrect branch name')
})
})
})