test_process.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. # coding: utf8
  2. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  3. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  4. """Tests for process behavior of coverage.py."""
  5. import glob
  6. import os
  7. import os.path
  8. import re
  9. import sys
  10. import textwrap
  11. import coverage
  12. from coverage import env, CoverageData
  13. from coverage.misc import output_encoding
  14. from tests.coveragetest import CoverageTest
  15. TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try_execfile.py")
  16. class ProcessTest(CoverageTest):
  17. """Tests of the per-process behavior of coverage.py."""
  18. def number_of_data_files(self):
  19. """Return the number of coverage data files in this directory."""
  20. num = 0
  21. for f in os.listdir('.'):
  22. if f.startswith('.coverage.') or f == '.coverage':
  23. num += 1
  24. return num
  25. def test_save_on_exit(self):
  26. self.make_file("mycode.py", """\
  27. h = "Hello"
  28. w = "world"
  29. """)
  30. self.assert_doesnt_exist(".coverage")
  31. self.run_command("coverage run mycode.py")
  32. self.assert_exists(".coverage")
  33. def test_environment(self):
  34. # Checks that we can import modules from the tests directory at all!
  35. self.make_file("mycode.py", """\
  36. import covmod1
  37. import covmodzip1
  38. a = 1
  39. print('done')
  40. """)
  41. self.assert_doesnt_exist(".coverage")
  42. out = self.run_command("coverage run mycode.py")
  43. self.assert_exists(".coverage")
  44. self.assertEqual(out, 'done\n')
  45. def make_b_or_c_py(self):
  46. """Create b_or_c.py, used in a few of these tests."""
  47. self.make_file("b_or_c.py", """\
  48. import sys
  49. a = 1
  50. if sys.argv[1] == 'b':
  51. b = 1
  52. else:
  53. c = 1
  54. d = 1
  55. print('done')
  56. """)
  57. def test_combine_parallel_data(self):
  58. self.make_b_or_c_py()
  59. out = self.run_command("coverage run -p b_or_c.py b")
  60. self.assertEqual(out, 'done\n')
  61. self.assert_doesnt_exist(".coverage")
  62. self.assertEqual(self.number_of_data_files(), 1)
  63. out = self.run_command("coverage run -p b_or_c.py c")
  64. self.assertEqual(out, 'done\n')
  65. self.assert_doesnt_exist(".coverage")
  66. # After two -p runs, there should be two .coverage.machine.123 files.
  67. self.assertEqual(self.number_of_data_files(), 2)
  68. # Combine the parallel coverage data files into .coverage .
  69. self.run_command("coverage combine")
  70. self.assert_exists(".coverage")
  71. # After combining, there should be only the .coverage file.
  72. self.assertEqual(self.number_of_data_files(), 1)
  73. # Read the coverage file and see that b_or_c.py has all 7 lines
  74. # executed.
  75. data = coverage.CoverageData()
  76. data.read_file(".coverage")
  77. self.assertEqual(data.line_counts()['b_or_c.py'], 7)
  78. def test_combine_parallel_data_with_a_corrupt_file(self):
  79. self.make_b_or_c_py()
  80. out = self.run_command("coverage run -p b_or_c.py b")
  81. self.assertEqual(out, 'done\n')
  82. self.assert_doesnt_exist(".coverage")
  83. self.assertEqual(self.number_of_data_files(), 1)
  84. out = self.run_command("coverage run -p b_or_c.py c")
  85. self.assertEqual(out, 'done\n')
  86. self.assert_doesnt_exist(".coverage")
  87. # After two -p runs, there should be two .coverage.machine.123 files.
  88. self.assertEqual(self.number_of_data_files(), 2)
  89. # Make a bogus data file.
  90. self.make_file(".coverage.bad", "This isn't a coverage data file.")
  91. # Combine the parallel coverage data files into .coverage .
  92. out = self.run_command("coverage combine")
  93. self.assert_exists(".coverage")
  94. self.assert_exists(".coverage.bad")
  95. warning_regex = (
  96. r"Coverage.py warning: Couldn't read data from '.*\.coverage\.bad': "
  97. r"CoverageException: Doesn't seem to be a coverage\.py data file"
  98. )
  99. self.assertRegex(out, warning_regex)
  100. # After combining, those two should be the only data files.
  101. self.assertEqual(self.number_of_data_files(), 2)
  102. # Read the coverage file and see that b_or_c.py has all 7 lines
  103. # executed.
  104. data = coverage.CoverageData()
  105. data.read_file(".coverage")
  106. self.assertEqual(data.line_counts()['b_or_c.py'], 7)
  107. def test_combine_parallel_data_in_two_steps(self):
  108. self.make_b_or_c_py()
  109. out = self.run_command("coverage run -p b_or_c.py b")
  110. self.assertEqual(out, 'done\n')
  111. self.assert_doesnt_exist(".coverage")
  112. self.assertEqual(self.number_of_data_files(), 1)
  113. # Combine the (one) parallel coverage data file into .coverage .
  114. self.run_command("coverage combine")
  115. self.assert_exists(".coverage")
  116. self.assertEqual(self.number_of_data_files(), 1)
  117. out = self.run_command("coverage run -p b_or_c.py c")
  118. self.assertEqual(out, 'done\n')
  119. self.assert_exists(".coverage")
  120. self.assertEqual(self.number_of_data_files(), 2)
  121. # Combine the parallel coverage data files into .coverage .
  122. self.run_command("coverage combine --append")
  123. self.assert_exists(".coverage")
  124. # After combining, there should be only the .coverage file.
  125. self.assertEqual(self.number_of_data_files(), 1)
  126. # Read the coverage file and see that b_or_c.py has all 7 lines
  127. # executed.
  128. data = coverage.CoverageData()
  129. data.read_file(".coverage")
  130. self.assertEqual(data.line_counts()['b_or_c.py'], 7)
  131. def test_append_data(self):
  132. self.make_b_or_c_py()
  133. out = self.run_command("coverage run b_or_c.py b")
  134. self.assertEqual(out, 'done\n')
  135. self.assert_exists(".coverage")
  136. self.assertEqual(self.number_of_data_files(), 1)
  137. out = self.run_command("coverage run --append b_or_c.py c")
  138. self.assertEqual(out, 'done\n')
  139. self.assert_exists(".coverage")
  140. self.assertEqual(self.number_of_data_files(), 1)
  141. # Read the coverage file and see that b_or_c.py has all 7 lines
  142. # executed.
  143. data = coverage.CoverageData()
  144. data.read_file(".coverage")
  145. self.assertEqual(data.line_counts()['b_or_c.py'], 7)
  146. def test_append_data_with_different_file(self):
  147. self.make_b_or_c_py()
  148. self.make_file(".coveragerc", """\
  149. [run]
  150. data_file = .mycovdata
  151. """)
  152. out = self.run_command("coverage run b_or_c.py b")
  153. self.assertEqual(out, 'done\n')
  154. self.assert_doesnt_exist(".coverage")
  155. self.assert_exists(".mycovdata")
  156. out = self.run_command("coverage run --append b_or_c.py c")
  157. self.assertEqual(out, 'done\n')
  158. self.assert_doesnt_exist(".coverage")
  159. self.assert_exists(".mycovdata")
  160. # Read the coverage file and see that b_or_c.py has all 7 lines
  161. # executed.
  162. data = coverage.CoverageData()
  163. data.read_file(".mycovdata")
  164. self.assertEqual(data.line_counts()['b_or_c.py'], 7)
  165. def test_append_can_create_a_data_file(self):
  166. self.make_b_or_c_py()
  167. out = self.run_command("coverage run --append b_or_c.py b")
  168. self.assertEqual(out, 'done\n')
  169. self.assert_exists(".coverage")
  170. self.assertEqual(self.number_of_data_files(), 1)
  171. # Read the coverage file and see that b_or_c.py has only 6 lines
  172. # executed.
  173. data = coverage.CoverageData()
  174. data.read_file(".coverage")
  175. self.assertEqual(data.line_counts()['b_or_c.py'], 6)
  176. def test_combine_with_rc(self):
  177. self.make_b_or_c_py()
  178. self.make_file(".coveragerc", """\
  179. [run]
  180. parallel = true
  181. """)
  182. out = self.run_command("coverage run b_or_c.py b")
  183. self.assertEqual(out, 'done\n')
  184. self.assert_doesnt_exist(".coverage")
  185. out = self.run_command("coverage run b_or_c.py c")
  186. self.assertEqual(out, 'done\n')
  187. self.assert_doesnt_exist(".coverage")
  188. # After two runs, there should be two .coverage.machine.123 files.
  189. self.assertEqual(self.number_of_data_files(), 2)
  190. # Combine the parallel coverage data files into .coverage .
  191. self.run_command("coverage combine")
  192. self.assert_exists(".coverage")
  193. self.assert_exists(".coveragerc")
  194. # After combining, there should be only the .coverage file.
  195. self.assertEqual(self.number_of_data_files(), 1)
  196. # Read the coverage file and see that b_or_c.py has all 7 lines
  197. # executed.
  198. data = coverage.CoverageData()
  199. data.read_file(".coverage")
  200. self.assertEqual(data.line_counts()['b_or_c.py'], 7)
  201. # Reporting should still work even with the .rc file
  202. out = self.run_command("coverage report")
  203. self.assertMultiLineEqual(out, textwrap.dedent("""\
  204. Name Stmts Miss Cover
  205. -------------------------------
  206. b_or_c.py 7 0 100%
  207. """))
  208. def test_combine_with_aliases(self):
  209. self.make_file("d1/x.py", """\
  210. a = 1
  211. b = 2
  212. print("%s %s" % (a, b))
  213. """)
  214. self.make_file("d2/x.py", """\
  215. # 1
  216. # 2
  217. # 3
  218. c = 4
  219. d = 5
  220. print("%s %s" % (c, d))
  221. """)
  222. self.make_file(".coveragerc", """\
  223. [run]
  224. parallel = True
  225. [paths]
  226. source =
  227. src
  228. */d1
  229. */d2
  230. """)
  231. out = self.run_command("coverage run " + os.path.normpath("d1/x.py"))
  232. self.assertEqual(out, '1 2\n')
  233. out = self.run_command("coverage run " + os.path.normpath("d2/x.py"))
  234. self.assertEqual(out, '4 5\n')
  235. self.assertEqual(self.number_of_data_files(), 2)
  236. self.run_command("coverage combine")
  237. self.assert_exists(".coverage")
  238. # After combining, there should be only the .coverage file.
  239. self.assertEqual(self.number_of_data_files(), 1)
  240. # Read the coverage data file and see that the two different x.py
  241. # files have been combined together.
  242. data = coverage.CoverageData()
  243. data.read_file(".coverage")
  244. summary = data.line_counts(fullpath=True)
  245. self.assertEqual(len(summary), 1)
  246. actual = os.path.normcase(os.path.abspath(list(summary.keys())[0]))
  247. expected = os.path.normcase(os.path.abspath('src/x.py'))
  248. self.assertEqual(actual, expected)
  249. self.assertEqual(list(summary.values())[0], 6)
  250. def test_erase_parallel(self):
  251. self.make_file(".coveragerc", """\
  252. [run]
  253. data_file = data.dat
  254. parallel = True
  255. """)
  256. self.make_file("data.dat")
  257. self.make_file("data.dat.fooey")
  258. self.make_file("data.dat.gooey")
  259. self.make_file(".coverage")
  260. self.run_command("coverage erase")
  261. self.assert_doesnt_exist("data.dat")
  262. self.assert_doesnt_exist("data.dat.fooey")
  263. self.assert_doesnt_exist("data.dat.gooey")
  264. self.assert_exists(".coverage")
  265. def test_missing_source_file(self):
  266. # Check what happens if the source is missing when reporting happens.
  267. self.make_file("fleeting.py", """\
  268. s = 'goodbye, cruel world!'
  269. """)
  270. self.run_command("coverage run fleeting.py")
  271. os.remove("fleeting.py")
  272. out = self.run_command("coverage html -d htmlcov")
  273. self.assertRegex(out, "No source for code: '.*fleeting.py'")
  274. self.assertNotIn("Traceback", out)
  275. # It happens that the code paths are different for *.py and other
  276. # files, so try again with no extension.
  277. self.make_file("fleeting", """\
  278. s = 'goodbye, cruel world!'
  279. """)
  280. self.run_command("coverage run fleeting")
  281. os.remove("fleeting")
  282. status, out = self.run_command_status("coverage html -d htmlcov")
  283. self.assertRegex(out, "No source for code: '.*fleeting'")
  284. self.assertNotIn("Traceback", out)
  285. self.assertEqual(status, 1)
  286. def test_running_missing_file(self):
  287. status, out = self.run_command_status("coverage run xyzzy.py")
  288. self.assertRegex(out, "No file to run: .*xyzzy.py")
  289. self.assertNotIn("raceback", out)
  290. self.assertNotIn("rror", out)
  291. self.assertEqual(status, 1)
  292. def test_code_throws(self):
  293. self.make_file("throw.py", """\
  294. def f1():
  295. raise Exception("hey!")
  296. def f2():
  297. f1()
  298. f2()
  299. """)
  300. # The important thing is for "coverage run" and "python" to report the
  301. # same traceback.
  302. status, out = self.run_command_status("coverage run throw.py")
  303. out2 = self.run_command("python throw.py")
  304. if env.PYPY:
  305. # Pypy has an extra frame in the traceback for some reason
  306. lines2 = out2.splitlines()
  307. out2 = "".join(l+"\n" for l in lines2 if "toplevel" not in l)
  308. self.assertMultiLineEqual(out, out2)
  309. # But also make sure that the output is what we expect.
  310. self.assertIn('File "throw.py", line 5, in f2', out)
  311. self.assertIn('raise Exception("hey!")', out)
  312. self.assertNotIn('coverage', out)
  313. self.assertEqual(status, 1)
  314. def test_code_exits(self):
  315. self.make_file("exit.py", """\
  316. import sys
  317. def f1():
  318. print("about to exit..")
  319. sys.exit(17)
  320. def f2():
  321. f1()
  322. f2()
  323. """)
  324. # The important thing is for "coverage run" and "python" to have the
  325. # same output. No traceback.
  326. status, out = self.run_command_status("coverage run exit.py")
  327. status2, out2 = self.run_command_status("python exit.py")
  328. self.assertMultiLineEqual(out, out2)
  329. self.assertMultiLineEqual(out, "about to exit..\n")
  330. self.assertEqual(status, status2)
  331. self.assertEqual(status, 17)
  332. def test_code_exits_no_arg(self):
  333. self.make_file("exit_none.py", """\
  334. import sys
  335. def f1():
  336. print("about to exit quietly..")
  337. sys.exit()
  338. f1()
  339. """)
  340. status, out = self.run_command_status("coverage run exit_none.py")
  341. status2, out2 = self.run_command_status("python exit_none.py")
  342. self.assertMultiLineEqual(out, out2)
  343. self.assertMultiLineEqual(out, "about to exit quietly..\n")
  344. self.assertEqual(status, status2)
  345. self.assertEqual(status, 0)
  346. def assert_execfile_output(self, out):
  347. """Assert that the output we got is a successful run of try_execfile.py"""
  348. self.assertIn('"DATA": "xyzzy"', out)
  349. def test_coverage_run_is_like_python(self):
  350. with open(TRY_EXECFILE) as f:
  351. self.make_file("run_me.py", f.read())
  352. out_cov = self.run_command("coverage run run_me.py")
  353. out_py = self.run_command("python run_me.py")
  354. self.assertMultiLineEqual(out_cov, out_py)
  355. self.assert_execfile_output(out_cov)
  356. def test_coverage_run_dashm_is_like_python_dashm(self):
  357. # These -m commands assume the coverage tree is on the path.
  358. out_cov = self.run_command("coverage run -m process_test.try_execfile")
  359. out_py = self.run_command("python -m process_test.try_execfile")
  360. self.assertMultiLineEqual(out_cov, out_py)
  361. self.assert_execfile_output(out_cov)
  362. def test_coverage_run_dir_is_like_python_dir(self):
  363. with open(TRY_EXECFILE) as f:
  364. self.make_file("with_main/__main__.py", f.read())
  365. out_cov = self.run_command("coverage run with_main")
  366. out_py = self.run_command("python with_main")
  367. # The coverage.py results are not identical to the Python results, and
  368. # I don't know why. For now, ignore those failures. If someone finds
  369. # a real problem with the discrepancies, we can work on it some more.
  370. ignored = r"__file__|__loader__|__package__"
  371. # PyPy includes the current directory in the path when running a
  372. # directory, while CPython and coverage.py do not. Exclude that from
  373. # the comparison also...
  374. if env.PYPY:
  375. ignored += "|"+re.escape(os.getcwd())
  376. out_cov = remove_matching_lines(out_cov, ignored)
  377. out_py = remove_matching_lines(out_py, ignored)
  378. self.assertMultiLineEqual(out_cov, out_py)
  379. self.assert_execfile_output(out_cov)
  380. def test_coverage_run_dashm_equal_to_doubledashsource(self):
  381. """regression test for #328
  382. When imported by -m, a module's __name__ is __main__, but we need the
  383. --source machinery to know and respect the original name.
  384. """
  385. # These -m commands assume the coverage tree is on the path.
  386. out_cov = self.run_command(
  387. "coverage run --source process_test.try_execfile -m process_test.try_execfile"
  388. )
  389. out_py = self.run_command("python -m process_test.try_execfile")
  390. self.assertMultiLineEqual(out_cov, out_py)
  391. self.assert_execfile_output(out_cov)
  392. def test_coverage_run_dashm_superset_of_doubledashsource(self):
  393. """Edge case: --source foo -m foo.bar"""
  394. # These -m commands assume the coverage tree is on the path.
  395. out_cov = self.run_command(
  396. "coverage run --source process_test -m process_test.try_execfile"
  397. )
  398. out_py = self.run_command("python -m process_test.try_execfile")
  399. self.assertMultiLineEqual(out_cov, out_py)
  400. self.assert_execfile_output(out_cov)
  401. st, out = self.run_command_status("coverage report")
  402. self.assertEqual(st, 0)
  403. self.assertEqual(self.line_count(out), 6, out)
  404. def test_coverage_run_script_imports_doubledashsource(self):
  405. # This file imports try_execfile, which compiles it to .pyc, so the
  406. # first run will have __file__ == "try_execfile.py" and the second will
  407. # have __file__ == "try_execfile.pyc", which throws off the comparison.
  408. # Setting dont_write_bytecode True stops the compilation to .pyc and
  409. # keeps the test working.
  410. self.make_file("myscript", """\
  411. import sys; sys.dont_write_bytecode = True
  412. import process_test.try_execfile
  413. """)
  414. # These -m commands assume the coverage tree is on the path.
  415. out_cov = self.run_command(
  416. "coverage run --source process_test myscript"
  417. )
  418. out_py = self.run_command("python myscript")
  419. self.assertMultiLineEqual(out_cov, out_py)
  420. self.assert_execfile_output(out_cov)
  421. st, out = self.run_command_status("coverage report")
  422. self.assertEqual(st, 0)
  423. self.assertEqual(self.line_count(out), 6, out)
  424. def test_coverage_run_dashm_is_like_python_dashm_off_path(self):
  425. # https://bitbucket.org/ned/coveragepy/issue/242
  426. self.make_file("sub/__init__.py", "")
  427. with open(TRY_EXECFILE) as f:
  428. self.make_file("sub/run_me.py", f.read())
  429. out_cov = self.run_command("coverage run -m sub.run_me")
  430. out_py = self.run_command("python -m sub.run_me")
  431. self.assertMultiLineEqual(out_cov, out_py)
  432. self.assert_execfile_output(out_cov)
  433. def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self):
  434. if sys.version_info < (2, 7):
  435. # Coverage.py isn't bug-for-bug compatible in the behavior of -m for
  436. # Pythons < 2.7
  437. self.skipTest("-m doesn't work the same < Python 2.7")
  438. # https://bitbucket.org/ned/coveragepy/issue/207
  439. self.make_file("package/__init__.py", "print('init')")
  440. self.make_file("package/__main__.py", "print('main')")
  441. out_cov = self.run_command("coverage run -m package")
  442. out_py = self.run_command("python -m package")
  443. self.assertMultiLineEqual(out_cov, out_py)
  444. def test_fork(self):
  445. if not hasattr(os, 'fork'):
  446. self.skipTest("Can't test os.fork since it doesn't exist.")
  447. self.make_file("fork.py", """\
  448. import os
  449. def child():
  450. print('Child!')
  451. def main():
  452. ret = os.fork()
  453. if ret == 0:
  454. child()
  455. else:
  456. os.waitpid(ret, 0)
  457. main()
  458. """)
  459. out = self.run_command("coverage run -p fork.py")
  460. self.assertEqual(out, 'Child!\n')
  461. self.assert_doesnt_exist(".coverage")
  462. # After running the forking program, there should be two
  463. # .coverage.machine.123 files.
  464. self.assertEqual(self.number_of_data_files(), 2)
  465. # Combine the parallel coverage data files into .coverage .
  466. self.run_command("coverage combine")
  467. self.assert_exists(".coverage")
  468. # After combining, there should be only the .coverage file.
  469. self.assertEqual(self.number_of_data_files(), 1)
  470. # Read the coverage file and see that b_or_c.py has all 7 lines
  471. # executed.
  472. data = coverage.CoverageData()
  473. data.read_file(".coverage")
  474. self.assertEqual(data.line_counts()['fork.py'], 9)
  475. def test_warnings(self):
  476. self.make_file("hello.py", """\
  477. import sys, os
  478. print("Hello")
  479. """)
  480. out = self.run_command("coverage run --source=sys,xyzzy,quux hello.py")
  481. self.assertIn("Hello\n", out)
  482. self.assertIn(textwrap.dedent("""\
  483. Coverage.py warning: Module sys has no Python source.
  484. Coverage.py warning: Module xyzzy was never imported.
  485. Coverage.py warning: Module quux was never imported.
  486. Coverage.py warning: No data was collected.
  487. """), out)
  488. def test_warnings_during_reporting(self):
  489. # While fixing issue #224, the warnings were being printed far too
  490. # often. Make sure they're not any more.
  491. self.make_file("hello.py", """\
  492. import sys, os, the_other
  493. print("Hello")
  494. """)
  495. self.make_file("the_other.py", """\
  496. print("What?")
  497. """)
  498. self.make_file(".coveragerc", """\
  499. [run]
  500. source =
  501. .
  502. xyzzy
  503. """)
  504. self.run_command("coverage run hello.py")
  505. out = self.run_command("coverage html")
  506. self.assertEqual(out.count("Module xyzzy was never imported."), 0)
  507. def test_warnings_if_never_run(self):
  508. out = self.run_command("coverage run i_dont_exist.py")
  509. self.assertIn("No file to run: 'i_dont_exist.py'", out)
  510. self.assertNotIn("warning", out)
  511. self.assertNotIn("Exception", out)
  512. out = self.run_command("coverage run -m no_such_module")
  513. self.assertTrue(
  514. ("No module named no_such_module" in out) or
  515. ("No module named 'no_such_module'" in out)
  516. )
  517. self.assertNotIn("warning", out)
  518. self.assertNotIn("Exception", out)
  519. def test_warnings_trace_function_changed_with_threads(self):
  520. # https://bitbucket.org/ned/coveragepy/issue/164
  521. self.make_file("bug164.py", """\
  522. import threading
  523. import time
  524. class MyThread (threading.Thread):
  525. def run(self):
  526. print("Hello")
  527. thr = MyThread()
  528. thr.start()
  529. thr.join()
  530. """)
  531. out = self.run_command("coverage run --timid bug164.py")
  532. self.assertIn("Hello\n", out)
  533. self.assertNotIn("warning", out)
  534. def test_warning_trace_function_changed(self):
  535. self.make_file("settrace.py", """\
  536. import sys
  537. print("Hello")
  538. sys.settrace(None)
  539. print("Goodbye")
  540. """)
  541. out = self.run_command("coverage run --timid settrace.py")
  542. self.assertIn("Hello\n", out)
  543. self.assertIn("Goodbye\n", out)
  544. self.assertIn("Trace function changed", out)
  545. def test_note(self):
  546. self.make_file(".coveragerc", """\
  547. [run]
  548. data_file = mydata.dat
  549. note = These are musical notes: ♫𝅗𝅥♩
  550. """)
  551. self.make_file("simple.py", """print('hello')""")
  552. self.run_command("coverage run simple.py")
  553. data = CoverageData()
  554. data.read_file("mydata.dat")
  555. infos = data.run_infos()
  556. self.assertEqual(len(infos), 1)
  557. self.assertEqual(infos[0]['note'], u"These are musical notes: ♫𝅗𝅥♩")
  558. def test_fullcoverage(self): # pragma: not covered
  559. if env.PY2: # This doesn't work on Python 2.
  560. self.skipTest("fullcoverage doesn't work on Python 2.")
  561. # It only works with the C tracer, and if we aren't measuring ourselves.
  562. if not env.C_TRACER or env.METACOV:
  563. self.skipTest("fullcoverage only works with the C tracer.")
  564. # fullcoverage is a trick to get stdlib modules measured from
  565. # the very beginning of the process. Here we import os and
  566. # then check how many lines are measured.
  567. self.make_file("getenv.py", """\
  568. import os
  569. print("FOOEY == %s" % os.getenv("FOOEY"))
  570. """)
  571. fullcov = os.path.join(
  572. os.path.dirname(coverage.__file__), "fullcoverage"
  573. )
  574. self.set_environ("FOOEY", "BOO")
  575. self.set_environ("PYTHONPATH", fullcov)
  576. out = self.run_command("python -m coverage run -L getenv.py")
  577. self.assertEqual(out, "FOOEY == BOO\n")
  578. data = coverage.CoverageData()
  579. data.read_file(".coverage")
  580. # The actual number of executed lines in os.py when it's
  581. # imported is 120 or so. Just running os.getenv executes
  582. # about 5.
  583. self.assertGreater(data.line_counts()['os.py'], 50)
  584. def test_deprecation_warnings(self):
  585. # Test that coverage doesn't trigger deprecation warnings.
  586. # https://bitbucket.org/ned/coveragepy/issue/305/pendingdeprecationwarning-the-imp-module
  587. self.make_file("allok.py", """\
  588. import warnings
  589. warnings.simplefilter('default')
  590. import coverage
  591. print("No warnings!")
  592. """)
  593. out = self.run_command("python allok.py")
  594. self.assertEqual(out, "No warnings!\n")
  595. def test_run_twice(self):
  596. # https://bitbucket.org/ned/coveragepy/issue/353/40a3-introduces-an-unexpected-third-case
  597. self.make_file("foo.py", """\
  598. def foo():
  599. pass
  600. """)
  601. self.make_file("run_twice.py", """\
  602. import coverage
  603. for _ in [1, 2]:
  604. inst = coverage.Coverage(source=['foo'])
  605. inst.load()
  606. inst.start()
  607. import foo
  608. inst.stop()
  609. inst.combine()
  610. inst.save()
  611. """)
  612. out = self.run_command("python run_twice.py")
  613. self.assertEqual(
  614. out,
  615. "Coverage.py warning: Module foo was previously imported, but not measured.\n"
  616. )
  617. def test_module_name(self):
  618. if sys.version_info < (2, 7):
  619. # Python 2.6 thinks that coverage is a package that can't be
  620. # executed
  621. self.skipTest("-m doesn't work the same < Python 2.7")
  622. # https://bitbucket.org/ned/coveragepy/issues/478/help-shows-silly-program-name-when-running
  623. out = self.run_command("python -m coverage")
  624. self.assertIn("Use 'coverage help' for help", out)
  625. class AliasedCommandTest(CoverageTest):
  626. """Tests of the version-specific command aliases."""
  627. run_in_temp_dir = False
  628. def test_major_version_works(self):
  629. # "coverage2" works on py2
  630. cmd = "coverage%d" % sys.version_info[0]
  631. out = self.run_command(cmd)
  632. self.assertIn("Code coverage for Python", out)
  633. def test_wrong_alias_doesnt_work(self):
  634. # "coverage3" doesn't work on py2
  635. badcmd = "coverage%d" % (5 - sys.version_info[0])
  636. out = self.run_command(badcmd)
  637. self.assertNotIn("Code coverage for Python", out)
  638. def test_specific_alias_works(self):
  639. # "coverage-2.7" works on py2.7
  640. cmd = "coverage-%d.%d" % sys.version_info[:2]
  641. out = self.run_command(cmd)
  642. self.assertIn("Code coverage for Python", out)
  643. def test_aliases_used_in_messages(self):
  644. cmds = [
  645. "coverage",
  646. "coverage%d" % sys.version_info[0],
  647. "coverage-%d.%d" % sys.version_info[:2],
  648. ]
  649. for cmd in cmds:
  650. out = self.run_command("%s foobar" % cmd)
  651. self.assertIn("Unknown command: 'foobar'", out)
  652. self.assertIn("Use '%s help' for help" % cmd, out)
  653. class PydocTest(CoverageTest):
  654. """Test that pydoc can get our information."""
  655. run_in_temp_dir = False
  656. def assert_pydoc_ok(self, name, thing):
  657. """Check that pydoc of `name` finds the docstring from `thing`."""
  658. # Run pydoc.
  659. out = self.run_command("python -m pydoc " + name)
  660. # It should say "Help on..", and not have a traceback
  661. self.assert_starts_with(out, "Help on ")
  662. self.assertNotIn("Traceback", out)
  663. # All of the lines in the docstring should be there somewhere.
  664. for line in thing.__doc__.splitlines():
  665. self.assertIn(line.strip(), out)
  666. def test_pydoc_coverage(self):
  667. self.assert_pydoc_ok("coverage", coverage)
  668. def test_pydoc_coverage_coverage(self):
  669. self.assert_pydoc_ok("coverage.Coverage", coverage.Coverage)
  670. class FailUnderTest(CoverageTest):
  671. """Tests of the --fail-under switch."""
  672. def setUp(self):
  673. super(FailUnderTest, self).setUp()
  674. self.make_file("forty_two_plus.py", """\
  675. # I have 42.857% (3/7) coverage!
  676. a = 1
  677. b = 2
  678. if a > 3:
  679. b = 4
  680. c = 5
  681. d = 6
  682. e = 7
  683. """)
  684. st, _ = self.run_command_status("coverage run forty_two_plus.py")
  685. self.assertEqual(st, 0)
  686. st, out = self.run_command_status("coverage report")
  687. self.assertEqual(st, 0)
  688. self.assertEqual(
  689. self.last_line_squeezed(out),
  690. "forty_two_plus.py 7 4 43%"
  691. )
  692. def test_report(self):
  693. st, _ = self.run_command_status("coverage report --fail-under=42")
  694. self.assertEqual(st, 0)
  695. st, _ = self.run_command_status("coverage report --fail-under=43")
  696. self.assertEqual(st, 0)
  697. st, _ = self.run_command_status("coverage report --fail-under=44")
  698. self.assertEqual(st, 2)
  699. def test_html_report(self):
  700. st, _ = self.run_command_status("coverage html --fail-under=42")
  701. self.assertEqual(st, 0)
  702. st, _ = self.run_command_status("coverage html --fail-under=43")
  703. self.assertEqual(st, 0)
  704. st, _ = self.run_command_status("coverage html --fail-under=44")
  705. self.assertEqual(st, 2)
  706. def test_xml_report(self):
  707. st, _ = self.run_command_status("coverage xml --fail-under=42")
  708. self.assertEqual(st, 0)
  709. st, _ = self.run_command_status("coverage xml --fail-under=43")
  710. self.assertEqual(st, 0)
  711. st, _ = self.run_command_status("coverage xml --fail-under=44")
  712. self.assertEqual(st, 2)
  713. def test_fail_under_in_config(self):
  714. self.make_file(".coveragerc", "[report]\nfail_under = 43\n")
  715. st, _ = self.run_command_status("coverage report")
  716. self.assertEqual(st, 0)
  717. self.make_file(".coveragerc", "[report]\nfail_under = 44\n")
  718. st, _ = self.run_command_status("coverage report")
  719. self.assertEqual(st, 2)
  720. class FailUnderNoFilesTest(CoverageTest):
  721. """Test that nothing to report results in an error exit status."""
  722. def setUp(self):
  723. super(FailUnderNoFilesTest, self).setUp()
  724. self.make_file(".coveragerc", "[report]\nfail_under = 99\n")
  725. def test_report(self):
  726. st, out = self.run_command_status("coverage report")
  727. self.assertIn('No data to report.', out)
  728. self.assertEqual(st, 1)
  729. def test_xml(self):
  730. st, out = self.run_command_status("coverage xml")
  731. self.assertIn('No data to report.', out)
  732. self.assertEqual(st, 1)
  733. def test_html(self):
  734. st, out = self.run_command_status("coverage html")
  735. self.assertIn('No data to report.', out)
  736. self.assertEqual(st, 1)
  737. class FailUnderEmptyFilesTest(CoverageTest):
  738. """Test that empty files produce the proper fail_under exit status."""
  739. def setUp(self):
  740. super(FailUnderEmptyFilesTest, self).setUp()
  741. self.make_file(".coveragerc", "[report]\nfail_under = 99\n")
  742. self.make_file("empty.py", "")
  743. st, _ = self.run_command_status("coverage run empty.py")
  744. self.assertEqual(st, 0)
  745. def test_report(self):
  746. st, _ = self.run_command_status("coverage report")
  747. self.assertEqual(st, 2)
  748. def test_xml(self):
  749. st, _ = self.run_command_status("coverage xml")
  750. self.assertEqual(st, 2)
  751. def test_html(self):
  752. st, _ = self.run_command_status("coverage html")
  753. self.assertEqual(st, 2)
  754. class FailUnder100Test(CoverageTest):
  755. """Tests of the --fail-under switch."""
  756. def test_99_8(self):
  757. self.make_file("ninety_nine_eight.py",
  758. "".join("v{i} = {i}\n".format(i=i) for i in range(498)) +
  759. "if v0 > 498:\n v499 = 499\n"
  760. )
  761. st, _ = self.run_command_status("coverage run ninety_nine_eight.py")
  762. self.assertEqual(st, 0)
  763. st, out = self.run_command_status("coverage report")
  764. self.assertEqual(st, 0)
  765. self.assertEqual(
  766. self.last_line_squeezed(out),
  767. "ninety_nine_eight.py 500 1 99%"
  768. )
  769. st, _ = self.run_command_status("coverage report --fail-under=100")
  770. self.assertEqual(st, 2)
  771. def test_100(self):
  772. self.make_file("one_hundred.py",
  773. "".join("v{i} = {i}\n".format(i=i) for i in range(500))
  774. )
  775. st, _ = self.run_command_status("coverage run one_hundred.py")
  776. self.assertEqual(st, 0)
  777. st, out = self.run_command_status("coverage report")
  778. self.assertEqual(st, 0)
  779. self.assertEqual(
  780. self.last_line_squeezed(out),
  781. "one_hundred.py 500 0 100%"
  782. )
  783. st, _ = self.run_command_status("coverage report --fail-under=100")
  784. self.assertEqual(st, 0)
  785. class UnicodeFilePathsTest(CoverageTest):
  786. """Tests of using non-ascii characters in the names of files."""
  787. def test_accented_dot_py(self):
  788. # Make a file with a non-ascii character in the filename.
  789. self.make_file(u"h\xe2t.py", "print('accented')")
  790. out = self.run_command(u"coverage run h\xe2t.py")
  791. self.assertEqual(out, "accented\n")
  792. # The HTML report uses ascii-encoded HTML entities.
  793. out = self.run_command("coverage html")
  794. self.assertEqual(out, "")
  795. self.assert_exists(u"htmlcov/h\xe2t_py.html")
  796. with open("htmlcov/index.html") as indexf:
  797. index = indexf.read()
  798. self.assertIn('<a href="h&#226;t_py.html">h&#226;t.py</a>', index)
  799. # The XML report is always UTF8-encoded.
  800. out = self.run_command("coverage xml")
  801. self.assertEqual(out, "")
  802. with open("coverage.xml", "rb") as xmlf:
  803. xml = xmlf.read()
  804. self.assertIn(u' filename="h\xe2t.py"'.encode('utf8'), xml)
  805. self.assertIn(u' name="h\xe2t.py"'.encode('utf8'), xml)
  806. report_expected = (
  807. u"Name Stmts Miss Cover\n"
  808. u"----------------------------\n"
  809. u"h\xe2t.py 1 0 100%\n"
  810. )
  811. if env.PY2:
  812. # pylint: disable=redefined-variable-type
  813. report_expected = report_expected.encode(output_encoding())
  814. out = self.run_command("coverage report")
  815. self.assertEqual(out, report_expected)
  816. def test_accented_directory(self):
  817. # Make a file with a non-ascii character in the directory name.
  818. self.make_file(u"\xe2/accented.py", "print('accented')")
  819. out = self.run_command(u"coverage run \xe2/accented.py")
  820. self.assertEqual(out, "accented\n")
  821. # The HTML report uses ascii-encoded HTML entities.
  822. out = self.run_command("coverage html")
  823. self.assertEqual(out, "")
  824. self.assert_exists(u"htmlcov/\xe2_accented_py.html")
  825. with open("htmlcov/index.html") as indexf:
  826. index = indexf.read()
  827. self.assertIn('<a href="&#226;_accented_py.html">&#226;%saccented.py</a>' % os.sep, index)
  828. # The XML report is always UTF8-encoded.
  829. out = self.run_command("coverage xml")
  830. self.assertEqual(out, "")
  831. with open("coverage.xml", "rb") as xmlf:
  832. xml = xmlf.read()
  833. self.assertIn(u' filename="\xe2/accented.py"'.encode('utf8'), xml)
  834. self.assertIn(u' name="accented.py"'.encode('utf8'), xml)
  835. self.assertIn(
  836. u'<package branch-rate="0" complexity="0" line-rate="1" name="\xe2">'.encode('utf8'),
  837. xml
  838. )
  839. report_expected = (
  840. u"Name Stmts Miss Cover\n"
  841. u"-----------------------------------\n"
  842. u"\xe2%saccented.py 1 0 100%%\n" % os.sep
  843. )
  844. if env.PY2:
  845. # pylint: disable=redefined-variable-type
  846. report_expected = report_expected.encode(output_encoding())
  847. out = self.run_command("coverage report")
  848. self.assertEqual(out, report_expected)
  849. def possible_pth_dirs():
  850. """Produce a sequence of directories for trying to write .pth files."""
  851. # First look through sys.path, and we find a .pth file, then it's a good
  852. # place to put ours.
  853. for d in sys.path:
  854. g = glob.glob(os.path.join(d, "*.pth"))
  855. if g:
  856. yield d
  857. # If we're still looking, then try the Python library directory.
  858. # https://bitbucket.org/ned/coveragepy/issue/339/pth-test-malfunctions
  859. import distutils.sysconfig # pylint: disable=import-error
  860. yield distutils.sysconfig.get_python_lib()
  861. class ProcessCoverageMixin(object):
  862. """Set up a .pth file to coverage-measure all sub-processes."""
  863. def setUp(self):
  864. super(ProcessCoverageMixin, self).setUp()
  865. # Find a place to put a .pth file.
  866. pth_contents = "import coverage; coverage.process_startup()\n"
  867. for pth_dir in possible_pth_dirs(): # pragma: part covered
  868. pth_path = os.path.join(pth_dir, "subcover.pth")
  869. with open(pth_path, "w") as pth:
  870. try:
  871. pth.write(pth_contents)
  872. self.pth_path = pth_path
  873. break
  874. except (IOError, OSError): # pragma: not covered
  875. pass
  876. else: # pragma: not covered
  877. raise Exception("Couldn't find a place for the .pth file")
  878. self.addCleanup(os.remove, self.pth_path)
  879. class ProcessStartupTest(ProcessCoverageMixin, CoverageTest):
  880. """Test that we can measure coverage in sub-processes."""
  881. def setUp(self):
  882. super(ProcessStartupTest, self).setUp()
  883. # Main will run sub.py
  884. self.make_file("main.py", """\
  885. import os, os.path, sys
  886. ex = os.path.basename(sys.executable)
  887. os.system(ex + " sub.py")
  888. """)
  889. # sub.py will write a few lines.
  890. self.make_file("sub.py", """\
  891. with open("out.txt", "w") as f:
  892. f.write("Hello, world!\\n")
  893. """)
  894. def test_subprocess_with_pth_files(self): # pragma: not covered
  895. if env.METACOV:
  896. self.skipTest("Can't test sub-process pth file suppport during metacoverage")
  897. self.make_file("coverage.ini", """\
  898. [run]
  899. data_file = .mycovdata
  900. """)
  901. self.set_environ("COVERAGE_PROCESS_START", "coverage.ini")
  902. import main # pylint: disable=import-error
  903. with open("out.txt") as f:
  904. self.assertEqual(f.read(), "Hello, world!\n")
  905. # Read the data from .coverage
  906. self.assert_exists(".mycovdata")
  907. data = coverage.CoverageData()
  908. data.read_file(".mycovdata")
  909. self.assertEqual(data.line_counts()['sub.py'], 2)
  910. def test_subprocess_with_pth_files_and_parallel(self): # pragma: not covered
  911. # https://bitbucket.org/ned/coveragepy/issues/492/subprocess-coverage-strange-detection-of
  912. if env.METACOV:
  913. self.skipTest("Can't test sub-process pth file suppport during metacoverage")
  914. self.make_file("coverage.ini", """\
  915. [run]
  916. parallel = true
  917. """)
  918. self.set_environ("COVERAGE_PROCESS_START", "coverage.ini")
  919. self.run_command("coverage run main.py")
  920. with open("out.txt") as f:
  921. self.assertEqual(f.read(), "Hello, world!\n")
  922. self.run_command("coverage combine")
  923. # assert that the combined .coverage data file is correct
  924. self.assert_exists(".coverage")
  925. data = coverage.CoverageData()
  926. data.read_file(".coverage")
  927. self.assertEqual(data.line_counts()['sub.py'], 2)
  928. # assert that there are *no* extra data files left over after a combine
  929. data_files = glob.glob(os.getcwd() + '/.coverage*')
  930. self.assertEquals(len(data_files), 1,
  931. "Expected only .coverage after combine, looks like there are " + \
  932. "extra data files that were not cleaned up: %r" % data_files)
  933. class ProcessStartupWithSourceTest(ProcessCoverageMixin, CoverageTest):
  934. """Show that we can configure {[run]source} during process-level coverage.
  935. There are three interesting variables, for a total of eight tests:
  936. 1. -m versus a simple script argument (for example, `python myscript`),
  937. 2. filtering for the top-level (main.py) or second-level (sub.py)
  938. module, and
  939. 3. whether the files are in a package or not.
  940. """
  941. def assert_pth_and_source_work_together(
  942. self, dashm, package, source
  943. ): # pragma: not covered
  944. """Run the test for a particular combination of factors.
  945. The arguments are all strings:
  946. * `dashm`: Either "" (run the program as a file) or "-m" (run the
  947. program as a module).
  948. * `package`: Either "" (put the source at the top level) or a
  949. package name to use to hold the source.
  950. * `source`: Either "main" or "sub", which file to use as the
  951. ``--source`` argument.
  952. """
  953. if env.METACOV:
  954. self.skipTest("Can't test sub-process pth file suppport during metacoverage")
  955. def fullname(modname):
  956. """What is the full module name for `modname` for this test?"""
  957. if package and dashm:
  958. return '.'.join((package, modname))
  959. else:
  960. return modname
  961. def path(basename):
  962. """Where should `basename` be created for this test?"""
  963. return os.path.join(package, basename)
  964. # Main will run sub.py.
  965. self.make_file(path("main.py"), """\
  966. import %s
  967. if True: pass
  968. """ % fullname('sub'))
  969. if package:
  970. self.make_file(path("__init__.py"), "")
  971. # sub.py will write a few lines.
  972. self.make_file(path("sub.py"), """\
  973. with open("out.txt", "w") as f:
  974. f.write("Hello, world!")
  975. """)
  976. self.make_file("coverage.ini", """\
  977. [run]
  978. source = %s
  979. """ % fullname(source))
  980. self.set_environ("COVERAGE_PROCESS_START", "coverage.ini")
  981. if dashm:
  982. cmd = "python -m %s" % fullname('main')
  983. else:
  984. cmd = "python %s" % path('main.py')
  985. self.run_command(cmd)
  986. with open("out.txt") as f:
  987. self.assertEqual(f.read(), "Hello, world!")
  988. # Read the data from .coverage
  989. self.assert_exists(".coverage")
  990. data = coverage.CoverageData()
  991. data.read_file(".coverage")
  992. summary = data.line_counts()
  993. print(summary)
  994. self.assertEqual(summary[source + '.py'], 2)
  995. self.assertEqual(len(summary), 1)
  996. def test_dashm_main(self):
  997. self.assert_pth_and_source_work_together('-m', '', 'main')
  998. def test_script_main(self):
  999. self.assert_pth_and_source_work_together('', '', 'main')
  1000. def test_dashm_sub(self):
  1001. self.assert_pth_and_source_work_together('-m', '', 'sub')
  1002. def test_script_sub(self):
  1003. self.assert_pth_and_source_work_together('', '', 'sub')
  1004. def test_dashm_pkg_main(self):
  1005. self.assert_pth_and_source_work_together('-m', 'pkg', 'main')
  1006. def test_script_pkg_main(self):
  1007. self.assert_pth_and_source_work_together('', 'pkg', 'main')
  1008. def test_dashm_pkg_sub(self):
  1009. self.assert_pth_and_source_work_together('-m', 'pkg', 'sub')
  1010. def test_script_pkg_sub(self):
  1011. self.assert_pth_and_source_work_together('', 'pkg', 'sub')
  1012. def remove_matching_lines(text, pat):
  1013. """Return `text` with all lines matching `pat` removed."""
  1014. lines = [l for l in text.splitlines(True) if not re.search(pat, l)]
  1015. return "".join(lines)