-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_test.go
403 lines (343 loc) · 15.6 KB
/
sync_test.go
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package dx
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSync(t *testing.T) {
_, clientDir := newGitTest(t)
t.Log("develop feature branch")
trun(t, clientDir, "git", "checkout", "-b", "feature")
twrite(t, clientDir+"/content", "hello world")
trun(t, clientDir, "git", "add", "content")
err := trunMainCommand(t, "commit", "-m", "commit message")
assert.NoError(t, err)
t.Log("testing sync dev")
err = trunMainCommand(t, "--debug", "sync", "dev")
tgitLog(t, clientDir)
assert.NoError(t, err)
assert.Equal(t, "feature", tgetHeadBranch(t, clientDir))
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits[0].subCommit, 1, "sub commits count is invalid")
assert.Len(t, actualCommits[0].changeIds, 1, "change ids count is invalid")
content := tread(t, clientDir+"/content")
assert.Equal(t, "hello world", content, "merged content is invalid")
assertNormalTeardown(t, clientDir)
}
func TestSync_FeatureBranchOutdated(t *testing.T) {
_, clientDir := newGitTest(t)
t.Log("create feature branch")
trun(t, clientDir, "git", "checkout", "-b", "feature")
t.Log("main has new feature during develop feature")
trun(t, clientDir, "git", "checkout", "main")
twrite(t, "feature1", "new feature")
trun(t, clientDir, "git", "add", "feature1")
trun(t, clientDir, "git", "commit", "-m", "feat: feature 1")
t.Log("dev branch is reset")
trun(t, clientDir, "git", "checkout", "dev")
trun(t, clientDir, "git", "reset", "--hard", "main")
t.Log("develop feature branch")
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "hello world")
trun(t, clientDir, "git", "add", "content")
err := trunMainCommand(t, "commit", "-m", "commit message")
require.NoError(t, err)
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "update")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: update")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "main")
t.Log("testing sync dev")
err = trunMainCommand(t, "--debug", "sync", "dev")
assert.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
assert.Equal(t, "feature", tgetHeadBranch(t, clientDir))
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits[0].changeIds, 2, "change ids count is invalid")
assert.Len(t, actualCommits[0].subCommit, 2, "sub commits count is invalid")
assert.Equal(t, actualCommits[0].subCommit[0].short, "commit message")
assert.Equal(t, actualCommits[0].subCommit[1].short, "fix: update")
content := tread(t, clientDir+"/content")
assert.Equal(t, "update", content, "merged content is invalid")
assertNormalTeardown(t, clientDir)
}
func TestSync_MultipleSync(t *testing.T) {
serverDir, clientDir := newGitTest(t)
t.Log("create feature branch")
trun(t, clientDir, "git", "checkout", "-b", "feature")
t.Log("main has new feature during develop feature")
trun(t, serverDir, "git", "checkout", "main")
twrite(t, serverDir+"/feature1", "new feature")
trun(t, serverDir, "git", "add", "feature1")
trun(t, serverDir, "git", "commit", "-m", "feat: feature 1")
t.Log("dev branch is reset")
trun(t, serverDir, "git", "checkout", "dev")
trun(t, serverDir, "git", "reset", "--hard", "main")
trun(t, serverDir, "git", "checkout", "main")
t.Log("develop feature branch")
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "hello world")
trun(t, clientDir, "git", "add", "content")
err := trunMainCommand(t, "commit", "-m", "commit message")
require.NoError(t, err)
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "update\n")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: update")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "main")
t.Log("sync dev - 1")
err = trunMainCommand(t, "sync", "dev")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
t.Log("push dev to origin")
trun(t, clientDir, "git", "push", "origin", "dev")
tgitLog(t, clientDir, "feature", "dev", "main")
t.Log("develop feature branch more")
tappend(t, clientDir+"/content", "fix bug\n")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: fix bug")
require.NoError(t, err)
t.Log("sync dev - 2")
err = trunMainCommand(t, "--debug", "sync", "dev")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
assert.Equal(t, "feature", tgetHeadBranch(t, clientDir))
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits[0].subCommit, 1, "new sub commits count is invalid")
assert.Len(t, actualCommits[0].changeIds, 1, "new change ids count is invalid")
assert.Len(t, actualCommits[1].subCommit, 2, "old sub commits count is invalid")
assert.Len(t, actualCommits[1].changeIds, 2, "old change ids count is invalid")
content := tread(t, clientDir+"/content")
assert.Equal(t, "update\nfix bug\n", content, "merged content is invalid")
assertNormalTeardown(t, clientDir)
}
func TestSync_SyncWithAnotherCommitWithoutDX(t *testing.T) {
serverDir, clientDir := newGitTest(t)
t.Log("client - create feature branch")
trun(t, clientDir, "git", "checkout", "-b", "feature")
t.Log("server - main has new feature during develop feature")
trun(t, serverDir, "git", "checkout", "main")
twrite(t, serverDir+"/feature1", "new feature")
trun(t, serverDir, "git", "add", "feature1")
trun(t, serverDir, "git", "commit", "-m", "feat: feature 1")
t.Log("server - dev branch is reset")
trun(t, serverDir, "git", "checkout", "dev")
trun(t, serverDir, "git", "reset", "--hard", "main")
trun(t, serverDir, "git", "checkout", "main")
t.Log("client - develop feature branch")
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "hello world")
trun(t, clientDir, "git", "add", "content")
err := trunMainCommand(t, "commit", "-m", "commit message")
require.NoError(t, err)
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "update\n")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: update")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "main")
t.Log("client - sync dev - 1")
err = trunMainCommand(t, "sync", "dev")
require.NoError(t, err)
t.Log("client - push dev to origin")
trun(t, clientDir, "git", "push", "origin", "dev")
t.Log("server - another dev guys push dev without using dx")
trun(t, serverDir, "git", "checkout", "dev")
twrite(t, serverDir+"/another_feature", "another feature without using dx")
trun(t, serverDir, "git", "add", "another_feature")
trun(t, serverDir, "git", "commit", "-m", "feat: another feature without using dx")
trun(t, serverDir, "git", "checkout", "main")
t.Log("client - develop feature branch more")
trun(t, clientDir, "git", "checkout", "feature")
tappend(t, clientDir+"/content", "fix bug\n")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: fix bug")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
t.Log("client - sync dev - 2")
err = trunMainCommand(t, "--debug", "sync", "dev")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
assert.Equal(t, "feature", tgetHeadBranch(t, clientDir))
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits[0].subCommit, 1, "new sub commits count is invalid")
assert.Len(t, actualCommits[0].changeIds, 1, "new change ids count is invalid")
assert.Len(t, actualCommits[1].subCommit, 0, "this is commit without using dx")
assert.Len(t, actualCommits[1].changeIds, 0, "this is commit without using dx")
assert.Len(t, actualCommits[2].subCommit, 2, "old sub commits count is invalid")
assert.Len(t, actualCommits[2].changeIds, 2, "old change ids count is invalid")
content := tread(t, clientDir+"/content")
assert.Equal(t, "update\nfix bug\n", content, "merged content is invalid")
assertNormalTeardown(t, clientDir)
}
func TestSync_CompactNonPushCommits(t *testing.T) {
serverDir, clientDir := newGitTest(t)
t.Log("create feature branch")
trun(t, clientDir, "git", "checkout", "-b", "feature")
t.Log("main has new feature during develop feature")
trun(t, serverDir, "git", "checkout", "main")
twrite(t, serverDir+"/feature1", "new feature")
trun(t, serverDir, "git", "add", "feature1")
trun(t, serverDir, "git", "commit", "-m", "feat: feature 1")
t.Log("dev branch is reset")
trun(t, serverDir, "git", "checkout", "dev")
trun(t, serverDir, "git", "reset", "--hard", "main")
trun(t, serverDir, "git", "checkout", "main")
t.Log("develop feature branch")
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "hello world")
trun(t, clientDir, "git", "add", "content")
err := trunMainCommand(t, "commit", "-m", "commit message")
require.NoError(t, err)
trun(t, clientDir, "git", "checkout", "feature")
twrite(t, clientDir+"/content", "update\n")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: update")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "main")
t.Log("sync dev - 1 without push to origin")
err = trunMainCommand(t, "sync", "dev")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
t.Log("develop feature branch more")
tappend(t, clientDir+"/content", "fix bug\n")
trun(t, clientDir, "git", "add", "content")
err = trunMainCommand(t, "commit", "-m", "fix: fix bug")
require.NoError(t, err)
t.Log("sync dev - 2 ")
err = trunMainCommand(t, "--debug", "sync", "dev")
require.NoError(t, err)
tgitLog(t, clientDir, "feature", "dev", "main")
assert.Equal(t, "feature", tgetHeadBranch(t, clientDir))
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Equal(t, actualCommits[0].short, "sync from feature")
assert.Len(t, actualCommits[0].subCommit, 3, "new sub commits count is invalid")
assert.Len(t, actualCommits[0].changeIds, 3, "new change ids count is invalid")
content := tread(t, clientDir+"/content")
assert.Equal(t, "update\nfix bug\n", content, "merged content is invalid")
assertNormalTeardown(t, clientDir)
}
func TestSync_PullSyncBranchBeforeSync(t *testing.T) {
serverDir, clientDir := newGitTest(t)
t.Log("server: make commit is git server")
trun(t, serverDir, "git", "checkout", "dev")
twrite(t, serverDir+"/srv_feature1", "srv_feature1")
trun(t, serverDir, "git", "add", "srv_feature1")
trun(t, serverDir, "git", "commit", "-m", "feat: server feature 1")
t.Log("client: develop feature branch")
trun(t, clientDir, "git", "checkout", "-b", "client_feature1")
twrite(t, clientDir+"/client_feature1", "client_feature1")
trun(t, clientDir, "git", "add", "client_feature1")
err := trunMainCommand(t, "commit", "-m", "feat: client feature 1")
require.NoError(t, err)
trun(t, clientDir, "git", "fetch")
tgitLog(t, clientDir, "client_feature1", "origin/dev", "dev", "main")
err = trunMainCommand(t, "--debug", "sync", "dev")
require.NoError(t, err)
tgitLog(t, clientDir, "client_feature1", "origin/dev", "dev", "main")
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits, 3)
assert.Equal(t, actualCommits[0].short, "sync from client_feature1")
assert.Equal(t, actualCommits[1].short, "feat: server feature 1")
assertNormalTeardown(t, clientDir)
}
func TestSync_CodeConflict(t *testing.T) {
serverDir, clientDir := newGitTest(t)
t.Log("server: make commit is git server")
trun(t, serverDir, "git", "checkout", "dev")
twrite(t, serverDir+"/main", "srv_feature1\n")
trun(t, serverDir, "git", "add", "main")
trun(t, serverDir, "git", "commit", "-m", "feat: server feature 1")
t.Log("client: develop feature branch")
trun(t, clientDir, "git", "checkout", "-b", "client_feature1")
twrite(t, clientDir+"/main", "client_feature1\n")
trun(t, clientDir, "git", "add", "main")
err := trunMainCommand(t, "commit", "-m", "feat: client feature 1")
require.NoError(t, err)
t.Log("client: try to sync")
err = trunMainCommand(t, "--debug", "sync", "dev")
require.ErrorContains(t, err, "code conflict")
tgitLog(t, clientDir)
assertBranchExist(t, clientDir, "tmp-sync*")
out := tread(t, clientDir+"/main")
t.Log("conflict content:\n", out)
t.Log("client: resolve conflict")
out = removeConflictAnnotate(t, out)
t.Log("resolved content:\n", out)
twrite(t, clientDir+"/main", out)
trun(t, clientDir, "git", "add", "main")
err = trunMainCommand(t, "--debug", "sync", "--continue")
tgitLog(t, clientDir)
require.NoError(t, err)
content := tread(t, clientDir+"/main")
assert.Equal(t, "client_feature1\n", content, "original content is invalid")
trun(t, clientDir, "git", "checkout", "dev")
content = tread(t, clientDir+"/main")
assert.Equal(t, "srv_feature1\nclient_feature1\n", content, "merged content is invalid")
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits[0].changeIds, 1)
assert.Len(t, actualCommits[0].subCommit, 1)
assert.Equal(t, actualCommits[0].subCommit[0].short, "feat: client feature 1")
assertNormalTeardown(t, clientDir)
}
func TestSync_CodeMultipleConflict(t *testing.T) {
serverDir, clientDir := newGitTest(t)
t.Log("server: make commit is git server")
trun(t, serverDir, "git", "checkout", "dev")
twrite(t, serverDir+"/main", "srv_feature1\n")
twrite(t, serverDir+"/lib", "lib_feature1\n")
trun(t, serverDir, "git", "add", "main", "lib")
trun(t, serverDir, "git", "commit", "-m", "feat: server feature 1")
t.Log("client: develop feature branch - 1")
trun(t, clientDir, "git", "checkout", "-b", "client_feature1")
twrite(t, clientDir+"/main", "client_feature1\n")
trun(t, clientDir, "git", "add", "main")
err := trunMainCommand(t, "commit", "-m", "feat: client feature 1")
require.NoError(t, err)
t.Log("client: develop feature branch - 2")
twrite(t, clientDir+"/lib", "lib_client_feature1\n")
trun(t, clientDir, "git", "add", "lib")
err = trunMainCommand(t, "commit", "-m", "feat(lib): update lib")
require.NoError(t, err)
t.Log("client: try to sync")
err = trunMainCommand(t, "sync", "dev")
require.ErrorContains(t, err, "code conflict")
assertBranchExist(t, clientDir, "tmp-sync*")
out := tread(t, clientDir+"/main")
t.Log("conflict content:\n", out)
t.Log("client: resolve conflict - 1")
out = removeConflictAnnotate(t, out)
t.Log("resolved content:\n", out)
twrite(t, clientDir+"/main", out)
trun(t, clientDir, "git", "add", "main")
err = trunMainCommand(t, "--debug", "sync", "--continue")
require.ErrorContains(t, err, "code conflict")
tgitLog(t, clientDir)
out = tread(t, clientDir+"/lib")
t.Log("conflict content:\n", out)
t.Log("client: resolve conflict - 2")
out = removeConflictAnnotate(t, out)
t.Log("resolved content:\n", out)
twrite(t, clientDir+"/lib", out)
trun(t, clientDir, "git", "add", "lib")
err = trunMainCommand(t, "--debug", "sync", "--continue")
tgitLog(t, clientDir)
require.NoError(t, err)
content := tread(t, clientDir+"/main")
assert.Equal(t, "client_feature1\n", content, "original main content is invalid")
content = tread(t, clientDir+"/lib")
assert.Equal(t, "lib_client_feature1\n", content, "original lib content is invalid")
trun(t, clientDir, "git", "checkout", "dev")
content = tread(t, clientDir+"/main")
assert.Equal(t, "srv_feature1\nclient_feature1\n", content, "merged main content is invalid")
content = tread(t, clientDir+"/lib")
assert.Equal(t, "lib_feature1\nlib_client_feature1\n", content, "merged lib content is invalid")
actualCommits := tgetCommits(t, clientDir, "dev")
assert.Len(t, actualCommits[0].changeIds, 2)
assert.Len(t, actualCommits[0].subCommit, 2)
assert.Equal(t, actualCommits[0].subCommit[0].short, "feat: client feature 1")
assert.Equal(t, actualCommits[0].subCommit[1].short, "feat(lib): update lib")
assertNormalTeardown(t, clientDir)
}