You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

699 lines
25 KiB

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