|
| 1 | +import shutil |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import git |
| 7 | +import tomli |
| 8 | + |
| 9 | +from guw.main import GUW |
| 10 | + |
| 11 | +class TestSyncWithConflicts(unittest.TestCase): |
| 12 | + file_name = "file.txt" |
| 13 | + fc_init = """ |
| 14 | + init |
| 15 | + """ |
| 16 | + fc_a1 = """ |
| 17 | + init |
| 18 | + A fo |
| 19 | + """ |
| 20 | + fc_b = """ |
| 21 | + B bar |
| 22 | + init |
| 23 | + A fo |
| 24 | + """ |
| 25 | + fc_a2 = """ |
| 26 | + init |
| 27 | + A foo |
| 28 | + """ |
| 29 | + fc_expected = """ |
| 30 | + B bar |
| 31 | + init |
| 32 | + A foo |
| 33 | + """ |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def _commit_file(cls, repo, file_path, file_contents, commit_message, *args): |
| 37 | + with open(file_path, "w") as f: |
| 38 | + f.write(file_contents) |
| 39 | + repo.index.add(file_path) |
| 40 | + repo.git.commit("--message", commit_message, *args) |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def _prepare_repo(cls, repo_dir): |
| 44 | + repo = git.Repo.init(repo_dir) |
| 45 | + fp = repo_dir / cls.file_name |
| 46 | + |
| 47 | + cls._commit_file(repo, fp, cls.fc_init, "init") |
| 48 | + repo.git.branch("final") |
| 49 | + |
| 50 | + repo.git.checkout("HEAD", b = "A") |
| 51 | + cls._commit_file(repo, fp, cls.fc_a1, "foo") |
| 52 | + |
| 53 | + repo.git.checkout("HEAD", b = "B") |
| 54 | + cls._commit_file(repo, fp, cls.fc_b, "bar") |
| 55 | + |
| 56 | + # Reviewer asked for changes: fix and ammend this branch |
| 57 | + repo.git.checkout("A") |
| 58 | + cls._commit_file(repo, fp, cls.fc_a2, "foo", "--amend") |
| 59 | + |
| 60 | + def setUp(self): |
| 61 | + self.tmpdir = Path(tempfile.mkdtemp()) |
| 62 | + self.guw_dir = self.tmpdir / "guw" |
| 63 | + self.repo_dir = self.tmpdir / "remote" |
| 64 | + |
| 65 | + self._prepare_repo(self.repo_dir) |
| 66 | + config = f""" |
| 67 | + [[remotes]] |
| 68 | + name = "origin" |
| 69 | + url = "file://{self.repo_dir}" |
| 70 | +
|
| 71 | + [target] |
| 72 | + remote = "origin" |
| 73 | + branch = "final" |
| 74 | +
|
| 75 | + [source] |
| 76 | + remote = "origin" |
| 77 | + branch = "main" |
| 78 | +
|
| 79 | + [[features]] |
| 80 | + remote = "origin" |
| 81 | + name = "A" |
| 82 | + status = "merging" |
| 83 | +
|
| 84 | + [[features]] |
| 85 | + remote = "origin" |
| 86 | + name = "B" |
| 87 | + status = "pending" |
| 88 | + """ |
| 89 | + |
| 90 | + self.guw = GUW(tomli.loads(config)) |
| 91 | + |
| 92 | + def cleanUp(self): |
| 93 | + shutil.rmtree(self.tmpdir) |
| 94 | + |
| 95 | + def check_file_contents(self, repo_path): |
| 96 | + with open(repo_path / self.file_name) as f: |
| 97 | + file_contents = f.read() |
| 98 | + self.assertEqual(self.fc_expected, file_contents) |
| 99 | + |
| 100 | + def test_sync_with_conflict(self): |
| 101 | + self.guw.sync( |
| 102 | + backup = False, |
| 103 | + keep = True, |
| 104 | + local = True, |
| 105 | + folder = self.guw_dir, |
| 106 | + ) |
| 107 | + repo = git.Repo(self.guw_dir) |
| 108 | + self.check_file_contents(self.guw_dir) |
| 109 | + |
| 110 | + def test_manual_rebase(self): |
| 111 | + repo = git.Repo(self.repo_dir) |
| 112 | + repo.git.rebase("B~", "B", "--onto", "A") |
| 113 | + self.check_file_contents(self.repo_dir) |
0 commit comments