16 import coverage |
16 import coverage |
17 from coverage import Coverage |
17 from coverage import Coverage |
18 from coverage import env |
18 from coverage import env |
19 from coverage.collector import CTracer |
19 from coverage.collector import CTracer |
20 from coverage.data import line_counts |
20 from coverage.data import line_counts |
21 from coverage.debug import info_formatter, info_header |
21 from coverage.debug import info_formatter, info_header, short_stack |
22 from coverage.execfile import PyRunner |
22 from coverage.execfile import PyRunner |
23 from coverage.misc import BaseCoverageException, ExceptionDuringRun, NoSource, output_encoding |
23 from coverage.misc import BaseCoverageException, ExceptionDuringRun, NoSource, output_encoding |
24 from coverage.results import should_fail_under |
24 from coverage.results import should_fail_under |
25 |
25 |
26 |
26 |
83 help=( |
83 help=( |
84 "Measure coverage even inside the Python installed library, " |
84 "Measure coverage even inside the Python installed library, " |
85 "which isn't done by default." |
85 "which isn't done by default." |
86 ), |
86 ), |
87 ) |
87 ) |
|
88 sort = optparse.make_option( |
|
89 '--sort', action='store', metavar='COLUMN', |
|
90 help="Sort the report by the named column: name, stmts, miss, branch, brpart, or cover. " |
|
91 "Default is name." |
|
92 ) |
88 show_missing = optparse.make_option( |
93 show_missing = optparse.make_option( |
89 '-m', '--show-missing', action='store_true', |
94 '-m', '--show-missing', action='store_true', |
90 help="Show line numbers of statements in each module that weren't executed.", |
95 help="Show line numbers of statements in each module that weren't executed.", |
91 ) |
96 ) |
92 skip_covered = optparse.make_option( |
97 skip_covered = optparse.make_option( |
93 '--skip-covered', action='store_true', |
98 '--skip-covered', action='store_true', |
94 help="Skip files with 100% coverage.", |
99 help="Skip files with 100% coverage.", |
|
100 ) |
|
101 no_skip_covered = optparse.make_option( |
|
102 '--no-skip-covered', action='store_false', dest='skip_covered', |
|
103 help="Disable --skip-covered.", |
95 ) |
104 ) |
96 skip_empty = optparse.make_option( |
105 skip_empty = optparse.make_option( |
97 '--skip-empty', action='store_true', |
106 '--skip-empty', action='store_true', |
98 help="Skip files with no code.", |
107 help="Skip files with no code.", |
99 ) |
108 ) |
142 module = optparse.make_option( |
151 module = optparse.make_option( |
143 '-m', '--module', action='store_true', |
152 '-m', '--module', action='store_true', |
144 help=( |
153 help=( |
145 "<pyfile> is an importable Python module, not a script path, " |
154 "<pyfile> is an importable Python module, not a script path, " |
146 "to be run as 'python -m' would run it." |
155 "to be run as 'python -m' would run it." |
|
156 ), |
|
157 ) |
|
158 precision = optparse.make_option( |
|
159 '', '--precision', action='store', metavar='N', type=int, |
|
160 help=( |
|
161 "Number of digits after the decimal point to display for " |
|
162 "reported coverage percentages." |
147 ), |
163 ), |
148 ) |
164 ) |
149 rcfile = optparse.make_option( |
165 rcfile = optparse.make_option( |
150 '', '--rcfile', action='store', |
166 '', '--rcfile', action='store', |
151 help=( |
167 help=( |
329 |
347 |
330 'debug': CmdOptionParser( |
348 'debug': CmdOptionParser( |
331 "debug", GLOBAL_ARGS, |
349 "debug", GLOBAL_ARGS, |
332 usage="<topic>", |
350 usage="<topic>", |
333 description=( |
351 description=( |
334 "Display information on the internals of coverage.py, " |
352 "Display information about the internals of coverage.py, " |
335 "for diagnosing problems. " |
353 "for diagnosing problems. " |
336 "Topics are 'data' to show a summary of the collected data, " |
354 "Topics are: " |
337 "or 'sys' to show installation information." |
355 "'data' to show a summary of the collected data; " |
|
356 "'sys' to show installation information; " |
|
357 "'config' to show the configuration; " |
|
358 "'premain' to show what is calling coverage." |
338 ), |
359 ), |
339 ), |
360 ), |
340 |
361 |
341 'erase': CmdOptionParser( |
362 'erase': CmdOptionParser( |
342 "erase", GLOBAL_ARGS, |
363 "erase", GLOBAL_ARGS, |
581 if options.action == "report": |
608 if options.action == "report": |
582 total = self.coverage.report( |
609 total = self.coverage.report( |
583 show_missing=options.show_missing, |
610 show_missing=options.show_missing, |
584 skip_covered=options.skip_covered, |
611 skip_covered=options.skip_covered, |
585 skip_empty=options.skip_empty, |
612 skip_empty=options.skip_empty, |
|
613 precision=options.precision, |
|
614 sort=options.sort, |
586 **report_args |
615 **report_args |
587 ) |
616 ) |
588 elif options.action == "annotate": |
617 elif options.action == "annotate": |
589 self.coverage.annotate(directory=options.directory, **report_args) |
618 self.coverage.annotate(directory=options.directory, **report_args) |
590 elif options.action == "html": |
619 elif options.action == "html": |
592 directory=options.directory, |
621 directory=options.directory, |
593 title=options.title, |
622 title=options.title, |
594 skip_covered=options.skip_covered, |
623 skip_covered=options.skip_covered, |
595 skip_empty=options.skip_empty, |
624 skip_empty=options.skip_empty, |
596 show_contexts=options.show_contexts, |
625 show_contexts=options.show_contexts, |
|
626 precision=options.precision, |
597 **report_args |
627 **report_args |
598 ) |
628 ) |
599 elif options.action == "xml": |
629 elif options.action == "xml": |
600 outfile = options.outfile |
630 outfile = options.outfile |
601 total = self.coverage.xml_report(outfile=outfile, **report_args) |
631 total = self.coverage.xml_report( |
|
632 outfile=outfile, skip_empty=options.skip_empty, |
|
633 **report_args |
|
634 ) |
602 elif options.action == "json": |
635 elif options.action == "json": |
603 outfile = options.outfile |
636 outfile = options.outfile |
604 total = self.coverage.json_report( |
637 total = self.coverage.json_report( |
605 outfile=outfile, |
638 outfile=outfile, |
606 pretty_print=options.pretty_print, |
639 pretty_print=options.pretty_print, |
615 self.coverage.set_option("report:fail_under", options.fail_under) |
648 self.coverage.set_option("report:fail_under", options.fail_under) |
616 |
649 |
617 fail_under = self.coverage.get_option("report:fail_under") |
650 fail_under = self.coverage.get_option("report:fail_under") |
618 precision = self.coverage.get_option("report:precision") |
651 precision = self.coverage.get_option("report:precision") |
619 if should_fail_under(total, fail_under, precision): |
652 if should_fail_under(total, fail_under, precision): |
|
653 msg = "total of {total:.{p}f} is less than fail-under={fail_under:.{p}f}".format( |
|
654 total=total, fail_under=fail_under, p=precision, |
|
655 ) |
|
656 print("Coverage failure:", msg) |
620 return FAIL_UNDER |
657 return FAIL_UNDER |
621 |
658 |
622 return OK |
659 return OK |
623 |
660 |
624 def do_help(self, options, args, parser): |
661 def do_help(self, options, args, parser): |
747 config_info = self.coverage.config.__dict__.items() |
784 config_info = self.coverage.config.__dict__.items() |
748 for line in info_formatter(config_info): |
785 for line in info_formatter(config_info): |
749 print(" %s" % line) |
786 print(" %s" % line) |
750 elif info == "premain": |
787 elif info == "premain": |
751 print(info_header("premain")) |
788 print(info_header("premain")) |
752 from coverage.debug import short_stack |
|
753 print(short_stack()) |
789 print(short_stack()) |
754 else: |
790 else: |
755 show_help("Don't know what you mean by %r" % info) |
791 show_help("Don't know what you mean by %r" % info) |
756 return ERR |
792 return ERR |
757 |
793 |
793 usage: {program_name} <command> [options] [args] |
829 usage: {program_name} <command> [options] [args] |
794 |
830 |
795 Commands: |
831 Commands: |
796 annotate Annotate source files with execution information. |
832 annotate Annotate source files with execution information. |
797 combine Combine a number of data files. |
833 combine Combine a number of data files. |
|
834 debug Display information about the internals of coverage.py |
798 erase Erase previously collected coverage data. |
835 erase Erase previously collected coverage data. |
799 help Get help on using coverage.py. |
836 help Get help on using coverage.py. |
800 html Create an HTML report. |
837 html Create an HTML report. |
801 json Create a JSON report of coverage results. |
838 json Create a JSON report of coverage results. |
802 report Report coverage stats on modules. |
839 report Report coverage stats on modules. |
856 from ox_profile.core.launchers import SimpleLauncher # pylint: disable=import-error |
893 from ox_profile.core.launchers import SimpleLauncher # pylint: disable=import-error |
857 original_main = main |
894 original_main = main |
858 |
895 |
859 def main(argv=None): # pylint: disable=function-redefined |
896 def main(argv=None): # pylint: disable=function-redefined |
860 """A wrapper around main that profiles.""" |
897 """A wrapper around main that profiles.""" |
|
898 profiler = SimpleLauncher.launch() |
861 try: |
899 try: |
862 profiler = SimpleLauncher.launch() |
|
863 return original_main(argv) |
900 return original_main(argv) |
864 finally: |
901 finally: |
865 data, _ = profiler.query(re_filter='coverage', max_records=100) |
902 data, _ = profiler.query(re_filter='coverage', max_records=100) |
866 print(profiler.show(query=data, limit=100, sep='', col='')) |
903 print(profiler.show(query=data, limit=100, sep='', col='')) |
867 profiler.cancel() |
904 profiler.cancel() |