3
3
import ast
4
4
import logging
5
5
import os .path
6
+ import textwrap
6
7
from pathlib import Path
7
8
8
9
import pytest
@@ -94,41 +95,38 @@ def test_pyfiles_package(monkeypatch):
94
95
(True , False , ['ast' ], [('spam.py' , 2 )]),
95
96
(True , True , ['ast' ], [('spam.py' , 2 )]),
96
97
])
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
+ )
120
117
121
- monkeypatch .setattr (common , 'open' , FakeFile , raising = False )
118
+ spam .write_text (data = spam_file_contents )
119
+ ham .write_text (data = ham_file_contents )
122
120
123
121
caplog .set_level (logging .INFO )
124
122
125
123
class options :
126
- paths = ['dummy' ]
124
+ paths = [str ( root ) ]
127
125
verbose = True
128
126
129
127
@staticmethod
130
128
def ignore_files (path ):
131
- if path == 'ham.py' and ignore_ham :
129
+ if Path ( path ). name == 'ham.py' and ignore_ham :
132
130
return True
133
131
return False
134
132
@@ -140,10 +138,15 @@ def ignore_mods(module):
140
138
141
139
result = common .find_imported_modules (options )
142
140
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 )
144
147
145
148
if ignore_ham :
146
- assert caplog .records [0 ].message == 'ignoring: ham.py '
149
+ assert caplog .records [0 ].message == f 'ignoring: { os . path . relpath ( ham ) } '
147
150
148
151
149
152
@pytest .mark .parametrize (["ignore_cfg" , "candidate" , "result" ], [
@@ -163,7 +166,7 @@ def test_ignorer(monkeypatch, tmp_path: Path, ignore_cfg, candidate, result):
163
166
assert ignorer (candidate ) == result
164
167
165
168
166
- def test_find_required_modules (monkeypatch , tmp_path : Path ):
169
+ def test_find_required_modules (tmp_path : Path ):
167
170
class options :
168
171
skip_incompatible = False
169
172
@@ -179,7 +182,7 @@ class options:
179
182
assert reqs == set (['foobar' ])
180
183
181
184
182
- def test_find_required_modules_env_markers (monkeypatch , tmp_path ):
185
+ def test_find_required_modules_env_markers (tmp_path ):
183
186
class options :
184
187
skip_incompatible = True
185
188
0 commit comments