12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310 |
- #!/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 collections
- import itertools
- import logging
- import os
- import sys
- import unittest
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- from third_party import schema
- import metrics_utils
- # We have to disable monitoring before importing gclient.
- metrics_utils.COLLECT_METRICS = False
- import gclient
- import gclient_eval
- import gclient_utils
- # TODO: Should fix these warnings.
- # pylint: disable=line-too-long
- 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: <_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('\n'.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('\n'.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('\n'.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('\n'.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(
- '\n'.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('\n'.join(before))
- gclient_eval.AddVar(local_scope, 'baz', 'lemur')
- results = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(results, '\n'.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('\n'.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, '\n'.join([
- 'vars = {',
- ' "v8_revision": "deadbeef",',
- ' "baz": "lemur",',
- ' "foo": "bar",',
- '}',
- ]))
- def test_gets_and_sets_var(self):
- local_scope = gclient_eval.Exec('\n'.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, '\n'.join([
- 'vars = {',
- ' "foo": "baz",',
- ' "quux": Str("corge")',
- '}',
- ]))
- def test_gets_and_sets_var_non_string(self):
- local_scope = gclient_eval.Exec('\n'.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, '\n'.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('\n'.join([
- 'vars = {',
- ' # Comment with trailing space ',
- ' "foo": \'bar\',',
- '}',
- ]))
- gclient_eval.SetVar(local_scope, 'foo', 'baz')
- result = gclient_eval.RenderDEPSFile(local_scope)
- self.assertEqual(
- result, '\n'.join([
- 'vars = {',
- ' # Comment with trailing space ',
- ' "foo": \'baz\',',
- '}',
- ]))
- class CipdTest(unittest.TestCase):
- def test_gets_and_sets_cipd(self):
- local_scope = gclient_eval.Exec('\n'.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, '\n'.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('\n'.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, '\n'.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('\n'.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, '\n'.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('\n'.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('\n'.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('\n'.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('\n'.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(
- '\n'.join([
- 'vars = {',
- ' "foo": "bar",',
- '}',
- 'deps = {',
- ' "a_dep": "a{foo}b",',
- '}',
- ]), '<unknown>', vars_override)
- def test_supports_vars_inside_vars(self):
- deps_file = '\n'.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 = '\n'.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 = '\n'.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 = '\n'.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 = '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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(
- '\n'.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()
|