utils.py 17 KB

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