1 # SPDX-License-Identifier: BSD-2-Clause
3 # Copyright (c) 2016, Matt Layman
5 from __future__
import print_function
10 from tap
.directive
import Directive
11 from tap
.i18n
import _
12 from tap
.line
import Result
15 class Tracker(object):
18 self
, outdir
=None, combined
=False, streaming
=False, stream
=None,
22 # Combine all the test results into one file.
23 self
.combined
= combined
24 self
.combined_line_number
= 0
25 # Test case ordering is important for the combined results
26 # because of how numbers are assigned. The test cases
27 # must be tracked in order so that reporting can sequence
28 # the line numbers properly.
29 self
.combined_test_cases_seen
= []
31 # Stream output directly to a stream instead of file output.
32 self
.streaming
= streaming
35 # Display the test case header unless told not to.
38 # Internal state for tracking each test case.
41 # Python versions 2 and 3 keep maketrans in different locations.
42 if sys
.version_info
[0] < 3:
43 self
._sanitized
_table
= string
.maketrans(' \\/\n', '----')
44 else: # pragma: no cover
45 self
._sanitized
_table
= str.maketrans(' \\/\n', '----')
47 def _get_outdir(self
):
50 def _set_outdir(self
, outdir
):
52 if outdir
and not os
.path
.exists(outdir
):
55 outdir
= property(_get_outdir
, _set_outdir
)
57 def _track(self
, class_name
):
58 """Keep track of which test cases have executed."""
59 if self
._test
_cases
.get(class_name
) is None:
60 if self
.streaming
and self
.header
:
61 self
._write
_test
_case
_header
(class_name
, self
.stream
)
63 self
._test
_cases
[class_name
] = []
65 self
.combined_test_cases_seen
.append(class_name
)
67 def add_ok(self
, class_name
, description
, directive
=''):
69 ok
=True, number
=self
._get
_next
_line
_number
(class_name
),
70 description
=description
, directive
=Directive(directive
))
71 self
._add
_line
(class_name
, result
)
74 self
, class_name
, description
, directive
='', diagnostics
=None):
76 ok
=False, number
=self
._get
_next
_line
_number
(class_name
),
77 description
=description
, diagnostics
=diagnostics
,
78 directive
=Directive(directive
))
79 self
._add
_line
(class_name
, result
)
81 def add_skip(self
, class_name
, description
, reason
):
82 directive
= 'SKIP {0}'.format(reason
)
84 ok
=True, number
=self
._get
_next
_line
_number
(class_name
),
85 description
=description
, directive
=Directive(directive
))
86 self
._add
_line
(class_name
, result
)
88 def _add_line(self
, class_name
, result
):
89 self
._track
(class_name
)
91 print(result
, file=self
.stream
)
92 self
._test
_cases
[class_name
].append(result
)
94 def _get_next_line_number(self
, class_name
):
95 if self
.combined
or self
.streaming
:
96 # This has an obvious side effect. Oh well.
97 self
.combined_line_number
+= 1
98 return self
.combined_line_number
101 return len(self
._test
_cases
[class_name
]) + 1
103 # A result is created before the call to _track so the test
104 # case may not be tracked yet. In that case, the line is 1.
107 def generate_tap_reports(self
):
108 """Generate TAP reports.
110 The results are either combined into a single output file or
111 the output file name is generated from the test case.
114 # The results already went to the stream, record the plan.
115 print('1..{0}'.format(self
.combined_line_number
), file=self
.stream
)
119 combined_file
= 'testresults.tap'
121 combined_file
= os
.path
.join(self
.outdir
, combined_file
)
122 with
open(combined_file
, 'w') as out_file
:
123 for test_case
in self
.combined_test_cases_seen
:
124 self
.generate_tap_report(
125 test_case
, self
._test
_cases
[test_case
], out_file
)
127 '1..{0}'.format(self
.combined_line_number
), file=out_file
)
129 for test_case
, tap_lines
in self
._test
_cases
.items():
130 with
open(self
._get
_tap
_file
_path
(test_case
), 'w') as out_file
:
131 self
.generate_tap_report(test_case
, tap_lines
, out_file
)
133 def generate_tap_report(self
, test_case
, tap_lines
, out_file
):
134 self
._write
_test
_case
_header
(test_case
, out_file
)
136 for tap_line
in tap_lines
:
137 print(tap_line
, file=out_file
)
139 # For combined results, the plan is only output once after
140 # all the test cases complete.
141 if not self
.combined
:
142 print('1..{0}'.format(len(tap_lines
)), file=out_file
)
144 def _write_test_case_header(self
, test_case
, stream
):
145 print(_('# TAP results for {test_case}').format(
146 test_case
=test_case
), file=stream
)
148 def _get_tap_file_path(self
, test_case
):
149 """Get the TAP output file path for the test case."""
150 sanitized_test_case
= test_case
.translate(self
._sanitized
_table
)
151 tap_file
= sanitized_test_case
+ '.tap'
153 return os
.path
.join(self
.outdir
, tap_file
)
This page took 0.033658 seconds and 5 git commands to generate.