|
1 |
| -import { merge, resolveRef, log, BlobMergeCallback, checkout } from 'git-essentials' |
2 |
| -import { MissingNameError, MergeNotSupportedError } from 'git-essentials/errors' |
| 1 | +import * as path from './helpers/path' |
| 2 | + |
| 3 | +import { |
| 4 | + BlobMergeCallback, |
| 5 | + add, |
| 6 | + branch, |
| 7 | + checkout, |
| 8 | + commit, |
| 9 | + init, |
| 10 | + log, |
| 11 | + merge, |
| 12 | + resolveRef |
| 13 | +} from 'git-essentials' |
| 14 | +import { FsFixtureData, makeFsFixture } from './helpers/makeFsFixture' |
| 15 | +import { MergeConflictError, MergeNotSupportedError, MissingNameError } from 'git-essentials/errors' |
3 | 16 |
|
4 |
| -import { makeFsFixture, FsFixtureData } from './helpers/makeFsFixture' |
5 | 17 | import { expectToFailWithTypeAsync } from './helpers/assertionHelper'
|
6 |
| - |
7 | 18 | import mergeFsFixtureData from './fixtures/fs/merge.json'
|
8 | 19 | import mergeNoFastForwardFsFixtureData from './fixtures/fs/merge-no-ff.json'
|
9 | 20 |
|
10 |
| - |
11 | 21 | const author = {
|
12 | 22 | name: 'Mr. Test',
|
13 | 23 | email: 'mrtest@example.com',
|
@@ -257,7 +267,7 @@ describe('merge', () => {
|
257 | 267 | }
|
258 | 268 |
|
259 | 269 | // assert
|
260 |
| - await expectToFailWithTypeAsync(action, MergeNotSupportedError) |
| 270 | + await expectToFailWithTypeAsync(action, MergeConflictError) |
261 | 271 | })
|
262 | 272 |
|
263 | 273 | it("merge two branches that modified the same file (no conflict)'", async () => {
|
@@ -302,7 +312,7 @@ describe('merge', () => {
|
302 | 312 | }
|
303 | 313 |
|
304 | 314 | // assert
|
305 |
| - await expectToFailWithTypeAsync(action, MergeNotSupportedError) |
| 315 | + await expectToFailWithTypeAsync(action, MergeConflictError) |
306 | 316 | })
|
307 | 317 |
|
308 | 318 | it("merge two branches that modified the same file, custom conflict resolver (prefer our changes)", async () => {
|
@@ -385,3 +395,162 @@ describe('merge', () => {
|
385 | 395 | expect(conflictedFile).toBe('text\nfile\nwas\nmodified\n')
|
386 | 396 | })
|
387 | 397 | })
|
| 398 | + |
| 399 | +describe('merge-e2e', () => { |
| 400 | + const branch1Name = 'branch1' |
| 401 | + const branch2Name = 'branch2' |
| 402 | + const newDirName = 'new-dir' |
| 403 | + |
| 404 | + it('merge new folders with the same name', async () => { |
| 405 | + // ARRANGE |
| 406 | + const { fs, dir } = await makeFsFixture() |
| 407 | + |
| 408 | + // initializing new repo |
| 409 | + await init({ fs, dir, defaultBranch: branch1Name }) |
| 410 | + await commit({ fs, dir, message: 'first commit', author: { name: 'author0' } }) |
| 411 | + await branch({ fs, dir, ref: branch2Name, checkout: false }) |
| 412 | + |
| 413 | + // writing files to the branch1 |
| 414 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 415 | + await fs.writeFile(path.resolve(dir, newDirName, 'new-file.txt'), 'some content') |
| 416 | + await fs.writeFile(path.resolve(dir, newDirName, 'new-file1.txt'), 'some content 1') |
| 417 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'new-file.txt') }) |
| 418 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'new-file1.txt') }) |
| 419 | + await commit({ fs, dir, message: 'add files', author: { name: 'author1' } }) |
| 420 | + |
| 421 | + // writing files to the branch2 |
| 422 | + await checkout({ fs, dir, ref: branch2Name }) |
| 423 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 424 | + await fs.writeFile(path.resolve(dir, newDirName, 'new-file.txt'), 'some content') |
| 425 | + await fs.writeFile(path.resolve(dir, newDirName, 'new-file2.txt'), 'some content 2') |
| 426 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'new-file.txt') }) |
| 427 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'new-file2.txt') }) |
| 428 | + await commit({ fs, dir, message: 'add files', author: { name: 'author2' } }) |
| 429 | + |
| 430 | + // switching back to the branch1 |
| 431 | + await checkout({ fs, dir, ref: branch1Name }) |
| 432 | + |
| 433 | + // ACT |
| 434 | + const m = await merge({ fs, dir, ours: branch1Name, theirs: branch2Name, author: { name: 'author3' } }) |
| 435 | + await checkout({ fs, dir, ref: branch1Name }) |
| 436 | + |
| 437 | + // ASSERT |
| 438 | + expect(m.alreadyMerged).toBeFalsy() |
| 439 | + expect(m.mergeCommit).toBeTruthy() |
| 440 | + const newDirFiles = await fs.readdir(path.resolve(dir, newDirName)) |
| 441 | + expect(newDirFiles.length).toBe(3) |
| 442 | + }) |
| 443 | + |
| 444 | + it('merge subfolders with new parent folder with the same name', async () => { |
| 445 | + // ARRANGE |
| 446 | + const { fs, dir } = await makeFsFixture() |
| 447 | + |
| 448 | + // initializing new repo |
| 449 | + await init({ fs, dir, defaultBranch: branch1Name }) |
| 450 | + await commit({ fs, dir, message: 'first commit', author: { name: 'author0' } }) |
| 451 | + await branch({ fs, dir, ref: branch2Name, checkout: false }) |
| 452 | + |
| 453 | + // writing files to the branch1 |
| 454 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 455 | + await fs.mkdir(path.resolve(dir, newDirName, 'sub-folder1')) |
| 456 | + await fs.writeFile(path.resolve(dir, newDirName, 'sub-folder1', 'new-file.txt'), 'some content 1') |
| 457 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'sub-folder1', 'new-file.txt') }) |
| 458 | + await commit({ fs, dir, message: 'add files', author: { name: 'author1' } }) |
| 459 | + |
| 460 | + // writing files to a branch2 |
| 461 | + await checkout({ fs, dir, ref: branch2Name }) |
| 462 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 463 | + await fs.mkdir(path.resolve(dir, newDirName, 'sub-folder2')) |
| 464 | + await fs.writeFile(path.resolve(dir, newDirName, 'sub-folder2', 'new-file.txt'), 'some content 2') |
| 465 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'sub-folder2', 'new-file.txt') }) |
| 466 | + await commit({ fs, dir, message: 'add files', author: { name: 'author2' } }) |
| 467 | + |
| 468 | + // switching back to the branch1 |
| 469 | + await checkout({ fs, dir, ref: branch1Name }) |
| 470 | + |
| 471 | + // ACT |
| 472 | + const m = await merge({ fs, dir, ours: branch1Name, theirs: branch2Name, author: { name: 'author3' } }) |
| 473 | + await checkout({ fs, dir, ref: branch1Name }) |
| 474 | + |
| 475 | + // ASSERT |
| 476 | + expect(m.alreadyMerged).toBeFalsy() |
| 477 | + expect(m.mergeCommit).toBeTruthy() |
| 478 | + const newDirFiles = await fs.readdir(path.resolve(dir, newDirName)) |
| 479 | + expect(newDirFiles.length).toBe(2) |
| 480 | + }) |
| 481 | + |
| 482 | + it('merge subfolders with the same name who have a new parent with the same name', async () => { |
| 483 | + // ARRANGE |
| 484 | + const { fs, dir } = await makeFsFixture() |
| 485 | + |
| 486 | + // initializing new repo |
| 487 | + await init({ fs, dir, defaultBranch: branch1Name }) |
| 488 | + await commit({ fs, dir, message: 'first commit', author: { name: 'author0' } }) |
| 489 | + await branch({ fs, dir, ref: branch2Name, checkout: false }) |
| 490 | + |
| 491 | + // writing files to the branch1 |
| 492 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 493 | + await fs.mkdir(path.resolve(dir, newDirName, 'sub-folder')) |
| 494 | + await fs.writeFile(path.resolve(dir, newDirName, 'sub-folder', 'new-file1.txt'), 'some content 1') |
| 495 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'sub-folder', 'new-file1.txt') }) |
| 496 | + await commit({ fs, dir, message: 'add files', author: { name: 'author1' } }) |
| 497 | + |
| 498 | + // writing files to a branch2 |
| 499 | + await checkout({ fs, dir, ref: branch2Name }) |
| 500 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 501 | + await fs.mkdir(path.resolve(dir, newDirName, 'sub-folder')) |
| 502 | + await fs.writeFile(path.resolve(dir, newDirName, 'sub-folder', 'new-file2.txt'), 'some content 2') |
| 503 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'sub-folder', 'new-file2.txt') }) |
| 504 | + await commit({ fs, dir, message: 'add files', author: { name: 'author2' } }) |
| 505 | + |
| 506 | + // switching back to the branch1 |
| 507 | + await checkout({ fs, dir, ref: branch1Name }) |
| 508 | + |
| 509 | + // ACT |
| 510 | + const m = await merge({ fs, dir, ours: branch1Name, theirs: branch2Name, author: { name: 'author3' } }) |
| 511 | + await checkout({ fs, dir, ref: branch1Name }) |
| 512 | + |
| 513 | + // ASSERT |
| 514 | + expect(m.alreadyMerged).toBeFalsy() |
| 515 | + expect(m.mergeCommit).toBeTruthy() |
| 516 | + const newDirFiles = await fs.readdir(path.resolve(dir, newDirName)) |
| 517 | + expect(newDirFiles.length).toBe(1) |
| 518 | + const newSubDirFiles = await fs.readdir(path.resolve(dir, newDirName, 'sub-folder')) |
| 519 | + expect(newSubDirFiles.length).toBe(2) |
| 520 | + }) |
| 521 | + |
| 522 | + it('merge new folder and file with the same name should fail', async () => { |
| 523 | + // ARRANGE |
| 524 | + const { fs, dir } = await makeFsFixture() |
| 525 | + |
| 526 | + // initializing new repo |
| 527 | + await init({ fs, dir, defaultBranch: branch1Name }) |
| 528 | + await commit({ fs, dir, message: 'first commit', author: { name: 'author0' } }) |
| 529 | + await branch({ fs, dir, ref: branch2Name, checkout: false }) |
| 530 | + |
| 531 | + // writing files to the branch1 |
| 532 | + await fs.mkdir(path.resolve(dir, newDirName)) |
| 533 | + await fs.writeFile(path.resolve(dir, newDirName, 'new-file.txt'), 'some content') |
| 534 | + await fs.writeFile(path.resolve(dir, newDirName, 'new-file1.txt'), 'some content 1') |
| 535 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'new-file.txt') }) |
| 536 | + await add({ fs, dir, filepath: path.resolve(newDirName, 'new-file1.txt') }) |
| 537 | + await commit({ fs, dir, message: 'add files', author: { name: 'author1' } }) |
| 538 | + |
| 539 | + // writing files to the branch2 |
| 540 | + await checkout({ fs, dir, ref: branch2Name }) |
| 541 | + await fs.writeFile(path.resolve(dir, newDirName), 'some content') |
| 542 | + await add({ fs, dir, filepath: newDirName }) |
| 543 | + await commit({ fs, dir, message: 'add files', author: { name: 'author2' } }) |
| 544 | + |
| 545 | + // switching back to the branch1 |
| 546 | + await checkout({ fs, dir, ref: branch1Name }) |
| 547 | + |
| 548 | + // ACT |
| 549 | + const action = async () => { |
| 550 | + await merge({ fs, dir, ours: branch1Name, theirs: branch2Name, author: { name: 'author3' } }) |
| 551 | + } |
| 552 | + |
| 553 | + // ASSERT |
| 554 | + await expectToFailWithTypeAsync(action, MergeConflictError) |
| 555 | + }) |
| 556 | +}) |
0 commit comments