33import ast
44import logging
55import os .path
6+ import textwrap
67from pathlib import Path
78
89import pytest
@@ -94,41 +95,38 @@ def test_pyfiles_package(monkeypatch):
9495 (True , False , ['ast' ], [('spam.py' , 2 )]),
9596 (True , True , ['ast' ], [('spam.py' , 2 )]),
9697])
97- def test_find_imported_modules (monkeypatch , caplog , ignore_ham , ignore_hashlib ,
98- expect , locs ):
99- monkeypatch .setattr (common , 'pyfiles' ,
100- pretend .call_recorder (lambda x : ['spam.py' , 'ham.py' ]))
101-
102- class FakeFile ():
103- contents = [
104- 'from os import path\n import ast, hashlib' ,
105- 'from __future__ import braces\n import ast, sys\n '
106- 'from . import friend' ,
107- ]
108-
109- def __init__ (self , filename , encoding = None ):
110- pass
111-
112- def read (self ):
113- return self .contents .pop ()
114-
115- def __enter__ (self ):
116- return self
117-
118- def __exit__ (self , * args ):
119- pass
98+ def test_find_imported_modules (caplog , ignore_ham , ignore_hashlib ,
99+ expect , locs , tmp_path ):
100+ root = tmp_path
101+ spam = root / "spam.py"
102+ ham = root / "ham.py"
103+
104+ spam_file_contents = textwrap .dedent (
105+ """\
106+ from __future__ import braces
107+ import ast, sys
108+ from . import friend
109+ """ ,
110+ )
111+ ham_file_contents = textwrap .dedent (
112+ """\
113+ from os import path
114+ import ast, hashlib
115+ """ ,
116+ )
120117
121- monkeypatch .setattr (common , 'open' , FakeFile , raising = False )
118+ spam .write_text (data = spam_file_contents )
119+ ham .write_text (data = ham_file_contents )
122120
123121 caplog .set_level (logging .INFO )
124122
125123 class options :
126- paths = ['dummy' ]
124+ paths = [str ( root ) ]
127125 verbose = True
128126
129127 @staticmethod
130128 def ignore_files (path ):
131- if path == 'ham.py' and ignore_ham :
129+ if Path ( path ). name == 'ham.py' and ignore_ham :
132130 return True
133131 return False
134132
@@ -140,10 +138,15 @@ def ignore_mods(module):
140138
141139 result = common .find_imported_modules (options )
142140 assert set (result ) == set (expect )
143- assert result ['ast' ].locations == locs
141+ absolute_locations = result ['ast' ].locations
142+ relative_locations = [
143+ (str (Path (item [0 ]).relative_to (root )), item [1 ])
144+ for item in absolute_locations
145+ ]
146+ assert sorted (relative_locations ) == sorted (locs )
144147
145148 if ignore_ham :
146- assert caplog .records [0 ].message == 'ignoring: ham.py '
149+ assert caplog .records [0 ].message == f 'ignoring: { os . path . relpath ( ham ) } '
147150
148151
149152@pytest .mark .parametrize (["ignore_cfg" , "candidate" , "result" ], [
@@ -163,7 +166,7 @@ def test_ignorer(monkeypatch, tmp_path: Path, ignore_cfg, candidate, result):
163166 assert ignorer (candidate ) == result
164167
165168
166- def test_find_required_modules (monkeypatch , tmp_path : Path ):
169+ def test_find_required_modules (tmp_path : Path ):
167170 class options :
168171 skip_incompatible = False
169172
@@ -179,7 +182,7 @@ class options:
179182 assert reqs == set (['foobar' ])
180183
181184
182- def test_find_required_modules_env_markers (monkeypatch , tmp_path ):
185+ def test_find_required_modules_env_markers (tmp_path ):
183186 class options :
184187 skip_incompatible = True
185188
0 commit comments