-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_repo.py
376 lines (231 loc) · 11.3 KB
/
test_repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from __future__ import annotations
from pathlib import Path
from typing import Any
import pytest
from freezegun import freeze_time
from pytest_httpx import HTTPXMock
from pytest_mock import MockerFixture
from pytoil.api import API
from pytoil.config import Config
from pytoil.environments import Conda, Flit, Poetry, Requirements, Venv
from pytoil.repo import Repo
PROJECT_ROOT = Path(__file__).parent.parent.resolve()
def test_clone_url() -> None:
repo = Repo(owner="me", name="project", local_path=Path("doesn't/matter"))
assert repo.clone_url == "https://github.com/me/project.git"
def test_html_url() -> None:
repo = Repo(owner="me", name="project", local_path=Path("doesn't/matter"))
assert repo.html_url == "https://github.com/me/project"
def test_issues_url() -> None:
repo = Repo(owner="me", name="project", local_path=Path("doesn't/matter"))
assert repo.issues_url == "https://github.com/me/project/issues"
def test_pulls_url() -> None:
repo = Repo(owner="me", name="project", local_path=Path("doesn't/matter"))
assert repo.pulls_url == "https://github.com/me/project/pulls"
def test_repo_repr() -> None:
repo = Repo(owner="me", name="project", local_path=Path("somewhere"))
assert repr(repo) == f"Repo(owner='me', name='project', local_path={Path('somewhere')!r})"
@pytest.mark.parametrize(
("path", "exists"),
[
(PROJECT_ROOT, True), # This project should hopefully exist!
(Path("not/here"), False),
],
)
def test_exists_local(path: Path, exists: bool) -> None:
repo = Repo(owner="me", name="test", local_path=path)
assert repo.exists_local() is exists
def test_exists_remote_returns_false_when_remote_doesnt_exist(
httpx_mock: HTTPXMock, fake_repo_exists_false_response: dict[str, Any]
) -> None:
api = API(username="me", token="something")
repo = Repo(owner="me", name="test", local_path=Path("doesn't/matter"))
httpx_mock.add_response(url=api.url, json=fake_repo_exists_false_response, status_code=200)
result = repo.exists_remote(api=api)
assert result is False
def test_exists_remote_returns_true_when_remote_exists(
httpx_mock: HTTPXMock, fake_repo_exists_true_response: dict[str, Any]
) -> None:
api = API(username="me", token="something")
repo = Repo(owner="me", name="test", local_path=Path("doesn't/matter"))
httpx_mock.add_response(url=api.url, json=fake_repo_exists_true_response, status_code=200)
result = repo.exists_remote(api=api)
assert result is True
@freeze_time("2022-01-22 09:00")
def test_remote_info_returns_correct_details(httpx_mock: HTTPXMock, fake_repo_info_response: dict[str, Any]) -> None:
api = API(username="me", token="something")
repo = Repo(owner="me", name="test", local_path=Path("somewhere"))
httpx_mock.add_response(url=api.url, json=fake_repo_info_response, status_code=200)
result = repo._remote_info(api)
assert result == {
"Created": "11 months ago",
"Description": "CLI to automate the development workflow :robot:",
"License": "Apache License 2.0",
"Name": "pytoil",
"Remote": True,
"Size": "3.2 MB",
"Updated": "25 days ago",
"Language": "Python",
}
@pytest.mark.parametrize(
("file", "exists"),
[
("here.txt", True),
("i_exist.yml", True),
("hello.py", True),
("me_too.json", True),
("not_me_though.csv", False),
("me_neither.toml", False),
("i_never_existed.cfg", False),
("what_file.ini", False),
],
)
def test_does_file_exist(
repo_folder_with_random_existing_files: Path,
file: str,
exists: bool,
) -> None:
repo = Repo(owner="me", name="test", local_path=repo_folder_with_random_existing_files)
assert repo._file_exists(file) is exists
@pytest.mark.parametrize(
("file", "expect"),
[
("setup.cfg", True),
("setup.py", True),
("dingle.cfg", False),
("dingle.py", False),
("pyproject.toml", False), # Empty pyproject.toml
("environment.yml", False),
],
)
def test_is_setuptools(repo_folder_with_random_existing_files: Path, file: str, expect: bool) -> None:
repo = Repo(owner="me", name="test", local_path=repo_folder_with_random_existing_files)
# Add in the required file to trigger
repo_folder_with_random_existing_files.joinpath(file).touch()
assert repo.is_setuptools() is expect
def test_is_setuptools_pep621(project_with_setuptools_pep621_backend: Path) -> None:
repo = Repo(owner="me", name="test", local_path=project_with_setuptools_pep621_backend)
assert repo.is_setuptools() is True
@pytest.mark.parametrize(
("file", "expect"),
[
("setup.cfg", False),
("setup.py", False),
("dingle.cfg", False),
("dingle.py", False),
("pyproject.toml", False),
("environment.yml", True),
],
)
def test_is_conda(repo_folder_with_random_existing_files: Path, file: str, expect: bool) -> None:
repo = Repo(owner="me", name="test", local_path=repo_folder_with_random_existing_files)
# Add in the required file to trigger
repo_folder_with_random_existing_files.joinpath(file).touch()
assert repo.is_conda() is expect
def test_is_poetry_true_on_valid_poetry_project(fake_poetry_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_poetry_project)
assert repo.is_poetry() is True
def test_is_poetry_false_on_non_poetry_project(fake_flit_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_flit_project)
assert repo.is_poetry() is False
def test_is_poetry_false_if_no_pyproject_toml() -> None:
repo = Repo(owner="blah", name="test", local_path=Path("nowhere"))
assert repo.is_poetry() is False
def test_is_poetry_false_if_no_build_system(project_with_no_build_system: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_system)
assert repo.is_poetry() is False
def test_is_poetry_false_if_no_build_backend(
project_with_no_build_backend: Path,
) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_backend)
assert repo.is_poetry() is False
def test_is_flit_true_on_valid_flit_project(fake_flit_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_flit_project)
assert repo.is_flit() is True
def test_is_flit_false_on_non_flit_project(fake_poetry_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_poetry_project)
assert repo.is_flit() is False
def test_is_flit_false_if_no_pyproject_toml() -> None:
repo = Repo(owner="blah", name="test", local_path=Path("nowhere"))
assert repo.is_flit() is False
def test_is_flit_false_if_no_build_system(project_with_no_build_system: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_system)
assert repo.is_flit() is False
def test_is_flit_false_if_no_build_backend(project_with_no_build_backend: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_backend)
assert repo.is_flit() is False
def test_is_hatch_true_on_valid_hatch_project(fake_hatch_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_hatch_project)
assert repo.is_hatch() is True
def test_is_hatch_false_on_non_hatch_project(fake_poetry_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_poetry_project)
assert repo.is_hatch() is False
def test_is_hatch_false_if_no_pyproject_toml() -> None:
repo = Repo(owner="blah", name="test", local_path=Path("nowhere"))
assert repo.is_hatch() is False
def test_is_hatch_false_if_no_build_system(project_with_no_build_system: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_system)
assert repo.is_hatch() is False
def test_is_hatch_false_if_no_build_backend(
project_with_no_build_backend: Path,
) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_backend)
assert repo.is_hatch() is False
def test_is_pep621_true_on_valid_pep621_project(fake_pep621_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_pep621_project)
assert repo.is_pep621() is True
def test_is_pep621_false_on_non_pep621_project(fake_poetry_project: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=fake_poetry_project)
assert repo.is_pep621() is False
def test_is_pep621_false_if_no_pyproject_toml() -> None:
repo = Repo(owner="blah", name="test", local_path=Path("nowhere"))
assert repo.is_pep621() is False
def test_is_pep621_false_if_no_build_system(project_with_no_build_system: Path) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_system)
assert repo.is_pep621() is False
def test_is_pep621_false_if_no_build_backend(
project_with_no_build_backend: Path,
) -> None:
repo = Repo(owner="blah", name="test", local_path=project_with_no_build_backend)
assert repo.is_pep621() is False
def test_dispatch_env_correctly_identifies_conda(mocker: MockerFixture) -> None:
mocker.patch("pytoil.repo.Repo.is_conda", autospec=True, return_value=True)
repo = Repo(name="test", owner="me", local_path=Path("somewhere"))
env = repo.dispatch_env(config=Config())
assert isinstance(env, Conda)
def test_dispatch_env_correctly_identifies_requirements_txt(
requirements_project: Path,
) -> None:
repo = Repo(name="test", owner="me", local_path=requirements_project)
env = repo.dispatch_env(config=Config())
assert isinstance(env, Requirements)
def test_dispatch_env_correctly_identifies_requirements_dev_txt(
requirements_dev_project: Path,
) -> None:
repo = Repo(name="test", owner="me", local_path=requirements_dev_project)
env = repo.dispatch_env(config=Config())
assert isinstance(env, Requirements)
def test_dispatch_env_correctly_identifies_setuptools(mocker: MockerFixture) -> None:
mocker.patch("pytoil.repo.Repo.is_conda", autospec=True, return_value=False)
mocker.patch("pytoil.repo.Repo.is_setuptools", autospec=True, return_value=True)
repo = Repo(name="test", owner="me", local_path=Path("somewhere"))
env = repo.dispatch_env(config=Config())
assert isinstance(env, Venv)
def test_dispatch_env_correctly_identifies_poetry(fake_poetry_project: Path) -> None:
repo = Repo(name="test", owner="me", local_path=fake_poetry_project)
env = repo.dispatch_env(config=Config())
assert isinstance(env, Poetry)
def test_dispatch_env_correctly_identifies_flit(fake_flit_project: Path) -> None:
repo = Repo(name="test", owner="me", local_path=fake_flit_project)
env = repo.dispatch_env(config=Config())
assert isinstance(env, Flit)
def test_dispatch_env_correctly_identifies_pep621(fake_pep621_project: Path) -> None:
repo = Repo(name="test", owner="me", local_path=fake_pep621_project)
env = repo.dispatch_env(config=Config())
assert isinstance(env, Venv)
def test_dispatch_env_returns_none_if_it_cant_detect(mocker: MockerFixture) -> None:
mocker.patch("pytoil.repo.Repo.is_conda", autospec=True, return_value=False)
mocker.patch("pytoil.repo.Repo.is_setuptools", autospec=True, return_value=False)
repo = Repo(name="test", owner="me", local_path=Path("somewhere"))
env = repo.dispatch_env(config=Config())
assert env is None