Bug 1262087 - Make the @checking callback not alter the behavior for bools. r=chmanchester

This commit is contained in:
Mike Hommey 2016-04-05 09:40:13 +09:00
parent 6878c848b4
commit c0808e5f6d
2 changed files with 104 additions and 5 deletions

View File

@ -54,14 +54,13 @@ def checking(what, callback=None):
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

View File

@ -52,6 +52,106 @@ class FindProgramSandbox(ConfigureSandbox):
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 = {}