Skip to content

Commit

Permalink
Add 'X in tuple()' support to gclient conditionals.
Browse files Browse the repository at this point in the history
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/+/1626479
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Ben Pastene <bpastene@chromium.org>
  • Loading branch information
bpastene authored and Commit Bot committed May 24, 2019
1 parent d390b31 commit a541b28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions gclient_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -612,12 +614,15 @@ def _convert(node):
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)' % (
Expand Down
15 changes: 15 additions & 0 deletions tests/gclient_eval_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,21 @@ def test_string_bool_typo(self):
'(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):
Expand Down

0 comments on commit a541b28

Please sign in to comment.