An helper fixture providing most common operations for tests. git and cli operations grant control over time.
| 31 | |
| 32 | @dataclass |
| 33 | class UtilFixture: |
| 34 | """ |
| 35 | An helper fixture providing most common operations for tests. |
| 36 | |
| 37 | git and cli operations grant control over time. |
| 38 | """ |
| 39 | |
| 40 | mocker: MockerFixture |
| 41 | monkeypatch: pytest.MonkeyPatch |
| 42 | freezer: FrozenDateTimeFactory |
| 43 | |
| 44 | def __post_init__(self): |
| 45 | self.patch_env() |
| 46 | |
| 47 | def create_file_and_commit( |
| 48 | self, |
| 49 | message: str, |
| 50 | filename: str | None = None, |
| 51 | committer_date: str | None = None, |
| 52 | ) -> None: |
| 53 | """Create a file and commit it.""" |
| 54 | if not filename: |
| 55 | filename = str(uuid.uuid4()) |
| 56 | |
| 57 | Path(filename).touch() |
| 58 | c = cmd.run(["git", "add", "."]) |
| 59 | if c.return_code != 0: |
| 60 | raise exceptions.CommitError(c.err) |
| 61 | c = git.commit(message, committer_date=committer_date) |
| 62 | if c.return_code != 0: |
| 63 | raise exceptions.CommitError(c.err) |
| 64 | self.tick() |
| 65 | |
| 66 | def create_branch(self, name: str) -> None: |
| 67 | """Create a new branch.""" |
| 68 | c = cmd.run(["git", "branch", name]) |
| 69 | if c.return_code != 0: |
| 70 | raise exceptions.GitCommandError(c.err) |
| 71 | |
| 72 | def switch_branch(self, branch: str) -> None: |
| 73 | """Switch to given branch.""" |
| 74 | c = cmd.run(["git", "switch", branch]) |
| 75 | if c.return_code != 0: |
| 76 | raise exceptions.GitCommandError(c.err) |
| 77 | |
| 78 | def merge_branch(self, branch: str) -> None: |
| 79 | """Merge given branch into current branch.""" |
| 80 | c = cmd.run(["git", "merge", branch]) |
| 81 | if c.return_code != 0: |
| 82 | raise exceptions.GitCommandError(c.err) |
| 83 | self.tick() |
| 84 | |
| 85 | def get_current_branch(self) -> str: |
| 86 | """Get current git branch name.""" |
| 87 | c = cmd.run(["git", "rev-parse", "--abbrev-ref", "HEAD"]) |
| 88 | if c.return_code != 0: |
| 89 | raise exceptions.GitCommandError(c.err) |
| 90 | return c.out.strip() |