|
10 | 10 | from tests import wrapped_config_get
|
11 | 11 |
|
12 | 12 |
|
13 |
| -class RepositoryTests(TestCase): |
14 |
| - @pytest.fixture(autouse=True) |
15 |
| - def inject_fixtures(self, caplog): |
16 |
| - self._caplog = caplog |
17 |
| - |
18 |
| - def test_upload_enabled(self): |
19 |
| - self.assertTrue(ArtifactRepo.upload_enabled()) |
20 |
| - |
21 |
| - @mock.patch( |
22 |
| - "semantic_release.repository.config.get", |
23 |
| - wrapped_config_get(upload_to_repository=False), |
| 13 | +def test_upload_enabled(): |
| 14 | + assert ArtifactRepo.upload_enabled() |
| 15 | + |
| 16 | + |
| 17 | +@mock.patch( |
| 18 | + "semantic_release.repository.config.get", |
| 19 | + wrapped_config_get(upload_to_repository=False), |
| 20 | +) |
| 21 | +def test_upload_enabled_with_upload_to_repository_false(): |
| 22 | + assert not ArtifactRepo.upload_enabled() |
| 23 | + |
| 24 | + |
| 25 | +@mock.patch( |
| 26 | + "semantic_release.repository.config.get", |
| 27 | + wrapped_config_get(upload_to_pypi=False), |
| 28 | +) |
| 29 | +def test_upload_enabled_with_upload_to_pypi_false(): |
| 30 | + assert not ArtifactRepo.upload_enabled() |
| 31 | + |
| 32 | + |
| 33 | +@mock.patch.dict( |
| 34 | + "os.environ", |
| 35 | + {"PYPI_USERNAME": "pypi-username", "PYPI_PASSWORD": "pypi-password"}, |
| 36 | +) |
| 37 | +def test_repo_with_pypi_credentials(): |
| 38 | + repo = ArtifactRepo(Path("dist")) |
| 39 | + assert repo.username == "pypi-username" |
| 40 | + assert repo.password == "pypi-password" |
| 41 | + |
| 42 | + |
| 43 | +@mock.patch.dict( |
| 44 | + "os.environ", |
| 45 | + { |
| 46 | + "REPOSITORY_USERNAME": "repo-username", |
| 47 | + "REPOSITORY_PASSWORD": "repo-password", |
| 48 | + }, |
| 49 | +) |
| 50 | +def test_repo_with_repository_credentials(): |
| 51 | + repo = ArtifactRepo(Path("dist")) |
| 52 | + assert repo.username == "repo-username" |
| 53 | + assert repo.password == "repo-password" |
| 54 | + |
| 55 | + |
| 56 | +@mock.patch.dict("os.environ", {"REPOSITORY_PASSWORD": "repo-password"}) |
| 57 | +def test_repo_with_repository_password_only(): |
| 58 | + repo = ArtifactRepo(Path("dist")) |
| 59 | + assert repo.username == "__token__" |
| 60 | + assert repo.password == "repo-password" |
| 61 | + |
| 62 | + |
| 63 | +@mock.patch("semantic_release.repository.Path.exists", return_value=True) |
| 64 | +def test_repo_with_pypirc_file(mock_path_exists): |
| 65 | + repo = ArtifactRepo(Path("dist")) |
| 66 | + assert repo.username is None |
| 67 | + assert repo.password is None |
| 68 | + |
| 69 | + |
| 70 | +@mock.patch("semantic_release.repository.Path.exists", return_value=False) |
| 71 | +def test_repo_without_pypirc_file(mock_path_exists): |
| 72 | + with pytest.raises(ImproperConfigurationError) as cm: |
| 73 | + ArtifactRepo(Path("dist")) |
| 74 | + assert str(cm.value) == "Missing credentials for uploading to artifact repository" |
| 75 | + |
| 76 | + |
| 77 | +@mock.patch.dict( |
| 78 | + "os.environ", |
| 79 | + { |
| 80 | + "PYPI_TOKEN": "pypi-token", |
| 81 | + "PYPI_USERNAME": "pypi-username", |
| 82 | + "PYPI_PASSWORD": "pypi-password", |
| 83 | + "REPOSITORY_USERNAME": "repo-username", |
| 84 | + "REPOSITORY_PASSWORD": "repo-password", |
| 85 | + }, |
| 86 | +) |
| 87 | +def test_repo_prefers_repository_over_pypi(): |
| 88 | + repo = ArtifactRepo(Path("dist")) |
| 89 | + assert repo.username == "repo-username" |
| 90 | + assert repo.password == "repo-password" |
| 91 | + |
| 92 | + |
| 93 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 94 | +def test_repo_with_custom_dist_path(mock_handle_creds): |
| 95 | + repo = ArtifactRepo(Path("custom-dist")) |
| 96 | + assert repo.dists == [os.path.join("custom-dist", "*")] |
| 97 | + |
| 98 | + |
| 99 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 100 | +@mock.patch( |
| 101 | + "semantic_release.repository.config.get", |
| 102 | + wrapped_config_get(dist_glob_patterns="*.tar.gz,*.whl"), |
| 103 | +) |
| 104 | +def test_repo_with_custom_dist_globs(mock_handle_creds): |
| 105 | + repo = ArtifactRepo(Path("dist")) |
| 106 | + assert repo.dists == [ |
| 107 | + os.path.join("dist", "*.tar.gz"), |
| 108 | + os.path.join("dist", "*.whl"), |
| 109 | + ] |
| 110 | + |
| 111 | + |
| 112 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 113 | +@mock.patch( |
| 114 | + "semantic_release.repository.config.get", |
| 115 | + wrapped_config_get(upload_to_pypi_glob_patterns="*.tar.gz,*.whl"), |
| 116 | +) |
| 117 | +def test_repo_with_custom_pypi_globs(mock_handle_creds): |
| 118 | + repo = ArtifactRepo(Path("dist")) |
| 119 | + assert repo.dists == [ |
| 120 | + os.path.join("dist", "*.tar.gz"), |
| 121 | + os.path.join("dist", "*.whl"), |
| 122 | + ] |
| 123 | + |
| 124 | + |
| 125 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 126 | +@mock.patch( |
| 127 | + "semantic_release.repository.config.get", |
| 128 | + wrapped_config_get(repository="testpypi"), |
| 129 | +) |
| 130 | +def test_repo_with_repo_name_testpypi(mock_handle_creds): |
| 131 | + repo = ArtifactRepo(Path("dist")) |
| 132 | + assert repo.repository_name == "testpypi" |
| 133 | + |
| 134 | + |
| 135 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 136 | +@mock.patch( |
| 137 | + "semantic_release.repository.config.get", |
| 138 | + wrapped_config_get(repository="invalid"), |
| 139 | +) |
| 140 | +def test_raises_error_when_repo_name_invalid(mock_handle_creds): |
| 141 | + repo = ArtifactRepo(Path("dist")) |
| 142 | + with pytest.raises( |
| 143 | + ImproperConfigurationError, match="Upload to artifact repository has failed" |
| 144 | + ): |
| 145 | + repo.upload(noop=False, verbose=False, skip_existing=False) |
| 146 | + |
| 147 | + |
| 148 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 149 | +@mock.patch( |
| 150 | + "semantic_release.repository.config.get", |
| 151 | + wrapped_config_get(repository_url="https://custom-repo"), |
| 152 | +) |
| 153 | +def test_repo_with_custom_repo_url(mock_handle_creds): |
| 154 | + repo = ArtifactRepo(Path("dist")) |
| 155 | + assert repo.repository_url == "https://custom-repo" |
| 156 | + |
| 157 | + |
| 158 | +@mock.patch("semantic_release.repository.twine_upload") |
| 159 | +@mock.patch( |
| 160 | + "semantic_release.repository.config.get", |
| 161 | + wrapped_config_get( |
| 162 | + dist_glob_patterns="*.tar.gz,*.whl", repository_url="https://custom-repo" |
| 163 | + ), |
| 164 | +) |
| 165 | +@mock.patch.dict( |
| 166 | + "os.environ", |
| 167 | + { |
| 168 | + "REPOSITORY_USERNAME": "repo-username", |
| 169 | + "REPOSITORY_PASSWORD": "repo-password", |
| 170 | + }, |
| 171 | +) |
| 172 | +def test_upload_with_custom_settings(mock_upload): |
| 173 | + repo = ArtifactRepo(Path("custom-dist")) |
| 174 | + repo.upload( |
| 175 | + noop=False, verbose=True, skip_existing=True, comment="distribution comment" |
24 | 176 | )
|
25 |
| - def test_upload_enabled_with_upload_to_repository_false(self): |
26 |
| - self.assertFalse(ArtifactRepo.upload_enabled()) |
27 |
| - |
28 |
| - @mock.patch( |
29 |
| - "semantic_release.repository.config.get", |
30 |
| - wrapped_config_get(upload_to_pypi=False), |
| 177 | + settings = mock_upload.call_args[1]["upload_settings"] |
| 178 | + assert isinstance(settings.auth, auth.Private) |
| 179 | + assert settings.comment == "distribution comment" |
| 180 | + assert settings.username == "repo-username" |
| 181 | + assert settings.password == "repo-password" |
| 182 | + assert settings.repository_config["repository"] == "https://custom-repo" |
| 183 | + assert settings.skip_existing |
| 184 | + assert settings.verbose |
| 185 | + assert mock_upload.called |
| 186 | + |
| 187 | + |
| 188 | +@mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
| 189 | +@mock.patch("semantic_release.repository.twine_upload") |
| 190 | +def test_upload_with_noop(mock_upload, mock_handle_creds): |
| 191 | + ArtifactRepo(Path("dist")).upload(noop=True, verbose=False, skip_existing=False) |
| 192 | + assert not mock_upload.called |
| 193 | + |
| 194 | + |
| 195 | +@mock.patch.dict("os.environ", {"PYPI_TOKEN": "pypi-token"}) |
| 196 | +def test_repo_with_token_only(caplog): |
| 197 | + repo = ArtifactRepo(Path("dist")) |
| 198 | + assert ( |
| 199 | + "Providing only password or token without username is deprecated" |
| 200 | + in caplog.messages |
31 | 201 | )
|
32 |
| - def test_upload_enabled_with_upload_to_pypi_false(self): |
33 |
| - self.assertFalse(ArtifactRepo.upload_enabled()) |
| 202 | + assert repo.username == "__token__" |
| 203 | + assert repo.password == "pypi-token" |
34 | 204 |
|
35 |
| - @mock.patch.dict( |
36 |
| - "os.environ", |
37 |
| - {"PYPI_USERNAME": "pypi-username", "PYPI_PASSWORD": "pypi-password"}, |
38 |
| - ) |
39 |
| - def test_repo_with_pypi_credentials(self): |
40 |
| - repo = ArtifactRepo(Path("dist")) |
41 |
| - self.assertEqual(repo.username, "pypi-username") |
42 |
| - self.assertEqual(repo.password, "pypi-password") |
43 |
| - |
44 |
| - @mock.patch.dict( |
45 |
| - "os.environ", |
46 |
| - { |
47 |
| - "REPOSITORY_USERNAME": "repo-username", |
48 |
| - "REPOSITORY_PASSWORD": "repo-password", |
49 |
| - }, |
50 |
| - ) |
51 |
| - def test_repo_with_repository_credentials(self): |
52 |
| - repo = ArtifactRepo(Path("dist")) |
53 |
| - self.assertEqual(repo.username, "repo-username") |
54 |
| - self.assertEqual(repo.password, "repo-password") |
55 |
| - |
56 |
| - @mock.patch.dict("os.environ", {"PYPI_TOKEN": "pypi-token"}) |
57 |
| - def test_repo_with_token_only(self): |
58 |
| - repo = ArtifactRepo(Path("dist")) |
59 |
| - self.assertIn( |
60 |
| - "Providing only password or token without username is deprecated", |
61 |
| - self._caplog.messages, |
62 |
| - ) |
63 |
| - self.assertEqual(repo.username, "__token__") |
64 |
| - self.assertEqual(repo.password, "pypi-token") |
65 |
| - |
66 |
| - @mock.patch.dict("os.environ", {"PYPI_PASSWORD": "pypi-password"}) |
67 |
| - def test_repo_with_pypi_password_only(self): |
68 |
| - repo = ArtifactRepo(Path("dist")) |
69 |
| - self.assertIn( |
70 |
| - "Providing only password or token without username is deprecated", |
71 |
| - self._caplog.messages, |
72 |
| - ) |
73 |
| - self.assertEqual(repo.username, "__token__") |
74 |
| - self.assertEqual(repo.password, "pypi-password") |
75 |
| - |
76 |
| - @mock.patch.dict("os.environ", {"REPOSITORY_PASSWORD": "repo-password"}) |
77 |
| - def test_repo_with_repository_password_only(self): |
78 |
| - repo = ArtifactRepo(Path("dist")) |
79 |
| - self.assertEqual(repo.username, "__token__") |
80 |
| - self.assertEqual(repo.password, "repo-password") |
81 |
| - |
82 |
| - @mock.patch("semantic_release.repository.Path.exists", return_value=True) |
83 |
| - def test_repo_with_pypirc_file(self, mock_path_exists): |
84 |
| - repo = ArtifactRepo(Path("dist")) |
85 |
| - self.assertIsNone(repo.username) |
86 |
| - self.assertIsNone(repo.password) |
87 |
| - |
88 |
| - @mock.patch("semantic_release.repository.Path.exists", return_value=False) |
89 |
| - def test_repo_without_pypirc_file(self, mock_path_exists): |
90 |
| - with self.assertRaises(ImproperConfigurationError) as cm: |
91 |
| - ArtifactRepo(Path("dist")) |
92 |
| - self.assertEqual( |
93 |
| - str(cm.exception), |
94 |
| - "Missing credentials for uploading to artifact repository", |
95 |
| - ) |
96 |
| - |
97 |
| - @mock.patch.dict( |
98 |
| - "os.environ", |
99 |
| - { |
100 |
| - "PYPI_TOKEN": "pypi-token", |
101 |
| - "PYPI_USERNAME": "pypi-username", |
102 |
| - "PYPI_PASSWORD": "pypi-password", |
103 |
| - "REPOSITORY_USERNAME": "repo-username", |
104 |
| - "REPOSITORY_PASSWORD": "repo-password", |
105 |
| - }, |
106 |
| - ) |
107 |
| - def test_repo_prefers_repository_over_pypi(self): |
108 |
| - repo = ArtifactRepo(Path("dist")) |
109 |
| - self.assertEqual(repo.username, "repo-username") |
110 |
| - self.assertEqual(repo.password, "repo-password") |
111 |
| - |
112 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
113 |
| - def test_repo_with_custom_dist_path(self, mock_handle_creds): |
114 |
| - repo = ArtifactRepo(Path("custom-dist")) |
115 |
| - self.assertEqual(repo.dists, [os.path.join("custom-dist", "*")]) |
116 |
| - |
117 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
118 |
| - @mock.patch( |
119 |
| - "semantic_release.repository.config.get", |
120 |
| - wrapped_config_get(dist_glob_patterns="*.tar.gz,*.whl"), |
121 |
| - ) |
122 |
| - def test_repo_with_custom_dist_globs(self, mock_handle_creds): |
123 |
| - repo = ArtifactRepo(Path("dist")) |
124 |
| - self.assertEqual( |
125 |
| - repo.dists, |
126 |
| - [os.path.join("dist", "*.tar.gz"), os.path.join("dist", "*.whl")], |
127 |
| - ) |
128 |
| - |
129 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
130 |
| - @mock.patch( |
131 |
| - "semantic_release.repository.config.get", |
132 |
| - wrapped_config_get(upload_to_pypi_glob_patterns="*.tar.gz,*.whl"), |
133 |
| - ) |
134 |
| - def test_repo_with_custom_pypi_globs(self, mock_handle_creds): |
135 |
| - repo = ArtifactRepo(Path("dist")) |
136 |
| - self.assertEqual( |
137 |
| - repo.dists, |
138 |
| - [os.path.join("dist", "*.tar.gz"), os.path.join("dist", "*.whl")], |
139 |
| - ) |
140 |
| - |
141 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
142 |
| - @mock.patch( |
143 |
| - "semantic_release.repository.config.get", |
144 |
| - wrapped_config_get(repository="testpypi"), |
145 |
| - ) |
146 |
| - def test_repo_with_repo_name_testpypi(self, mock_handle_creds): |
147 |
| - repo = ArtifactRepo(Path("dist")) |
148 |
| - self.assertEqual(repo.repository_name, "testpypi") |
149 |
| - |
150 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
151 |
| - @mock.patch( |
152 |
| - "semantic_release.repository.config.get", |
153 |
| - wrapped_config_get(repository="invalid"), |
154 |
| - ) |
155 |
| - def test_raises_error_when_repo_name_invalid(self, mock_handle_creds): |
156 |
| - repo = ArtifactRepo(Path("dist")) |
157 |
| - with self.assertRaises( |
158 |
| - ImproperConfigurationError, msg="Upload to artifact repository has failed" |
159 |
| - ): |
160 |
| - repo.upload(noop=False, verbose=False, skip_existing=False) |
161 |
| - |
162 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
163 |
| - @mock.patch( |
164 |
| - "semantic_release.repository.config.get", |
165 |
| - wrapped_config_get(repository_url="https://custom-repo"), |
166 |
| - ) |
167 |
| - def test_repo_with_custom_repo_url(self, mock_handle_creds): |
168 |
| - repo = ArtifactRepo(Path("dist")) |
169 |
| - self.assertEqual(repo.repository_url, "https://custom-repo") |
170 |
| - |
171 |
| - @mock.patch("semantic_release.repository.twine_upload") |
172 |
| - @mock.patch( |
173 |
| - "semantic_release.repository.config.get", |
174 |
| - wrapped_config_get( |
175 |
| - dist_glob_patterns="*.tar.gz,*.whl", repository_url="https://custom-repo" |
176 |
| - ), |
177 |
| - ) |
178 |
| - @mock.patch.dict( |
179 |
| - "os.environ", |
180 |
| - { |
181 |
| - "REPOSITORY_USERNAME": "repo-username", |
182 |
| - "REPOSITORY_PASSWORD": "repo-password", |
183 |
| - }, |
184 |
| - ) |
185 |
| - def test_upload_with_custom_settings(self, mock_upload): |
186 |
| - repo = ArtifactRepo(Path("custom-dist")) |
187 |
| - repo.upload( |
188 |
| - noop=False, verbose=True, skip_existing=True, comment="distribution comment" |
189 |
| - ) |
190 |
| - settings = mock_upload.call_args[1]["upload_settings"] |
191 |
| - self.assertIsInstance(settings.auth, auth.Private) |
192 |
| - self.assertEqual(settings.comment, "distribution comment") |
193 |
| - self.assertEqual(settings.username, "repo-username") |
194 |
| - self.assertEqual(settings.password, "repo-password") |
195 |
| - self.assertEqual( |
196 |
| - settings.repository_config["repository"], "https://custom-repo" |
197 |
| - ) |
198 |
| - self.assertTrue(settings.skip_existing) |
199 |
| - self.assertTrue(settings.verbose) |
200 |
| - self.assertTrue(mock_upload.called) |
201 |
| - |
202 |
| - @mock.patch.object(ArtifactRepo, "_handle_credentials_init") |
203 |
| - @mock.patch("semantic_release.repository.twine_upload") |
204 |
| - def test_upload_with_noop(self, mock_upload, mock_handle_creds): |
205 |
| - ArtifactRepo(Path("dist")).upload(noop=True, verbose=False, skip_existing=False) |
206 |
| - self.assertFalse(mock_upload.called) |
| 205 | + |
| 206 | +@mock.patch.dict("os.environ", {"PYPI_PASSWORD": "pypi-password"}) |
| 207 | +def test_repo_with_pypi_password_only(caplog): |
| 208 | + repo = ArtifactRepo(Path("dist")) |
| 209 | + assert "Providing only password or token without username is deprecated" in caplog.messages |
| 210 | + assert repo.username == "__token__" |
| 211 | + assert repo.password == "pypi-password" |
0 commit comments