3
3
import py
4
4
import apipkg
5
5
import subprocess
6
+ import pytest
6
7
#
7
8
# test support for importing modules
8
9
#
9
- ModuleType = py .std .types .ModuleType
10
+ ModuleType = types .ModuleType
11
+
10
12
11
13
class TestRealModule :
12
14
@@ -61,7 +63,7 @@ def test_realmodule(self):
61
63
62
64
def test_realmodule_repr (self ):
63
65
import realtest .x
64
- assert "<ApiModule 'realtest.x'>" == repr (realtest .x )
66
+ assert "<ApiModule 'realtest.x'>" == repr (realtest .x )
65
67
66
68
def test_realmodule_from (self ):
67
69
from realtest .x import module
@@ -86,6 +88,7 @@ def test_realmodule___doc__(self):
86
88
print (realtest .x .module .__map__ )
87
89
assert realtest .x .module .__doc__ == 'test module'
88
90
91
+
89
92
class TestScenarios :
90
93
def test_relative_import (self , monkeypatch , tmpdir ):
91
94
pkgdir = tmpdir .mkdir ("mymodule" )
@@ -150,9 +153,9 @@ def test_from_module_alias_import(self, monkeypatch, tmpdir):
150
153
from fromaliasimport .some import join
151
154
assert join is py .std .os .path .join
152
155
156
+
153
157
def xtest_nested_absolute_imports ():
154
- import email
155
- api_email = apipkg .ApiModule ('email' ,{
158
+ apipkg .ApiModule ('email' , {
156
159
'message2' : {
157
160
'Message' : 'email.message:Message' ,
158
161
},
@@ -162,6 +165,8 @@ def xtest_nested_absolute_imports():
162
165
163
166
# alternate ideas for specifying package + preliminary code
164
167
#
168
+
169
+
165
170
def test_parsenamespace ():
166
171
spec = """
167
172
path.local __.path.local::LocalPath
@@ -170,10 +175,14 @@ def test_parsenamespace():
170
175
"""
171
176
d = parsenamespace (spec )
172
177
print (d )
173
- assert d == {'test' : {'raises' : '__.test.outcome::raises' },
174
- 'path' : {'svnwc' : '__.path.svnwc::WCCommandPath' ,
175
- 'local' : '__.path.local::LocalPath' }
176
- }
178
+ assert d == {
179
+ 'test' : {'raises' : '__.test.outcome::raises' },
180
+ 'path' : {
181
+ 'svnwc' : '__.path.svnwc::WCCommandPath' ,
182
+ 'local' : '__.path.local::LocalPath' }
183
+ }
184
+
185
+
177
186
def xtest_parsenamespace_errors ():
178
187
py .test .raises (ValueError , """
179
188
parsenamespace('path.local xyz')
@@ -182,6 +191,7 @@ def xtest_parsenamespace_errors():
182
191
parsenamespace('x y z')
183
192
""" )
184
193
194
+
185
195
def parsenamespace (spec ):
186
196
ns = {}
187
197
for line in spec .split ("\n " ):
@@ -190,10 +200,10 @@ def parsenamespace(spec):
190
200
continue
191
201
parts = [x .strip () for x in line .split ()]
192
202
if len (parts ) != 2 :
193
- raise ValueError ("Wrong format: %r" % (line ,))
203
+ raise ValueError ("Wrong format: %r" % (line ,))
194
204
apiname , spec = parts
195
205
if not spec .startswith ("__" ):
196
- raise ValueError ("%r does not start with __" % (spec ,))
206
+ raise ValueError ("%r does not start with __" % (spec ,))
197
207
apinames = apiname .split ("." )
198
208
cur = ns
199
209
for name in apinames [:- 1 ]:
@@ -202,6 +212,7 @@ def parsenamespace(spec):
202
212
cur [apinames [- 1 ]] = spec
203
213
return ns
204
214
215
+
205
216
def test_initpkg_replaces_sysmodules (monkeypatch ):
206
217
mod = ModuleType ('hello' )
207
218
monkeypatch .setitem (sys .modules , 'hello' , mod )
@@ -210,6 +221,7 @@ def test_initpkg_replaces_sysmodules(monkeypatch):
210
221
assert newmod != mod
211
222
assert newmod .x == py .std .os .path .abspath
212
223
224
+
213
225
def test_initpkg_transfers_attrs (monkeypatch ):
214
226
mod = ModuleType ('hello' )
215
227
mod .__version__ = 10
@@ -225,6 +237,7 @@ def test_initpkg_transfers_attrs(monkeypatch):
225
237
assert newmod .__loader__ == mod .__loader__
226
238
assert newmod .__doc__ == mod .__doc__
227
239
240
+
228
241
def test_initpkg_nodoc (monkeypatch ):
229
242
mod = ModuleType ('hello' )
230
243
mod .__file__ = "hello.py"
@@ -233,6 +246,7 @@ def test_initpkg_nodoc(monkeypatch):
233
246
newmod = sys .modules ['hello' ]
234
247
assert not newmod .__doc__
235
248
249
+
236
250
def test_initpkg_overwrite_doc (monkeypatch ):
237
251
hello = ModuleType ('hello' )
238
252
hello .__doc__ = "this is the documentation"
@@ -242,6 +256,7 @@ def test_initpkg_overwrite_doc(monkeypatch):
242
256
assert newhello != hello
243
257
assert newhello .__doc__ == sys .__doc__
244
258
259
+
245
260
def test_initpkg_not_transfers_not_existing_attrs (monkeypatch ):
246
261
mod = ModuleType ('hello' )
247
262
mod .__file__ = "hello.py"
@@ -273,16 +288,18 @@ def test_initpkg_defaults(monkeypatch):
273
288
monkeypatch .setitem (sys .modules , 'hello' , mod )
274
289
apipkg .initpkg ('hello' , {})
275
290
newmod = sys .modules ['hello' ]
276
- assert newmod .__file__ == None
291
+ assert newmod .__file__ is None
277
292
assert not hasattr (newmod , '__version__' )
278
293
294
+
279
295
def test_name_attribute ():
280
296
api = apipkg .ApiModule ('name_test' , {
281
297
'subpkg' : {},
282
298
})
283
299
assert api .__name__ == 'name_test'
284
300
assert api .subpkg .__name__ == 'name_test.subpkg'
285
301
302
+
286
303
def test_error_loading_one_element (monkeypatch , tmpdir ):
287
304
pkgdir = tmpdir .mkdir ("errorloading1" )
288
305
pkgdir .join ('__init__.py' ).write (py .code .Source ("""
@@ -301,6 +318,7 @@ def test_error_loading_one_element(monkeypatch, tmpdir):
301
318
py .test .raises (ImportError , 'errorloading1.x' )
302
319
py .test .raises (ImportError , 'errorloading1.x' )
303
320
321
+
304
322
def test_onfirstaccess (tmpdir , monkeypatch ):
305
323
pkgdir = tmpdir .mkdir ("firstaccess" )
306
324
pkgdir .join ('__init__.py' ).write (py .code .Source ("""
@@ -323,7 +341,8 @@ def init():
323
341
assert len (firstaccess .l ) == 1
324
342
assert '__onfirstaccess__' not in firstaccess .__all__
325
343
326
- @py .test .mark .multi (mode = ['attr' , 'dict' , 'onfirst' ])
344
+
345
+ @pytest .mark .parametrize ('mode' , ['attr' , 'dict' , 'onfirst' ])
327
346
def test_onfirstaccess_setsnewattr (tmpdir , monkeypatch , mode ):
328
347
pkgname = 'mode_' + mode
329
348
pkgdir = tmpdir .mkdir (pkgname )
@@ -352,6 +371,7 @@ def init():
352
371
assert not hasattr (mod , '__onfirstaccess__' )
353
372
assert '__onfirstaccess__' not in vars (mod )
354
373
374
+
355
375
def test_bpython_getattr_override (tmpdir , monkeypatch ):
356
376
def patchgetattr (self , name ):
357
377
raise AttributeError (name )
@@ -363,12 +383,10 @@ def patchgetattr(self, name):
363
383
assert 'abspath' in d
364
384
365
385
366
-
367
-
368
386
def test_chdir_with_relative_imports_shouldnt_break_lazy_loading (tmpdir ):
369
387
tmpdir .join ('apipkg.py' ).write (py .code .Source (apipkg ))
370
388
pkg = tmpdir .mkdir ('pkg' )
371
- messy = tmpdir .mkdir ('messy' )
389
+ tmpdir .mkdir ('messy' )
372
390
pkg .join ('__init__.py' ).write (py .code .Source ("""
373
391
import apipkg
374
392
apipkg.initpkg(__name__, {
@@ -408,6 +426,7 @@ def test_dotted_name_lookup(tmpdir, monkeypatch):
408
426
import dotted_name_lookup
409
427
assert dotted_name_lookup .abs == py .std .os .path .abspath
410
428
429
+
411
430
def test_extra_attributes (tmpdir , monkeypatch ):
412
431
pkgdir = tmpdir .mkdir ("extra_attributes" )
413
432
pkgdir .join ('__init__.py' ).write (py .code .Source ("""
@@ -418,30 +437,35 @@ def test_extra_attributes(tmpdir, monkeypatch):
418
437
import extra_attributes
419
438
assert extra_attributes .foo == 'bar'
420
439
440
+
421
441
def test_aliasmodule_aliases_an_attribute ():
422
442
am = apipkg .AliasModule ("mymod" , "pprint" , 'PrettyPrinter' )
423
443
r = repr (am )
424
444
assert "<AliasModule 'mymod' for 'pprint.PrettyPrinter'>" == r
425
445
assert am .format
426
446
assert not hasattr (am , "lqkje" )
427
447
448
+
428
449
def test_aliasmodule_aliases_unimportable ():
429
450
am = apipkg .AliasModule ("mymod" , "qlwkejqlwe" , 'main' )
430
451
r = repr (am )
431
452
assert "<AliasModule 'mymod' for 'qlwkejqlwe.main'>" == r
432
453
assert am .qwe is None
433
454
455
+
434
456
def test_aliasmodule_unicode ():
435
457
am = apipkg .AliasModule (py .builtin ._totext ("mymod" ), "pprint" )
436
458
assert am
437
459
460
+
438
461
def test_aliasmodule_repr ():
439
462
am = apipkg .AliasModule ("mymod" , "sys" )
440
463
r = repr (am )
441
464
assert "<AliasModule 'mymod' for 'sys'>" == r
442
465
am .version
443
466
assert repr (am ) == r
444
467
468
+
445
469
def test_aliasmodule_proxy_methods (tmpdir , monkeypatch ):
446
470
pkgdir = tmpdir
447
471
pkgdir .join ('aliasmodule_proxy.py' ).write (py .code .Source ("""
@@ -470,6 +494,7 @@ def doit():
470
494
proxy .doit = doit
471
495
assert orig .doit is doit
472
496
497
+
473
498
def test_aliasmodule_nested_import_with_from (tmpdir , monkeypatch ):
474
499
import os
475
500
pkgdir = tmpdir .mkdir ("api1" )
0 commit comments