-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.test.ts
297 lines (239 loc) · 9.76 KB
/
git.test.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import * as exec from '@actions/exec'
import * as uuid from 'uuid'
import * as io from '@actions/io'
import * as path from 'path'
import * as fs from 'fs'
import {Git} from '../src/git'
import {Settings} from '../src/settings'
import {TEST_DIR} from './common.test.utils'
const GIT_TEST_DIR = path.join(TEST_DIR, 'git')
async function runGit(args: string[], cwd: string) {
const gitPath = await io.which('git', true)
await exec.exec(`"${gitPath}"`, args, {cwd})
}
async function initTestRepo(): Promise<string> {
const cwd = path.join(GIT_TEST_DIR, uuid.v4())
await io.mkdirP(cwd)
await runGit(['init', '.'], cwd)
return cwd
}
async function createTag(tagName: string, cwd: string) {
await runGit(['tag', tagName], cwd)
}
async function createAndCommitFile(name: string, message: string, cwd: string) {
const file = path.join(cwd, name)
fs.writeFile(file, 'data', () => {})
await runGit(['add', '.'], cwd)
await runGit(['commit', '-m', `${message}`], cwd)
}
describe('Git commands', () => {
afterAll(() => {
io.rmRF(GIT_TEST_DIR)
})
it('Verifies tag is returned if checked out on same commit', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('myTag', cwd)
const tag = await g.currentTag()
expect(tag).toEqual('myTag')
})
it('Verifies empty string is returned if no tag exists', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
const tag = await g.currentTag()
expect(tag).toEqual('')
})
it('Verifies empty string is returned if current tag does not match regex', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.tagRegex = RegExp('[0-9]+.[0-9]+.[0-9]+.*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('myTag', cwd)
const tag = await g.currentTag()
expect(tag).toEqual('')
})
it('Verifies tag is returned if regex is matched', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.tagRegex = RegExp('^[0-9]+.[0-9]+.[0-9]+.*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('0.0.1', cwd)
const tag = await g.currentTag()
expect(tag).toEqual('0.0.1')
})
it('Verifies previous tag is returned', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.tagRegex = RegExp('.*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('0.0.1', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createTag('someTag', cwd)
const tag = await g.previousTag('someTag')
expect(tag).toEqual('0.0.1')
})
it('Verifies empty string is returned if no previous tag matches regex', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.tagRegex = RegExp('[0-9]+.[0-9]+.[0-9]+.*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('someTag', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createTag('0.0.1', cwd)
const tag = await g.previousTag('0.0.1')
expect(tag).toEqual('')
})
it('Verifies previous matching tag is found with invalid tag inbetween', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.tagRegex = RegExp('[0-9]+.[0-9]+.[0-9]+.*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('0.0.1', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createTag('someTag', cwd)
await createAndCommitFile('third', 'Third commit', cwd)
await createTag('0.0.2', cwd)
const tag = await g.previousTag('0.0.2')
expect(tag).toEqual('0.0.1')
})
it('Verifies all commits are returned when no refs are specified', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createAndCommitFile('third', 'Third commit', cwd)
const log = await g.log('', '')
expect(log).toHaveLength(3)
expect(log[0].hash).toHaveLength(7)
expect(log[0].message).toBe('Third commit')
expect(log[1].hash).toHaveLength(7)
expect(log[1].message).toBe('Second commit')
expect(log[2].hash).toHaveLength(7)
expect(log[2].message).toBe('First commit')
})
it('Verifies log if from ref is specified', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createTag('myTag', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createAndCommitFile('third', 'Third commit', cwd)
const log = await g.log('myTag', '')
expect(log).toHaveLength(2)
expect(log[0].hash).toHaveLength(7)
expect(log[0].message).toBe('Third commit')
expect(log[1].hash).toHaveLength(7)
expect(log[1].message).toBe('Second commit')
})
it('Verifies log if to ref is specified', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createTag('myTag', cwd)
await createAndCommitFile('third', 'Third commit', cwd)
const log = await g.log('', 'myTag')
expect(log).toHaveLength(2)
expect(log[0].hash).toHaveLength(7)
expect(log[0].message).toBe('Second commit')
expect(log[1].hash).toHaveLength(7)
expect(log[1].message).toBe('First commit')
})
it('Verifies log with from and to refs specified', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createAndCommitFile('second', 'Second commit', cwd)
await createTag('fromTag', cwd)
await createAndCommitFile('third', 'Third commit', cwd)
await createAndCommitFile('fourth', 'Fourth commit', cwd)
await createAndCommitFile('fifth', 'Fifth commit', cwd)
await createTag('toTag', cwd)
await createAndCommitFile('sixth', 'Sixth commit', cwd)
const log = await g.log('fromTag', 'toTag')
expect(log).toHaveLength(3)
expect(log[0].hash).toHaveLength(7)
expect(log[0].message).toBe('Fifth commit')
expect(log[1].hash).toHaveLength(7)
expect(log[1].message).toBe('Fourth commit')
expect(log[2].hash).toHaveLength(7)
expect(log[2].message).toBe('Third commit')
})
it('Verifies log does not contain commits matching regex', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.filterRegex = RegExp('^\\[skip\\].*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createAndCommitFile('second', '[skip] Second commit', cwd)
await createAndCommitFile('third', 'Third commit', cwd)
await createAndCommitFile('fourth', 'Fourth commit', cwd)
await createAndCommitFile('fifth', '[skip] Fifth commit', cwd)
await createAndCommitFile('sixth', '[skip] Sixth commit', cwd)
const log = await g.log('', '')
expect(log).toHaveLength(3)
expect(log[0].hash).toHaveLength(7)
expect(log[0].message).toBe('Fourth commit')
expect(log[1].hash).toHaveLength(7)
expect(log[1].message).toBe('Third commit')
expect(log[2].hash).toHaveLength(7)
expect(log[2].message).toBe('First commit')
})
it('Verifies log does not contain commit matching case insensitive regex', async () => {
const cwd = await initTestRepo()
process.chdir(cwd)
const settings = {} as Settings
settings.gitPath = await io.which('git', true)
settings.filterRegex = RegExp('^\\[SkIp\\].*', 'i')
const g = new Git(settings)
await createAndCommitFile('first', 'First commit', cwd)
await createAndCommitFile('second', '[Skip] Second commit', cwd)
await createAndCommitFile('third', '[skip] Third commit', cwd)
await createAndCommitFile('fourth', 'Fourth commit', cwd)
await createAndCommitFile('fifth', '[sKiP] Fifth commit', cwd)
await createAndCommitFile('sixth', '[SKIP] Sixth commit', cwd)
const log = await g.log('', '')
expect(log).toHaveLength(2)
expect(log[0].hash).toHaveLength(7)
expect(log[0].message).toBe('Fourth commit')
expect(log[1].hash).toHaveLength(7)
expect(log[1].message).toBe('First commit')
})
})