4 # Copyright (c) 2015 Remko Tronçon (https://el-tramo.be)
5 # Copied from https://github.com/remko/pycotap/
7 # SPDX-License-Identifier: MIT
9 # Released under the MIT license
11 # Permission is hereby granted, free of charge, to any person obtaining a copy
12 # of this software and associated documentation files (the "Software"), to deal
13 # in the Software without restriction, including without limitation the rights
14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 # copies of the Software, and to permit persons to whom the Software is
16 # furnished to do so, subject to the following conditions:
18 # The above copyright notice and this permission notice shall be included in
19 # all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 from io import StringIO
37 class LogMode(object):
38 LogToError, LogToDiagnostics, LogToYAML, LogToAttachment = range(4)
41 class TAPTestResult(unittest.TestResult):
42 def __init__(self, output_stream, error_stream, message_log, test_output_log):
43 super(TAPTestResult, self).__init__(self, output_stream)
44 self.output_stream = output_stream
45 self.error_stream = error_stream
46 self.orig_stdout = None
47 self.orig_stderr = None
49 self.test_output = None
50 self.message_log = message_log
51 self.test_output_log = test_output_log
52 self.output_stream.write("TAP version 13\n")
55 def printErrors(self):
56 self.print_raw("1..%d\n" % self.testsRun)
59 def _set_streams(self):
60 self.orig_stdout = sys.stdout
61 self.orig_stderr = sys.stderr
62 if self.message_log == LogMode.LogToError:
63 self.message = self.error_stream
65 self.message = StringIO()
66 if self.test_output_log == LogMode.LogToError:
67 self.test_output = self.error_stream
69 self.test_output = StringIO()
71 if self.message_log == self.test_output_log:
72 self.test_output = self.message
73 sys.stdout = sys.stderr = self.test_output
75 def _reset_streams(self):
76 sys.stdout = self.orig_stdout
77 sys.stderr = self.orig_stderr
79 def print_raw(self, text):
80 self.output_stream.write(text)
81 self.output_stream.flush()
83 def print_result(self, result, test, directive=None):
84 self.output_stream.write("%s %d %s" % (result, self.testsRun, test.id()))
86 self.output_stream.write(" # " + directive)
87 self.output_stream.write("\n")
88 self.output_stream.flush()
90 def ok(self, test, directive=None):
91 self.print_result("ok", test, directive)
93 def not_ok(self, test):
94 self.print_result("not ok", test)
96 def startTest(self, test):
97 super(TAPTestResult, self).startTest(test)
99 def stopTest(self, test):
100 super(TAPTestResult, self).stopTest(test)
101 if self.message_log == self.test_output_log:
102 logs = [(self.message_log, self.message, "output")]
105 (self.test_output_log, self.test_output, "test_output"),
106 (self.message_log, self.message, "message"),
108 for log_mode, log, log_name in logs:
109 if log_mode != LogMode.LogToError:
110 output = log.getvalue()
112 if log_mode == LogMode.LogToYAML:
113 self.print_raw(" ---\n")
114 self.print_raw(" " + log_name + ": |\n")
116 " " + output.rstrip().replace("\n", "\n ") + "\n"
118 self.print_raw(" ...\n")
119 elif log_mode == LogMode.LogToAttachment:
120 self.print_raw(" ---\n")
121 self.print_raw(" " + log_name + ":\n")
122 self.print_raw(" File-Name: " + log_name + ".txt\n")
123 self.print_raw(" File-Type: text/plain\n")
125 " File-Content: " + base64.b64encode(output) + "\n"
127 self.print_raw(" ...\n")
130 "# " + output.rstrip().replace("\n", "\n# ") + "\n"
132 # Truncate doesn't change the current stream position.
133 # Seek to the beginning to avoid extensions on subsequent writes.
137 def addSuccess(self, test):
138 super(TAPTestResult, self).addSuccess(test)
141 def addError(self, test, err):
142 super(TAPTestResult, self).addError(test, err)
143 self.message.write(self.errors[-1][1] + "\n")
146 def addFailure(self, test, err):
147 super(TAPTestResult, self).addFailure(test, err)
148 self.message.write(self.failures[-1][1] + "\n")
151 def addSkip(self, test, reason):
152 super(TAPTestResult, self).addSkip(test, reason)
153 self.ok(test, "SKIP " + reason)
155 def addExpectedFailure(self, test, err):
156 super(TAPTestResult, self).addExpectedFailure(test, err)
159 def addUnexpectedSuccess(self, test):
160 super(TAPTestResult, self).addUnexpectedSuccess(test)
161 self.message.write("Unexpected success" + "\n")
165 class TAPTestRunner(object):
168 message_log=LogMode.LogToYAML,
169 test_output_log=LogMode.LogToDiagnostics,
170 output_stream=sys.stdout,
171 error_stream=sys.stderr,
173 self.output_stream = output_stream
174 self.error_stream = error_stream
175 self.message_log = message_log
176 self.test_output_log = test_output_log
179 result = TAPTestResult(
183 self.test_output_log,