Commit a541b28d authored by Ben Pastene's avatar Ben Pastene Committed by Commit Bot

Add 'X in tuple()' support to gclient conditionals.

This is purely so I can land:
https://chromium-review.googlesource.com/c/chromium/src/+/1625812
instead of:
https://chromium-review.googlesource.com/c/chromium/src/+/1625813

That is, the former is a slightly cleaner version of the latter. However,
it doesn't work w/o this patch.

Bug: 947531
Change-Id: If8e7f8080ba5f40408680b598fed511df89be8bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1626479Reviewed-by: 's avatarDirk Pranke <dpranke@chromium.org>
Commit-Queue: Ben Pastene <bpastene@chromium.org>
parent d390b317
......@@ -538,9 +538,11 @@ def EvaluateCondition(condition, variables, referenced_variables=None):
main_node = ast.parse(condition, mode='eval')
if isinstance(main_node, ast.Expression):
main_node = main_node.body
def _convert(node):
def _convert(node, allow_tuple=False):
if isinstance(node, ast.Str):
return node.s
elif isinstance(node, ast.Tuple) and allow_tuple:
return tuple(map(_convert, node.elts))
elif isinstance(node, ast.Name):
if node.id in referenced_variables:
raise ValueError(
......@@ -612,12 +614,15 @@ def EvaluateCondition(condition, variables, referenced_variables=None):
condition))
left = _convert(node.left)
right = _convert(node.comparators[0])
right = _convert(
node.comparators[0], allow_tuple=isinstance(node.ops[0], ast.In))
if isinstance(node.ops[0], ast.Eq):
return left == right
if isinstance(node.ops[0], ast.NotEq):
return left != right
if isinstance(node.ops[0], ast.In):
return left in right
raise ValueError(
'unexpected operator: %s %s (inside %r)' % (
......
......@@ -265,6 +265,21 @@ class EvaluateConditionTest(unittest.TestCase):
'(inside \'false_var_str and true_var\')',
str(cm.exception))
def test_tuple_presence(self):
self.assertTrue(gclient_eval.EvaluateCondition(
'foo in ("bar", "baz")', {'foo': 'bar'}))
self.assertFalse(gclient_eval.EvaluateCondition(
'foo in ("bar", "baz")', {'foo': 'not_bar'}))
def test_unsupported_tuple_operation(self):
with self.assertRaises(ValueError) as cm:
gclient_eval.EvaluateCondition('foo == ("bar", "baz")', {'foo': 'bar'})
self.assertIn('unexpected AST node', str(cm.exception))
with self.assertRaises(ValueError) as cm:
gclient_eval.EvaluateCondition('(foo,) == "bar"', {'foo': 'bar'})
self.assertIn('unexpected AST node', str(cm.exception))
class VarTest(unittest.TestCase):
def assert_adds_var(self, before, after):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment