gclient_eval_unittest.py 43 KB

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