Bug 1262087 - Make the @checking callback not alter the behavior for bools. r?chmanchester draft
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 05 Apr 2016 09:40:13 +0900
changeset 347608 95210ed19f0a990d529019ba659f6228c7c9247e
parent 347607 c14dc478cfa3616c8ffefea5380b0fb9d89f3d78
child 517671 6e42ee0be49f58da75d826ce3d35b81e09d5741e
push id14626
push userbmo:mh+mozilla@glandium.org
push dateTue, 05 Apr 2016 12:02:53 +0000
reviewerschmanchester
bugs1262087
milestone48.0a1
Bug 1262087 - Make the @checking callback not alter the behavior for bools. r?chmanchester
build/moz.configure/checks.configure
python/mozbuild/mozbuild/test/configure/test_checks_configure.py
--- a/build/moz.configure/checks.configure
+++ b/build/moz.configure/checks.configure
@@ -49,24 +49,23 @@ def checking(what, callback=None):
         def wrapped(*args, **kwargs):
             log.info('checking %s... ', what)
             with log.queue_debug():
                 error, ret = None, None
                 try:
                     ret = func(*args, **kwargs)
                 except FatalCheckError as e:
                     error = e.message
-                if callback:
-                    log.info(callback(ret))
-                elif ret is True:
+                display_ret = callback(ret) if callback else ret
+                if display_ret is True:
                     log.info('yes')
-                elif ret is False:
+                elif display_ret is False or display_ret is None:
                     log.info('no')
                 else:
-                    log.info(ret)
+                    log.info(display_ret)
                 if error:
                     die(error)
             return ret
         return wrapped
     return decorator
 
 
 # Template to check for programs in $PATH.
--- a/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
@@ -47,16 +47,116 @@ class FindProgramSandbox(ConfigureSandbo
         # Avoid util.configure overwriting our mock find_program
         if key == 'find_program':
             return
 
         super(FindProgramSandbox, self).__setitem__(key, value)
 
 
 class TestChecksConfigure(unittest.TestCase):
+    def test_checking(self):
+        out = StringIO()
+        sandbox = FindProgramSandbox({}, stdout=out, stderr=out)
+        base_dir = os.path.join(topsrcdir, 'build', 'moz.configure')
+        sandbox.exec_file(os.path.join(base_dir, 'checks.configure'))
+
+        exec(textwrap.dedent('''
+            @checking('for a thing')
+            def foo(value):
+                return value
+        '''), sandbox)
+
+        foo = sandbox['foo']
+
+        foo(True)
+        self.assertEqual(out.getvalue(), 'checking for a thing... yes\n')
+
+        out.truncate(0)
+        foo(False)
+        self.assertEqual(out.getvalue(), 'checking for a thing... no\n')
+
+        out.truncate(0)
+        foo(42)
+        self.assertEqual(out.getvalue(), 'checking for a thing... 42\n')
+
+        out.truncate(0)
+        foo('foo')
+        self.assertEqual(out.getvalue(), 'checking for a thing... foo\n')
+
+        out.truncate(0)
+        data = ['foo', 'bar']
+        foo(data)
+        self.assertEqual(out.getvalue(), 'checking for a thing... %r\n' % data)
+
+        # When the function given to checking does nothing interesting, the
+        # behavior is not altered
+        exec(textwrap.dedent('''
+            @checking('for a thing', lambda x: x)
+            def foo(value):
+                return value
+        '''), sandbox)
+
+        foo = sandbox['foo']
+
+        out.truncate(0)
+        foo(True)
+        self.assertEqual(out.getvalue(), 'checking for a thing... yes\n')
+
+        out.truncate(0)
+        foo(False)
+        self.assertEqual(out.getvalue(), 'checking for a thing... no\n')
+
+        out.truncate(0)
+        foo(42)
+        self.assertEqual(out.getvalue(), 'checking for a thing... 42\n')
+
+        out.truncate(0)
+        foo('foo')
+        self.assertEqual(out.getvalue(), 'checking for a thing... foo\n')
+
+        out.truncate(0)
+        data = ['foo', 'bar']
+        foo(data)
+        self.assertEqual(out.getvalue(), 'checking for a thing... %r\n' % data)
+
+        exec(textwrap.dedent('''
+            def munge(x):
+                if not x:
+                    return 'not found'
+                if isinstance(x, (str, bool, int)):
+                    return x
+                return ' '.join(x)
+
+            @checking('for a thing', munge)
+            def foo(value):
+                return value
+        '''), sandbox)
+
+        foo = sandbox['foo']
+
+        out.truncate(0)
+        foo(True)
+        self.assertEqual(out.getvalue(), 'checking for a thing... yes\n')
+
+        out.truncate(0)
+        foo(False)
+        self.assertEqual(out.getvalue(), 'checking for a thing... not found\n')
+
+        out.truncate(0)
+        foo(42)
+        self.assertEqual(out.getvalue(), 'checking for a thing... 42\n')
+
+        out.truncate(0)
+        foo('foo')
+        self.assertEqual(out.getvalue(), 'checking for a thing... foo\n')
+
+        out.truncate(0)
+        foo(['foo', 'bar'])
+        self.assertEqual(out.getvalue(), 'checking for a thing... foo bar\n')
+
     def get_result(self, command='', args=[], environ={},
                    prog='/bin/configure'):
         config = {}
         out = StringIO()
         sandbox = FindProgramSandbox(config, environ, [prog] + args, out, out)
         base_dir = os.path.join(topsrcdir, 'build', 'moz.configure')
         sandbox.exec_file(os.path.join(base_dir, 'util.configure'))
         sandbox.exec_file(os.path.join(base_dir, 'checks.configure'))