tests: use unimported modules in test-demandimport.py
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 01 Feb 2019 13:20:13 -0800
changeset 41521 30248d6bc057
parent 41520 3e89736b98ce
child 41522 b0865b5919c6
tests: use unimported modules in test-demandimport.py contextlib isn't a good module to test because it is likely already imported by code above. Let's use modules that shouldn't have been imported. And let's verify that with asserts. Differential Revision: https://phab.mercurial-scm.org/D5797
tests/test-demandimport.py
--- a/tests/test-demandimport.py	Fri Feb 01 16:47:29 2019 -0800
+++ b/tests/test-demandimport.py	Fri Feb 01 13:20:13 2019 -0800
@@ -194,20 +194,21 @@
     )
     assert f(re) == "<proxied module 'sys'>", f(re)
 
-import contextlib
+assert 'telnetlib' not in sys.modules
+import telnetlib
 
 if ispy3:
-    assert not isinstance(contextlib, _LazyModule)
-    assert f(contextlib) == "<module 'contextlib' from '?'>"
+    assert not isinstance(telnetlib, _LazyModule)
+    assert f(telnetlib) == "<module 'telnetlib' from '?'>"
 else:
-    assert f(contextlib) == "<unloaded module 'contextlib'>", f(contextlib)
+    assert f(telnetlib) == "<unloaded module 'telnetlib'>", f(telnetlib)
 
 try:
-    from contextlib import unknownattr
+    from telnetlib import unknownattr
 
     assert False, (
         'no demandmod should be created for attribute of non-package '
-        'module:\ncontextlib.unknownattr = %s' % f(unknownattr)
+        'module:\ntelnetlib.unknownattr = %s' % f(unknownattr)
     )
 except ImportError as inst:
     assert rsub(r"'", '', str(inst)).startswith(
@@ -219,6 +220,7 @@
 # Unlike the import statement, __import__() function should not raise
 # ImportError even if fromlist has an unknown item
 # (see Python/import.c:import_module_level() and ensure_fromlist())
-contextlibimp = __import__('contextlib', globals(), locals(), ['unknownattr'])
-assert f(contextlibimp) == "<module 'contextlib' from '?'>", f(contextlibimp)
-assert not util.safehasattr(contextlibimp, 'unknownattr')
+assert 'zipfile' not in sys.modules
+zipfileimp = __import__('zipfile', globals(), locals(), ['unknownattr'])
+assert f(zipfileimp) == "<module 'zipfile' from '?'>", f(zipfileimp)
+assert not util.safehasattr(zipfileimp, 'unknownattr')