test_utils.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import unittest2
  2. from reportbug import utils
  3. import os.path
  4. import platform
  5. from nose.plugins.attrib import attr
  6. import debianbts
  7. import mock
  8. import commands
  9. import subprocess
  10. class TestUtils(unittest2.TestCase):
  11. def test_modes_and_modelist(self):
  12. """Check MODES items and MODELIST are in sync"""
  13. self.assertItemsEqual(utils.MODES.keys(), utils.MODELIST)
  14. class TestEmail(unittest2.TestCase):
  15. def test_check_email_addr(self):
  16. real_addr = 'reportbug-maint@lists.alioth.debian.org'
  17. self.assertTrue(utils.check_email_addr(real_addr))
  18. self.assertFalse(utils.check_email_addr('dummy'))
  19. self.assertFalse(utils.check_email_addr('nouser@nodomain'))
  20. self.assertFalse(utils.check_email_addr('.nouser@nodomain'))
  21. self.assertFalse(utils.check_email_addr('nouser.@nodomain'))
  22. self.assertFalse(utils.check_email_addr('nouser@.nodomain'))
  23. self.assertFalse(utils.check_email_addr('nouser@nodomain.'))
  24. self.assertFalse(utils.check_email_addr('too@many@at@signs'))
  25. def test_get_email_addr(self):
  26. email = 'Reportbug Maintainers <reportbug-maint@lists.alioth.debian.org>'
  27. name, email_addr = utils.get_email_addr(email)
  28. self.assertEqual(name, 'Reportbug Maintainers')
  29. self.assertEqual(email_addr, 'reportbug-maint@lists.alioth.debian.org')
  30. def test_get_email(self):
  31. name = 'Reportbug Maintainers'
  32. mail = 'reportbug-maint@lists.alioth.debian.org'
  33. n, m = utils.get_email(mail, name)
  34. self.assertEqual(name, n)
  35. self.assertEqual(mail, m)
  36. def test_get_user_id(self):
  37. name = 'Reportbug Maintainers'
  38. mail = 'reportbug-maint@lists.alioth.debian.org'
  39. addr = utils.get_user_id(mail, name)
  40. self.assertEqual(addr, "%s <%s>" % (name, mail))
  41. name = 'test'
  42. mail = 'faked'
  43. addr = utils.get_user_id(mail, name)
  44. self.assertIn(mail + '@', addr)
  45. mail = 'Reportbug Maintainers <reportbug-maint@lists.alioth.debian.org>'
  46. addr = utils.get_user_id(mail)
  47. self.assertEqual(mail, addr)
  48. mail = 'reportbug-maint@lists.alioth.debian.org'
  49. addr = utils.get_user_id(mail)
  50. self.assertIn(mail, addr)
  51. def test_find_rewritten(self):
  52. unittest2.skip("Is utils.find_rewritten actually useful to someone? deprecate it?")
  53. class TestPackages(unittest2.TestCase):
  54. def test_get_package_status(self):
  55. status = utils.get_package_status('non-existing-package')
  56. (pkgversion, pkgavail, depends, recommends, conffiles, maintainer,
  57. installed, origin, vendor, reportinfo, priority, desc, src_name,
  58. fulldesc, state, suggests, section) = status
  59. self.assertIsNone(pkgversion)
  60. self.assertIsNone(pkgavail)
  61. self.assertEqual(depends, ())
  62. self.assertEqual(recommends, ())
  63. self.assertEqual(conffiles, ())
  64. self.assertIsNone(maintainer)
  65. self.assertFalse(installed)
  66. self.assertIsNone(origin)
  67. self.assertEqual(vendor, '')
  68. self.assertIsNone(reportinfo)
  69. self.assertIsNone(priority)
  70. self.assertIsNone(desc)
  71. self.assertIsNone(src_name)
  72. self.assertEqual(fulldesc, '')
  73. self.assertEqual(state, '')
  74. self.assertEqual(suggests, ())
  75. self.assertIsNone(section)
  76. # Using an 'Essential: yes' package, what's better than 'dpkg'?
  77. status = utils.get_package_status('dpkg')
  78. (pkgversion, pkgavail, depends, recommends, conffiles, maintainer,
  79. installed, origin, vendor, reportinfo, priority, desc, src_name,
  80. fulldesc, state, suggests, section) = status
  81. self.assertIsNotNone(pkgversion)
  82. self.assertEqual(pkgavail, 'dpkg')
  83. # let's just check Depends is not null
  84. self.assertIsNotNone(depends)
  85. self.assertIsNotNone(maintainer)
  86. self.assertTrue(installed)
  87. self.assertEqual(origin, 'debian')
  88. self.assertEqual(priority, 'required')
  89. self.assertIsNotNone(desc)
  90. self.assertIsNotNone(fulldesc)
  91. self.assertEqual(state, 'installed')
  92. self.assertEqual(section, 'admin')
  93. # it exploits the 'statuscache', it's already called before
  94. # so it's now in the cache
  95. status = utils.get_package_status('dpkg')
  96. status = utils.get_package_status('reportbug', avail=True)
  97. (pkgversion, pkgavail, depends, recommends, conffiles, maintainer,
  98. installed, origin, vendor, reportinfo, priority, desc, src_name,
  99. fulldesc, state, suggests, section) = status
  100. self.assertIsNotNone(pkgversion)
  101. self.assertEqual(pkgavail, 'reportbug')
  102. # let's just check Depends is not null
  103. self.assertIsNotNone(depends)
  104. self.assertIsNotNone(maintainer)
  105. self.assertEqual(priority, 'standard')
  106. self.assertIsNotNone(desc)
  107. self.assertIsNotNone(fulldesc)
  108. status = utils.get_package_status('python-matplotlib')
  109. (pkgversion, pkgavail, depends, recommends, conffiles, maintainer,
  110. installed, origin, vendor, reportinfo, priority, desc, src_name,
  111. fulldesc, state, suggests, section) = status
  112. self.assertIsNotNone(recommends)
  113. def test_bts791577(self):
  114. # Verify obsolete config files are correctly parsed
  115. # bonus points for testing also conffiles with spaces in the filename
  116. pkgstatus = """Conffiles:
  117. /etc/reportbug.conf 17b8e0850fa74d18b96ce5856321de0d
  118. /etc/reportbug with spaces.conf feedcafefeedcafefeedcafefeedcafe
  119. /etc/reportbug.conf.obsolete deadbeefdeadbeefdeadbeefdeadbeef obsolete
  120. /etc/reportbug with spaces and obsolete.conf cafebabecafebabecafebabecafebabe obsolete
  121. """
  122. pkg = 'test_bts791577'
  123. expected_conffiles = [u'/etc/reportbug.conf',
  124. u'/etc/reportbug with spaces.conf',
  125. u'/etc/reportbug.conf.obsolete',
  126. u'/etc/reportbug with spaces and obsolete.conf']
  127. __save = commands.getoutput
  128. commands.getoutput = mock.MagicMock(return_value=pkgstatus)
  129. result = utils.get_package_status(pkg)
  130. conffile = [x[0] for x in result[4]]
  131. commands.getoutput = __save
  132. del __save
  133. self.assertListEqual(conffile, expected_conffiles)
  134. def test_get_changed_config_files(self):
  135. status = utils.get_package_status('dpkg')
  136. (pkgversion, pkgavail, depends, recommends, conffiles, maintainer,
  137. installed, origin, vendor, reportinfo, priority, desc, src_name,
  138. fulldesc, state, suggests, section) = status
  139. confinfo, changed = utils.get_changed_config_files(conffiles)
  140. self.assertIsNotNone(confinfo)
  141. def test_find_package_for(self):
  142. result = utils.find_package_for('dpkg')
  143. self.assertNotEqual(result[1], {})
  144. filename = 'reportbug-bugfree'
  145. result = utils.find_package_for(filename, pathonly=True)
  146. self.assertEqual(result[0], filename)
  147. self.assertIsNone(result[1])
  148. result = utils.find_package_for('/usr/bin/reportbug')
  149. self.assertNotEqual(result[1], {})
  150. result = utils.find_package_for('/var/lib/dpkg/info/reportbug.md5sums')
  151. self.assertNotEqual(result[1], {})
  152. result = utils.find_package_for('/usr/bin/')
  153. self.assertNotEqual(result[1], {})
  154. def test_get_package_info(self):
  155. result = utils.get_package_info([])
  156. self.assertEqual(result, [])
  157. pkg = 'reportbug'
  158. result = utils.get_package_info([((pkg,), pkg)])
  159. self.assertEqual(len(result), 1)
  160. self.assertEqual(result[0][0], pkg)
  161. # open package surely not available on my client systems
  162. # to cover line 568
  163. pkg = 'slapd'
  164. result = utils.get_package_info([((pkg,), pkg)])
  165. self.assertEqual(result[0][0], pkg)
  166. self.assertEqual(result[0][2], '<none>')
  167. result = utils.get_package_info([((pkg,), pkg)], skip_notfound=True)
  168. self.assertEqual(result, [])
  169. # package with a Provides
  170. # pkg = 'emacs'
  171. # result = utils.get_package_info([((pkg,), pkg)])
  172. # self.assertEqual(result[0][0], pkg)
  173. def test_bts683116(self):
  174. """Check Description and Description-LANG are recognized"""
  175. pkginfo = """Package: reportbug
  176. Status: install ok installed
  177. Version: 6.6.3
  178. %s: reports bugs in the Debian distribution
  179. """
  180. pkg = 'reportbug'
  181. __save = commands.getoutput
  182. commands.getoutput = mock.MagicMock(return_value=pkginfo % 'Description')
  183. result = utils.available_package_description(pkg)
  184. self.assertEqual(u'reports bugs in the Debian distribution', result)
  185. commands.getoutput = mock.MagicMock(return_value=pkginfo % 'Description-en')
  186. result = utils.available_package_description(pkg)
  187. self.assertEqual(u'reports bugs in the Debian distribution', result)
  188. commands.getoutput = __save
  189. del __save
  190. __save = commands.getoutput
  191. commands.getoutput = mock.MagicMock(return_value=pkginfo % 'Description')
  192. result = utils.get_package_status(pkg)
  193. self.assertEqual(u'reports bugs in the Debian distribution', result[11])
  194. commands.getoutput = mock.MagicMock(return_value=pkginfo % 'Description-en')
  195. result = utils.get_package_status(pkg)
  196. self.assertEqual(u'reports bugs in the Debian distribution', result[11])
  197. commands.getoutput = __save
  198. del __save
  199. __save = utils.get_dpkg_database
  200. utils.get_dpkg_database = mock.MagicMock(return_value=[pkginfo % 'Description', ])
  201. result = utils.get_package_info([((pkg,), pkg)])
  202. self.assertEqual(u'reports bugs in the Debian distribution', result[0][3])
  203. utils.get_dpkg_database = mock.MagicMock(return_value=[pkginfo % 'Description-en', ])
  204. result = utils.get_package_info([((pkg,), pkg)])
  205. self.assertEqual(u'reports bugs in the Debian distribution', result[0][3])
  206. utils.get_dpkg_database = __save
  207. del __save
  208. def test_packages_providing(self):
  209. pkg = 'editor'
  210. result = utils.packages_providing(pkg)
  211. self.assertGreater(len(result), 0)
  212. def test_get_avail_database(self):
  213. avail_db = utils.get_avail_database()
  214. entry = avail_db.next()
  215. self.assertIsNotNone(entry)
  216. def test_available_package_description(self):
  217. descr = utils.available_package_description('reportbug')
  218. self.assertEquals(descr, 'reports bugs in the Debian distribution')
  219. descr = utils.available_package_description('reportbug-bugfree')
  220. self.assertIsNone(descr)
  221. class TestSourcePackages(unittest2.TestCase):
  222. # @unittest2.skip("Too slow")
  223. def test_get_source_name(self):
  224. binpkg = 'python-reportbug'
  225. src = utils.get_source_name(binpkg)
  226. self.assertEqual(src, 'reportbug')
  227. src = utils.get_source_name('reportbug-bugfree')
  228. self.assertIsNone(src)
  229. # @unittest2.skip("Too slow")
  230. def test_get_source_package(self):
  231. src = 'reportbug'
  232. binpkgs = utils.get_source_package(src)
  233. self.assertItemsEqual([bin[0] for bin in binpkgs], ['python-reportbug', 'reportbug'])
  234. bin = 'python-reportbug'
  235. binpkgs_frombin = utils.get_source_package(bin)
  236. self.assertEqual(binpkgs, binpkgs_frombin)
  237. class TestSystemInformation(unittest2.TestCase):
  238. def test_get_cpu_cores(self):
  239. cores = utils.get_cpu_cores()
  240. self.assertGreaterEqual(cores, 1)
  241. def test_lsb_release_info(self):
  242. res = utils.lsb_release_info()
  243. self.assertIn('Debian', res)
  244. def test_get_running_kernel_pkg(self):
  245. package = utils.get_running_kernel_pkg()
  246. self.assertIn(platform.release(), package)
  247. def test_get_multiarch(self):
  248. orig = commands.getoutput
  249. commands.getoutput = mock.MagicMock(return_value='')
  250. multiarch = utils.get_multiarch()
  251. self.assertEqual(multiarch, '')
  252. commands.getoutput = mock.MagicMock(return_value='i386')
  253. multiarch = utils.get_multiarch()
  254. self.assertEqual(multiarch, 'i386')
  255. commands.getoutput = mock.MagicMock(return_value='i386\namd64')
  256. multiarch = utils.get_multiarch()
  257. self.assertItemsEqual(multiarch.split(', '), ['i386', 'amd64'])
  258. commands.getoutput = orig
  259. def test_get_init_system(self):
  260. __save = os.path.isdir
  261. os.path.isdir = mock.MagicMock(return_value=True)
  262. init = utils.get_init_system()
  263. self.assertTrue(init.startswith('systemd'))
  264. os.path.isdir = __save
  265. del __save
  266. __save = subprocess.call
  267. subprocess.call = mock.MagicMock(return_value=0)
  268. init = utils.get_init_system()
  269. self.assertTrue(init.startswith('upstart'))
  270. subprocess.call = __save
  271. del __save
  272. __save1 = os.path.isfile
  273. __save2 = os.path.islink
  274. os.path.isfile = mock.MagicMock(return_value=True)
  275. os.path.islink = mock.MagicMock(return_value=False)
  276. init = utils.get_init_system()
  277. self.assertTrue(init.startswith('sysvinit'))
  278. os.path.isfile = __save1
  279. os.path.islink = __save2
  280. del __save1
  281. del __save2
  282. class TestMua(unittest2.TestCase):
  283. def test_mua_is_supported(self):
  284. for mua in ('mh', 'nmh', 'gnus', 'mutt', 'claws-mail'):
  285. self.assertTrue(utils.mua_is_supported(mua))
  286. self.assertFalse(utils.mua_is_supported('mua-of-my-dreams'))
  287. def test_mua_exists(self):
  288. for mua in ('mh', 'nmh', 'gnus', 'mutt', 'claws-mail'):
  289. if not utils.mua_exists(mua):
  290. self.fail("%s MUA program not available" % mua)
  291. def test_mua_name(self):
  292. for mua in ('mh', 'nmh', 'gnus', 'mutt', 'claws-mail'):
  293. self.assertIsInstance(utils.mua_name(mua), utils.Mua)
  294. self.assertEqual(utils.mua_name('mua-of-my-dreams'), 'mua-of-my-dreams')
  295. class TestBugreportBody(unittest2.TestCase):
  296. def test_get_dependency_info(self):
  297. pkg = 'reportbug'
  298. result = utils.get_dependency_info('reportbug', '')
  299. self.assertIn('no packages', result)
  300. result = utils.get_dependency_info('reportbug', [['dpkg']])
  301. self.assertIn('dpkg', result)
  302. # check for the provides stuff
  303. result = utils.get_dependency_info('reportbug', [['awk']])
  304. self.assertIn('awk', result)
  305. def test_bts657753(self):
  306. # check that non-existing deps gets a correct installation info
  307. # and not just the last one applied to anyone
  308. result = utils.get_dependency_info('reportbug',
  309. (('reportbug',), ('nonexisting',)))
  310. for line in result.split('\n'):
  311. if 'nonexisting' in line:
  312. self.assertFalse(line.startswith('ii'))
  313. def test_bts650659(self):
  314. # verify that the dependency list doesn't have tailing white spaces
  315. status = utils.get_package_status('reportbug')
  316. (pkgversion, pkgavail, depends, recommends, conffiles, maintainer,
  317. installed, origin, vendor, reportinfo, priority, desc, src_name,
  318. fulldesc, state, suggests, section) = status
  319. for l in [depends, recommends, suggests]:
  320. result = utils.get_dependency_info('reportbug', l)
  321. for line in result.split('\n'):
  322. self.assertEqual(line.rstrip(), line)
  323. def test_cleanup_msg(self):
  324. message = """Subject: unblock: reportbug/4.12.6
  325. Package: release.debian.org
  326. User: release.debian.org@packages.debian.org
  327. Usertags: unblock
  328. Severity: normal
  329. Morph: cool
  330. Control: testcontrol1
  331. Control: testcontrol2
  332. Continuation:
  333. header
  334. Please unblock package reportbug
  335. (explain the reason for the unblock here)
  336. unblock reportbug/4.12.6
  337. -- System Information:
  338. Debian Release: squeeze/sid
  339. APT prefers unstable
  340. APT policy: (500, 'unstable'), (1, 'experimental')
  341. Architecture: amd64 (x86_64)
  342. Kernel: Linux 2.6.31-1-amd64 (SMP w/4 CPU cores)
  343. Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
  344. Shell: /bin/sh linked to /bin/bash"""
  345. header = [u'X-Debbugs-CC: reportbug@packages.qa.debian.org']
  346. pseudos = ['Morph: cool', 'Control: testcontrol1', 'Control: testcontrol2']
  347. rtype = 'debbugs'
  348. body, headers, pseudo = utils.cleanup_msg(message, header, pseudos,
  349. rtype)
  350. # check body content
  351. self.assertIn('reportbug/4.12.6', body)
  352. self.assertIn('System Information', body)
  353. # check expected headers are there
  354. h = dict(headers)
  355. self.assertIn('Subject', h)
  356. self.assertIn('X-Debbugs-CC', h)
  357. # check expected pseudo headers are there
  358. p = dict([p.split(': ') for p in pseudo])
  359. self.assertIn('Package', p)
  360. self.assertIn('Severity', p)
  361. self.assertIn('User', p)
  362. self.assertIn('Usertags', p)
  363. self.assertIn('Morph', p)
  364. # bts687679, verify 2 'Control' pseudo-headers are present
  365. for ph in pseudos:
  366. self.assertIn(ph, pseudo)
  367. @attr('network') # marking the test as using network
  368. def test_generate_blank_report(self):
  369. report = utils.generate_blank_report('reportbug', '1.2.3', 'normal',
  370. '', '', '', type='debbugs')
  371. self.assertIsNotNone(report)
  372. self.assertIn('Package: reportbug', report)
  373. self.assertIn('Version: 1.2.3', report)
  374. self.assertIn('Severity: normal', report)
  375. report = utils.generate_blank_report('reportbug', '1.2.3', 'normal',
  376. '', '', '', type='debbugs',
  377. issource=True)
  378. self.assertIn('Source: reportbug', report)
  379. # test with exinfo (represents the bug number if this is a followup):
  380. # int, string, unconvertible (to int) datatype
  381. report = utils.generate_blank_report('reportbug', '1.2.3', 'normal',
  382. '', '', '', type='debbugs',
  383. exinfo=123456)
  384. self.assertIn('Followup-For: Bug #123456', report)
  385. bug = debianbts.get_status(123456)[0]
  386. report = utils.generate_blank_report('reportbug', '1.2.3', 'normal',
  387. '', '', '', type='debbugs',
  388. exinfo=bug)
  389. self.assertIn('Followup-For: Bug #123456', report)
  390. with self.assertRaises(TypeError):
  391. report = utils.generate_blank_report('reportbug', '1.2.3', 'normal',
  392. '', '', '', type='debbugs',
  393. exinfo={'123456': ''})
  394. class TestConfig(unittest2.TestCase):
  395. # Use an "internal" file for testing
  396. def setUp(self):
  397. self._FILES = utils.FILES
  398. utils.FILES = [os.path.dirname(__file__) + '/data/reportbug.conf']
  399. def tearDown(self):
  400. utils.FILES = self._FILES
  401. def test_parse_config_files(self):
  402. desired_conf = {
  403. 'bts': 'debian',
  404. 'check_available': True,
  405. 'check_uid': False,
  406. 'debconf': False,
  407. 'dontquery': False,
  408. 'editor': u'emacs -nw',
  409. 'email': u'reportbug-maint@lists.alioth.debian.org',
  410. 'envelopefrom': u'reportbug-maint@lists.alioth.debian.org',
  411. 'headers': ['X-Reportbug-Testsuite: this is the test suite'],
  412. 'http_proxy': u'http://proxy.example.com:3128/',
  413. 'interface': 'gtk2',
  414. 'keyid': u'deadbeef',
  415. 'max_attachment_size': 1024000,
  416. 'mbox_reader_cmd': u'mutt -f %s',
  417. 'mirrors': ['this_is_a_bts_mirror'],
  418. 'mode': 'novice',
  419. 'mta': u'/usr/sbin/sendmail',
  420. 'nocc': False,
  421. 'nocompress': False,
  422. 'noconf': False,
  423. 'offline': True,
  424. 'paranoid': True,
  425. 'query_src': False,
  426. 'realname': u'Reportbug Maintainers',
  427. 'replyto': u'We dont care <dev@null.org>',
  428. 'sendto': 'submit',
  429. 'severity': 'normal',
  430. 'sign': 'gpg',
  431. 'smtphost': u'reportbug.debian.org:587',
  432. 'smtppasswd': u'James Bond',
  433. 'smtptls': True,
  434. 'smtpuser': u'Bond',
  435. 'template': True,
  436. 'verify': True}
  437. args = utils.parse_config_files()
  438. for conf in desired_conf:
  439. self.assertIn(conf, args)
  440. self.assertEqual(desired_conf[conf], args[conf])
  441. # mua returns an instance of utils.Mua, need to check differently
  442. self.assertIn('mua', args)
  443. self.assertIsInstance(args['mua'], utils.Mua)
  444. def test_bts579891(self):
  445. lex = utils.our_lex('realname "Paul \\"TBBle\\" Hampson"', posix=True)
  446. option = lex.get_token()
  447. self.assertEqual(option, 'realname')
  448. realname = lex.get_token()
  449. self.assertEqual(realname, 'Paul "TBBle" Hampson')
  450. class TestControl(unittest2.TestCase):
  451. def test_parse_bug_control_file(self):
  452. ctrl_file = os.path.dirname(__file__) + '/data/control'
  453. submitas, submitto, reportwith, supplemental = \
  454. utils.parse_bug_control_file(ctrl_file)
  455. self.assertEquals(submitas, 'reportbug2')
  456. self.assertEquals(submitto, 'reportbug-maint@lists.alioth.debian.org')
  457. self.assertIn('python', reportwith)
  458. self.assertIn('perl', reportwith)
  459. self.assertIn('python', supplemental)
  460. self.assertIn('perl', supplemental)
  461. class TestPaths(unittest2.TestCase):
  462. def test_search_path_for(self):
  463. p = 'not-existing'
  464. res = utils.search_path_for(p)
  465. self.assertIsNone(res)
  466. p = '/tmp'
  467. res = utils.search_path_for(p)
  468. self.assertEquals(p, res)
  469. p = 'dpkg'
  470. res = utils.search_path_for(p)
  471. self.assertEquals(res, '/usr/bin/dpkg')
  472. class TestEditor(unittest2.TestCase):
  473. def test_which_editor(self):
  474. res = utils.which_editor()
  475. self.assertIsNotNone(res)
  476. e = 'reportbug-editor'
  477. res = utils.which_editor(e)
  478. self.assertEquals(e, res)
  479. class TestSearch(unittest2.TestCase):
  480. def test_search_pipe(self):
  481. f = 'reportbug'
  482. dlocate = True
  483. pipe, dloc = utils.search_pipe(f, dlocate)
  484. res = pipe.readlines()
  485. pipe.close()
  486. self.assertEquals(dloc, dlocate)
  487. self.assertGreater(len(res), 0)
  488. dlocate = False
  489. pipe, dloc = utils.search_pipe(f, dlocate)
  490. res = pipe.readlines()
  491. pipe.close()
  492. self.assertEquals(dloc, dlocate)
  493. self.assertGreater(len(res), 0)
  494. class TestDpkg(unittest2.TestCase):
  495. def test_query_dpkg_for(self):
  496. p = 'reportbug'
  497. dlocate = True
  498. res = utils.query_dpkg_for(p, dlocate)
  499. self.assertEquals(res[0], p)
  500. self.assertGreater(len(res[1].keys()), 0)
  501. dlocate = False
  502. res = utils.query_dpkg_for(p, dlocate)
  503. self.assertEquals(res[0], p)
  504. self.assertGreater(len(res[1].keys()), 0)
  505. # to trigger 'Try again without dlocate if no packages found'
  506. p = 'blablabla'
  507. dlocate = True
  508. res = utils.query_dpkg_for(p, dlocate)
  509. self.assertEquals(res[0], p)
  510. self.assertEquals(res[1], {})
  511. class TestMisc(unittest2.TestCase):
  512. def test_first_run(self):
  513. isfirstrun = utils.first_run()
  514. self.assertIsNotNone(isfirstrun)
  515. def test_exec_and_parse_bugscript(self):
  516. handler = os.path.dirname(__file__) + '/../share/handle_bugscript'
  517. bugscript_file = os.path.dirname(__file__) + '/data/bugscript'
  518. (rc, h, ph, t, a) = utils.exec_and_parse_bugscript(handler, bugscript_file)
  519. self.assertIn('python', t)
  520. self.assertIn('debian', t)
  521. self.assertIn('From: morph@dummy.int', h)
  522. self.assertIn('User: morph@debian.org', ph)
  523. self.assertIn('/etc/fstab', a)
  524. def test_check_package_name(self):
  525. self.assertTrue(utils.check_package_name('reportbug'))
  526. self.assertTrue(utils.check_package_name('ab'))
  527. self.assertFalse(utils.check_package_name('a'))
  528. self.assertFalse(utils.check_package_name('.a'))
  529. self.assertFalse(utils.check_package_name('dfffff '))
  530. self.assertFalse(utils.check_package_name('reportbug_reportbug'))
  531. self.assertTrue(utils.check_package_name('reportbug+love-war.com'))
  532. self.assertTrue(utils.check_package_name('reportbug2001'))
  533. self.assertFalse(utils.check_package_name('UPPERCASE'))
  534. self.assertFalse(utils.check_package_name('((()))'))