Skip to content

Commit 06528fb

Browse files
committed
tests: refactor test_pkgcheck_scan & REPO_ROOT
- use `Path` object for REPO_ROOT - various improvements and cleanups around test_pkgcheck_scan Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent 5dec066 commit 06528fb

4 files changed

Lines changed: 92 additions & 103 deletions

File tree

tests/checks/test_network.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import glob
21
import importlib.util
32
import os
43
import socket
@@ -15,31 +14,31 @@
1514
HomepageUrlCheck)
1615
from pkgcheck.packages import RawCPV
1716
from snakeoil.formatters import PlainTextFormatter
18-
from snakeoil.osutils import pjoin
1917

2018
# skip module tests if requests isn't available
2119
requests = pytest.importorskip('requests')
2220

2321

2422
class TestNetworkChecks:
2523

26-
repos_data = pjoin(pytest.REPO_ROOT, 'testdata', 'data', 'repos')
27-
repos_dir = pjoin(pytest.REPO_ROOT, 'testdata', 'repos')
24+
repos_data = pytest.REPO_ROOT / 'testdata/data/repos'
25+
repos_dir = pytest.REPO_ROOT / 'testdata/repos'
2826

2927
@pytest.fixture(autouse=True)
3028
def _setup(self, testconfig, tmp_path):
3129
base_args = ['--config', testconfig]
3230
self.scan = partial(scan, base_args=base_args)
3331
self.scan_args = [
3432
'--config', 'no', '--cache-dir', str(tmp_path), '--net',
35-
'-r', pjoin(self.repos_dir, 'network'),
33+
'-r', str(self.repos_dir / 'network'),
3634
]
3735

38-
_net_results = []
39-
for name, cls in sorted(objects.CHECKS.items()):
40-
if issubclass(cls, NetworkCheck):
41-
for result in sorted(cls.known_results, key=attrgetter('__name__')):
42-
_net_results.append((cls, result))
36+
_net_results = [
37+
(cls, result)
38+
for _name, cls in sorted(objects.CHECKS.items())
39+
if issubclass(cls, NetworkCheck)
40+
for result in sorted(cls.known_results, key=attrgetter('__name__'))
41+
]
4342

4443
def _render_results(self, results, **kwargs):
4544
"""Render a given set of result objects into their related string form."""
@@ -55,18 +54,18 @@ def _render_results(self, results, **kwargs):
5554
def test_scan(self, check, result):
5655
check_name = check.__name__
5756
keyword = result.__name__
58-
result_dir = pjoin(self.repos_dir, 'network', check_name, keyword)
5957

60-
paths = glob.glob(f'{result_dir}*')
58+
result_dir = self.repos_dir / 'network' / check_name
59+
paths = tuple(result_dir.glob(keyword + '*'))
6160
if not paths:
6261
pytest.skip('data unavailable')
6362

6463
for path in paths:
6564
ebuild_name = os.path.basename(path)
66-
data_dir = pjoin(self.repos_data, 'network', check_name, ebuild_name)
65+
data_dir = self.repos_data / 'network' / check_name / ebuild_name
6766

6867
# load response data to fake
69-
module_path = pjoin(path, 'responses.py')
68+
module_path = path / 'responses.py'
7069
spec = importlib.util.spec_from_file_location('responses_mod', module_path)
7170
responses_mod = importlib.util.module_from_spec(spec)
7271
spec.loader.exec_module(responses_mod)
@@ -78,7 +77,7 @@ def test_scan(self, check, result):
7877

7978
# load expected results if they exist
8079
try:
81-
with open(pjoin(data_dir, 'expected.json')) as f:
80+
with (data_dir / 'expected.json').open() as f:
8281
expected_results = set(reporters.JsonStream.from_iter(f))
8382
except FileNotFoundError:
8483
# check stopped before making request or completed successfully
@@ -94,8 +93,10 @@ def test_scan(self, check, result):
9493
error.append(f'got:\n{rendered_results}')
9594
pytest.fail('\n'.join(error))
9695

