utils.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. #
  2. # BitBake Tests for utils.py
  3. #
  4. # Copyright (C) 2012 Richard Purdie
  5. #
  6. # SPDX-License-Identifier: GPL-2.0-only
  7. #
  8. import unittest
  9. import bb
  10. import os
  11. import tempfile
  12. import re
  13. class VerCmpString(unittest.TestCase):
  14. def test_vercmpstring(self):
  15. result = bb.utils.vercmp_string('1', '2')
  16. self.assertTrue(result < 0)
  17. result = bb.utils.vercmp_string('2', '1')
  18. self.assertTrue(result > 0)
  19. result = bb.utils.vercmp_string('1', '1.0')
  20. self.assertTrue(result < 0)
  21. result = bb.utils.vercmp_string('1', '1.1')
  22. self.assertTrue(result < 0)
  23. result = bb.utils.vercmp_string('1.1', '1_p2')
  24. self.assertTrue(result < 0)
  25. result = bb.utils.vercmp_string('1.0', '1.0+1.1-beta1')
  26. self.assertTrue(result < 0)
  27. result = bb.utils.vercmp_string('1.1', '1.0+1.1-beta1')
  28. self.assertTrue(result > 0)
  29. result = bb.utils.vercmp_string('1.', '1.1')
  30. self.assertTrue(result < 0)
  31. result = bb.utils.vercmp_string('1.1', '1.')
  32. self.assertTrue(result > 0)
  33. def test_explode_dep_versions(self):
  34. correctresult = {"foo" : ["= 1.10"]}
  35. result = bb.utils.explode_dep_versions2("foo (= 1.10)")
  36. self.assertEqual(result, correctresult)
  37. result = bb.utils.explode_dep_versions2("foo (=1.10)")
  38. self.assertEqual(result, correctresult)
  39. result = bb.utils.explode_dep_versions2("foo ( = 1.10)")
  40. self.assertEqual(result, correctresult)
  41. result = bb.utils.explode_dep_versions2("foo ( =1.10)")
  42. self.assertEqual(result, correctresult)
  43. result = bb.utils.explode_dep_versions2("foo ( = 1.10 )")
  44. self.assertEqual(result, correctresult)
  45. result = bb.utils.explode_dep_versions2("foo ( =1.10 )")
  46. self.assertEqual(result, correctresult)
  47. def test_vercmp_string_op(self):
  48. compareops = [('1', '1', '=', True),
  49. ('1', '1', '==', True),
  50. ('1', '1', '!=', False),
  51. ('1', '1', '>', False),
  52. ('1', '1', '<', False),
  53. ('1', '1', '>=', True),
  54. ('1', '1', '<=', True),
  55. ('1', '0', '=', False),
  56. ('1', '0', '==', False),
  57. ('1', '0', '!=', True),
  58. ('1', '0', '>', True),
  59. ('1', '0', '<', False),
  60. ('1', '0', '>>', True),
  61. ('1', '0', '<<', False),
  62. ('1', '0', '>=', True),
  63. ('1', '0', '<=', False),
  64. ('0', '1', '=', False),
  65. ('0', '1', '==', False),
  66. ('0', '1', '!=', True),
  67. ('0', '1', '>', False),
  68. ('0', '1', '<', True),
  69. ('0', '1', '>>', False),
  70. ('0', '1', '<<', True),
  71. ('0', '1', '>=', False),
  72. ('0', '1', '<=', True)]
  73. for arg1, arg2, op, correctresult in compareops:
  74. result = bb.utils.vercmp_string_op(arg1, arg2, op)
  75. self.assertEqual(result, correctresult, 'vercmp_string_op("%s", "%s", "%s") != %s' % (arg1, arg2, op, correctresult))
  76. # Check that clearly invalid operator raises an exception
  77. self.assertRaises(bb.utils.VersionStringException, bb.utils.vercmp_string_op, '0', '0', '$')
  78. class Path(unittest.TestCase):
  79. def test_unsafe_delete_path(self):
  80. checkitems = [('/', True),
  81. ('//', True),
  82. ('///', True),
  83. (os.getcwd().count(os.sep) * ('..' + os.sep), True),
  84. (os.environ.get('HOME', '/home/test'), True),
  85. ('/home/someone', True),
  86. ('/home/other/', True),
  87. ('/home/other/subdir', False),
  88. ('', False)]
  89. for arg1, correctresult in checkitems:
  90. result = bb.utils._check_unsafe_delete_path(arg1)
  91. self.assertEqual(result, correctresult, '_check_unsafe_delete_path("%s") != %s' % (arg1, correctresult))
  92. class EditMetadataFile(unittest.TestCase):
  93. _origfile = """
  94. # A comment
  95. HELLO = "oldvalue"
  96. THIS = "that"
  97. # Another comment
  98. NOCHANGE = "samevalue"
  99. OTHER = 'anothervalue'
  100. MULTILINE = "a1 \\
  101. a2 \\
  102. a3"
  103. MULTILINE2 := " \\
  104. b1 \\
  105. b2 \\
  106. b3 \\
  107. "
  108. MULTILINE3 = " \\
  109. c1 \\
  110. c2 \\
  111. c3 \\
  112. "
  113. do_functionname() {
  114. command1 ${VAL1} ${VAL2}
  115. command2 ${VAL3} ${VAL4}
  116. }
  117. """
  118. def _testeditfile(self, varvalues, compareto, dummyvars=None):
  119. if dummyvars is None:
  120. dummyvars = []
  121. with tempfile.NamedTemporaryFile('w', delete=False) as tf:
  122. tf.write(self._origfile)
  123. tf.close()
  124. try:
  125. varcalls = []
  126. def handle_file(varname, origvalue, op, newlines):
  127. self.assertIn(varname, varvalues, 'Callback called for variable %s not in the list!' % varname)
  128. self.assertNotIn(varname, dummyvars, 'Callback called for variable %s in dummy list!' % varname)
  129. varcalls.append(varname)
  130. return varvalues[varname]
  131. bb.utils.edit_metadata_file(tf.name, varvalues.keys(), handle_file)
  132. with open(tf.name) as f:
  133. modfile = f.readlines()
  134. # Ensure the output matches the expected output
  135. self.assertEqual(compareto.splitlines(True), modfile)
  136. # Ensure the callback function was called for every variable we asked for
  137. # (plus allow testing behaviour when a requested variable is not present)
  138. self.assertEqual(sorted(varvalues.keys()), sorted(varcalls + dummyvars))
  139. finally:
  140. os.remove(tf.name)
  141. def test_edit_metadata_file_nochange(self):
  142. # Test file doesn't get modified with nothing to do
  143. self._testeditfile({}, self._origfile)
  144. # Test file doesn't get modified with only dummy variables
  145. self._testeditfile({'DUMMY1': ('should_not_set', None, 0, True),
  146. 'DUMMY2': ('should_not_set_again', None, 0, True)}, self._origfile, dummyvars=['DUMMY1', 'DUMMY2'])
  147. # Test file doesn't get modified with some the same values
  148. self._testeditfile({'THIS': ('that', None, 0, True),
  149. 'OTHER': ('anothervalue', None, 0, True),
  150. 'MULTILINE3': (' c1 c2 c3 ', None, 4, False)}, self._origfile)
  151. def test_edit_metadata_file_1(self):
  152. newfile1 = """
  153. # A comment
  154. HELLO = "newvalue"
  155. THIS = "that"
  156. # Another comment
  157. NOCHANGE = "samevalue"
  158. OTHER = 'anothervalue'
  159. MULTILINE = "a1 \\
  160. a2 \\
  161. a3"
  162. MULTILINE2 := " \\
  163. b1 \\
  164. b2 \\
  165. b3 \\
  166. "
  167. MULTILINE3 = " \\
  168. c1 \\
  169. c2 \\
  170. c3 \\
  171. "
  172. do_functionname() {
  173. command1 ${VAL1} ${VAL2}
  174. command2 ${VAL3} ${VAL4}
  175. }
  176. """
  177. self._testeditfile({'HELLO': ('newvalue', None, 4, True)}, newfile1)
  178. def test_edit_metadata_file_2(self):
  179. newfile2 = """
  180. # A comment
  181. HELLO = "oldvalue"
  182. THIS = "that"
  183. # Another comment
  184. NOCHANGE = "samevalue"
  185. OTHER = 'anothervalue'
  186. MULTILINE = " \\
  187. d1 \\
  188. d2 \\
  189. d3 \\
  190. "
  191. MULTILINE2 := " \\
  192. b1 \\
  193. b2 \\
  194. b3 \\
  195. "
  196. MULTILINE3 = "nowsingle"
  197. do_functionname() {
  198. command1 ${VAL1} ${VAL2}
  199. command2 ${VAL3} ${VAL4}
  200. }
  201. """
  202. self._testeditfile({'MULTILINE': (['d1','d2','d3'], None, 4, False),
  203. 'MULTILINE3': ('nowsingle', None, 4, True),
  204. 'NOTPRESENT': (['a', 'b'], None, 4, False)}, newfile2, dummyvars=['NOTPRESENT'])
  205. def test_edit_metadata_file_3(self):
  206. newfile3 = """
  207. # A comment
  208. HELLO = "oldvalue"
  209. # Another comment
  210. NOCHANGE = "samevalue"
  211. OTHER = "yetanothervalue"
  212. MULTILINE = "e1 \\
  213. e2 \\
  214. e3 \\
  215. "
  216. MULTILINE2 := "f1 \\
  217. \tf2 \\
  218. \t"
  219. MULTILINE3 = " \\
  220. c1 \\
  221. c2 \\
  222. c3 \\
  223. "
  224. do_functionname() {
  225. othercommand_one a b c
  226. othercommand_two d e f
  227. }
  228. """
  229. self._testeditfile({'do_functionname()': (['othercommand_one a b c', 'othercommand_two d e f'], None, 4, False),
  230. 'MULTILINE2': (['f1', 'f2'], None, '\t', True),
  231. 'MULTILINE': (['e1', 'e2', 'e3'], None, -1, True),
  232. 'THIS': (None, None, 0, False),
  233. 'OTHER': ('yetanothervalue', None, 0, True)}, newfile3)
  234. def test_edit_metadata_file_4(self):
  235. newfile4 = """
  236. # A comment
  237. HELLO = "oldvalue"
  238. THIS = "that"
  239. # Another comment
  240. OTHER = 'anothervalue'
  241. MULTILINE = "a1 \\
  242. a2 \\
  243. a3"
  244. MULTILINE2 := " \\
  245. b1 \\
  246. b2 \\
  247. b3 \\
  248. "
  249. """
  250. self._testeditfile({'NOCHANGE': (None, None, 0, False),
  251. 'MULTILINE3': (None, None, 0, False),
  252. 'THIS': ('that', None, 0, False),
  253. 'do_functionname()': (None, None, 0, False)}, newfile4)
  254. def test_edit_metadata(self):
  255. newfile5 = """
  256. # A comment
  257. HELLO = "hithere"
  258. # A new comment
  259. THIS += "that"
  260. # Another comment
  261. NOCHANGE = "samevalue"
  262. OTHER = 'anothervalue'
  263. MULTILINE = "a1 \\
  264. a2 \\
  265. a3"
  266. MULTILINE2 := " \\
  267. b1 \\
  268. b2 \\
  269. b3 \\
  270. "
  271. MULTILINE3 = " \\
  272. c1 \\
  273. c2 \\
  274. c3 \\
  275. "
  276. NEWVAR = "value"
  277. do_functionname() {
  278. command1 ${VAL1} ${VAL2}
  279. command2 ${VAL3} ${VAL4}
  280. }
  281. """
  282. def handle_var(varname, origvalue, op, newlines):
  283. if varname == 'THIS':
  284. newlines.append('# A new comment\n')
  285. elif varname == 'do_functionname()':
  286. newlines.append('NEWVAR = "value"\n')
  287. newlines.append('\n')
  288. valueitem = varvalues.get(varname, None)
  289. if valueitem:
  290. return valueitem
  291. else:
  292. return (origvalue, op, 0, True)
  293. varvalues = {'HELLO': ('hithere', None, 0, True), 'THIS': ('that', '+=', 0, True)}
  294. varlist = ['HELLO', 'THIS', 'do_functionname()']
  295. (updated, newlines) = bb.utils.edit_metadata(self._origfile.splitlines(True), varlist, handle_var)
  296. self.assertTrue(updated, 'List should be updated but isn\'t')
  297. self.assertEqual(newlines, newfile5.splitlines(True))
  298. # Make sure the orig value matches what we expect it to be
  299. def test_edit_metadata_origvalue(self):
  300. origfile = """
  301. MULTILINE = " stuff \\
  302. morestuff"
  303. """
  304. expected_value = "stuff morestuff"
  305. global value_in_callback
  306. value_in_callback = ""
  307. def handle_var(varname, origvalue, op, newlines):
  308. global value_in_callback
  309. value_in_callback = origvalue
  310. return (origvalue, op, -1, False)
  311. bb.utils.edit_metadata(origfile.splitlines(True),
  312. ['MULTILINE'],
  313. handle_var)
  314. testvalue = re.sub('\s+', ' ', value_in_callback.strip())
  315. self.assertEqual(expected_value, testvalue)
  316. class EditBbLayersConf(unittest.TestCase):
  317. def _test_bblayers_edit(self, before, after, add, remove, notadded, notremoved):
  318. with tempfile.NamedTemporaryFile('w', delete=False) as tf:
  319. tf.write(before)
  320. tf.close()
  321. try:
  322. actual_notadded, actual_notremoved = bb.utils.edit_bblayers_conf(tf.name, add, remove)
  323. with open(tf.name) as f:
  324. actual_after = f.readlines()
  325. self.assertEqual(after.splitlines(True), actual_after)
  326. self.assertEqual(notadded, actual_notadded)
  327. self.assertEqual(notremoved, actual_notremoved)
  328. finally:
  329. os.remove(tf.name)
  330. def test_bblayers_remove(self):
  331. before = r"""
  332. # A comment
  333. BBPATH = "${TOPDIR}"
  334. BBFILES ?= ""
  335. BBLAYERS = " \
  336. /home/user/path/layer1 \
  337. /home/user/path/layer2 \
  338. /home/user/path/subpath/layer3 \
  339. /home/user/path/layer4 \
  340. "
  341. """
  342. after = r"""
  343. # A comment
  344. BBPATH = "${TOPDIR}"
  345. BBFILES ?= ""
  346. BBLAYERS = " \
  347. /home/user/path/layer1 \
  348. /home/user/path/subpath/layer3 \
  349. /home/user/path/layer4 \
  350. "
  351. """
  352. self._test_bblayers_edit(before, after,
  353. None,
  354. '/home/user/path/layer2',
  355. [],
  356. [])
  357. def test_bblayers_add(self):
  358. before = r"""
  359. # A comment
  360. BBPATH = "${TOPDIR}"
  361. BBFILES ?= ""
  362. BBLAYERS = " \
  363. /home/user/path/layer1 \
  364. /home/user/path/layer2 \
  365. /home/user/path/subpath/layer3 \
  366. /home/user/path/layer4 \
  367. "
  368. """
  369. after = r"""
  370. # A comment
  371. BBPATH = "${TOPDIR}"
  372. BBFILES ?= ""
  373. BBLAYERS = " \
  374. /home/user/path/layer1 \
  375. /home/user/path/layer2 \
  376. /home/user/path/subpath/layer3 \
  377. /home/user/path/layer4 \
  378. /other/path/to/layer5 \
  379. "
  380. """
  381. self._test_bblayers_edit(before, after,
  382. '/other/path/to/layer5/',
  383. None,
  384. [],
  385. [])
  386. def test_bblayers_add_remove(self):
  387. before = r"""
  388. # A comment
  389. BBPATH = "${TOPDIR}"
  390. BBFILES ?= ""
  391. BBLAYERS = " \
  392. /home/user/path/layer1 \
  393. /home/user/path/layer2 \
  394. /home/user/path/subpath/layer3 \
  395. /home/user/path/layer4 \
  396. "
  397. """
  398. after = r"""
  399. # A comment
  400. BBPATH = "${TOPDIR}"
  401. BBFILES ?= ""
  402. BBLAYERS = " \
  403. /home/user/path/layer1 \
  404. /home/user/path/layer2 \
  405. /home/user/path/layer4 \
  406. /other/path/to/layer5 \
  407. "
  408. """
  409. self._test_bblayers_edit(before, after,
  410. ['/other/path/to/layer5', '/home/user/path/layer2/'], '/home/user/path/subpath/layer3/',
  411. ['/home/user/path/layer2'],
  412. [])
  413. def test_bblayers_add_remove_home(self):
  414. before = r"""
  415. # A comment
  416. BBPATH = "${TOPDIR}"
  417. BBFILES ?= ""
  418. BBLAYERS = " \
  419. ~/path/layer1 \
  420. ~/path/layer2 \
  421. ~/otherpath/layer3 \
  422. ~/path/layer4 \
  423. "
  424. """
  425. after = r"""
  426. # A comment
  427. BBPATH = "${TOPDIR}"
  428. BBFILES ?= ""
  429. BBLAYERS = " \
  430. ~/path/layer2 \
  431. ~/path/layer4 \
  432. ~/path2/layer5 \
  433. "
  434. """
  435. self._test_bblayers_edit(before, after,
  436. [os.environ['HOME'] + '/path/layer4', '~/path2/layer5'],
  437. [os.environ['HOME'] + '/otherpath/layer3', '~/path/layer1', '~/path/notinlist'],
  438. [os.environ['HOME'] + '/path/layer4'],
  439. ['~/path/notinlist'])
  440. def test_bblayers_add_remove_plusequals(self):
  441. before = r"""
  442. # A comment
  443. BBPATH = "${TOPDIR}"
  444. BBFILES ?= ""
  445. BBLAYERS += " \
  446. /home/user/path/layer1 \
  447. /home/user/path/layer2 \
  448. "
  449. """
  450. after = r"""
  451. # A comment
  452. BBPATH = "${TOPDIR}"
  453. BBFILES ?= ""
  454. BBLAYERS += " \
  455. /home/user/path/layer2 \
  456. /home/user/path/layer3 \
  457. "
  458. """
  459. self._test_bblayers_edit(before, after,
  460. '/home/user/path/layer3',
  461. '/home/user/path/layer1',
  462. [],
  463. [])
  464. def test_bblayers_add_remove_plusequals2(self):
  465. before = r"""
  466. # A comment
  467. BBPATH = "${TOPDIR}"
  468. BBFILES ?= ""
  469. BBLAYERS += " \
  470. /home/user/path/layer1 \
  471. /home/user/path/layer2 \
  472. /home/user/path/layer3 \
  473. "
  474. BBLAYERS += "/home/user/path/layer4"
  475. BBLAYERS += "/home/user/path/layer5"
  476. """
  477. after = r"""
  478. # A comment
  479. BBPATH = "${TOPDIR}"
  480. BBFILES ?= ""
  481. BBLAYERS += " \
  482. /home/user/path/layer2 \
  483. /home/user/path/layer3 \
  484. "
  485. BBLAYERS += "/home/user/path/layer5"
  486. BBLAYERS += "/home/user/otherpath/layer6"
  487. """
  488. self._test_bblayers_edit(before, after,
  489. ['/home/user/otherpath/layer6', '/home/user/path/layer3'], ['/home/user/path/layer1', '/home/user/path/layer4', '/home/user/path/layer7'],
  490. ['/home/user/path/layer3'],
  491. ['/home/user/path/layer7'])