|
2 | 2 | # All rights reserved |
3 | 3 | # Licensed under a 3-clause BSD style license (see LICENSE) |
4 | 4 | import subprocess |
| 5 | +from contextlib import nullcontext |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
|
10 | 11 | from tests.helpers.git import ClonedRepo |
11 | 12 |
|
12 | 13 |
|
| 14 | +@pytest.fixture(scope="module") |
| 15 | +def set_up_repository(local_clone: ClonedRepo): |
| 16 | + repo = Repository(local_clone.path.name, str(local_clone.path)) |
| 17 | + |
| 18 | + repo.git.capture("config", "user.name", "test_user") |
| 19 | + |
| 20 | + repo.git.capture("checkout", "master") |
| 21 | + (repo.path / "test1.txt").touch() |
| 22 | + repo.git.capture("add", ".") |
| 23 | + repo.git.capture("commit", "-m", "test1") |
| 24 | + (repo.path / "test2.txt").touch() |
| 25 | + repo.git.capture("add", ".") |
| 26 | + repo.git.capture("commit", "-m", "test2") |
| 27 | + |
| 28 | + repo.git.capture("checkout", "-b", "my-branch") |
| 29 | + |
| 30 | + (repo.path / "test3.txt").touch() |
| 31 | + repo.git.capture("add", ".") |
| 32 | + repo.git.capture("commit", "-m", "test3") |
| 33 | + |
| 34 | + repo.git.capture("checkout", "master") |
| 35 | + |
| 36 | + yield repo |
| 37 | + local_clone.reset_branch() |
| 38 | + |
| 39 | + |
13 | 40 | def test_current_branch(repository): |
14 | 41 | repo = Repository(repository.path.name, str(repository.path)) |
15 | 42 |
|
@@ -44,6 +71,78 @@ def test_get_latest_commit(repository): |
44 | 71 | assert short_sha2 not in commit_status1 |
45 | 72 |
|
46 | 73 |
|
| 74 | +@pytest.mark.parametrize( |
| 75 | + "args, n, source, expected, context", |
| 76 | + [ |
| 77 | + ( |
| 78 | + ["author:%an", "message:%f"], |
| 79 | + None, |
| 80 | + None, |
| 81 | + [ |
| 82 | + {"author": "test_user", "message": "test2"}, |
| 83 | + {"author": "test_user", "message": "test1"}, |
| 84 | + ], |
| 85 | + nullcontext(), |
| 86 | + ), |
| 87 | + ( |
| 88 | + ["author:%an", "message:%f"], |
| 89 | + 2, |
| 90 | + None, |
| 91 | + [{"author": "test_user", "message": "test2"}, {"author": "test_user", "message": "test1"}], |
| 92 | + nullcontext(), |
| 93 | + ), |
| 94 | + ( |
| 95 | + ["author:%an", "message:%f"], |
| 96 | + 0, |
| 97 | + None, |
| 98 | + [], |
| 99 | + nullcontext(), |
| 100 | + ), |
| 101 | + ( |
| 102 | + ["author:%an", "message:%f"], |
| 103 | + 3, |
| 104 | + "my-branch", |
| 105 | + [ |
| 106 | + {"author": "test_user", "message": "test3"}, |
| 107 | + {"author": "test_user", "message": "test2"}, |
| 108 | + {"author": "test_user", "message": "test1"}, |
| 109 | + ], |
| 110 | + nullcontext(), |
| 111 | + ), |
| 112 | + ( |
| 113 | + ["%H", "%f"], |
| 114 | + 1, |
| 115 | + None, |
| 116 | + None, |
| 117 | + pytest.raises(ValueError), |
| 118 | + ), |
| 119 | + ], |
| 120 | + ids=[ |
| 121 | + "test_log_no_n", |
| 122 | + "test_log_two_commits", |
| 123 | + "test_log_zero_commits", |
| 124 | + "test_log_branch_three_commits", |
| 125 | + "test_log_invalid_format_raises", |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_get_log(set_up_repository, local_clone, config_file, args, n, source, expected, context): |
| 129 | + config_file.model.repos['core'] = str(local_clone.path) |
| 130 | + config_file.save() |
| 131 | + |
| 132 | + repo = set_up_repository |
| 133 | + kwargs = {} |
| 134 | + if n is not None: |
| 135 | + kwargs['n'] = n |
| 136 | + if source: |
| 137 | + kwargs['source'] = source |
| 138 | + |
| 139 | + with context: |
| 140 | + if n is None: |
| 141 | + assert len(expected) < len(repo.git.log(args, **kwargs)) |
| 142 | + else: |
| 143 | + assert repo.git.log(args, **kwargs) == expected |
| 144 | + |
| 145 | + |
47 | 146 | def test_tags(repository): |
48 | 147 | repo = Repository(repository.path.name, str(repository.path)) |
49 | 148 |
|
|
0 commit comments