1- import glob
21import importlib .util
32import os
43import socket
1514 HomepageUrlCheck )
1615from pkgcheck .packages import RawCPV
1716from snakeoil .formatters import PlainTextFormatter
18- from snakeoil .osutils import pjoin
1917
2018# skip module tests if requests isn't available
2119requests = pytest .importorskip ('requests' )
2220
2321
2422class 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'
0 commit comments