| 1 | # SPDX-License-Identifier: BSD-2-Clause |
| 2 | # |
| 3 | # Copyright (c) 2016, Matt Layman |
| 4 | |
| 5 | from __future__ import print_function |
| 6 | import os |
| 7 | import string |
| 8 | import sys |
| 9 | |
| 10 | from tap.directive import Directive |
| 11 | from tap.i18n import _ |
| 12 | from tap.line import Result |
| 13 | |
| 14 | |
| 15 | class Tracker(object): |
| 16 | def __init__( |
| 17 | self, outdir=None, combined=False, streaming=False, stream=None, header=True |
| 18 | ): |
| 19 | self.outdir = outdir |
| 20 | |
| 21 | # Combine all the test results into one file. |
| 22 | self.combined = combined |
| 23 | self.combined_line_number = 0 |
| 24 | # Test case ordering is important for the combined results |
| 25 | # because of how numbers are assigned. The test cases |
| 26 | # must be tracked in order so that reporting can sequence |
| 27 | # the line numbers properly. |
| 28 | self.combined_test_cases_seen = [] |
| 29 | |
| 30 | # Stream output directly to a stream instead of file output. |
| 31 | self.streaming = streaming |
| 32 | self.stream = stream |
| 33 | |
| 34 | # Display the test case header unless told not to. |
| 35 | self.header = header |
| 36 | |
| 37 | # Internal state for tracking each test case. |
| 38 | self._test_cases = {} |
| 39 | |
| 40 | # Python versions 2 and 3 keep maketrans in different locations. |
| 41 | if sys.version_info[0] < 3: |
| 42 | self._sanitized_table = string.maketrans(' \\/\n', '----') |
| 43 | else: # pragma: no cover |
| 44 | self._sanitized_table = str.maketrans(' \\/\n', '----') |
| 45 | |
| 46 | def _get_outdir(self): |
| 47 | return self._outdir |
| 48 | |
| 49 | def _set_outdir(self, outdir): |
| 50 | self._outdir = outdir |
| 51 | if outdir and not os.path.exists(outdir): |
| 52 | os.makedirs(outdir) |
| 53 | |
| 54 | outdir = property(_get_outdir, _set_outdir) |
| 55 | |
| 56 | def _track(self, class_name): |
| 57 | """Keep track of which test cases have executed.""" |
| 58 | if self._test_cases.get(class_name) is None: |
| 59 | if self.streaming and self.header: |
| 60 | self._write_test_case_header(class_name, self.stream) |
| 61 | |
| 62 | self._test_cases[class_name] = [] |
| 63 | if self.combined: |
| 64 | self.combined_test_cases_seen.append(class_name) |
| 65 | |
| 66 | def add_ok(self, class_name, description, directive=''): |
| 67 | result = Result( |
| 68 | ok=True, |
| 69 | number=self._get_next_line_number(class_name), |
| 70 | description=description, |
| 71 | directive=Directive(directive), |
| 72 | ) |
| 73 | self._add_line(class_name, result) |
| 74 | |
| 75 | def add_not_ok(self, class_name, description, directive='', diagnostics=None): |
| 76 | result = Result( |
| 77 | ok=False, |
| 78 | number=self._get_next_line_number(class_name), |
| 79 | description=description, |
| 80 | diagnostics=diagnostics, |
| 81 | directive=Directive(directive), |
| 82 | ) |
| 83 | self._add_line(class_name, result) |
| 84 | |
| 85 | def add_skip(self, class_name, description, reason): |
| 86 | directive = 'SKIP {0}'.format(reason) |
| 87 | result = Result( |
| 88 | ok=True, |
| 89 | number=self._get_next_line_number(class_name), |
| 90 | description=description, |
| 91 | directive=Directive(directive), |
| 92 | ) |
| 93 | self._add_line(class_name, result) |
| 94 | |
| 95 | def _add_line(self, class_name, result): |
| 96 | self._track(class_name) |
| 97 | if self.streaming: |
| 98 | print(result, file=self.stream) |
| 99 | self._test_cases[class_name].append(result) |
| 100 | |
| 101 | def _get_next_line_number(self, class_name): |
| 102 | if self.combined or self.streaming: |
| 103 | # This has an obvious side effect. Oh well. |
| 104 | self.combined_line_number += 1 |
| 105 | return self.combined_line_number |
| 106 | else: |
| 107 | try: |
| 108 | return len(self._test_cases[class_name]) + 1 |
| 109 | except KeyError: |
| 110 | # A result is created before the call to _track so the test |
| 111 | # case may not be tracked yet. In that case, the line is 1. |
| 112 | return 1 |
| 113 | |
| 114 | def generate_tap_reports(self): |
| 115 | """Generate TAP reports. |
| 116 | |
| 117 | The results are either combined into a single output file or |
| 118 | the output file name is generated from the test case. |
| 119 | """ |
| 120 | if self.streaming: |
| 121 | # The results already went to the stream, record the plan. |
| 122 | print('1..{0}'.format(self.combined_line_number), file=self.stream) |
| 123 | return |
| 124 | |
| 125 | if self.combined: |
| 126 | combined_file = 'testresults.tap' |
| 127 | if self.outdir: |
| 128 | combined_file = os.path.join(self.outdir, combined_file) |
| 129 | with open(combined_file, 'w') as out_file: |
| 130 | for test_case in self.combined_test_cases_seen: |
| 131 | self.generate_tap_report( |
| 132 | test_case, self._test_cases[test_case], out_file |
| 133 | ) |
| 134 | print('1..{0}'.format(self.combined_line_number), file=out_file) |
| 135 | else: |
| 136 | for test_case, tap_lines in self._test_cases.items(): |
| 137 | with open(self._get_tap_file_path(test_case), 'w') as out_file: |
| 138 | self.generate_tap_report(test_case, tap_lines, out_file) |
| 139 | |
| 140 | def generate_tap_report(self, test_case, tap_lines, out_file): |
| 141 | self._write_test_case_header(test_case, out_file) |
| 142 | |
| 143 | for tap_line in tap_lines: |
| 144 | print(tap_line, file=out_file) |
| 145 | |
| 146 | # For combined results, the plan is only output once after |
| 147 | # all the test cases complete. |
| 148 | if not self.combined: |
| 149 | print('1..{0}'.format(len(tap_lines)), file=out_file) |
| 150 | |
| 151 | def _write_test_case_header(self, test_case, stream): |
| 152 | print( |
| 153 | _('# TAP results for {test_case}').format(test_case=test_case), file=stream |
| 154 | ) |
| 155 | |
| 156 | def _get_tap_file_path(self, test_case): |
| 157 | """Get the TAP output file path for the test case.""" |
| 158 | sanitized_test_case = test_case.translate(self._sanitized_table) |
| 159 | tap_file = sanitized_test_case + '.tap' |
| 160 | if self.outdir: |
| 161 | return os.path.join(self.outdir, tap_file) |
| 162 | return tap_file |