Bug 1262087 - Make the @checking callback not alter the behavior for bools. r=chmanchester
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 05 Apr 2016 09:40:13 +0900
changeset 315566 06029a4d4a7d95d2bd3c2a0c42dc1705e4e9adf6
parent 315565 25365989efaf2c526c8d7e8346b70bbf27a769e5
child 315567 9b38a13b6f15cb49cecc606b2f4cb160c9cb9449
push id9480
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 17:12:58 +0000
treeherdermozilla-aurora@0d6a91c76a9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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'))