97-
@pytest.mark.parametrize(
98-
'check, result', ((HomepageUrlCheck, DeadUrl), (FetchablesUrlCheck, DeadUrl)))
96+
@pytest.mark.parametrize('check, result', (
97+
(HomepageUrlCheck, DeadUrl),
98+
(FetchablesUrlCheck, DeadUrl),
99+
))
99100
def test_scan_ftp(self, check, result):
100101
check_name = check.__name__
101102
keyword = result.__name__
@@ -121,6 +122,5 @@ def test_scan_ftp(self, check, result):
121122
if side_effect is None:
122123
assert not results
123124
else:
124-
assert len(results) == 1
125-
assert results[0] == expected_result
125+
assert results == [expected_result]
126126
assert self._render_results(results), 'failed rendering results'

tests/conftest.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33
import textwrap
44
from contextlib import ExitStack
5+
from pathlib import Path
56
from unittest.mock import patch
67

78
import pytest
@@ -15,7 +16,7 @@
1516
from snakeoil.osutils import pjoin
1617

1718
pytest_plugins = ['pkgcore']
18-
REPO_ROOT = os.path.dirname(os.path.dirname(__file__))
19+
REPO_ROOT = Path(__file__).parent.parent
1920

2021

2122
def pytest_configure():
@@ -57,17 +58,17 @@ def testconfig(tmp_path_factory):
5758
config = tmp_path_factory.mktemp('testconfig')
5859
repos_conf = config / 'repos.conf'
5960
stubrepo = pjoin(pkgcore_const.DATA_PATH, 'stubrepo')
60-
testdir = pjoin(REPO_ROOT, 'testdata', 'repos')
61+
testdir = REPO_ROOT / 'testdata/repos'
6162
with open(repos_conf, 'w') as f:
6263
f.write(textwrap.dedent(f"""\
6364
[DEFAULT]
6465
main-repo = standalone
6566
[stubrepo]
6667
location = {stubrepo}
6768
"""))
68-
for repo in os.listdir(testdir):
69-
f.write(f'[{repo}]\n')
70-
f.write(f'location = {pjoin(testdir, repo)}\n')
69+
for repo in testdir.iterdir():
70+
f.write(f'[{repo.name}]\n')
71+
f.write(f'location = {repo}\n')
7172
profile_path = pjoin(stubrepo, 'profiles', 'default')
7273
os.symlink(profile_path, str(config / 'make.profile'))
7374
return str(config)
@@ -83,13 +84,11 @@ def cache_dir(tmp_path_factory):
8384
@pytest.fixture
8485
def fakerepo(tmp_path_factory):
8586
"""Generate a stub repo."""
86-
fakerepo = str(tmp_path_factory.mktemp('fakerepo'))
87-
os.makedirs(pjoin(fakerepo, 'profiles'))
88-
os.makedirs(pjoin(fakerepo, 'metadata'))
89-
with open(pjoin(fakerepo, 'profiles', 'repo_name'), 'w') as f:
90-
f.write('fakerepo\n')
91-
with open(pjoin(fakerepo, 'metadata', 'layout.conf'), 'w') as f:
92-
f.write('masters =\n')
87+
fakerepo = tmp_path_factory.mktemp('fakerepo')
88+
(profiles := fakerepo / 'profiles').mkdir(parents=True)
89+
(profiles / 'repo_name').write_text('fakerepo\n')
90+
(metadata := fakerepo / 'metadata').mkdir(parents=True)
91+
(metadata / 'layout.conf').write_text('masters =\n')
9392
return fakerepo
9493

9594

tests/scripts/test_pkgcheck_replay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_invalid_file(self, capsys):
7373
assert excinfo.value.code == 2
7474

7575
def test_replay_pipe_stdin(self):
76-
script = os.path.join(pytest.REPO_ROOT, 'bin', 'pkgcheck')
76+
script = pytest.REPO_ROOT / 'bin/pkgcheck'
7777
result = ProfileWarning('profile warning: foo')
7878
with tempfile.NamedTemporaryFile() as f:
7979
out = PlainTextFormatter(f)

0 commit comments

Comments
 (0)