-
-
Notifications
You must be signed in to change notification settings - Fork 443
/
Copy pathtest_process.py
1649 lines (1343 loc) · 60.1 KB
/
test_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Tests for process behavior of coverage.py."""
import glob
import os
import os.path
import re
import stat
import sys
import sysconfig
import textwrap
import time
from xml.etree import ElementTree
import pytest
import coverage
from coverage import env
from coverage.data import line_counts
from coverage.files import python_reported_file
from coverage.misc import output_encoding
from tests.coveragetest import CoverageTest, TESTS_DIR, xfail
from tests.helpers import re_lines
class ProcessTest(CoverageTest):
"""Tests of the per-process behavior of coverage.py."""
def test_save_on_exit(self):
self.make_file("mycode.py", """\
h = "Hello"
w = "world"
""")
self.assert_doesnt_exist(".coverage")
self.run_command("coverage run mycode.py")
self.assert_exists(".coverage")
def test_environment(self):
# Checks that we can import modules from the tests directory at all!
self.make_file("mycode.py", """\
import covmod1
import covmodzip1
a = 1
print('done')
""")
self.assert_doesnt_exist(".coverage")
out = self.run_command("coverage run mycode.py")
self.assert_exists(".coverage")
self.assertEqual(out, 'done\n')
def make_b_or_c_py(self):
"""Create b_or_c.py, used in a few of these tests."""
# "b_or_c.py b" will run 6 lines.
# "b_or_c.py c" will run 7 lines.
# Together, they run 8 lines.
self.make_file("b_or_c.py", """\
import sys
a = 1
if sys.argv[1] == 'b':
b = 1
else:
c = 1
c2 = 2
d = 1
print('done')
""")
def test_combine_parallel_data(self):
self.make_b_or_c_py()
out = self.run_command("coverage run -p b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
self.assert_file_count(".coverage.*", 1)
out = self.run_command("coverage run -p b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
# After two -p runs, there should be two .coverage.machine.123 files.
self.assert_file_count(".coverage.*", 2)
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage combine")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assert_file_count(".coverage.*", 0)
# Read the coverage file and see that b_or_c.py has all 8 lines
# executed.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
# Running combine again should fail, because there are no parallel data
# files to combine.
status, out = self.run_command_status("coverage combine")
self.assertEqual(status, 1)
self.assertEqual(out, "No data to combine\n")
# And the originally combined data is still there.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
def test_combine_parallel_data_with_a_corrupt_file(self):
self.make_b_or_c_py()
out = self.run_command("coverage run -p b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
self.assert_file_count(".coverage.*", 1)
out = self.run_command("coverage run -p b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
# After two -p runs, there should be two .coverage.machine.123 files.
self.assert_file_count(".coverage.*", 2)
# Make a bogus data file.
self.make_file(".coverage.bad", "This isn't a coverage data file.")
# Combine the parallel coverage data files into .coverage .
out = self.run_command("coverage combine")
self.assert_exists(".coverage")
self.assert_exists(".coverage.bad")
warning_regex = (
r"Coverage.py warning: Couldn't use data file '.*\.coverage\.bad': "
r"file (is encrypted or )?is not a database"
)
self.assertRegex(out, warning_regex)
# After combining, those two should be the only data files.
self.assert_file_count(".coverage.*", 1)
# Read the coverage file and see that b_or_c.py has all 8 lines
# executed.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
def test_combine_no_usable_files(self):
# https://github.com/nedbat/coveragepy/issues/629
self.make_b_or_c_py()
out = self.run_command("coverage run b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 0)
# Make bogus data files.
self.make_file(".coverage.bad1", "This isn't a coverage data file.")
self.make_file(".coverage.bad2", "This isn't a coverage data file.")
# Combine the parallel coverage data files into .coverage, but nothing is readable.
status, out = self.run_command_status("coverage combine")
self.assertEqual(status, 1)
for n in "12":
self.assert_exists(".coverage.bad{}".format(n))
warning_regex = (
r"Coverage.py warning: Couldn't use data file '.*\.coverage.bad{0}': "
r"file (is encrypted or )?is not a database"
.format(n)
)
self.assertRegex(out, warning_regex)
self.assertRegex(out, r"No usable data files")
# After combining, we should have a main file and two parallel files.
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 2)
# Read the coverage file and see that b_or_c.py has 6 lines
# executed (we only did b, not c).
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 6)
def test_combine_parallel_data_in_two_steps(self):
self.make_b_or_c_py()
out = self.run_command("coverage run -p b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
self.assert_file_count(".coverage.*", 1)
# Combine the (one) parallel coverage data file into .coverage .
self.run_command("coverage combine")
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 0)
out = self.run_command("coverage run -p b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 1)
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage combine --append")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assert_file_count(".coverage.*", 0)
# Read the coverage file and see that b_or_c.py has all 8 lines
# executed.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
def test_combine_parallel_data_no_append(self):
self.make_b_or_c_py()
out = self.run_command("coverage run -p b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
self.assert_file_count(".coverage.*", 1)
# Combine the (one) parallel coverage data file into .coverage .
self.run_command("coverage combine")
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 0)
out = self.run_command("coverage run -p b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 1)
# Combine the parallel coverage data files into .coverage, but don't
# use the data in .coverage already.
self.run_command("coverage combine")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assert_file_count(".coverage.*", 0)
# Read the coverage file and see that b_or_c.py has only 7 lines
# because we didn't keep the data from running b.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 7)
def test_append_data(self):
self.make_b_or_c_py()
out = self.run_command("coverage run b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 0)
out = self.run_command("coverage run --append b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 0)
# Read the coverage file and see that b_or_c.py has all 8 lines
# executed.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
def test_append_data_with_different_file(self):
self.make_b_or_c_py()
self.make_file(".coveragerc", """\
[run]
data_file = .mycovdata
""")
out = self.run_command("coverage run b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
self.assert_exists(".mycovdata")
out = self.run_command("coverage run --append b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
self.assert_exists(".mycovdata")
# Read the coverage file and see that b_or_c.py has all 8 lines
# executed.
data = coverage.CoverageData(".mycovdata")
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
def test_append_can_create_a_data_file(self):
self.make_b_or_c_py()
out = self.run_command("coverage run --append b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_exists(".coverage")
self.assert_file_count(".coverage.*", 0)
# Read the coverage file and see that b_or_c.py has only 6 lines
# executed.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 6)
def test_combine_with_rc(self):
self.make_b_or_c_py()
self.make_file(".coveragerc", """\
[run]
source = .
parallel = true
""")
out = self.run_command("coverage run b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
out = self.run_command("coverage run b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
# After two runs, there should be two .coverage.machine.123 files.
self.assert_file_count(".coverage.*", 2)
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage combine")
self.assert_exists(".coverage")
self.assert_exists(".coveragerc")
# After combining, there should be only the .coverage file.
self.assert_file_count(".coverage.*", 0)
# Read the coverage file and see that b_or_c.py has all 8 lines
# executed.
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['b_or_c.py'], 8)
# Reporting should still work even with the .rc file
out = self.run_command("coverage report")
self.assertMultiLineEqual(out, textwrap.dedent("""\
Name Stmts Miss Cover
-------------------------------
b_or_c.py 8 0 100%
"""))
def test_combine_with_aliases(self):
self.make_file("d1/x.py", """\
a = 1
b = 2
print("%s %s" % (a, b))
""")
self.make_file("d2/x.py", """\
# 1
# 2
# 3
c = 4
d = 5
print("%s %s" % (c, d))
""")
self.make_file(".coveragerc", """\
[run]
source = .
parallel = True
[paths]
source =
src
*/d1
*/d2
""")
out = self.run_command("coverage run " + os.path.normpath("d1/x.py"))
self.assertEqual(out, '1 2\n')
out = self.run_command("coverage run " + os.path.normpath("d2/x.py"))
self.assertEqual(out, '4 5\n')
self.assert_file_count(".coverage.*", 2)
self.run_command("coverage combine")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assert_file_count(".coverage.*", 0)
# Read the coverage data file and see that the two different x.py
# files have been combined together.
data = coverage.CoverageData()
data.read()
summary = line_counts(data, fullpath=True)
self.assertEqual(len(summary), 1)
actual = os.path.normcase(os.path.abspath(list(summary.keys())[0]))
expected = os.path.normcase(os.path.abspath('src/x.py'))
self.assertEqual(expected, actual)
self.assertEqual(list(summary.values())[0], 6)
def test_erase_parallel(self):
self.make_file(".coveragerc", """\
[run]
data_file = data.dat
parallel = True
""")
self.make_file("data.dat")
self.make_file("data.dat.fooey")
self.make_file("data.dat.gooey")
self.make_file(".coverage")
self.run_command("coverage erase")
self.assert_doesnt_exist("data.dat")
self.assert_doesnt_exist("data.dat.fooey")
self.assert_doesnt_exist("data.dat.gooey")
self.assert_exists(".coverage")
def test_missing_source_file(self):
# Check what happens if the source is missing when reporting happens.
self.make_file("fleeting.py", """\
s = 'goodbye, cruel world!'
""")
self.run_command("coverage run fleeting.py")
os.remove("fleeting.py")
out = self.run_command("coverage html -d htmlcov")
self.assertRegex(out, "No source for code: '.*fleeting.py'")
self.assertNotIn("Traceback", out)
# It happens that the code paths are different for *.py and other
# files, so try again with no extension.
self.make_file("fleeting", """\
s = 'goodbye, cruel world!'
""")
self.run_command("coverage run fleeting")
os.remove("fleeting")
status, out = self.run_command_status("coverage html -d htmlcov")
self.assertRegex(out, "No source for code: '.*fleeting'")
self.assertNotIn("Traceback", out)
self.assertEqual(status, 1)
def test_running_missing_file(self):
status, out = self.run_command_status("coverage run xyzzy.py")
self.assertRegex(out, "No file to run: .*xyzzy.py")
self.assertNotIn("raceback", out)
self.assertNotIn("rror", out)
self.assertEqual(status, 1)
def test_code_throws(self):
self.make_file("throw.py", """\
def f1():
raise Exception("hey!")
def f2():
f1()
f2()
""")
# The important thing is for "coverage run" and "python" to report the
# same traceback.
status, out = self.run_command_status("coverage run throw.py")
out2 = self.run_command("python throw.py")
if env.PYPY:
# Pypy has an extra frame in the traceback for some reason
out2 = re_lines(out2, "toplevel", match=False)
self.assertMultiLineEqual(out, out2)
# But also make sure that the output is what we expect.
path = python_reported_file('throw.py')
msg = 'File "{}", line 5,? in f2'.format(re.escape(path))
self.assertRegex(out, msg)
self.assertIn('raise Exception("hey!")', out)
self.assertEqual(status, 1)
def test_code_exits(self):
self.make_file("exit.py", """\
import sys
def f1():
print("about to exit..")
sys.exit(17)
def f2():
f1()
f2()
""")
# The important thing is for "coverage run" and "python" to have the
# same output. No traceback.
status, out = self.run_command_status("coverage run exit.py")
status2, out2 = self.run_command_status("python exit.py")
self.assertMultiLineEqual(out, out2)
self.assertMultiLineEqual(out, "about to exit..\n")
self.assertEqual(status, status2)
self.assertEqual(status, 17)
def test_code_exits_no_arg(self):
self.make_file("exit_none.py", """\
import sys
def f1():
print("about to exit quietly..")
sys.exit()
f1()
""")
status, out = self.run_command_status("coverage run exit_none.py")
status2, out2 = self.run_command_status("python exit_none.py")
self.assertMultiLineEqual(out, out2)
self.assertMultiLineEqual(out, "about to exit quietly..\n")
self.assertEqual(status, status2)
self.assertEqual(status, 0)
def test_fork(self):
if not hasattr(os, 'fork'):
self.skipTest("Can't test os.fork since it doesn't exist.")
self.make_file("fork.py", """\
import os
def child():
print('Child!')
def main():
ret = os.fork()
if ret == 0:
child()
else:
os.waitpid(ret, 0)
main()
""")
out = self.run_command("coverage run -p fork.py")
self.assertEqual(out, 'Child!\n')
self.assert_doesnt_exist(".coverage")
# After running the forking program, there should be two
# .coverage.machine.123 files.
self.assert_file_count(".coverage.*", 2)
# The two data files should have different random numbers at the end of
# the file name.
data_files = glob.glob(".coverage.*")
nums = set(name.rpartition(".")[-1] for name in data_files)
self.assertEqual(len(nums), 2, "Same random: %s" % (data_files,))
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage combine")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assert_file_count(".coverage.*", 0)
data = coverage.CoverageData()
data.read()
self.assertEqual(line_counts(data)['fork.py'], 9)
def test_warnings_during_reporting(self):
# While fixing issue #224, the warnings were being printed far too
# often. Make sure they're not any more.
self.make_file("hello.py", """\
import sys, os, the_other
print("Hello")
""")
self.make_file("the_other.py", """\
print("What?")
""")
self.make_file(".coveragerc", """\
[run]
source =
.
xyzzy
""")
self.run_command("coverage run hello.py")
out = self.run_command("coverage html")
self.assertEqual(out.count("Module xyzzy was never imported."), 0)
def test_warns_if_never_run(self):
# Note: the name of the function can't have "warning" in it, or the
# absolute path of the file will have "warning" in it, and an assertion
# will fail.
out = self.run_command("coverage run i_dont_exist.py")
path = python_reported_file('i_dont_exist.py')
self.assertIn("No file to run: '{}'".format(path), out)
self.assertNotIn("warning", out)
self.assertNotIn("Exception", out)
out = self.run_command("coverage run -m no_such_module")
self.assertTrue(
("No module named no_such_module" in out) or
("No module named 'no_such_module'" in out)
)
self.assertNotIn("warning", out)
self.assertNotIn("Exception", out)
def test_warnings_trace_function_changed_with_threads(self):
# https://github.com/nedbat/coveragepy/issues/164
if env.METACOV:
self.skipTest("Can't test tracers changing during metacoverage")
self.make_file("bug164.py", """\
import threading
import time
class MyThread (threading.Thread):
def run(self):
print("Hello")
thr = MyThread()
thr.start()
thr.join()
""")
out = self.run_command("coverage run --timid bug164.py")
self.assertIn("Hello\n", out)
self.assertNotIn("warning", out)
def test_warning_trace_function_changed(self):
self.make_file("settrace.py", """\
import sys
print("Hello")
sys.settrace(None)
print("Goodbye")
""")
out = self.run_command("coverage run --timid settrace.py")
self.assertIn("Hello\n", out)
self.assertIn("Goodbye\n", out)
self.assertIn("Trace function changed", out)
def test_timid(self):
# Test that the --timid command line argument properly swaps the tracer
# function for a simpler one.
#
# This is complicated by the fact that the tests are run twice for each
# version: once with a compiled C-based trace function, and once without
# it, to also test the Python trace function. So this test has to examine
# an environment variable set in igor.py to know whether to expect to see
# the C trace function or not.
# When meta-coverage testing, this test doesn't work, because it finds
# coverage.py's own trace function.
if os.environ.get('COVERAGE_COVERAGE', ''):
self.skipTest("Can't test timid during coverage measurement.")
self.make_file("showtrace.py", """\
# Show the current frame's trace function, so that we can test what the
# command-line options do to the trace function used.
import sys
# Show what the trace function is. If a C-based function is used, then f_trace
# may be None.
trace_fn = sys._getframe(0).f_trace
if trace_fn is None:
trace_name = "None"
else:
# Get the name of the tracer class. Py3k has a different way to get it.
try:
trace_name = trace_fn.im_class.__name__
except AttributeError:
try:
trace_name = trace_fn.__self__.__class__.__name__
except AttributeError:
# A C-based function could also manifest as an f_trace value
# which doesn't have im_class or __self__.
trace_name = trace_fn.__class__.__name__
print(trace_name)
""")
# When running without coverage, no trace function
py_out = self.run_command("python showtrace.py")
self.assertEqual(py_out, "None\n")
cov_out = self.run_command("coverage run showtrace.py")
if os.environ.get('COVERAGE_TEST_TRACER', 'c') == 'c':
# If the C trace function is being tested, then regular running should have
# the C function, which registers itself as f_trace.
self.assertEqual(cov_out, "CTracer\n")
else:
# If the Python trace function is being tested, then regular running will
# also show the Python function.
self.assertEqual(cov_out, "PyTracer\n")
# When running timidly, the trace function is always Python.
timid_out = self.run_command("coverage run --timid showtrace.py")
self.assertEqual(timid_out, "PyTracer\n")
def test_warn_preimported(self):
self.make_file("hello.py", """\
import goodbye
import coverage
cov = coverage.Coverage(include=["good*"], check_preimported=True)
cov.start()
print(goodbye.f())
cov.stop()
""")
self.make_file("goodbye.py", """\
def f():
return "Goodbye!"
""")
goodbye_path = os.path.abspath("goodbye.py")
out = self.run_command("python hello.py")
self.assertIn("Goodbye!", out)
msg = (
"Coverage.py warning: "
"Already imported a file that will be measured: {0} "
"(already-imported)").format(goodbye_path)
self.assertIn(msg, out)
@pytest.mark.expensive
def test_fullcoverage(self): # pragma: no metacov
if env.PY2: # This doesn't work on Python 2.
self.skipTest("fullcoverage doesn't work on Python 2.")
# It only works with the C tracer, and if we aren't measuring ourselves.
if not env.C_TRACER or env.METACOV:
self.skipTest("fullcoverage only works with the C tracer.")
# fullcoverage is a trick to get stdlib modules measured from
# the very beginning of the process. Here we import os and
# then check how many lines are measured.
self.make_file("getenv.py", """\
import os
print("FOOEY == %s" % os.getenv("FOOEY"))
""")
fullcov = os.path.join(
os.path.dirname(coverage.__file__), "fullcoverage"
)
self.set_environ("FOOEY", "BOO")
self.set_environ("PYTHONPATH", fullcov)
out = self.run_command("python -m coverage run -L getenv.py")
self.assertEqual(out, "FOOEY == BOO\n")
data = coverage.CoverageData()
data.read()
# The actual number of executed lines in os.py when it's
# imported is 120 or so. Just running os.getenv executes
# about 5.
self.assertGreater(line_counts(data)['os.py'], 50)
@xfail(
env.PYPY3 and env.PYPYVERSION >= (7, 1, 1),
"https://bitbucket.org/pypy/pypy/issues/3074"
)
def test_lang_c(self):
if env.JYTHON:
# Jython as of 2.7.1rc3 won't compile a filename that isn't utf8.
self.skipTest("Jython can't handle this test")
# LANG=C forces getfilesystemencoding on Linux to 'ascii', which causes
# failures with non-ascii file names. We don't want to make a real file
# with strange characters, though, because that gets the test runners
# tangled up. This will isolate the concerns to the coverage.py code.
# https://github.com/nedbat/coveragepy/issues/533
self.make_file("weird_file.py", r"""
globs = {}
code = "a = 1\nb = 2\n"
exec(compile(code, "wut\xe9\xea\xeb\xec\x01\x02.py", 'exec'), globs)
print(globs['a'])
print(globs['b'])
""")
self.set_environ("LANG", "C")
out = self.run_command("coverage run weird_file.py")
self.assertEqual(out, "1\n2\n")
def test_deprecation_warnings(self):
# Test that coverage doesn't trigger deprecation warnings.
# https://github.com/nedbat/coveragepy/issues/305
self.make_file("allok.py", """\
import warnings
warnings.simplefilter('default')
import coverage
print("No warnings!")
""")
# Some of our testing infrastructure can issue warnings.
# Turn it all off for the sub-process.
self.del_environ("COVERAGE_TESTING")
out = self.run_command("python allok.py")
self.assertEqual(out, "No warnings!\n")
def test_run_twice(self):
# https://github.com/nedbat/coveragepy/issues/353
self.make_file("foo.py", """\
def foo():
pass
""")
self.make_file("run_twice.py", """\
import sys
import coverage
for i in [1, 2]:
sys.stderr.write("Run %s\\n" % i)
inst = coverage.Coverage(source=['foo'])
inst.load()
inst.start()
import foo
inst.stop()
inst.save()
""")
out = self.run_command("python run_twice.py")
self.assertEqual(
out,
"Run 1\n"
"Run 2\n"
"Coverage.py warning: Module foo was previously imported, but not measured "
"(module-not-measured)\n"
)
def test_module_name(self):
# https://github.com/nedbat/coveragepy/issues/478
out = self.run_command("python -m coverage")
self.assertIn("Use 'coverage help' for help", out)
TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try_execfile.py")
class EnvironmentTest(CoverageTest):
"""Tests using try_execfile.py to test the execution environment."""
def assert_tryexecfile_output(self, expected, actual):
"""Assert that the output we got is a successful run of try_execfile.py.
`expected` and `actual` must be the same, modulo a few slight known
platform differences.
"""
# First, is this even credible try_execfile.py output?
self.assertIn('"DATA": "xyzzy"', actual)
if env.JYTHON: # pragma: only jython
# Argv0 is different for Jython, remove that from the comparison.
expected = re_lines(expected, r'\s+"argv0":', match=False)
actual = re_lines(actual, r'\s+"argv0":', match=False)
self.assertMultiLineEqual(expected, actual)
def test_coverage_run_is_like_python(self):
with open(TRY_EXECFILE) as f:
self.make_file("run_me.py", f.read())
expected = self.run_command("python run_me.py")
actual = self.run_command("coverage run run_me.py")
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_far_away_is_like_python(self):
with open(TRY_EXECFILE) as f:
self.make_file("sub/overthere/prog.py", f.read())
expected = self.run_command("python sub/overthere/prog.py")
actual = self.run_command("coverage run sub/overthere/prog.py")
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_is_like_python_dashm(self):
# These -m commands assume the coverage tree is on the path.
expected = self.run_command("python -m process_test.try_execfile")
actual = self.run_command("coverage run -m process_test.try_execfile")
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dir_is_like_python_dir(self):
if env.PYVERSION == (3, 5, 4, 'final', 0, 0): # pragma: obscure
self.skipTest("3.5.4 broke this: https://bugs.python.org/issue32551")
with open(TRY_EXECFILE) as f:
self.make_file("with_main/__main__.py", f.read())
expected = self.run_command("python with_main")
actual = self.run_command("coverage run with_main")
# PyPy includes the current directory in the path when running a
# directory, while CPython and coverage.py do not. Exclude that from
# the comparison also...
if env.PYPY:
ignored = re.escape(os.getcwd())
expected = re_lines(expected, ignored, match=False)
actual = re_lines(actual, ignored, match=False)
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_dir_no_init_is_like_python(self):
with open(TRY_EXECFILE) as f:
self.make_file("with_main/__main__.py", f.read())
expected = self.run_command("python -m with_main")
actual = self.run_command("coverage run -m with_main")
if env.PY2:
assert expected.endswith("No module named with_main\n")
assert actual.endswith("No module named with_main\n")
else:
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_dir_with_init_is_like_python(self):
if env.PY2:
self.skipTest("Python 2 runs __main__ twice, I can't be bothered to make it work.")
with open(TRY_EXECFILE) as f:
self.make_file("with_main/__main__.py", f.read())
self.make_file("with_main/__init__.py", "")
expected = self.run_command("python -m with_main")
actual = self.run_command("coverage run -m with_main")
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_equal_to_doubledashsource(self):
"""regression test for #328
When imported by -m, a module's __name__ is __main__, but we need the
--source machinery to know and respect the original name.
"""
# These -m commands assume the coverage tree is on the path.
expected = self.run_command("python -m process_test.try_execfile")
actual = self.run_command(
"coverage run --source process_test.try_execfile -m process_test.try_execfile"
)
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_superset_of_doubledashsource(self):
"""Edge case: --source foo -m foo.bar"""
# Ugh: without this config file, we'll get a warning about
# Coverage.py warning: Module process_test was previously imported,
# but not measured (module-not-measured)
#
# This is because process_test/__init__.py is imported while looking
# for process_test.try_execfile. That import happens while setting
# sys.path before start() is called.
self.make_file(".coveragerc", """\
[run]
disable_warnings = module-not-measured
""")
# These -m commands assume the coverage tree is on the path.
expected = self.run_command("python -m process_test.try_execfile")
actual = self.run_command(
"coverage run --source process_test -m process_test.try_execfile"
)
self.assert_tryexecfile_output(expected, actual)
st, out = self.run_command_status("coverage report")
self.assertEqual(st, 0)
self.assertEqual(self.line_count(out), 6, out)
def test_coverage_run_script_imports_doubledashsource(self):
# This file imports try_execfile, which compiles it to .pyc, so the
# first run will have __file__ == "try_execfile.py" and the second will
# have __file__ == "try_execfile.pyc", which throws off the comparison.
# Setting dont_write_bytecode True stops the compilation to .pyc and
# keeps the test working.
self.make_file("myscript", """\
import sys; sys.dont_write_bytecode = True
import process_test.try_execfile
""")
expected = self.run_command("python myscript")
actual = self.run_command("coverage run --source process_test myscript")
self.assert_tryexecfile_output(expected, actual)
st, out = self.run_command_status("coverage report")
self.assertEqual(st, 0)
self.assertEqual(self.line_count(out), 6, out)
def test_coverage_run_dashm_is_like_python_dashm_off_path(self):
# https://github.com/nedbat/coveragepy/issues/242
self.make_file("sub/__init__.py", "")
with open(TRY_EXECFILE) as f:
self.make_file("sub/run_me.py", f.read())
expected = self.run_command("python -m sub.run_me")
actual = self.run_command("coverage run -m sub.run_me")
self.assert_tryexecfile_output(expected, actual)
def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self):
# https://github.com/nedbat/coveragepy/issues/207
self.make_file("package/__init__.py", "print('init')")
self.make_file("package/__main__.py", "print('main')")
expected = self.run_command("python -m package")
actual = self.run_command("coverage run -m package")
self.assertMultiLineEqual(expected, actual)
def test_coverage_zip_is_like_python(self):
# Test running coverage from a zip file itself. Some environments
# (windows?) zip up the coverage main to be used as the coverage
# command.
with open(TRY_EXECFILE) as f:
self.make_file("run_me.py", f.read())
expected = self.run_command("python run_me.py")
cov_main = os.path.join(TESTS_DIR, "covmain.zip")
actual = self.run_command("python {} run run_me.py".format(cov_main))
self.assert_tryexecfile_output(expected, actual)
def test_coverage_custom_script(self):
# https://github.com/nedbat/coveragepy/issues/678
# If sys.path[0] isn't the Python default, then coverage.py won't
# fiddle with it.
self.make_file("a/b/c/thing.py", """\
SOMETHING = "hello-xyzzy"
""")
abc = os.path.abspath("a/b/c")
self.make_file("run_coverage.py", """\
import sys
sys.path[0:0] = [
r'{abc}',
'/Users/somebody/temp/something/eggs/something-4.5.1-py2.7-xxx-10.13-x86_64.egg',