|
1 | 1 | import textwrap |
2 | 2 | from io import StringIO |
3 | 3 | from typing import List |
4 | | -from unittest.mock import MagicMock, PropertyMock, patch |
| 4 | +from unittest.mock import MagicMock, PropertyMock, call, patch |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from click.testing import CliRunner |
@@ -127,6 +127,78 @@ def test_migrate_repo_no_match( |
127 | 127 | mock_load_config.assert_called_once() |
128 | 128 |
|
129 | 129 |
|
| 130 | +NO_REPOS: List[Repository] = [] |
| 131 | +MULTIPLE_REPOS = [ |
| 132 | + Repository("some-team/a-repo", Visibility.PUBLIC), |
| 133 | + Repository("some-team/b-repo", Visibility.PRIVATE), |
| 134 | + Repository("some-team/c-repo", Visibility.UNKNOWN), |
| 135 | +] |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize( |
| 139 | + "repos_to_sync, expected_output", |
| 140 | + [ |
| 141 | + ( |
| 142 | + NO_REPOS, |
| 143 | + textwrap.dedent( |
| 144 | + """\ |
| 145 | + Starting migration for 0 repos |
| 146 | + Migrated 0 repos successfully |
| 147 | + """ |
| 148 | + ), |
| 149 | + ), |
| 150 | + ( |
| 151 | + MULTIPLE_REPOS, |
| 152 | + textwrap.dedent( |
| 153 | + """\ |
| 154 | + Starting migration for 3 repos |
| 155 | + Migrating some-team/a-repo |
| 156 | + Migrating some-team/b-repo |
| 157 | + Migrating some-team/c-repo |
| 158 | + Migrated 3 repos successfully |
| 159 | + """ |
| 160 | + ), |
| 161 | + ), |
| 162 | + ], |
| 163 | +) |
| 164 | +@patch("gitea_github_sync.cli.migration.list_missing_github_repos", autospec=True) |
| 165 | +@patch("gitea_github_sync.cli.config.load_config", autospec=True) |
| 166 | +@patch("gitea_github_sync.cli.github.list_all_repositories", autospec=True) |
| 167 | +@patch("gitea_github_sync.cli.github.get_github", autospec=True) |
| 168 | +@patch("gitea_github_sync.cli.gitea.get_gitea", autospec=True) |
| 169 | +def test_sync( |
| 170 | + mock_get_gitea: MagicMock, |
| 171 | + mock_get_github: MagicMock, |
| 172 | + mock_list_all_repositories: MagicMock, |
| 173 | + mock_load_config: MagicMock, |
| 174 | + mock_list_missing_github_repos: MagicMock, |
| 175 | + repos_to_sync: List[Repository], |
| 176 | + expected_output: str, |
| 177 | +) -> None: |
| 178 | + expected_github_token = "some-github-token" |
| 179 | + |
| 180 | + type(mock_load_config.return_value).github_token = PropertyMock( |
| 181 | + return_value=expected_github_token |
| 182 | + ) |
| 183 | + mock_list_missing_github_repos.return_value = repos_to_sync |
| 184 | + |
| 185 | + runner = CliRunner() |
| 186 | + command = ["sync"] |
| 187 | + result = runner.invoke(cli, command) |
| 188 | + |
| 189 | + assert result.exit_code == 0 |
| 190 | + assert result.stdout == expected_output |
| 191 | + mock_load_config.assert_called_once() |
| 192 | + mock_list_all_repositories.assert_called_once_with(mock_get_github.return_value) |
| 193 | + mock_list_missing_github_repos.assert_called_once_with( |
| 194 | + gh_repos=mock_list_all_repositories.return_value, |
| 195 | + gitea_repos=mock_get_gitea.return_value.get_repos.return_value, |
| 196 | + ) |
| 197 | + mock_get_gitea.return_value.migrate_repo.assert_has_calls( |
| 198 | + [call(repo=repo, github_token=expected_github_token) for repo in repos_to_sync] |
| 199 | + ) |
| 200 | + |
| 201 | + |
130 | 202 | @patch("sys.stdout", new_callable=StringIO) |
131 | 203 | def test_print_repositories_without_stats( |
132 | 204 | stdout: StringIO, |
|
0 commit comments