123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326 |
- #!/usr/bin/env vpython3
- # Copyright 2017 The Chromium Authors. All rights reserved.
- # Use of this source code is governed by a BSD-style license that can be
- # found in the LICENSE file.
- import itertools
- import logging
- import os
- import sys
- import unittest
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- import metrics_utils
- # We have to disable monitoring before importing gclient.
- metrics_utils.COLLECT_METRICS = False
- import gclient_eval
- import gclient_utils
- # TODO: Should fix these warnings.
- # pylint: disable=line-too-long
- def file_join(lines):
- return ''.join([l + '\n' for l in lines])
- class GClientEvalTest(unittest.TestCase):
- def test_str(self):
- self.assertEqual('foo', gclient_eval._gclient_eval('"foo"'))
- def test_tuple(self):
- self.assertEqual(('a', 'b'), gclient_eval._gclient_eval('("a", "b")'))
- def test_list(self):
- self.assertEqual(['a', 'b'], gclient_eval._gclient_eval('["a", "b"]'))
- def test_dict(self):
- self.assertEqual({'a': 'b'}, gclient_eval._gclient_eval('{"a": "b"}'))
- def test_name_safe(self):
- self.assertEqual(True, gclient_eval._gclient_eval('True'))
- def test_name_unsafe(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval._gclient_eval('UnsafeName')
- self.assertIn('invalid name \'UnsafeName\'', str(cm.exception))
- def test_invalid_call(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval._gclient_eval('Foo("bar")')
- self.assertIn('Str and Var are the only allowed functions',
- str(cm.exception))
- def test_expands_vars(self):
- self.assertEqual(
- 'foo',
- gclient_eval._gclient_eval('Var("bar")', vars_dict={'bar': 'foo'}))
- self.assertEqual(
- 'baz',
- gclient_eval._gclient_eval(
- 'Var("bar")',
- vars_dict={'bar': gclient_eval.ConstantString('baz')}))
- def test_expands_vars_with_braces(self):
- self.assertEqual(
- 'foo',
- gclient_eval._gclient_eval('"{bar}"', vars_dict={'bar': 'foo'}))
- self.assertEqual(
- 'baz',
- gclient_eval._gclient_eval(
- '"{bar}"',
- vars_dict={'bar': gclient_eval.ConstantString('baz')}))
- def test_invalid_var(self):
- with self.assertRaises(KeyError) as cm:
- gclient_eval._gclient_eval('"{bar}"', vars_dict={})
- self.assertIn('bar was used as a variable, but was not declared',
- str(cm.exception))
- def test_plus(self):
- self.assertEqual('foo', gclient_eval._gclient_eval('"f" + "o" + "o"'))
- def test_format(self):
- self.assertEqual('foo', gclient_eval._gclient_eval('"%s" % "foo"'))
- def test_not_expression(self):
- with self.assertRaises(SyntaxError) as cm:
- gclient_eval._gclient_eval('def foo():\n pass')
- self.assertIn('invalid syntax', str(cm.exception))
- def test_not_whitelisted(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval._gclient_eval('[x for x in [1, 2, 3]]')
- self.assertIn('unexpected AST node', str(cm.exception))
- self.assertIn('ast.ListComp object', str(cm.exception))
- def test_dict_ordered(self):
- for test_case in itertools.permutations(range(4)):
- input_data = ['{'] + ['"%s": "%s",' % (n, n)
- for n in test_case] + ['}']
- expected = [(str(n), str(n)) for n in test_case]
- result = gclient_eval._gclient_eval(''.join(input_data))
- self.assertEqual(expected, list(result.items()))
- class ExecTest(unittest.TestCase):
- def test_multiple_assignment(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval.Exec('a, b, c = "a", "b", "c"')
- self.assertIn('invalid assignment: target should be a name',
- str(cm.exception))
- def test_override(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval.Exec('a = "a"\na = "x"')
- self.assertIn('invalid assignment: overrides var \'a\'',
- str(cm.exception))
- def test_schema_wrong_type(self):
- with self.assertRaises(gclient_utils.Error):
- gclient_eval.Exec('include_rules = {}')
- def test_recursedeps_list(self):
- local_scope = gclient_eval.Exec(
- 'recursedeps = [["src/third_party/angle", "DEPS.chromium"]]')
- self.assertEqual(
- {'recursedeps': [['src/third_party/angle', 'DEPS.chromium']]},
- local_scope)
- def test_var(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' "foo": "bar",',
- ' "baz": Str("quux")',
- '}',
- 'deps = {',
- ' "a_dep": "a" + Var("foo") + "b" + Var("baz"),',
- '}',
- ]))
- Str = gclient_eval.ConstantString
- self.assertEqual(
- {
- 'vars': {
- 'foo': 'bar',
- 'baz': Str('quux')
- },
- 'deps': {
- 'a_dep': 'abarbquux'
- },
- }, local_scope)
- def test_braces_var(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' "foo": "bar",',
- ' "baz": Str("quux")',
- '}',
- 'deps = {',
- ' "a_dep": "a{foo}b{baz}",',
- '}',
- ]))
- Str = gclient_eval.ConstantString
- self.assertEqual(
- {
- 'vars': {
- 'foo': 'bar',
- 'baz': Str('quux')
- },
- 'deps': {
- 'a_dep': 'abarbquux'
- },
- }, local_scope)
- def test_empty_deps(self):
- local_scope = gclient_eval.Exec('deps = {}')
- self.assertEqual({'deps': {}}, local_scope)
- def test_overrides_vars(self):
- local_scope = gclient_eval.Exec(file_join([
- 'vars = {',
- ' "foo": "bar",',
- ' "quux": Str("quuz")',
- '}',
- 'deps = {',
- ' "a_dep": "a{foo}b",',
- ' "b_dep": "c{quux}d",',
- '}',
- ]),
- vars_override={
- 'foo': 'baz',
- 'quux': 'corge'
- })
- Str = gclient_eval.ConstantString
- self.assertEqual(
- {
- 'vars': {
- 'foo': 'bar',
- 'quux': Str('quuz')
- },
- 'deps': {
- 'a_dep': 'abazb',
- 'b_dep': 'ccorged'
- },
- }, local_scope)
- def test_doesnt_override_undeclared_vars(self):
- with self.assertRaises(KeyError) as cm:
- gclient_eval.Exec(file_join([
- 'vars = {',
- ' "foo": "bar",',
- '}',
- 'deps = {',
- ' "a_dep": "a{baz}b",',
- '}',
- ]),
- vars_override={'baz': 'lalala'})
- self.assertIn('baz was used as a variable, but was not declared',
- str(cm.exception))
- def test_doesnt_allow_duplicate_deps(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": {',
- ' "url": "a_url@a_rev",',
- ' "condition": "foo",',
- ' },',
- ' "a_dep": {',
- ' "url": "a_url@another_rev",',
- ' "condition": "not foo",',
- ' }',
- '}',
- ]), '<unknown>')
- self.assertIn('duplicate key in dictionary: a_dep', str(cm.exception))
- class UpdateConditionTest(unittest.TestCase):
- def test_both_present(self):
- info = {'condition': 'foo'}
- gclient_eval.UpdateCondition(info, 'and', 'bar')
- self.assertEqual(info, {'condition': '(foo) and (bar)'})
- info = {'condition': 'foo'}
- gclient_eval.UpdateCondition(info, 'or', 'bar')
- self.assertEqual(info, {'condition': '(foo) or (bar)'})
- def test_one_present_and(self):
- # If one of info's condition or new_condition is present, and |op| ==
- # 'and' then the the result must be the present condition.
- info = {'condition': 'foo'}
- gclient_eval.UpdateCondition(info, 'and', None)
- self.assertEqual(info, {'condition': 'foo'})
- info = {}
- gclient_eval.UpdateCondition(info, 'and', 'bar')
- self.assertEqual(info, {'condition': 'bar'})
- def test_both_absent_and(self):
- # Nothing happens
- info = {}
- gclient_eval.UpdateCondition(info, 'and', None)
- self.assertEqual(info, {})
- def test_or(self):
- # If one of info's condition and new_condition is not present, then
- # there shouldn't be a condition. An absent value is treated as
- # implicitly True.
- info = {'condition': 'foo'}
- gclient_eval.UpdateCondition(info, 'or', None)
- self.assertEqual(info, {})
- info = {}
- gclient_eval.UpdateCondition(info, 'or', 'bar')
- self.assertEqual(info, {})
- info = {}
- gclient_eval.UpdateCondition(info, 'or', None)
- self.assertEqual(info, {})
- class EvaluateConditionTest(unittest.TestCase):
- def test_true(self):
- self.assertTrue(gclient_eval.EvaluateCondition('True', {}))
- def test_variable(self):
- self.assertFalse(gclient_eval.EvaluateCondition('foo',
- {'foo': 'False'}))
- def test_variable_cyclic_reference(self):
- with self.assertRaises(ValueError) as cm:
- self.assertTrue(
- gclient_eval.EvaluateCondition('bar', {'bar': 'bar'}))
- self.assertIn('invalid cyclic reference to \'bar\' (inside \'bar\')',
- str(cm.exception))
- def test_operators(self):
- self.assertFalse(
- gclient_eval.EvaluateCondition('a and not (b or c)', {
- 'a': 'True',
- 'b': 'False',
- 'c': 'True'
- }))
- def test_expansion(self):
- self.assertTrue(
- gclient_eval.EvaluateCondition('a or b', {
- 'a': 'b and c',
- 'b': 'not c',
- 'c': 'False'
- }))
- def test_string_equality(self):
- self.assertTrue(
- gclient_eval.EvaluateCondition('foo == "baz"', {'foo': '"baz"'}))
- self.assertFalse(
- gclient_eval.EvaluateCondition('foo == "bar"', {'foo': '"baz"'}))
- def test_string_inequality(self):
- self.assertTrue(
- gclient_eval.EvaluateCondition('foo != "bar"', {'foo': '"baz"'}))
- self.assertFalse(
- gclient_eval.EvaluateCondition('foo != "baz"', {'foo': '"baz"'}))
- def test_triple_or(self):
- self.assertTrue(
- gclient_eval.EvaluateCondition('a or b or c', {
- 'a': 'False',
- 'b': 'False',
- 'c': 'True'
- }))
- self.assertFalse(
- gclient_eval.EvaluateCondition('a or b or c', {
- 'a': 'False',
- 'b': 'False',
- 'c': 'False'
- }))
- def test_triple_and(self):
- self.assertTrue(
- gclient_eval.EvaluateCondition('a and b and c', {
- 'a': 'True',
- 'b': 'True',
- 'c': 'True'
- }))
- self.assertFalse(
- gclient_eval.EvaluateCondition('a and b and c', {
- 'a': 'True',
- 'b': 'True',
- 'c': 'False'
- }))
- def test_triple_and_and_or(self):
- self.assertTrue(
- gclient_eval.EvaluateCondition('a and b and c or d or e', {
- 'a': 'False',
- 'b': 'False',
- 'c': 'False',
- 'd': 'False',
- 'e': 'True'
- }))
- self.assertFalse(
- gclient_eval.EvaluateCondition('a and b and c or d or e', {
- 'a': 'True',
- 'b': 'True',
- 'c': 'False',
- 'd': 'False',
- 'e': 'False'
- }))
- def test_string_bool(self):
- self.assertFalse(
- gclient_eval.EvaluateCondition('false_str_var and true_var', {
- 'false_str_var': 'False',
- 'true_var': True
- }))
- def test_string_bool_typo(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval.EvaluateCondition('false_var_str and true_var', {
- 'false_str_var': 'False',
- 'true_var': True
- })
- self.assertIn(
- 'invalid "and" operand \'false_var_str\' '
- '(inside \'false_var_str and true_var\')', str(cm.exception))
- def test_non_bool_in_or(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval.EvaluateCondition('string_var or true_var', {
- 'string_var': 'Kittens',
- 'true_var': True
- })
- self.assertIn(
- 'invalid "or" operand \'Kittens\' '
- '(inside \'string_var or true_var\')', str(cm.exception))
- def test_non_bool_in_and(self):
- with self.assertRaises(ValueError) as cm:
- gclient_eval.EvaluateCondition('string_var and true_var', {
- 'string_var': 'Kittens',
- 'true_var': True
- })
- self.assertIn(
- 'invalid "and" operand \'Kittens\' '
- '(inside \'string_var 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))
- def test_str_in_condition(self):
- Str = gclient_eval.ConstantString
- self.assertTrue(
- gclient_eval.EvaluateCondition('s_var == "foo"',
- {'s_var': Str("foo")}))
- self.assertFalse(
- gclient_eval.EvaluateCondition('s_var in ("baz", "quux")',
- {'s_var': Str("foo")}))
- class VarTest(unittest.TestCase):
- def assert_adds_var(self, before, after):
- local_scope = gclient_eval.Exec(file_join(before))
- gclient_eval.AddVar(local_scope, 'baz', 'lemur')
- results = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(results, file_join(after))
- def test_adds_var(self):
- before = [
- 'vars = {',
- ' "foo": "bar",',
- '}',
- ]
- after = [
- 'vars = {',
- ' "baz": "lemur",',
- ' "foo": "bar",',
- '}',
- ]
- self.assert_adds_var(before, after)
- def test_adds_var_twice(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' "foo": "bar",',
- '}',
- ]))
- gclient_eval.AddVar(local_scope, 'baz', 'lemur')
- gclient_eval.AddVar(local_scope, 'v8_revision', 'deadbeef')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result,
- file_join([
- 'vars = {',
- ' "v8_revision": "deadbeef",',
- ' "baz": "lemur",',
- ' "foo": "bar",',
- '}',
- ]))
- def test_gets_and_sets_var(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' "foo": "bar",',
- ' "quux": Str("quuz")',
- '}',
- ]))
- self.assertEqual(gclient_eval.GetVar(local_scope, 'foo'), "bar")
- self.assertEqual(gclient_eval.GetVar(local_scope, 'quux'), "quuz")
- gclient_eval.SetVar(local_scope, 'foo', 'baz')
- gclient_eval.SetVar(local_scope, 'quux', 'corge')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result,
- file_join([
- 'vars = {',
- ' "foo": "baz",',
- ' "quux": Str("corge")',
- '}',
- ]))
- def test_gets_and_sets_var_non_string(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' "foo": True,',
- '}',
- ]))
- result = gclient_eval.GetVar(local_scope, 'foo')
- self.assertEqual(result, True)
- gclient_eval.SetVar(local_scope, 'foo', 'False')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(result,
- file_join([
- 'vars = {',
- ' "foo": False,',
- '}',
- ]))
- def test_add_preserves_formatting(self):
- before = [
- '# Copyright stuff',
- '# some initial comments',
- '',
- 'vars = { ',
- ' # Some comments.',
- ' "foo": "bar",',
- '',
- ' # More comments.',
- ' # Even more comments.',
- ' "v8_revision": ',
- ' "deadbeef",',
- ' # Someone formatted this wrong',
- '}',
- ]
- after = [
- '# Copyright stuff',
- '# some initial comments',
- '',
- 'vars = { ',
- ' "baz": "lemur",',
- ' # Some comments.',
- ' "foo": "bar",',
- '',
- ' # More comments.',
- ' # Even more comments.',
- ' "v8_revision": ',
- ' "deadbeef",',
- ' # Someone formatted this wrong',
- '}',
- ]
- self.assert_adds_var(before, after)
- def test_set_preserves_formatting(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' # Comment with trailing space ',
- ' "foo": \'bar\',',
- '}',
- ]))
- gclient_eval.SetVar(local_scope, 'foo', 'baz')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result,
- file_join([
- 'vars = {',
- ' # Comment with trailing space ',
- ' "foo": \'baz\',',
- '}',
- ]))
- class CipdTest(unittest.TestCase):
- def test_gets_and_sets_cipd(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'deps = {',
- ' "src/cipd/package": {',
- ' "packages": [',
- ' {',
- ' "package": "some/cipd/package",',
- ' "version": "deadbeef",',
- ' },',
- ' {',
- ' "package": "another/cipd/package",',
- ' "version": "version:5678",',
- ' },',
- ' ],',
- ' "condition": "checkout_android",',
- ' "dep_type": "cipd",',
- ' },',
- '}',
- ]))
- self.assertEqual(
- gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
- 'some/cipd/package'), 'deadbeef')
- self.assertEqual(
- gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
- 'another/cipd/package'), 'version:5678')
- gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
- 'another/cipd/package', 'version:6789')
- gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
- 'some/cipd/package', 'foobar')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result,
- file_join([
- 'deps = {',
- ' "src/cipd/package": {',
- ' "packages": [',
- ' {',
- ' "package": "some/cipd/package",',
- ' "version": "foobar",',
- ' },',
- ' {',
- ' "package": "another/cipd/package",',
- ' "version": "version:6789",',
- ' },',
- ' ],',
- ' "condition": "checkout_android",',
- ' "dep_type": "cipd",',
- ' },',
- '}',
- ]))
- def test_gets_and_sets_cipd_vars(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'vars = {',
- ' "cipd-rev": "git_revision:deadbeef",',
- ' "another-cipd-rev": "version:1.0.3",',
- '}',
- 'deps = {',
- ' "src/cipd/package": {',
- ' "packages": [',
- ' {',
- ' "package": "some/cipd/package",',
- ' "version": Var("cipd-rev"),',
- ' },',
- ' {',
- ' "package": "another/cipd/package",',
- ' "version": "{another-cipd-rev}",',
- ' },',
- ' ],',
- ' "condition": "checkout_android",',
- ' "dep_type": "cipd",',
- ' },',
- '}',
- ]))
- self.assertEqual(
- gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
- 'some/cipd/package'), 'git_revision:deadbeef')
- self.assertEqual(
- gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
- 'another/cipd/package'), 'version:1.0.3')
- gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
- 'another/cipd/package', 'version:1.1.0')
- gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
- 'some/cipd/package', 'git_revision:foobar')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result,
- file_join([
- 'vars = {',
- ' "cipd-rev": "git_revision:foobar",',
- ' "another-cipd-rev": "version:1.1.0",',
- '}',
- 'deps = {',
- ' "src/cipd/package": {',
- ' "packages": [',
- ' {',
- ' "package": "some/cipd/package",',
- ' "version": Var("cipd-rev"),',
- ' },',
- ' {',
- ' "package": "another/cipd/package",',
- ' "version": "{another-cipd-rev}",',
- ' },',
- ' ],',
- ' "condition": "checkout_android",',
- ' "dep_type": "cipd",',
- ' },',
- '}',
- ]))
- def test_preserves_escaped_vars(self):
- local_scope = gclient_eval.Exec(
- file_join([
- 'deps = {',
- ' "src/cipd/package": {',
- ' "packages": [',
- ' {',
- ' "package": "package/${{platform}}",',
- ' "version": "version:abcd",',
- ' },',
- ' ],',
- ' "dep_type": "cipd",',
- ' },',
- '}',
- ]))
- gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
- 'package/${platform}', 'version:dcba')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result,
- file_join([
- 'deps = {',
- ' "src/cipd/package": {',
- ' "packages": [',
- ' {',
- ' "package": "package/${{platform}}",',
- ' "version": "version:dcba",',
- ' },',
- ' ],',
- ' "dep_type": "cipd",',
- ' },',
- '}',
- ]))
- class RevisionTest(unittest.TestCase):
- def assert_gets_and_sets_revision(self,
- before,
- after,
- rev_before='deadbeef'):
- local_scope = gclient_eval.Exec(file_join(before))
- result = gclient_eval.GetRevision(local_scope, 'src/dep')
- self.assertEqual(result, rev_before)
- gclient_eval.SetRevision(local_scope, 'src/dep', 'deadfeed')
- self.assertEqual(file_join(after),
- gclient_eval.RenderDEPSFile(local_scope))
- def test_revision(self):
- before = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@deadbeef",',
- '}',
- ]
- after = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@deadfeed",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_revision_new_line(self):
- before = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@"',
- ' + "deadbeef",',
- '}',
- ]
- after = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@"',
- ' + "deadfeed",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_revision_windows_local_path(self):
- before = [
- 'deps = {',
- ' "src/dep": "file:///C:\\\\path.git@deadbeef",',
- '}',
- ]
- after = [
- 'deps = {',
- ' "src/dep": "file:///C:\\\\path.git@deadfeed",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_revision_multiline_strings(self):
- deps = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@"',
- ' "deadbeef",',
- '}',
- ]
- with self.assertRaises(ValueError) as e:
- local_scope = gclient_eval.Exec(file_join(deps))
- gclient_eval.SetRevision(local_scope, 'src/dep', 'deadfeed')
- self.assertEqual(
- 'Can\'t update value for src/dep. Multiline strings and implicitly '
- 'concatenated strings are not supported.\n'
- 'Consider reformatting the DEPS file.', str(e.exception))
- def test_revision_implicitly_concatenated_strings(self):
- deps = [
- 'deps = {',
- ' "src/dep": "https://example.com" + "/dep.git@" "deadbeef",',
- '}',
- ]
- with self.assertRaises(ValueError) as e:
- local_scope = gclient_eval.Exec(file_join(deps))
- gclient_eval.SetRevision(local_scope, 'src/dep', 'deadfeed')
- self.assertEqual(
- 'Can\'t update value for src/dep. Multiline strings and implicitly '
- 'concatenated strings are not supported.\n'
- 'Consider reformatting the DEPS file.', str(e.exception))
- def test_revision_inside_dict(self):
- before = [
- 'deps = {',
- ' "src/dep": {',
- ' "url": "https://example.com/dep.git@deadbeef",',
- ' "condition": "some_condition",',
- ' },',
- '}',
- ]
- after = [
- 'deps = {',
- ' "src/dep": {',
- ' "url": "https://example.com/dep.git@deadfeed",',
- ' "condition": "some_condition",',
- ' },',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_follows_var_braces(self):
- before = [
- 'vars = {',
- ' "dep_revision": "deadbeef",',
- '}',
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@{dep_revision}",',
- '}',
- ]
- after = [
- 'vars = {',
- ' "dep_revision": "deadfeed",',
- '}',
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@{dep_revision}",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_follows_var_braces_newline(self):
- before = [
- 'vars = {',
- ' "dep_revision": "deadbeef",',
- '}',
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git"',
- ' + "@{dep_revision}",',
- '}',
- ]
- after = [
- 'vars = {',
- ' "dep_revision": "deadfeed",',
- '}',
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git"',
- ' + "@{dep_revision}",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_follows_var_function(self):
- before = [
- 'vars = {',
- ' "dep_revision": "deadbeef",',
- '}',
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@" + Var("dep_revision"),',
- '}',
- ]
- after = [
- 'vars = {',
- ' "dep_revision": "deadfeed",',
- '}',
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@" + Var("dep_revision"),',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_pins_revision(self):
- before = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git",',
- '}',
- ]
- after = [
- 'deps = {',
- ' "src/dep": "https://example.com/dep.git@deadfeed",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after, rev_before=None)
- def test_preserves_variables(self):
- before = [
- 'vars = {',
- ' "src_root": "src"',
- '}',
- 'deps = {',
- ' "{src_root}/dep": "https://example.com/dep.git@deadbeef",',
- '}',
- ]
- after = [
- 'vars = {',
- ' "src_root": "src"',
- '}',
- 'deps = {',
- ' "{src_root}/dep": "https://example.com/dep.git@deadfeed",',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- def test_preserves_formatting(self):
- before = [
- 'vars = {',
- ' # Some comment on deadbeef ',
- ' "dep_revision": "deadbeef",',
- '}',
- 'deps = {',
- ' "src/dep": {',
- ' "url": "https://example.com/dep.git@" + Var("dep_revision"),',
- '',
- ' "condition": "some_condition",',
- ' },',
- '}',
- ]
- after = [
- 'vars = {',
- ' # Some comment on deadbeef ',
- ' "dep_revision": "deadfeed",',
- '}',
- 'deps = {',
- ' "src/dep": {',
- ' "url": "https://example.com/dep.git@" + Var("dep_revision"),',
- '',
- ' "condition": "some_condition",',
- ' },',
- '}',
- ]
- self.assert_gets_and_sets_revision(before, after)
- class ParseTest(unittest.TestCase):
- def callParse(self, vars_override=None):
- return gclient_eval.Parse(
- file_join([
- 'vars = {',
- ' "foo": "bar",',
- '}',
- 'deps = {',
- ' "a_dep": "a{foo}b",',
- '}',
- ]), '<unknown>', vars_override)
- def test_supports_vars_inside_vars(self):
- deps_file = file_join([
- 'vars = {',
- ' "foo": "bar",',
- ' "baz": "\\"{foo}\\" == \\"bar\\"",',
- '}',
- 'deps = {',
- ' "src/baz": {',
- ' "url": "baz_url",',
- ' "condition": "baz",',
- ' },',
- '}',
- ])
- local_scope = gclient_eval.Parse(deps_file, '<unknown>', None)
- self.assertEqual(
- {
- 'vars': {
- 'foo': 'bar',
- 'baz': '"bar" == "bar"'
- },
- 'deps': {
- 'src/baz': {
- 'url': 'baz_url',
- 'dep_type': 'git',
- 'condition': 'baz'
- }
- },
- }, local_scope)
- def test_has_builtin_vars(self):
- builtin_vars = {'builtin_var': 'foo'}
- deps_file = file_join([
- 'deps = {',
- ' "a_dep": "a{builtin_var}b",',
- '}',
- ])
- local_scope = gclient_eval.Parse(deps_file, '<unknown>', None,
- builtin_vars)
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'afoob',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_declaring_builtin_var_has_no_effect(self):
- builtin_vars = {'builtin_var': 'foo'}
- deps_file = file_join([
- 'vars = {',
- ' "builtin_var": "bar",',
- '}',
- 'deps = {',
- ' "a_dep": "a{builtin_var}b",',
- '}',
- ])
- local_scope = gclient_eval.Parse(deps_file, '<unknown>', None,
- builtin_vars)
- self.assertEqual(
- {
- 'vars': {
- 'builtin_var': 'bar'
- },
- 'deps': {
- 'a_dep': {
- 'url': 'afoob',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_override_builtin_var(self):
- builtin_vars = {'builtin_var': 'foo'}
- vars_override = {'builtin_var': 'override'}
- deps_file = file_join([
- 'deps = {',
- ' "a_dep": "a{builtin_var}b",',
- '}',
- ])
- local_scope = gclient_eval.Parse(deps_file, '<unknown>', vars_override,
- builtin_vars)
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'aoverrideb',
- 'dep_type': 'git'
- }
- },
- }, local_scope, str(local_scope))
- def test_expands_vars(self):
- local_scope = self.callParse()
- self.assertEqual(
- {
- 'vars': {
- 'foo': 'bar'
- },
- 'deps': {
- 'a_dep': {
- 'url': 'abarb',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_overrides_vars(self):
- local_scope = self.callParse(vars_override={'foo': 'baz'})
- self.assertEqual(
- {
- 'vars': {
- 'foo': 'bar'
- },
- 'deps': {
- 'a_dep': {
- 'url': 'abazb',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_no_extra_vars(self):
- deps_file = file_join([
- 'vars = {',
- ' "foo": "bar",',
- '}',
- 'deps = {',
- ' "a_dep": "a{baz}b",',
- '}',
- ])
- with self.assertRaises(KeyError) as cm:
- gclient_eval.Parse(deps_file, '<unknown>', {'baz': 'lalala'})
- self.assertIn('baz was used as a variable, but was not declared',
- str(cm.exception))
- def test_standardizes_deps_string_dep(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": "a_url@a_rev",',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_standardizes_deps_dict_dep(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": {',
- ' "url": "a_url@a_rev",',
- ' "condition": "checkout_android",',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git',
- 'condition': 'checkout_android'
- }
- },
- }, local_scope)
- def test_ignores_none_in_deps_os(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": "a_url@a_rev",',
- '}',
- 'deps_os = {',
- ' "mac": {',
- ' "a_dep": None,',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_merges_deps_os_extra_dep(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": "a_url@a_rev",',
- '}',
- 'deps_os = {',
- ' "mac": {',
- ' "b_dep": "b_url@b_rev"',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git'
- },
- 'b_dep': {
- 'url': 'b_url@b_rev',
- 'dep_type': 'git',
- 'condition': 'checkout_mac'
- }
- },
- }, local_scope)
- def test_merges_deps_os_existing_dep_with_no_condition(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": "a_url@a_rev",',
- '}',
- 'deps_os = {',
- ' "mac": {',
- ' "a_dep": "a_url@a_rev"',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git'
- }
- },
- }, local_scope)
- def test_merges_deps_os_existing_dep_with_condition(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": {',
- ' "url": "a_url@a_rev",',
- ' "condition": "some_condition",',
- ' },',
- '}',
- 'deps_os = {',
- ' "mac": {',
- ' "a_dep": "a_url@a_rev"',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git',
- 'condition': '(checkout_mac) or (some_condition)'
- },
- },
- }, local_scope)
- def test_merges_deps_os_multiple_os(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'deps_os = {',
- ' "win": {'
- ' "a_dep": "a_url@a_rev"',
- ' },',
- ' "mac": {',
- ' "a_dep": "a_url@a_rev"',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- 'deps': {
- 'a_dep': {
- 'url': 'a_url@a_rev',
- 'dep_type': 'git',
- 'condition': '(checkout_mac) or (checkout_win)'
- },
- },
- }, local_scope)
- def test_fails_to_merge_same_dep_with_different_revisions(self):
- with self.assertRaises(gclient_eval.gclient_utils.Error) as cm:
- gclient_eval.Parse(
- file_join([
- 'deps = {',
- ' "a_dep": {',
- ' "url": "a_url@a_rev",',
- ' "condition": "some_condition",',
- ' },',
- '}',
- 'deps_os = {',
- ' "mac": {',
- ' "a_dep": "a_url@b_rev"',
- ' },',
- '}',
- ]), '<unknown>')
- self.assertIn('conflicts with existing deps', str(cm.exception))
- def test_merges_hooks_os(self):
- local_scope = gclient_eval.Parse(
- file_join([
- 'hooks = [',
- ' {',
- ' "action": ["a", "action"],',
- ' },',
- ']',
- 'hooks_os = {',
- ' "mac": [',
- ' {',
- ' "action": ["b", "action"]',
- ' },',
- ' ]',
- '}',
- ]), '<unknown>')
- self.assertEqual(
- {
- "hooks": [{
- "action": ["a", "action"]
- }, {
- "action": ["b", "action"],
- "condition": "checkout_mac"
- }],
- }, local_scope)
- if __name__ == '__main__':
- level = logging.DEBUG if '-v' in sys.argv else logging.FATAL
- logging.basicConfig(level=level,
- format='%(asctime).19s %(levelname)s %(filename)s:'
- '%(lineno)s %(message)s')
- unittest.main()
|