ce04c91472
Python tool to check for any merge or rebase conflicts between two branches All operations done in temporary git worktrees leaving all dev repo's untouched
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from conflict_catcher import CheckError, check_conflicts, parse_branch_spec, run_git
|
|
|
|
|
|
def git(repo: Path, *args: str) -> None:
|
|
result = run_git(repo, list(args))
|
|
if result.code != 0:
|
|
raise AssertionError(result.combined)
|
|
|
|
|
|
def write(path: Path, text: str) -> None:
|
|
path.write_text(text, encoding="utf-8")
|
|
|
|
|
|
class ConflictCatcherTests(unittest.TestCase):
|
|
def test_parse_branch_spec(self) -> None:
|
|
self.assertEqual(parse_branch_spec("main<develop", "", ""), ("main", "develop"))
|
|
self.assertEqual(parse_branch_spec("", "main", "develop"), ("main", "develop"))
|
|
with self.assertRaises(CheckError):
|
|
parse_branch_spec("main", "", "")
|
|
|
|
def test_detects_merge_and_rebase_conflicts(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
repo = Path(temp)
|
|
git(repo, "init", "-b", "main")
|
|
git(repo, "config", "user.email", "test@example.com")
|
|
git(repo, "config", "user.name", "Test User")
|
|
write(repo / "note.txt", "base\n")
|
|
git(repo, "add", "note.txt")
|
|
git(repo, "commit", "-m", "base")
|
|
|
|
git(repo, "checkout", "-b", "develop")
|
|
write(repo / "note.txt", "develop\n")
|
|
git(repo, "commit", "-am", "develop edit")
|
|
|
|
git(repo, "checkout", "main")
|
|
write(repo / "note.txt", "main\n")
|
|
git(repo, "commit", "-am", "main edit")
|
|
|
|
result = check_conflicts(
|
|
{
|
|
"repoPath": str(repo),
|
|
"branchSpec": "main<develop",
|
|
"operation": "both",
|
|
}
|
|
)
|
|
|
|
self.assertEqual(result["target"], "main")
|
|
self.assertEqual(result["source"], "develop")
|
|
self.assertEqual(len(result["checks"]), 2)
|
|
self.assertTrue(all(not check["ok"] for check in result["checks"]))
|
|
self.assertTrue(
|
|
all(
|
|
any(conflict["path"] == "note.txt" for conflict in check["conflicts"])
|
|
for check in result["checks"]
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|