|
1 | 1 | import {
|
2 | 2 | createOrUpdateBranch,
|
3 | 3 | tryFetch,
|
4 |
| - getWorkingBaseAndType |
| 4 | + getWorkingBaseAndType, |
| 5 | + buildBranchCommits |
5 | 6 | } from '../lib/create-or-update-branch'
|
6 | 7 | import * as fs from 'fs'
|
7 | 8 | import {GitCommandManager} from '../lib/git-command-manager'
|
@@ -229,6 +230,77 @@ describe('create-or-update-branch tests', () => {
|
229 | 230 | expect(workingBaseType).toEqual('commit')
|
230 | 231 | })
|
231 | 232 |
|
| 233 | + it('tests buildBranchCommits with no diff', async () => { |
| 234 | + await git.checkout(BRANCH, BASE) |
| 235 | + const branchCommits = await buildBranchCommits(git, BASE, BRANCH) |
| 236 | + expect(branchCommits.length).toEqual(0) |
| 237 | + }) |
| 238 | + |
| 239 | + it('tests buildBranchCommits with addition and modification', async () => { |
| 240 | + await git.checkout(BRANCH, BASE) |
| 241 | + await createChanges() |
| 242 | + const UNTRACKED_EXE_FILE = 'a/script.sh' |
| 243 | + const filepath = path.join(REPO_PATH, UNTRACKED_EXE_FILE) |
| 244 | + await fs.promises.writeFile(filepath, '#!/usr/bin/env bash', {mode: 0o755}) |
| 245 | + await git.exec(['add', '-A']) |
| 246 | + await git.commit(['-m', 'Test changes']) |
| 247 | + |
| 248 | + const branchCommits = await buildBranchCommits(git, BASE, BRANCH) |
| 249 | + |
| 250 | + expect(branchCommits.length).toEqual(1) |
| 251 | + expect(branchCommits[0].subject).toEqual('Test changes') |
| 252 | + expect(branchCommits[0].changes.length).toEqual(3) |
| 253 | + expect(branchCommits[0].changes).toEqual([ |
| 254 | + {mode: '100755', path: UNTRACKED_EXE_FILE, status: 'A'}, |
| 255 | + {mode: '100644', path: TRACKED_FILE, status: 'M'}, |
| 256 | + {mode: '100644', path: UNTRACKED_FILE, status: 'A'} |
| 257 | + ]) |
| 258 | + }) |
| 259 | + |
| 260 | + it('tests buildBranchCommits with addition and deletion', async () => { |
| 261 | + await git.checkout(BRANCH, BASE) |
| 262 | + await createChanges() |
| 263 | + const TRACKED_FILE_NEW_PATH = 'c/tracked-file.txt' |
| 264 | + const filepath = path.join(REPO_PATH, TRACKED_FILE_NEW_PATH) |
| 265 | + await fs.promises.mkdir(path.dirname(filepath), {recursive: true}) |
| 266 | + await fs.promises.rename(path.join(REPO_PATH, TRACKED_FILE), filepath) |
| 267 | + await git.exec(['add', '-A']) |
| 268 | + await git.commit(['-m', 'Test changes']) |
| 269 | + |
| 270 | + const branchCommits = await buildBranchCommits(git, BASE, BRANCH) |
| 271 | + |
| 272 | + expect(branchCommits.length).toEqual(1) |
| 273 | + expect(branchCommits[0].subject).toEqual('Test changes') |
| 274 | + expect(branchCommits[0].changes.length).toEqual(3) |
| 275 | + expect(branchCommits[0].changes).toEqual([ |
| 276 | + {mode: '100644', path: TRACKED_FILE, status: 'D'}, |
| 277 | + {mode: '100644', path: UNTRACKED_FILE, status: 'A'}, |
| 278 | + {mode: '100644', path: TRACKED_FILE_NEW_PATH, status: 'A'} |
| 279 | + ]) |
| 280 | + }) |
| 281 | + |
| 282 | + it('tests buildBranchCommits with multiple commits', async () => { |
| 283 | + await git.checkout(BRANCH, BASE) |
| 284 | + for (let i = 0; i < 3; i++) { |
| 285 | + await createChanges() |
| 286 | + await git.exec(['add', '-A']) |
| 287 | + await git.commit(['-m', `Test changes ${i}`]) |
| 288 | + } |
| 289 | + |
| 290 | + const branchCommits = await buildBranchCommits(git, BASE, BRANCH) |
| 291 | + |
| 292 | + expect(branchCommits.length).toEqual(3) |
| 293 | + for (let i = 0; i < 3; i++) { |
| 294 | + expect(branchCommits[i].subject).toEqual(`Test changes ${i}`) |
| 295 | + expect(branchCommits[i].changes.length).toEqual(2) |
| 296 | + const untrackedFileStatus = i == 0 ? 'A' : 'M' |
| 297 | + expect(branchCommits[i].changes).toEqual([ |
| 298 | + {mode: '100644', path: TRACKED_FILE, status: 'M'}, |
| 299 | + {mode: '100644', path: UNTRACKED_FILE, status: untrackedFileStatus} |
| 300 | + ]) |
| 301 | + } |
| 302 | + }) |
| 303 | + |
232 | 304 | it('tests no changes resulting in no new branch being created', async () => {
|
233 | 305 | const commitMessage = uuidv4()
|
234 | 306 | const result = await createOrUpdateBranch(
|
@@ -585,6 +657,76 @@ describe('create-or-update-branch tests', () => {
|
585 | 657 | ).toBeTruthy()
|
586 | 658 | })
|
587 | 659 |
|
| 660 | + it('tests create, commit with partial changes on the base, and update', async () => { |
| 661 | + // This is an edge case where the changes for a single commit are partially merged to the base |
| 662 | + |
| 663 | + // Create tracked and untracked file changes |
| 664 | + const changes = await createChanges() |
| 665 | + const commitMessage = uuidv4() |
| 666 | + const result = await createOrUpdateBranch( |
| 667 | + git, |
| 668 | + commitMessage, |
| 669 | + '', |
| 670 | + BRANCH, |
| 671 | + REMOTE_NAME, |
| 672 | + false, |
| 673 | + ADD_PATHS_DEFAULT |
| 674 | + ) |
| 675 | + await git.checkout(BRANCH) |
| 676 | + expect(result.action).toEqual('created') |
| 677 | + expect(await getFileContent(TRACKED_FILE)).toEqual(changes.tracked) |
| 678 | + expect(await getFileContent(UNTRACKED_FILE)).toEqual(changes.untracked) |
| 679 | + expect( |
| 680 | + await gitLogMatches([commitMessage, INIT_COMMIT_MESSAGE]) |
| 681 | + ).toBeTruthy() |
| 682 | + |
| 683 | + // Push pull request branch to remote |
| 684 | + await git.push([ |
| 685 | + '--force-with-lease', |
| 686 | + REMOTE_NAME, |
| 687 | + `HEAD:refs/heads/${BRANCH}` |
| 688 | + ]) |
| 689 | + |
| 690 | + await afterTest(false) |
| 691 | + await beforeTest() |
| 692 | + |
| 693 | + // Create a commit on the base with a partial merge of the changes |
| 694 | + await createFile(TRACKED_FILE, changes.tracked) |
| 695 | + const baseCommitMessage = uuidv4() |
| 696 | + await git.exec(['add', '-A']) |
| 697 | + await git.commit(['-m', baseCommitMessage]) |
| 698 | + await git.push([ |
| 699 | + '--force', |
| 700 | + REMOTE_NAME, |
| 701 | + `HEAD:refs/heads/${DEFAULT_BRANCH}` |
| 702 | + ]) |
| 703 | + |
| 704 | + // Create the same tracked and untracked file changes |
| 705 | + const _changes = await createChanges(changes.tracked, changes.untracked) |
| 706 | + const _commitMessage = uuidv4() |
| 707 | + const _result = await createOrUpdateBranch( |
| 708 | + git, |
| 709 | + _commitMessage, |
| 710 | + '', |
| 711 | + BRANCH, |
| 712 | + REMOTE_NAME, |
| 713 | + false, |
| 714 | + ADD_PATHS_DEFAULT |
| 715 | + ) |
| 716 | + await git.checkout(BRANCH) |
| 717 | + expect(_result.action).toEqual('updated') |
| 718 | + expect(_result.hasDiffWithBase).toBeTruthy() |
| 719 | + expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked) |
| 720 | + expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked) |
| 721 | + expect( |
| 722 | + await gitLogMatches([ |
| 723 | + _commitMessage, |
| 724 | + baseCommitMessage, |
| 725 | + INIT_COMMIT_MESSAGE |
| 726 | + ]) |
| 727 | + ).toBeTruthy() |
| 728 | + }) |
| 729 | + |
588 | 730 | it('tests create, squash merge, and update with identical changes', async () => {
|
589 | 731 | // Branches that have been squash merged appear to have a diff with the base due to
|
590 | 732 | // different commits for the same changes. To prevent creating pull requests
|
@@ -1607,6 +1749,81 @@ describe('create-or-update-branch tests', () => {
|
1607 | 1749 | ).toBeTruthy()
|
1608 | 1750 | })
|
1609 | 1751 |
|
| 1752 | + it('tests create, commit with partial changes on the base, and update (WBNB)', async () => { |
| 1753 | + // This is an edge case where the changes for a single commit are partially merged to the base |
| 1754 | + |
| 1755 | + // Set the working base to a branch that is not the pull request base |
| 1756 | + await git.checkout(NOT_BASE_BRANCH) |
| 1757 | + |
| 1758 | + // Create tracked and untracked file changes |
| 1759 | + const changes = await createChanges() |
| 1760 | + const commitMessage = uuidv4() |
| 1761 | + const result = await createOrUpdateBranch( |
| 1762 | + git, |
| 1763 | + commitMessage, |
| 1764 | + BASE, |
| 1765 | + BRANCH, |
| 1766 | + REMOTE_NAME, |
| 1767 | + false, |
| 1768 | + ADD_PATHS_DEFAULT |
| 1769 | + ) |
| 1770 | + await git.checkout(BRANCH) |
| 1771 | + expect(result.action).toEqual('created') |
| 1772 | + expect(await getFileContent(TRACKED_FILE)).toEqual(changes.tracked) |
| 1773 | + expect(await getFileContent(UNTRACKED_FILE)).toEqual(changes.untracked) |
| 1774 | + expect( |
| 1775 | + await gitLogMatches([commitMessage, INIT_COMMIT_MESSAGE]) |
| 1776 | + ).toBeTruthy() |
| 1777 | + |
| 1778 | + // Push pull request branch to remote |
| 1779 | + await git.push([ |
| 1780 | + '--force-with-lease', |
| 1781 | + REMOTE_NAME, |
| 1782 | + `HEAD:refs/heads/${BRANCH}` |
| 1783 | + ]) |
| 1784 | + |
| 1785 | + await afterTest(false) |
| 1786 | + await beforeTest() |
| 1787 | + |
| 1788 | + // Create a commit on the base with a partial merge of the changes |
| 1789 | + await createFile(TRACKED_FILE, changes.tracked) |
| 1790 | + const baseCommitMessage = uuidv4() |
| 1791 | + await git.exec(['add', '-A']) |
| 1792 | + await git.commit(['-m', baseCommitMessage]) |
| 1793 | + await git.push([ |
| 1794 | + '--force', |
| 1795 | + REMOTE_NAME, |
| 1796 | + `HEAD:refs/heads/${DEFAULT_BRANCH}` |
| 1797 | + ]) |
| 1798 | + |
| 1799 | + // Set the working base to a branch that is not the pull request base |
| 1800 | + await git.checkout(NOT_BASE_BRANCH) |
| 1801 | + |
| 1802 | + // Create the same tracked and untracked file changes |
| 1803 | + const _changes = await createChanges(changes.tracked, changes.untracked) |
| 1804 | + const _commitMessage = uuidv4() |
| 1805 | + const _result = await createOrUpdateBranch( |
| 1806 | + git, |
| 1807 | + _commitMessage, |
| 1808 | + BASE, |
| 1809 | + BRANCH, |
| 1810 | + REMOTE_NAME, |
| 1811 | + false, |
| 1812 | + ADD_PATHS_DEFAULT |
| 1813 | + ) |
| 1814 | + await git.checkout(BRANCH) |
| 1815 | + expect(_result.action).toEqual('updated') |
| 1816 | + expect(_result.hasDiffWithBase).toBeTruthy() |
| 1817 | + expect(await getFileContent(TRACKED_FILE)).toEqual(_changes.tracked) |
| 1818 | + expect(await getFileContent(UNTRACKED_FILE)).toEqual(_changes.untracked) |
| 1819 | + expect( |
| 1820 | + await gitLogMatches([ |
| 1821 | + _commitMessage, |
| 1822 | + baseCommitMessage // fetch depth of base is 1 |
| 1823 | + ]) |
| 1824 | + ).toBeTruthy() |
| 1825 | + }) |
| 1826 | + |
1610 | 1827 | it('tests create, squash merge, and update with identical changes (WBNB)', async () => {
|
1611 | 1828 | // Branches that have been squash merged appear to have a diff with the base due to
|
1612 | 1829 | // different commits for the same changes. To prevent creating pull requests
|
|
0 commit comments