Bug 1316250 - Don't pretend imported modules in templates are inherited by functions they contain. r=chmanchester
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 09 Nov 2016 15:22:17 +0900
changeset 321877 53f988e1c1fd34ca2ff5f42a59fa05b4a768a1e5
parent 321876 30d3890fca9847a283859a53043277713b7eadab
child 321878 fb579bc49c2f153e61cf06b34bcd6a03a409e6c4
push id21
push usermaklebus@msu.edu
push dateThu, 01 Dec 2016 06:22:08 +0000
reviewerschmanchester
bugs1316250
milestone52.0a1
Bug 1316250 - Don't pretend imported modules in templates are inherited by functions they contain. r=chmanchester The current state of python configure sandbox execution is that if a template imports a module, and a function defined in the template tries to use the module, it doesn't work. Ideally, it would, but rather than try to fix this corner case, we remove the unit tests that assume it works (and consequently pass for half bad reasons), and add a unit test so that the behavior doesn't change unwillingly.
python/mozbuild/mozbuild/test/configure/test_configure.py
python/mozbuild/mozbuild/test/configure/test_lint.py
--- a/python/mozbuild/mozbuild/test/configure/test_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_configure.py
@@ -334,16 +334,34 @@ class TestConfigure(unittest.TestCase):
         )
 
         self.assertIs(sandbox['foo'](), sandbox)
 
         # Nothing leaked from the function being executed
         self.assertEquals(sandbox.keys(), ['__builtins__', 'foo'])
         self.assertEquals(sandbox['__builtins__'], ConfigureSandbox.BUILTINS)
 
+        exec_(textwrap.dedent('''
+            @template
+            @imports('sys')
+            def foo():
+                @depends(when=True)
+                def bar():
+                    return sys
+                return bar
+            bar = foo()'''),
+            sandbox
+        )
+
+        with self.assertRaises(NameError) as e:
+            sandbox._depends[sandbox['bar']].result
+
+        self.assertEquals(e.exception.message,
+                          "global name 'sys' is not defined")
+
     def test_apply_imports(self):
         imports = []
 
         class CountApplyImportsSandbox(ConfigureSandbox):
             def _apply_imports(self, *args, **kwargs):
                 imports.append((args, kwargs))
                 super(CountApplyImportsSandbox, self)._apply_imports(
                     *args, **kwargs)
--- a/python/mozbuild/mozbuild/test/configure/test_lint.py
+++ b/python/mozbuild/mozbuild/test/configure/test_lint.py
@@ -84,38 +84,16 @@ class TestLint(unittest.TestCase):
                 tmpl()
             '''):
                 self.lint_test()
 
         self.assertEquals(e.exception.message,
                           "`bar` depends on '--help' and `foo`. "
                           "`foo` must depend on '--help'")
 
-        with self.assertRaises(ConfigureError) as e:
-            with self.moz_configure('''
-                @template
-                @imports('os')
-                def tmpl():
-                    option('--foo', help='foo')
-                    @depends('--foo')
-                    def foo(value):
-                        os
-                        return value
-
-                    @depends('--help', foo)
-                    def bar(help, foo):
-                        return
-                tmpl()
-            '''):
-                self.lint_test()
-
-        self.assertEquals(e.exception.message,
-                          "`bar` depends on '--help' and `foo`. "
-                          "`foo` must depend on '--help'")
-
         with self.moz_configure('''
             option('--foo', help='foo')
             @depends('--foo')
             def foo(value):
                 return value
 
             include(foo)
         '''):
@@ -131,32 +109,11 @@ class TestLint(unittest.TestCase):
 
                 include(foo)
             '''):
                 self.lint_test()
 
         self.assertEquals(e.exception.message,
                           "Missing @depends for `foo`: '--help'")
 
-        with self.assertRaises(ConfigureError) as e:
-            with self.moz_configure('''
-                @template
-                @imports('os')
-                def tmpl():
-                    option('--foo', help='foo')
-                    @depends('--foo')
-                    def foo(value):
-                        os
-                        return value
-                    return foo
-
-                foo = tmpl()
-
-                include(foo)
-            '''):
-                self.lint_test()
-
-        self.assertEquals(e.exception.message,
-                          "Missing @depends for `foo`: '--help'")
-
 
 if __name__ == '__main__':
     main()