Patchwork demandimport: Add unit test capturing desired behavior when absolute_import is present (issue4029)

login
register
mail settings
Submitter Jason R. Coombs
Date Sept. 7, 2013, 8:31 p.m.
Message ID <2be62965121941f99b6924fae764b883@BLUPR06MB434.namprd06.prod.outlook.com>
Download mbox | patch
Permalink /patch/2404/
State Deferred
Headers show

Comments

Jason R. Coombs - Sept. 7, 2013, 8:31 p.m.
# HG changeset patch
# User Jason R. Coombs <jaraco@jaraco.com>
# Date 1378585703 14400
#      Sat Sep 07 16:28:23 2013 -0400
# Node ID 26ba0c745ebba091ed9cc3fdfaa5bdbfaebbc15a
# Parent  1d07bf106c2ad1c7ef5e257e754ca8d858bd04b0
demandimport: Add unit test capturing desired behavior when absolute_import is present (issue4029)

Patch

diff -r 1d07bf106c2a -r 26ba0c745ebb tests/test-demandimport-absolute.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-demandimport-absolute.py       Sat Sep 07 16:28:23 2013 -0400
@@ -0,0 +1,46 @@ 
+"test demand import when absolute_import is indicated"
+
+import os
+import sys
+import shutil
+import textwrap
+import unittest
+
+import silenttestrunner
+
+from mercurial import demandimport
+
+class TestAbsoluteImport(unittest.TestCase):
+
+    def setUp(self):
+        os.mkdir('pkg')
+        f = open('pkg/mod.py', 'w')
+        f.write(textwrap.dedent("""
+            from __future__ import absolute_import
+            import os
+            """).lstrip())
+        f.close()
+        f = open('pkg/__init__.py', 'w')
+        f.close()
+        f = open('pkg/os.py', 'w')
+        f.write('''val = "this is not the module you're looking for"\n''')
+        f.close()
+        demandimport.enable()
+
+    def tearDown(self):
+        shutil.rmtree('pkg')
+        demandimport.disable()
+
+    def test_absolute_import(self):
+        if sys.version_info < (2,5):
+            print("Test only viable on Python 2.5 or later")
+            return
+        import pkg.mod
+        # trigger the loading of the module
+        pkg.mod.__name__
+        assert pkg.mod.os.__name__ == 'os'
+        assert 'devnull' in dir(pkg.mod.os)
+        assert 'val' not in dir(pkg.mod.os)
+
+if __name__ == '__main__':
+    silenttestrunner.main(__name__)