gclient_eval_unittest.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. #!/usr/bin/env vpython3
  2. # Copyright 2017 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import collections
  6. import itertools
  7. import logging
  8. import os
  9. import sys
  10. import unittest
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. from third_party import schema
  13. import metrics_utils
  14. # We have to disable monitoring before importing gclient.
  15. metrics_utils.COLLECT_METRICS = False
  16. import gclient
  17. import gclient_eval
  18. import gclient_utils
  19. # TODO: Should fix these warnings.
  20. # pylint: disable=line-too-long
  21. class GClientEvalTest(unittest.TestCase):
  22. def test_str(self):
  23. self.assertEqual('foo', gclient_eval._gclient_eval('"foo"'))
  24. def test_tuple(self):
  25. self.assertEqual(('a', 'b'), gclient_eval._gclient_eval('("a", "b")'))
  26. def test_list(self):
  27. self.assertEqual(['a', 'b'], gclient_eval._gclient_eval('["a", "b"]'))
  28. def test_dict(self):
  29. self.assertEqual({'a': 'b'}, gclient_eval._gclient_eval('{"a": "b"}'))
  30. def test_name_safe(self):
  31. self.assertEqual(True, gclient_eval._gclient_eval('True'))
  32. def test_name_unsafe(self):
  33. with self.assertRaises(ValueError) as cm:
  34. gclient_eval._gclient_eval('UnsafeName')
  35. self.assertIn('invalid name \'UnsafeName\'', str(cm.exception))
  36. def test_invalid_call(self):
  37. with self.assertRaises(ValueError) as cm:
  38. gclient_eval._gclient_eval('Foo("bar")')
  39. self.assertIn('Str and Var are the only allowed functions',
  40. str(cm.exception))
  41. def test_expands_vars(self):
  42. self.assertEqual(
  43. 'foo',
  44. gclient_eval._gclient_eval('Var("bar")', vars_dict={'bar': 'foo'}))
  45. self.assertEqual(
  46. 'baz',
  47. gclient_eval._gclient_eval(
  48. 'Var("bar")',
  49. vars_dict={'bar': gclient_eval.ConstantString('baz')}))
  50. def test_expands_vars_with_braces(self):
  51. self.assertEqual(
  52. 'foo',
  53. gclient_eval._gclient_eval('"{bar}"', vars_dict={'bar': 'foo'}))
  54. self.assertEqual(
  55. 'baz',
  56. gclient_eval._gclient_eval(
  57. '"{bar}"',
  58. vars_dict={'bar': gclient_eval.ConstantString('baz')}))
  59. def test_invalid_var(self):
  60. with self.assertRaises(KeyError) as cm:
  61. gclient_eval._gclient_eval('"{bar}"', vars_dict={})
  62. self.assertIn('bar was used as a variable, but was not declared',
  63. str(cm.exception))
  64. def test_plus(self):
  65. self.assertEqual('foo', gclient_eval._gclient_eval('"f" + "o" + "o"'))
  66. def test_format(self):
  67. self.assertEqual('foo', gclient_eval._gclient_eval('"%s" % "foo"'))
  68. def test_not_expression(self):
  69. with self.assertRaises(SyntaxError) as cm:
  70. gclient_eval._gclient_eval('def foo():\n pass')
  71. self.assertIn('invalid syntax', str(cm.exception))
  72. def test_not_whitelisted(self):
  73. with self.assertRaises(ValueError) as cm:
  74. gclient_eval._gclient_eval('[x for x in [1, 2, 3]]')
  75. self.assertIn('unexpected AST node: <_ast.ListComp object',
  76. str(cm.exception))
  77. def test_dict_ordered(self):
  78. for test_case in itertools.permutations(range(4)):
  79. input_data = ['{'] + ['"%s": "%s",' % (n, n)
  80. for n in test_case] + ['}']
  81. expected = [(str(n), str(n)) for n in test_case]
  82. result = gclient_eval._gclient_eval(''.join(input_data))
  83. self.assertEqual(expected, list(result.items()))
  84. class ExecTest(unittest.TestCase):
  85. def test_multiple_assignment(self):
  86. with self.assertRaises(ValueError) as cm:
  87. gclient_eval.Exec('a, b, c = "a", "b", "c"')
  88. self.assertIn('invalid assignment: target should be a name',
  89. str(cm.exception))
  90. def test_override(self):
  91. with self.assertRaises(ValueError) as cm:
  92. gclient_eval.Exec('a = "a"\na = "x"')
  93. self.assertIn('invalid assignment: overrides var \'a\'',
  94. str(cm.exception))
  95. def test_schema_wrong_type(self):
  96. with self.assertRaises(gclient_utils.Error):
  97. gclient_eval.Exec('include_rules = {}')
  98. def test_recursedeps_list(self):
  99. local_scope = gclient_eval.Exec(
  100. 'recursedeps = [["src/third_party/angle", "DEPS.chromium"]]')
  101. self.assertEqual(
  102. {'recursedeps': [['src/third_party/angle', 'DEPS.chromium']]},
  103. local_scope)
  104. def test_var(self):
  105. local_scope = gclient_eval.Exec('\n'.join([
  106. 'vars = {',
  107. ' "foo": "bar",',
  108. ' "baz": Str("quux")',
  109. '}',
  110. 'deps = {',
  111. ' "a_dep": "a" + Var("foo") + "b" + Var("baz"),',
  112. '}',
  113. ]))
  114. Str = gclient_eval.ConstantString
  115. self.assertEqual(
  116. {
  117. 'vars': {
  118. 'foo': 'bar',
  119. 'baz': Str('quux')
  120. },
  121. 'deps': {
  122. 'a_dep': 'abarbquux'
  123. },
  124. }, local_scope)
  125. def test_braces_var(self):
  126. local_scope = gclient_eval.Exec('\n'.join([
  127. 'vars = {',
  128. ' "foo": "bar",',
  129. ' "baz": Str("quux")',
  130. '}',
  131. 'deps = {',
  132. ' "a_dep": "a{foo}b{baz}",',
  133. '}',
  134. ]))
  135. Str = gclient_eval.ConstantString
  136. self.assertEqual(
  137. {
  138. 'vars': {
  139. 'foo': 'bar',
  140. 'baz': Str('quux')
  141. },
  142. 'deps': {
  143. 'a_dep': 'abarbquux'
  144. },
  145. }, local_scope)
  146. def test_empty_deps(self):
  147. local_scope = gclient_eval.Exec('deps = {}')
  148. self.assertEqual({'deps': {}}, local_scope)
  149. def test_overrides_vars(self):
  150. local_scope = gclient_eval.Exec('\n'.join([
  151. 'vars = {',
  152. ' "foo": "bar",',
  153. ' "quux": Str("quuz")',
  154. '}',
  155. 'deps = {',
  156. ' "a_dep": "a{foo}b",',
  157. ' "b_dep": "c{quux}d",',
  158. '}',
  159. ]),
  160. vars_override={
  161. 'foo': 'baz',
  162. 'quux': 'corge'
  163. })
  164. Str = gclient_eval.ConstantString
  165. self.assertEqual(
  166. {
  167. 'vars': {
  168. 'foo': 'bar',
  169. 'quux': Str('quuz')
  170. },
  171. 'deps': {
  172. 'a_dep': 'abazb',
  173. 'b_dep': 'ccorged'
  174. },
  175. }, local_scope)
  176. def test_doesnt_override_undeclared_vars(self):
  177. with self.assertRaises(KeyError) as cm:
  178. gclient_eval.Exec('\n'.join([
  179. 'vars = {',
  180. ' "foo": "bar",',
  181. '}',
  182. 'deps = {',
  183. ' "a_dep": "a{baz}b",',
  184. '}',
  185. ]),
  186. vars_override={'baz': 'lalala'})
  187. self.assertIn('baz was used as a variable, but was not declared',
  188. str(cm.exception))
  189. def test_doesnt_allow_duplicate_deps(self):
  190. with self.assertRaises(ValueError) as cm:
  191. gclient_eval.Parse(
  192. '\n'.join([
  193. 'deps = {',
  194. ' "a_dep": {',
  195. ' "url": "a_url@a_rev",',
  196. ' "condition": "foo",',
  197. ' },',
  198. ' "a_dep": {',
  199. ' "url": "a_url@another_rev",',
  200. ' "condition": "not foo",',
  201. ' }',
  202. '}',
  203. ]), '<unknown>')
  204. self.assertIn('duplicate key in dictionary: a_dep', str(cm.exception))
  205. class UpdateConditionTest(unittest.TestCase):
  206. def test_both_present(self):
  207. info = {'condition': 'foo'}
  208. gclient_eval.UpdateCondition(info, 'and', 'bar')
  209. self.assertEqual(info, {'condition': '(foo) and (bar)'})
  210. info = {'condition': 'foo'}
  211. gclient_eval.UpdateCondition(info, 'or', 'bar')
  212. self.assertEqual(info, {'condition': '(foo) or (bar)'})
  213. def test_one_present_and(self):
  214. # If one of info's condition or new_condition is present, and |op| ==
  215. # 'and' then the the result must be the present condition.
  216. info = {'condition': 'foo'}
  217. gclient_eval.UpdateCondition(info, 'and', None)
  218. self.assertEqual(info, {'condition': 'foo'})
  219. info = {}
  220. gclient_eval.UpdateCondition(info, 'and', 'bar')
  221. self.assertEqual(info, {'condition': 'bar'})
  222. def test_both_absent_and(self):
  223. # Nothing happens
  224. info = {}
  225. gclient_eval.UpdateCondition(info, 'and', None)
  226. self.assertEqual(info, {})
  227. def test_or(self):
  228. # If one of info's condition and new_condition is not present, then
  229. # there shouldn't be a condition. An absent value is treated as
  230. # implicitly True.
  231. info = {'condition': 'foo'}
  232. gclient_eval.UpdateCondition(info, 'or', None)
  233. self.assertEqual(info, {})
  234. info = {}
  235. gclient_eval.UpdateCondition(info, 'or', 'bar')
  236. self.assertEqual(info, {})
  237. info = {}
  238. gclient_eval.UpdateCondition(info, 'or', None)
  239. self.assertEqual(info, {})
  240. class EvaluateConditionTest(unittest.TestCase):
  241. def test_true(self):
  242. self.assertTrue(gclient_eval.EvaluateCondition('True', {}))
  243. def test_variable(self):
  244. self.assertFalse(gclient_eval.EvaluateCondition('foo',
  245. {'foo': 'False'}))
  246. def test_variable_cyclic_reference(self):
  247. with self.assertRaises(ValueError) as cm:
  248. self.assertTrue(
  249. gclient_eval.EvaluateCondition('bar', {'bar': 'bar'}))
  250. self.assertIn('invalid cyclic reference to \'bar\' (inside \'bar\')',
  251. str(cm.exception))
  252. def test_operators(self):
  253. self.assertFalse(
  254. gclient_eval.EvaluateCondition('a and not (b or c)', {
  255. 'a': 'True',
  256. 'b': 'False',
  257. 'c': 'True'
  258. }))
  259. def test_expansion(self):
  260. self.assertTrue(
  261. gclient_eval.EvaluateCondition('a or b', {
  262. 'a': 'b and c',
  263. 'b': 'not c',
  264. 'c': 'False'
  265. }))
  266. def test_string_equality(self):
  267. self.assertTrue(
  268. gclient_eval.EvaluateCondition('foo == "baz"', {'foo': '"baz"'}))
  269. self.assertFalse(
  270. gclient_eval.EvaluateCondition('foo == "bar"', {'foo': '"baz"'}))
  271. def test_string_inequality(self):
  272. self.assertTrue(
  273. gclient_eval.EvaluateCondition('foo != "bar"', {'foo': '"baz"'}))
  274. self.assertFalse(
  275. gclient_eval.EvaluateCondition('foo != "baz"', {'foo': '"baz"'}))
  276. def test_triple_or(self):
  277. self.assertTrue(
  278. gclient_eval.EvaluateCondition('a or b or c', {
  279. 'a': 'False',
  280. 'b': 'False',
  281. 'c': 'True'
  282. }))
  283. self.assertFalse(
  284. gclient_eval.EvaluateCondition('a or b or c', {
  285. 'a': 'False',
  286. 'b': 'False',
  287. 'c': 'False'
  288. }))
  289. def test_triple_and(self):
  290. self.assertTrue(
  291. gclient_eval.EvaluateCondition('a and b and c', {
  292. 'a': 'True',
  293. 'b': 'True',
  294. 'c': 'True'
  295. }))
  296. self.assertFalse(
  297. gclient_eval.EvaluateCondition('a and b and c', {
  298. 'a': 'True',
  299. 'b': 'True',
  300. 'c': 'False'
  301. }))
  302. def test_triple_and_and_or(self):
  303. self.assertTrue(
  304. gclient_eval.EvaluateCondition('a and b and c or d or e', {
  305. 'a': 'False',
  306. 'b': 'False',
  307. 'c': 'False',
  308. 'd': 'False',
  309. 'e': 'True'
  310. }))
  311. self.assertFalse(
  312. gclient_eval.EvaluateCondition('a and b and c or d or e', {
  313. 'a': 'True',
  314. 'b': 'True',
  315. 'c': 'False',
  316. 'd': 'False',
  317. 'e': 'False'
  318. }))
  319. def test_string_bool(self):
  320. self.assertFalse(
  321. gclient_eval.EvaluateCondition('false_str_var and true_var', {
  322. 'false_str_var': 'False',
  323. 'true_var': True
  324. }))
  325. def test_string_bool_typo(self):
  326. with self.assertRaises(ValueError) as cm:
  327. gclient_eval.EvaluateCondition('false_var_str and true_var', {
  328. 'false_str_var': 'False',
  329. 'true_var': True
  330. })
  331. self.assertIn(
  332. 'invalid "and" operand \'false_var_str\' '
  333. '(inside \'false_var_str and true_var\')', str(cm.exception))
  334. def test_non_bool_in_or(self):
  335. with self.assertRaises(ValueError) as cm:
  336. gclient_eval.EvaluateCondition('string_var or true_var', {
  337. 'string_var': 'Kittens',
  338. 'true_var': True
  339. })
  340. self.assertIn(
  341. 'invalid "or" operand \'Kittens\' '
  342. '(inside \'string_var or true_var\')', str(cm.exception))
  343. def test_non_bool_in_and(self):
  344. with self.assertRaises(ValueError) as cm:
  345. gclient_eval.EvaluateCondition('string_var and true_var', {
  346. 'string_var': 'Kittens',
  347. 'true_var': True
  348. })
  349. self.assertIn(
  350. 'invalid "and" operand \'Kittens\' '
  351. '(inside \'string_var and true_var\')', str(cm.exception))
  352. def test_tuple_presence(self):
  353. self.assertTrue(
  354. gclient_eval.EvaluateCondition('foo in ("bar", "baz")',
  355. {'foo': 'bar'}))
  356. self.assertFalse(
  357. gclient_eval.EvaluateCondition('foo in ("bar", "baz")',
  358. {'foo': 'not_bar'}))
  359. def test_unsupported_tuple_operation(self):
  360. with self.assertRaises(ValueError) as cm:
  361. gclient_eval.EvaluateCondition('foo == ("bar", "baz")',
  362. {'foo': 'bar'})
  363. self.assertIn('unexpected AST node', str(cm.exception))
  364. with self.assertRaises(ValueError) as cm:
  365. gclient_eval.EvaluateCondition('(foo,) == "bar"', {'foo': 'bar'})
  366. self.assertIn('unexpected AST node', str(cm.exception))
  367. def test_str_in_condition(self):
  368. Str = gclient_eval.ConstantString
  369. self.assertTrue(
  370. gclient_eval.EvaluateCondition('s_var == "foo"',
  371. {'s_var': Str("foo")}))
  372. self.assertFalse(
  373. gclient_eval.EvaluateCondition('s_var in ("baz", "quux")',
  374. {'s_var': Str("foo")}))
  375. class VarTest(unittest.TestCase):
  376. def assert_adds_var(self, before, after):
  377. local_scope = gclient_eval.Exec('\n'.join(before))
  378. gclient_eval.AddVar(local_scope, 'baz', 'lemur')
  379. results = gclient_eval.RenderDEPSFile(local_scope)
  380. self.assertEqual(results, '\n'.join(after))
  381. def test_adds_var(self):
  382. before = [
  383. 'vars = {',
  384. ' "foo": "bar",',
  385. '}',
  386. ]
  387. after = [
  388. 'vars = {',
  389. ' "baz": "lemur",',
  390. ' "foo": "bar",',
  391. '}',
  392. ]
  393. self.assert_adds_var(before, after)
  394. def test_adds_var_twice(self):
  395. local_scope = gclient_eval.Exec('\n'.join([
  396. 'vars = {',
  397. ' "foo": "bar",',
  398. '}',
  399. ]))
  400. gclient_eval.AddVar(local_scope, 'baz', 'lemur')
  401. gclient_eval.AddVar(local_scope, 'v8_revision', 'deadbeef')
  402. result = gclient_eval.RenderDEPSFile(local_scope)
  403. self.assertEqual(
  404. result, '\n'.join([
  405. 'vars = {',
  406. ' "v8_revision": "deadbeef",',
  407. ' "baz": "lemur",',
  408. ' "foo": "bar",',
  409. '}',
  410. ]))
  411. def test_gets_and_sets_var(self):
  412. local_scope = gclient_eval.Exec('\n'.join([
  413. 'vars = {',
  414. ' "foo": "bar",',
  415. ' "quux": Str("quuz")',
  416. '}',
  417. ]))
  418. self.assertEqual(gclient_eval.GetVar(local_scope, 'foo'), "bar")
  419. self.assertEqual(gclient_eval.GetVar(local_scope, 'quux'), "quuz")
  420. gclient_eval.SetVar(local_scope, 'foo', 'baz')
  421. gclient_eval.SetVar(local_scope, 'quux', 'corge')
  422. result = gclient_eval.RenderDEPSFile(local_scope)
  423. self.assertEqual(
  424. result, '\n'.join([
  425. 'vars = {',
  426. ' "foo": "baz",',
  427. ' "quux": Str("corge")',
  428. '}',
  429. ]))
  430. def test_gets_and_sets_var_non_string(self):
  431. local_scope = gclient_eval.Exec('\n'.join([
  432. 'vars = {',
  433. ' "foo": True,',
  434. '}',
  435. ]))
  436. result = gclient_eval.GetVar(local_scope, 'foo')
  437. self.assertEqual(result, True)
  438. gclient_eval.SetVar(local_scope, 'foo', 'False')
  439. result = gclient_eval.RenderDEPSFile(local_scope)
  440. self.assertEqual(result, '\n'.join([
  441. 'vars = {',
  442. ' "foo": False,',
  443. '}',
  444. ]))
  445. def test_add_preserves_formatting(self):
  446. before = [
  447. '# Copyright stuff',
  448. '# some initial comments',
  449. '',
  450. 'vars = { ',
  451. ' # Some comments.',
  452. ' "foo": "bar",',
  453. '',
  454. ' # More comments.',
  455. ' # Even more comments.',
  456. ' "v8_revision": ',
  457. ' "deadbeef",',
  458. ' # Someone formatted this wrong',
  459. '}',
  460. ]
  461. after = [
  462. '# Copyright stuff',
  463. '# some initial comments',
  464. '',
  465. 'vars = { ',
  466. ' "baz": "lemur",',
  467. ' # Some comments.',
  468. ' "foo": "bar",',
  469. '',
  470. ' # More comments.',
  471. ' # Even more comments.',
  472. ' "v8_revision": ',
  473. ' "deadbeef",',
  474. ' # Someone formatted this wrong',
  475. '}',
  476. ]
  477. self.assert_adds_var(before, after)
  478. def test_set_preserves_formatting(self):
  479. local_scope = gclient_eval.Exec('\n'.join([
  480. 'vars = {',
  481. ' # Comment with trailing space ',
  482. ' "foo": \'bar\',',
  483. '}',
  484. ]))
  485. gclient_eval.SetVar(local_scope, 'foo', 'baz')
  486. result = gclient_eval.RenderDEPSFile(local_scope)
  487. self.assertEqual(
  488. result, '\n'.join([
  489. 'vars = {',
  490. ' # Comment with trailing space ',
  491. ' "foo": \'baz\',',
  492. '}',
  493. ]))
  494. class CipdTest(unittest.TestCase):
  495. def test_gets_and_sets_cipd(self):
  496. local_scope = gclient_eval.Exec('\n'.join([
  497. 'deps = {',
  498. ' "src/cipd/package": {',
  499. ' "packages": [',
  500. ' {',
  501. ' "package": "some/cipd/package",',
  502. ' "version": "deadbeef",',
  503. ' },',
  504. ' {',
  505. ' "package": "another/cipd/package",',
  506. ' "version": "version:5678",',
  507. ' },',
  508. ' ],',
  509. ' "condition": "checkout_android",',
  510. ' "dep_type": "cipd",',
  511. ' },',
  512. '}',
  513. ]))
  514. self.assertEqual(
  515. gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
  516. 'some/cipd/package'), 'deadbeef')
  517. self.assertEqual(
  518. gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
  519. 'another/cipd/package'), 'version:5678')
  520. gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
  521. 'another/cipd/package', 'version:6789')
  522. gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
  523. 'some/cipd/package', 'foobar')
  524. result = gclient_eval.RenderDEPSFile(local_scope)
  525. self.assertEqual(
  526. result, '\n'.join([
  527. 'deps = {',
  528. ' "src/cipd/package": {',
  529. ' "packages": [',
  530. ' {',
  531. ' "package": "some/cipd/package",',
  532. ' "version": "foobar",',
  533. ' },',
  534. ' {',
  535. ' "package": "another/cipd/package",',
  536. ' "version": "version:6789",',
  537. ' },',
  538. ' ],',
  539. ' "condition": "checkout_android",',
  540. ' "dep_type": "cipd",',
  541. ' },',
  542. '}',
  543. ]))
  544. def test_gets_and_sets_cipd_vars(self):
  545. local_scope = gclient_eval.Exec('\n'.join([
  546. 'vars = {',
  547. ' "cipd-rev": "git_revision:deadbeef",',
  548. ' "another-cipd-rev": "version:1.0.3",',
  549. '}',
  550. 'deps = {',
  551. ' "src/cipd/package": {',
  552. ' "packages": [',
  553. ' {',
  554. ' "package": "some/cipd/package",',
  555. ' "version": Var("cipd-rev"),',
  556. ' },',
  557. ' {',
  558. ' "package": "another/cipd/package",',
  559. ' "version": "{another-cipd-rev}",',
  560. ' },',
  561. ' ],',
  562. ' "condition": "checkout_android",',
  563. ' "dep_type": "cipd",',
  564. ' },',
  565. '}',
  566. ]))
  567. self.assertEqual(
  568. gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
  569. 'some/cipd/package'), 'git_revision:deadbeef')
  570. self.assertEqual(
  571. gclient_eval.GetCIPD(local_scope, 'src/cipd/package',
  572. 'another/cipd/package'), 'version:1.0.3')
  573. gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
  574. 'another/cipd/package', 'version:1.1.0')
  575. gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
  576. 'some/cipd/package', 'git_revision:foobar')
  577. result = gclient_eval.RenderDEPSFile(local_scope)
  578. self.assertEqual(
  579. result, '\n'.join([
  580. 'vars = {',
  581. ' "cipd-rev": "git_revision:foobar",',
  582. ' "another-cipd-rev": "version:1.1.0",',
  583. '}',
  584. 'deps = {',
  585. ' "src/cipd/package": {',
  586. ' "packages": [',
  587. ' {',
  588. ' "package": "some/cipd/package",',
  589. ' "version": Var("cipd-rev"),',
  590. ' },',
  591. ' {',
  592. ' "package": "another/cipd/package",',
  593. ' "version": "{another-cipd-rev}",',
  594. ' },',
  595. ' ],',
  596. ' "condition": "checkout_android",',
  597. ' "dep_type": "cipd",',
  598. ' },',
  599. '}',
  600. ]))
  601. def test_preserves_escaped_vars(self):
  602. local_scope = gclient_eval.Exec('\n'.join([
  603. 'deps = {',
  604. ' "src/cipd/package": {',
  605. ' "packages": [',
  606. ' {',
  607. ' "package": "package/${{platform}}",',
  608. ' "version": "version:abcd",',
  609. ' },',
  610. ' ],',
  611. ' "dep_type": "cipd",',
  612. ' },',
  613. '}',
  614. ]))
  615. gclient_eval.SetCIPD(local_scope, 'src/cipd/package',
  616. 'package/${platform}', 'version:dcba')
  617. result = gclient_eval.RenderDEPSFile(local_scope)
  618. self.assertEqual(
  619. result, '\n'.join([
  620. 'deps = {',
  621. ' "src/cipd/package": {',
  622. ' "packages": [',
  623. ' {',
  624. ' "package": "package/${{platform}}",',
  625. ' "version": "version:dcba",',
  626. ' },',
  627. ' ],',
  628. ' "dep_type": "cipd",',
  629. ' },',
  630. '}',
  631. ]))
  632. class RevisionTest(unittest.TestCase):
  633. def assert_gets_and_sets_revision(self,
  634. before,
  635. after,
  636. rev_before='deadbeef'):
  637. local_scope = gclient_eval.Exec('\n'.join(before))
  638. result = gclient_eval.GetRevision(local_scope, 'src/dep')
  639. self.assertEqual(result, rev_before)
  640. gclient_eval.SetRevision(local_scope, 'src/dep', 'deadfeed')
  641. self.assertEqual('\n'.join(after),
  642. gclient_eval.RenderDEPSFile(local_scope))
  643. def test_revision(self):
  644. before = [
  645. 'deps = {',
  646. ' "src/dep": "https://example.com/dep.git@deadbeef",',
  647. '}',
  648. ]
  649. after = [
  650. 'deps = {',
  651. ' "src/dep": "https://example.com/dep.git@deadfeed",',
  652. '}',
  653. ]
  654. self.assert_gets_and_sets_revision(before, after)
  655. def test_revision_new_line(self):
  656. before = [
  657. 'deps = {',
  658. ' "src/dep": "https://example.com/dep.git@"',
  659. ' + "deadbeef",',
  660. '}',
  661. ]
  662. after = [
  663. 'deps = {',
  664. ' "src/dep": "https://example.com/dep.git@"',
  665. ' + "deadfeed",',
  666. '}',
  667. ]
  668. self.assert_gets_and_sets_revision(before, after)
  669. def test_revision_windows_local_path(self):
  670. before = [
  671. 'deps = {',
  672. ' "src/dep": "file:///C:\\\\path.git@deadbeef",',
  673. '}',
  674. ]
  675. after = [
  676. 'deps = {',
  677. ' "src/dep": "file:///C:\\\\path.git@deadfeed",',
  678. '}',
  679. ]
  680. self.assert_gets_and_sets_revision(before, after)
  681. def test_revision_multiline_strings(self):
  682. deps = [
  683. 'deps = {',
  684. ' "src/dep": "https://example.com/dep.git@"',
  685. ' "deadbeef",',
  686. '}',
  687. ]
  688. with self.assertRaises(ValueError) as e:
  689. local_scope = gclient_eval.Exec('\n'.join(deps))
  690. gclient_eval.SetRevision(local_scope, 'src/dep', 'deadfeed')
  691. self.assertEqual(
  692. 'Can\'t update value for src/dep. Multiline strings and implicitly '
  693. 'concatenated strings are not supported.\n'
  694. 'Consider reformatting the DEPS file.', str(e.exception))
  695. def test_revision_implicitly_concatenated_strings(self):
  696. deps = [
  697. 'deps = {',
  698. ' "src/dep": "https://example.com" + "/dep.git@" "deadbeef",',
  699. '}',
  700. ]
  701. with self.assertRaises(ValueError) as e:
  702. local_scope = gclient_eval.Exec('\n'.join(deps))
  703. gclient_eval.SetRevision(local_scope, 'src/dep', 'deadfeed')
  704. self.assertEqual(
  705. 'Can\'t update value for src/dep. Multiline strings and implicitly '
  706. 'concatenated strings are not supported.\n'
  707. 'Consider reformatting the DEPS file.', str(e.exception))
  708. def test_revision_inside_dict(self):
  709. before = [
  710. 'deps = {',
  711. ' "src/dep": {',
  712. ' "url": "https://example.com/dep.git@deadbeef",',
  713. ' "condition": "some_condition",',
  714. ' },',
  715. '}',
  716. ]
  717. after = [
  718. 'deps = {',
  719. ' "src/dep": {',
  720. ' "url": "https://example.com/dep.git@deadfeed",',
  721. ' "condition": "some_condition",',
  722. ' },',
  723. '}',
  724. ]
  725. self.assert_gets_and_sets_revision(before, after)
  726. def test_follows_var_braces(self):
  727. before = [
  728. 'vars = {',
  729. ' "dep_revision": "deadbeef",',
  730. '}',
  731. 'deps = {',
  732. ' "src/dep": "https://example.com/dep.git@{dep_revision}",',
  733. '}',
  734. ]
  735. after = [
  736. 'vars = {',
  737. ' "dep_revision": "deadfeed",',
  738. '}',
  739. 'deps = {',
  740. ' "src/dep": "https://example.com/dep.git@{dep_revision}",',
  741. '}',
  742. ]
  743. self.assert_gets_and_sets_revision(before, after)
  744. def test_follows_var_braces_newline(self):
  745. before = [
  746. 'vars = {',
  747. ' "dep_revision": "deadbeef",',
  748. '}',
  749. 'deps = {',
  750. ' "src/dep": "https://example.com/dep.git"',
  751. ' + "@{dep_revision}",',
  752. '}',
  753. ]
  754. after = [
  755. 'vars = {',
  756. ' "dep_revision": "deadfeed",',
  757. '}',
  758. 'deps = {',
  759. ' "src/dep": "https://example.com/dep.git"',
  760. ' + "@{dep_revision}",',
  761. '}',
  762. ]
  763. self.assert_gets_and_sets_revision(before, after)
  764. def test_follows_var_function(self):
  765. before = [
  766. 'vars = {',
  767. ' "dep_revision": "deadbeef",',
  768. '}',
  769. 'deps = {',
  770. ' "src/dep": "https://example.com/dep.git@" + Var("dep_revision"),',
  771. '}',
  772. ]
  773. after = [
  774. 'vars = {',
  775. ' "dep_revision": "deadfeed",',
  776. '}',
  777. 'deps = {',
  778. ' "src/dep": "https://example.com/dep.git@" + Var("dep_revision"),',
  779. '}',
  780. ]
  781. self.assert_gets_and_sets_revision(before, after)
  782. def test_pins_revision(self):
  783. before = [
  784. 'deps = {',
  785. ' "src/dep": "https://example.com/dep.git",',
  786. '}',
  787. ]
  788. after = [
  789. 'deps = {',
  790. ' "src/dep": "https://example.com/dep.git@deadfeed",',
  791. '}',
  792. ]
  793. self.assert_gets_and_sets_revision(before, after, rev_before=None)
  794. def test_preserves_variables(self):
  795. before = [
  796. 'vars = {',
  797. ' "src_root": "src"',
  798. '}',
  799. 'deps = {',
  800. ' "{src_root}/dep": "https://example.com/dep.git@deadbeef",',
  801. '}',
  802. ]
  803. after = [
  804. 'vars = {',
  805. ' "src_root": "src"',
  806. '}',
  807. 'deps = {',
  808. ' "{src_root}/dep": "https://example.com/dep.git@deadfeed",',
  809. '}',
  810. ]
  811. self.assert_gets_and_sets_revision(before, after)
  812. def test_preserves_formatting(self):
  813. before = [
  814. 'vars = {',
  815. ' # Some comment on deadbeef ',
  816. ' "dep_revision": "deadbeef",',
  817. '}',
  818. 'deps = {',
  819. ' "src/dep": {',
  820. ' "url": "https://example.com/dep.git@" + Var("dep_revision"),',
  821. '',
  822. ' "condition": "some_condition",',
  823. ' },',
  824. '}',
  825. ]
  826. after = [
  827. 'vars = {',
  828. ' # Some comment on deadbeef ',
  829. ' "dep_revision": "deadfeed",',
  830. '}',
  831. 'deps = {',
  832. ' "src/dep": {',
  833. ' "url": "https://example.com/dep.git@" + Var("dep_revision"),',
  834. '',
  835. ' "condition": "some_condition",',
  836. ' },',
  837. '}',
  838. ]
  839. self.assert_gets_and_sets_revision(before, after)
  840. class ParseTest(unittest.TestCase):
  841. def callParse(self, vars_override=None):
  842. return gclient_eval.Parse(
  843. '\n'.join([
  844. 'vars = {',
  845. ' "foo": "bar",',
  846. '}',
  847. 'deps = {',
  848. ' "a_dep": "a{foo}b",',
  849. '}',
  850. ]), '<unknown>', vars_override)
  851. def test_supports_vars_inside_vars(self):
  852. deps_file = '\n'.join([
  853. 'vars = {',
  854. ' "foo": "bar",',
  855. ' "baz": "\\"{foo}\\" == \\"bar\\"",',
  856. '}',
  857. 'deps = {',
  858. ' "src/baz": {',
  859. ' "url": "baz_url",',
  860. ' "condition": "baz",',
  861. ' },',
  862. '}',
  863. ])
  864. local_scope = gclient_eval.Parse(deps_file, '<unknown>', None)
  865. self.assertEqual(
  866. {
  867. 'vars': {
  868. 'foo': 'bar',
  869. 'baz': '"bar" == "bar"'
  870. },
  871. 'deps': {
  872. 'src/baz': {
  873. 'url': 'baz_url',
  874. 'dep_type': 'git',
  875. 'condition': 'baz'
  876. }
  877. },
  878. }, local_scope)
  879. def test_has_builtin_vars(self):
  880. builtin_vars = {'builtin_var': 'foo'}
  881. deps_file = '\n'.join([
  882. 'deps = {',
  883. ' "a_dep": "a{builtin_var}b",',
  884. '}',
  885. ])
  886. local_scope = gclient_eval.Parse(deps_file, '<unknown>', None,
  887. builtin_vars)
  888. self.assertEqual(
  889. {
  890. 'deps': {
  891. 'a_dep': {
  892. 'url': 'afoob',
  893. 'dep_type': 'git'
  894. }
  895. },
  896. }, local_scope)
  897. def test_declaring_builtin_var_has_no_effect(self):
  898. builtin_vars = {'builtin_var': 'foo'}
  899. deps_file = '\n'.join([
  900. 'vars = {',
  901. ' "builtin_var": "bar",',
  902. '}',
  903. 'deps = {',
  904. ' "a_dep": "a{builtin_var}b",',
  905. '}',
  906. ])
  907. local_scope = gclient_eval.Parse(deps_file, '<unknown>', None,
  908. builtin_vars)
  909. self.assertEqual(
  910. {
  911. 'vars': {
  912. 'builtin_var': 'bar'
  913. },
  914. 'deps': {
  915. 'a_dep': {
  916. 'url': 'afoob',
  917. 'dep_type': 'git'
  918. }
  919. },
  920. }, local_scope)
  921. def test_override_builtin_var(self):
  922. builtin_vars = {'builtin_var': 'foo'}
  923. vars_override = {'builtin_var': 'override'}
  924. deps_file = '\n'.join([
  925. 'deps = {',
  926. ' "a_dep": "a{builtin_var}b",',
  927. '}',
  928. ])
  929. local_scope = gclient_eval.Parse(deps_file, '<unknown>', vars_override,
  930. builtin_vars)
  931. self.assertEqual(
  932. {
  933. 'deps': {
  934. 'a_dep': {
  935. 'url': 'aoverrideb',
  936. 'dep_type': 'git'
  937. }
  938. },
  939. }, local_scope, str(local_scope))
  940. def test_expands_vars(self):
  941. local_scope = self.callParse()
  942. self.assertEqual(
  943. {
  944. 'vars': {
  945. 'foo': 'bar'
  946. },
  947. 'deps': {
  948. 'a_dep': {
  949. 'url': 'abarb',
  950. 'dep_type': 'git'
  951. }
  952. },
  953. }, local_scope)
  954. def test_overrides_vars(self):
  955. local_scope = self.callParse(vars_override={'foo': 'baz'})
  956. self.assertEqual(
  957. {
  958. 'vars': {
  959. 'foo': 'bar'
  960. },
  961. 'deps': {
  962. 'a_dep': {
  963. 'url': 'abazb',
  964. 'dep_type': 'git'
  965. }
  966. },
  967. }, local_scope)
  968. def test_no_extra_vars(self):
  969. deps_file = '\n'.join([
  970. 'vars = {',
  971. ' "foo": "bar",',
  972. '}',
  973. 'deps = {',
  974. ' "a_dep": "a{baz}b",',
  975. '}',
  976. ])
  977. with self.assertRaises(KeyError) as cm:
  978. gclient_eval.Parse(deps_file, '<unknown>', {'baz': 'lalala'})
  979. self.assertIn('baz was used as a variable, but was not declared',
  980. str(cm.exception))
  981. def test_standardizes_deps_string_dep(self):
  982. local_scope = gclient_eval.Parse(
  983. '\n'.join([
  984. 'deps = {',
  985. ' "a_dep": "a_url@a_rev",',
  986. '}',
  987. ]), '<unknown>')
  988. self.assertEqual(
  989. {
  990. 'deps': {
  991. 'a_dep': {
  992. 'url': 'a_url@a_rev',
  993. 'dep_type': 'git'
  994. }
  995. },
  996. }, local_scope)
  997. def test_standardizes_deps_dict_dep(self):
  998. local_scope = gclient_eval.Parse(
  999. '\n'.join([
  1000. 'deps = {',
  1001. ' "a_dep": {',
  1002. ' "url": "a_url@a_rev",',
  1003. ' "condition": "checkout_android",',
  1004. ' },',
  1005. '}',
  1006. ]), '<unknown>')
  1007. self.assertEqual(
  1008. {
  1009. 'deps': {
  1010. 'a_dep': {
  1011. 'url': 'a_url@a_rev',
  1012. 'dep_type': 'git',
  1013. 'condition': 'checkout_android'
  1014. }
  1015. },
  1016. }, local_scope)
  1017. def test_ignores_none_in_deps_os(self):
  1018. local_scope = gclient_eval.Parse(
  1019. '\n'.join([
  1020. 'deps = {',
  1021. ' "a_dep": "a_url@a_rev",',
  1022. '}',
  1023. 'deps_os = {',
  1024. ' "mac": {',
  1025. ' "a_dep": None,',
  1026. ' },',
  1027. '}',
  1028. ]), '<unknown>')
  1029. self.assertEqual(
  1030. {
  1031. 'deps': {
  1032. 'a_dep': {
  1033. 'url': 'a_url@a_rev',
  1034. 'dep_type': 'git'
  1035. }
  1036. },
  1037. }, local_scope)
  1038. def test_merges_deps_os_extra_dep(self):
  1039. local_scope = gclient_eval.Parse(
  1040. '\n'.join([
  1041. 'deps = {',
  1042. ' "a_dep": "a_url@a_rev",',
  1043. '}',
  1044. 'deps_os = {',
  1045. ' "mac": {',
  1046. ' "b_dep": "b_url@b_rev"',
  1047. ' },',
  1048. '}',
  1049. ]), '<unknown>')
  1050. self.assertEqual(
  1051. {
  1052. 'deps': {
  1053. 'a_dep': {
  1054. 'url': 'a_url@a_rev',
  1055. 'dep_type': 'git'
  1056. },
  1057. 'b_dep': {
  1058. 'url': 'b_url@b_rev',
  1059. 'dep_type': 'git',
  1060. 'condition': 'checkout_mac'
  1061. }
  1062. },
  1063. }, local_scope)
  1064. def test_merges_deps_os_existing_dep_with_no_condition(self):
  1065. local_scope = gclient_eval.Parse(
  1066. '\n'.join([
  1067. 'deps = {',
  1068. ' "a_dep": "a_url@a_rev",',
  1069. '}',
  1070. 'deps_os = {',
  1071. ' "mac": {',
  1072. ' "a_dep": "a_url@a_rev"',
  1073. ' },',
  1074. '}',
  1075. ]), '<unknown>')
  1076. self.assertEqual(
  1077. {
  1078. 'deps': {
  1079. 'a_dep': {
  1080. 'url': 'a_url@a_rev',
  1081. 'dep_type': 'git'
  1082. }
  1083. },
  1084. }, local_scope)
  1085. def test_merges_deps_os_existing_dep_with_condition(self):
  1086. local_scope = gclient_eval.Parse(
  1087. '\n'.join([
  1088. 'deps = {',
  1089. ' "a_dep": {',
  1090. ' "url": "a_url@a_rev",',
  1091. ' "condition": "some_condition",',
  1092. ' },',
  1093. '}',
  1094. 'deps_os = {',
  1095. ' "mac": {',
  1096. ' "a_dep": "a_url@a_rev"',
  1097. ' },',
  1098. '}',
  1099. ]), '<unknown>')
  1100. self.assertEqual(
  1101. {
  1102. 'deps': {
  1103. 'a_dep': {
  1104. 'url': 'a_url@a_rev',
  1105. 'dep_type': 'git',
  1106. 'condition': '(checkout_mac) or (some_condition)'
  1107. },
  1108. },
  1109. }, local_scope)
  1110. def test_merges_deps_os_multiple_os(self):
  1111. local_scope = gclient_eval.Parse(
  1112. '\n'.join([
  1113. 'deps_os = {',
  1114. ' "win": {'
  1115. ' "a_dep": "a_url@a_rev"',
  1116. ' },',
  1117. ' "mac": {',
  1118. ' "a_dep": "a_url@a_rev"',
  1119. ' },',
  1120. '}',
  1121. ]), '<unknown>')
  1122. self.assertEqual(
  1123. {
  1124. 'deps': {
  1125. 'a_dep': {
  1126. 'url': 'a_url@a_rev',
  1127. 'dep_type': 'git',
  1128. 'condition': '(checkout_mac) or (checkout_win)'
  1129. },
  1130. },
  1131. }, local_scope)
  1132. def test_fails_to_merge_same_dep_with_different_revisions(self):
  1133. with self.assertRaises(gclient_eval.gclient_utils.Error) as cm:
  1134. gclient_eval.Parse(
  1135. '\n'.join([
  1136. 'deps = {',
  1137. ' "a_dep": {',
  1138. ' "url": "a_url@a_rev",',
  1139. ' "condition": "some_condition",',
  1140. ' },',
  1141. '}',
  1142. 'deps_os = {',
  1143. ' "mac": {',
  1144. ' "a_dep": "a_url@b_rev"',
  1145. ' },',
  1146. '}',
  1147. ]), '<unknown>')
  1148. self.assertIn('conflicts with existing deps', str(cm.exception))
  1149. def test_merges_hooks_os(self):
  1150. local_scope = gclient_eval.Parse(
  1151. '\n'.join([
  1152. 'hooks = [',
  1153. ' {',
  1154. ' "action": ["a", "action"],',
  1155. ' },',
  1156. ']',
  1157. 'hooks_os = {',
  1158. ' "mac": [',
  1159. ' {',
  1160. ' "action": ["b", "action"]',
  1161. ' },',
  1162. ' ]',
  1163. '}',
  1164. ]), '<unknown>')
  1165. self.assertEqual(
  1166. {
  1167. "hooks": [{
  1168. "action": ["a", "action"]
  1169. }, {
  1170. "action": ["b", "action"],
  1171. "condition": "checkout_mac"
  1172. }],
  1173. }, local_scope)
  1174. if __name__ == '__main__':
  1175. level = logging.DEBUG if '-v' in sys.argv else logging.FATAL
  1176. logging.basicConfig(level=level,
  1177. format='%(asctime).19s %(levelname)s %(filename)s:'
  1178. '%(lineno)s %(message)s')
  1179. unittest.main()