1
- import types
1
+ import os .path
2
+ import subprocess
2
3
import sys
3
- import py
4
+ import textwrap
5
+ import types
4
6
import apipkg
5
- import subprocess
6
7
import pytest
7
8
#
8
9
# test support for importing modules
13
14
class TestRealModule :
14
15
15
16
def setup_class (cls ):
16
- cls .tmpdir = py . test .ensuretemp ('test_apipkg' )
17
+ cls .tmpdir = pytest .ensuretemp ('test_apipkg' )
17
18
sys .path = [str (cls .tmpdir )] + sys .path
18
19
pkgdir = cls .tmpdir .ensure ('realtest' , dir = 1 )
19
20
20
21
tfile = pkgdir .join ('__init__.py' )
21
- tfile .write (py . code . Source ("""
22
+ tfile .write (textwrap . dedent ("""
22
23
import apipkg
23
24
apipkg.initpkg(__name__, {
24
25
'x': {
@@ -36,7 +37,7 @@ def setup_class(cls):
36
37
ipkgdir = cls .tmpdir .ensure ("_xyz" , dir = 1 )
37
38
tfile = ipkgdir .join ('testmodule.py' )
38
39
ipkgdir .ensure ("__init__.py" )
39
- tfile .write (py . code . Source ("""
40
+ tfile .write (textwrap . dedent ("""
40
41
'test module'
41
42
from _xyz.othermodule import MyTest
42
43
@@ -92,7 +93,7 @@ def test_realmodule___doc__(self):
92
93
class TestScenarios :
93
94
def test_relative_import (self , monkeypatch , tmpdir ):
94
95
pkgdir = tmpdir .mkdir ("mymodule" )
95
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
96
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
96
97
import apipkg
97
98
apipkg.initpkg(__name__, exportdefs={
98
99
'__doc__': '.submod:maindoc',
@@ -112,13 +113,13 @@ def test_relative_import(self, monkeypatch, tmpdir):
112
113
113
114
def test_recursive_import (self , monkeypatch , tmpdir ):
114
115
pkgdir = tmpdir .mkdir ("recmodule" )
115
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
116
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
116
117
import apipkg
117
118
apipkg.initpkg(__name__, exportdefs={
118
119
'some': '.submod:someclass',
119
120
})
120
121
""" ))
121
- pkgdir .join ('submod.py' ).write (py . code . Source ("""
122
+ pkgdir .join ('submod.py' ).write (textwrap . dedent ("""
122
123
import recmodule
123
124
class someclass: pass
124
125
print(recmodule.__dict__)
@@ -130,28 +131,28 @@ class someclass: pass
130
131
131
132
def test_module_alias_import (self , monkeypatch , tmpdir ):
132
133
pkgdir = tmpdir .mkdir ("aliasimport" )
133
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
134
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
134
135
import apipkg
135
136
apipkg.initpkg(__name__, exportdefs={
136
137
'some': 'os.path',
137
138
})
138
139
""" ))
139
140
monkeypatch .syspath_prepend (tmpdir )
140
141
import aliasimport
141
- for k , v in py . std . os .path .__dict__ .items ():
142
+ for k , v in os .path .__dict__ .items ():
142
143
assert getattr (aliasimport .some , k ) == v
143
144
144
145
def test_from_module_alias_import (self , monkeypatch , tmpdir ):
145
146
pkgdir = tmpdir .mkdir ("fromaliasimport" )
146
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
147
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
147
148
import apipkg
148
149
apipkg.initpkg(__name__, exportdefs={
149
150
'some': 'os.path',
150
151
})
151
152
""" ))
152
153
monkeypatch .syspath_prepend (tmpdir )
153
154
from fromaliasimport .some import join
154
- assert join is py . std . os .path .join
155
+ assert join is os .path .join
155
156
156
157
157
158
def xtest_nested_absolute_imports ():
@@ -184,10 +185,10 @@ def test_parsenamespace():
184
185
185
186
186
187
def xtest_parsenamespace_errors ():
187
- py . test .raises (ValueError , """
188
+ pytest .raises (ValueError , """
188
189
parsenamespace('path.local xyz')
189
190
""" )
190
- py . test .raises (ValueError , """
191
+ pytest .raises (ValueError , """
191
192
parsenamespace('x y z')
192
193
""" )
193
194
@@ -219,7 +220,7 @@ def test_initpkg_replaces_sysmodules(monkeypatch):
219
220
apipkg .initpkg ('hello' , {'x' : 'os.path:abspath' })
220
221
newmod = sys .modules ['hello' ]
221
222
assert newmod != mod
222
- assert newmod .x == py . std . os .path .abspath
223
+ assert newmod .x == os .path .abspath
223
224
224
225
225
226
def test_initpkg_transfers_attrs (monkeypatch ):
@@ -233,7 +234,7 @@ def test_initpkg_transfers_attrs(monkeypatch):
233
234
apipkg .initpkg ('hello' , {})
234
235
newmod = sys .modules ['hello' ]
235
236
assert newmod != mod
236
- assert newmod .__file__ == py .path .local (mod .__file__ )
237
+ assert newmod .__file__ == os .path .abspath (mod .__file__ )
237
238
assert newmod .__version__ == mod .__version__
238
239
assert newmod .__loader__ == mod .__loader__
239
240
assert newmod .__package__ == mod .__package__
@@ -268,7 +269,7 @@ def test_initpkg_not_transfers_not_existing_attrs(monkeypatch):
268
269
apipkg .initpkg ('hello' , {})
269
270
newmod = sys .modules ['hello' ]
270
271
assert newmod != mod
271
- assert newmod .__file__ == py .path .local (mod .__file__ )
272
+ assert newmod .__file__ == os .path .abspath (mod .__file__ )
272
273
assert not hasattr (newmod , '__path__' )
273
274
assert not hasattr (newmod , '__package__' ) or mod .__package__ is None
274
275
@@ -306,7 +307,7 @@ def test_name_attribute():
306
307
307
308
def test_error_loading_one_element (monkeypatch , tmpdir ):
308
309
pkgdir = tmpdir .mkdir ("errorloading1" )
309
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
310
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
310
311
import apipkg
311
312
apipkg.initpkg(__name__, exportdefs={
312
313
'x': '.notexists:x',
@@ -319,21 +320,21 @@ def test_error_loading_one_element(monkeypatch, tmpdir):
319
320
import errorloading1
320
321
assert isinstance (errorloading1 , apipkg .ApiModule )
321
322
assert errorloading1 .y == 0
322
- py . test .raises (ImportError , 'errorloading1.x' )
323
- py . test .raises (ImportError , 'errorloading1.x' )
323
+ pytest .raises (ImportError , 'errorloading1.x' )
324
+ pytest .raises (ImportError , 'errorloading1.x' )
324
325
325
326
326
327
def test_onfirstaccess (tmpdir , monkeypatch ):
327
328
pkgdir = tmpdir .mkdir ("firstaccess" )
328
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
329
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
329
330
import apipkg
330
331
apipkg.initpkg(__name__, exportdefs={
331
332
'__onfirstaccess__': '.submod:init',
332
333
'l': '.submod:l',
333
334
},
334
335
)
335
336
""" ))
336
- pkgdir .join ('submod.py' ).write (py . code . Source ("""
337
+ pkgdir .join ('submod.py' ).write (textwrap . dedent ("""
337
338
l = []
338
339
def init():
339
340
l.append(1)
@@ -350,14 +351,14 @@ def init():
350
351
def test_onfirstaccess_setsnewattr (tmpdir , monkeypatch , mode ):
351
352
pkgname = 'mode_' + mode
352
353
pkgdir = tmpdir .mkdir (pkgname )
353
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
354
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
354
355
import apipkg
355
356
apipkg.initpkg(__name__, exportdefs={
356
357
'__onfirstaccess__': '.submod:init',
357
358
},
358
359
)
359
360
""" ))
360
- pkgdir .join ('submod.py' ).write (py . code . Source ("""
361
+ pkgdir .join ('submod.py' ).write (textwrap . dedent ("""
361
362
def init():
362
363
import %s as pkg
363
364
pkg.newattr = 42
@@ -388,52 +389,51 @@ def patchgetattr(self, name):
388
389
389
390
390
391
def test_chdir_with_relative_imports_support_lazy_loading (tmpdir , monkeypatch ):
391
- monkeypatch .syspath_prepend (py .path .local ('src' ))
392
+ monkeypatch .syspath_prepend (os .path .abspath ('src' ))
392
393
pkg = tmpdir .mkdir ('pkg' )
393
394
tmpdir .mkdir ('messy' )
394
- pkg .join ('__init__.py' ).write (py . code . Source ("""
395
+ pkg .join ('__init__.py' ).write (textwrap . dedent ("""
395
396
import apipkg
396
397
apipkg.initpkg(__name__, {
397
398
'test': '.sub:test',
398
399
})
399
400
""" ))
400
401
pkg .join ('sub.py' ).write ('def test(): pass' )
401
402
402
- tmpdir .join ('main.py' ).write (py .code .Source ("""
403
+ tmpdir .join ('main.py' ).write (textwrap .dedent ("""
404
+ from __future__ import print_function
403
405
import os
404
406
import sys
405
407
sys.path.insert(0, '')
406
408
import pkg
407
- import py
408
- print(py.__file__)
409
- py.builtin.print_(pkg.__path__, file=sys.stderr)
410
- py.builtin.print_(pkg.__file__, file=sys.stderr)
411
- py.builtin.print_(pkg, file=sys.stderr)
409
+ print(pkg.__path__, file=sys.stderr)
410
+ print(pkg.__file__, file=sys.stderr)
411
+ print(pkg, file=sys.stderr)
412
412
os.chdir('messy')
413
413
pkg.test()
414
414
assert os.path.isabs(pkg.sub.__file__), pkg.sub.__file__
415
415
""" ))
416
416
res = subprocess .call (
417
- [py . std . sys .executable , 'main.py' ],
417
+ [sys .executable , 'main.py' ],
418
418
cwd = str (tmpdir ),
419
419
)
420
420
assert res == 0
421
421
422
422
423
423
def test_dotted_name_lookup (tmpdir , monkeypatch ):
424
424
pkgdir = tmpdir .mkdir ("dotted_name_lookup" )
425
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
425
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
426
426
import apipkg
427
427
apipkg.initpkg(__name__, dict(abs='os:path.abspath'))
428
428
""" ))
429
429
monkeypatch .syspath_prepend (tmpdir )
430
430
import dotted_name_lookup
431
- assert dotted_name_lookup .abs == py . std . os .path .abspath
431
+ assert dotted_name_lookup .abs == os .path .abspath
432
432
433
433
434
434
def test_extra_attributes (tmpdir , monkeypatch ):
435
435
pkgdir = tmpdir .mkdir ("extra_attributes" )
436
- pkgdir .join ('__init__.py' ).write (py . code . Source ("""
436
+ pkgdir .join ('__init__.py' ).write (textwrap . dedent ("""
437
437
import apipkg
438
438
apipkg.initpkg(__name__, dict(abs='os:path.abspath'), dict(foo='bar'))
439
439
""" ))
@@ -458,7 +458,7 @@ def test_aliasmodule_aliases_unimportable():
458
458
459
459
460
460
def test_aliasmodule_unicode ():
461
- am = apipkg .AliasModule (py . builtin . _totext ( "mymod" ) , "pprint" )
461
+ am = apipkg .AliasModule (u "mymod" , "pprint" )
462
462
assert am
463
463
464
464
@@ -472,12 +472,12 @@ def test_aliasmodule_repr():
472
472
473
473
def test_aliasmodule_proxy_methods (tmpdir , monkeypatch ):
474
474
pkgdir = tmpdir
475
- pkgdir .join ('aliasmodule_proxy.py' ).write (py . code . Source ("""
475
+ pkgdir .join ('aliasmodule_proxy.py' ).write (textwrap . dedent ("""
476
476
def doit():
477
477
return 42
478
478
""" ))
479
479
480
- pkgdir .join ('my_aliasmodule_proxy.py' ).write (py . code . Source ("""
480
+ pkgdir .join ('my_aliasmodule_proxy.py' ).write (textwrap . dedent ("""
481
481
import apipkg
482
482
apipkg.initpkg(__name__, dict(proxy='aliasmodule_proxy'))
483
483
@@ -493,7 +493,7 @@ def doit():
493
493
assert doit is orig .doit
494
494
495
495
del proxy .doit
496
- py . test .raises (AttributeError , "orig.doit" )
496
+ pytest .raises (AttributeError , "orig.doit" )
497
497
498
498
proxy .doit = doit
499
499
assert orig .doit is doit
@@ -502,14 +502,14 @@ def doit():
502
502
def test_aliasmodule_nested_import_with_from (tmpdir , monkeypatch ):
503
503
import os
504
504
pkgdir = tmpdir .mkdir ("api1" )
505
- pkgdir .ensure ("__init__.py" ).write (py . std . textwrap .dedent ("""
505
+ pkgdir .ensure ("__init__.py" ).write (textwrap .dedent ("""
506
506
import apipkg
507
507
apipkg.initpkg(__name__, {
508
508
'os2': 'api2',
509
509
'os2.path': 'api2.path2',
510
510
})
511
511
""" ))
512
- tmpdir .join ("api2.py" ).write (py . std . textwrap .dedent ("""
512
+ tmpdir .join ("api2.py" ).write (textwrap .dedent ("""
513
513
import os, sys
514
514
from os import path
515
515
sys.modules['api2.path2'] = path
0 commit comments