|
| 1 | +import subprocess |
| 2 | +import tempfile |
| 3 | +from typing import List |
| 4 | +import unittest |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from .scheme_mock import UPDATE_CHECKOUT_PATH |
| 8 | + |
| 9 | + |
| 10 | +class TestStatusCommand(unittest.TestCase): |
| 11 | + def call(self, args: List[str], cwd: Path): |
| 12 | + subprocess.check_call( |
| 13 | + args, cwd=cwd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL |
| 14 | + ) |
| 15 | + |
| 16 | + def init_repo(self, path: Path, with_changes: bool): |
| 17 | + self.call(args=["git", "init"], cwd=path) |
| 18 | + self.call( |
| 19 | + args=["git", "config", "--local", "user.name", "swift_test"], cwd=path |
| 20 | + ) |
| 21 | + self.call( |
| 22 | + args=[ "git", "config", "--local", "user.email", "[email protected]"], |
| 23 | + cwd=path, |
| 24 | + ) |
| 25 | + self.call(args=["git", "config", "commit.gpgsign", "false"], cwd=path) |
| 26 | + |
| 27 | + (path / "file.txt").write_text("initial\n") |
| 28 | + self.call(args=["git", "add", "file.txt"], cwd=path) |
| 29 | + self.call(args=["git", "commit", "-m", "initial commit"], cwd=path) |
| 30 | + |
| 31 | + if with_changes: |
| 32 | + (path / "file.txt").write_text("modified\n") |
| 33 | + |
| 34 | + def test_repositories_with_active_changes(self): |
| 35 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 36 | + root = Path(tmpdir) |
| 37 | + |
| 38 | + repo_1 = root / "repo_1" |
| 39 | + repo_2 = root / "repo_2" |
| 40 | + repo_1.mkdir() |
| 41 | + repo_2.mkdir() |
| 42 | + |
| 43 | + self.init_repo(repo_1, True) |
| 44 | + self.init_repo(repo_2, True) |
| 45 | + |
| 46 | + output = subprocess.run( |
| 47 | + [UPDATE_CHECKOUT_PATH, "--source-root", str(root), "status"], |
| 48 | + capture_output=True, |
| 49 | + text=True, |
| 50 | + ).stdout |
| 51 | + |
| 52 | + self.assertIn("The following repositories have active changes:", output) |
| 53 | + self.assertIn(" - [repo_1] 1 active change", output) |
| 54 | + self.assertIn(" - [repo_2] 1 active change", output) |
| 55 | + |
| 56 | + def test_repositories_without_active_changes(self): |
| 57 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 58 | + root = Path(tmpdir) |
| 59 | + |
| 60 | + repo_1 = root / "repo_1" |
| 61 | + repo_2 = root / "repo_2" |
| 62 | + repo_1.mkdir() |
| 63 | + repo_2.mkdir() |
| 64 | + |
| 65 | + self.init_repo(repo_1, False) |
| 66 | + self.init_repo(repo_2, False) |
| 67 | + |
| 68 | + output = subprocess.run( |
| 69 | + [UPDATE_CHECKOUT_PATH, "--source-root", str(root), "status"], |
| 70 | + capture_output=True, |
| 71 | + text=True, |
| 72 | + ).stdout |
| 73 | + |
| 74 | + self.assertIn("No repositories have active changes.", output) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + unittest.main() |
0 commit comments