|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding=utf-8 |
| 3 | + |
| 4 | +# Copyright (c) 2015 Remko Tronçon (https://el-tramo.be) |
| 5 | +# Released under the MIT license |
| 6 | +# See COPYING for details |
| 7 | + |
| 8 | + |
| 9 | +import base64 |
| 10 | +import sys |
| 11 | +import unittest |
| 12 | + |
| 13 | +if sys.hexversion >= 0x03000000: |
| 14 | + from io import StringIO |
| 15 | +else: |
| 16 | + from StringIO import StringIO |
| 17 | + |
| 18 | +# Log modes |
| 19 | +class LogMode(object): |
| 20 | + LogToError, LogToDiagnostics, LogToYAML, LogToAttachment = range(4) |
| 21 | + |
| 22 | + |
| 23 | +class TAPTestResult(unittest.TestResult): |
| 24 | + def __init__(self, output_stream, error_stream, message_log, test_output_log): |
| 25 | + super(TAPTestResult, self).__init__(self, output_stream) |
| 26 | + self.output_stream = output_stream |
| 27 | + self.error_stream = error_stream |
| 28 | + self.orig_stdout = None |
| 29 | + self.orig_stderr = None |
| 30 | + self.message = error_stream |
| 31 | + self.test_output = None |
| 32 | + self.message_log = message_log |
| 33 | + self.test_output_log = test_output_log |
| 34 | + self.output_stream.write("TAP version 13\n") |
| 35 | + |
| 36 | + def print_raw(self, text): |
| 37 | + self.output_stream.write(text) |
| 38 | + self.output_stream.flush() |
| 39 | + |
| 40 | + def print_result(self, result, test, directive=None): |
| 41 | + self.output_stream.write("%s %d %s" % (result, self.testsRun, test.id())) |
| 42 | + if directive: |
| 43 | + self.output_stream.write(" # " + directive) |
| 44 | + self.output_stream.write("\n") |
| 45 | + self.output_stream.flush() |
| 46 | + |
| 47 | + def ok(self, test, directive=None): |
| 48 | + self.print_result("ok", test, directive) |
| 49 | + |
| 50 | + def not_ok(self, test): |
| 51 | + self.print_result("not ok", test) |
| 52 | + |
| 53 | + def startTest(self, test): |
| 54 | + self.orig_stdout = sys.stdout |
| 55 | + self.orig_stderr = sys.stderr |
| 56 | + if self.message_log == LogMode.LogToError: |
| 57 | + self.message = self.error_stream |
| 58 | + else: |
| 59 | + self.message = StringIO() |
| 60 | + if self.test_output_log == LogMode.LogToError: |
| 61 | + self.test_output = self.error_stream |
| 62 | + else: |
| 63 | + self.test_output = StringIO() |
| 64 | + |
| 65 | + if self.message_log == self.test_output_log: |
| 66 | + self.test_output = self.message |
| 67 | + |
| 68 | + sys.stdout = sys.stderr = self.test_output |
| 69 | + super(TAPTestResult, self).startTest(test) |
| 70 | + |
| 71 | + def stopTest(self, test): |
| 72 | + super(TAPTestResult, self).stopTest(test) |
| 73 | + sys.stdout = self.orig_stdout |
| 74 | + sys.stderr = self.orig_stderr |
| 75 | + if self.message_log == self.test_output_log: |
| 76 | + logs = [(self.message_log, self.message, "output")] |
| 77 | + else: |
| 78 | + logs = [ |
| 79 | + (self.test_output_log, self.test_output, "test_output"), |
| 80 | + (self.message_log, self.message, "message"), |
| 81 | + ] |
| 82 | + for log_mode, log, log_name in logs: |
| 83 | + if log_mode != LogMode.LogToError: |
| 84 | + output = log.getvalue() |
| 85 | + if len(output): |
| 86 | + if log_mode == LogMode.LogToYAML: |
| 87 | + self.print_raw(" ---\n") |
| 88 | + self.print_raw(" " + log_name + ": |\n") |
| 89 | + self.print_raw( |
| 90 | + " " + output.rstrip().replace("\n", "\n ") + "\n" |
| 91 | + ) |
| 92 | + self.print_raw(" ...\n") |
| 93 | + elif log_mode == LogMode.LogToAttachment: |
| 94 | + self.print_raw(" ---\n") |
| 95 | + self.print_raw(" " + log_name + ":\n") |
| 96 | + self.print_raw(" File-Name: " + log_name + ".txt\n") |
| 97 | + self.print_raw(" File-Type: text/plain\n") |
| 98 | + self.print_raw( |
| 99 | + " File-Content: " + base64.b64encode(output) + "\n" |
| 100 | + ) |
| 101 | + self.print_raw(" ...\n") |
| 102 | + else: |
| 103 | + self.print_raw( |
| 104 | + "# " + output.rstrip().replace("\n", "\n# ") + "\n" |
| 105 | + ) |
| 106 | + |
| 107 | + def addSuccess(self, test): |
| 108 | + super(TAPTestResult, self).addSuccess(test) |
| 109 | + self.ok(test) |
| 110 | + |
| 111 | + def addError(self, test, err): |
| 112 | + super(TAPTestResult, self).addError(test, err) |
| 113 | + self.message.write(self.errors[-1][1] + "\n") |
| 114 | + self.not_ok(test) |
| 115 | + |
| 116 | + def addFailure(self, test, err): |
| 117 | + super(TAPTestResult, self).addFailure(test, err) |
| 118 | + self.message.write(self.failures[-1][1] + "\n") |
| 119 | + self.not_ok(test) |
| 120 | + |
| 121 | + def addSkip(self, test, reason): |
| 122 | + super(TAPTestResult, self).addSkip(test, reason) |
| 123 | + self.ok(test, "SKIP " + reason) |
| 124 | + |
| 125 | + def addExpectedFailure(self, test, err): |
| 126 | + super(TAPTestResult, self).addExpectedFailure(test, err) |
| 127 | + self.message.write(self.expectedFailures[-1][1] + "\n") |
| 128 | + self.ok(test) |
| 129 | + |
| 130 | + def addUnexpectedSuccess(self, test): |
| 131 | + super(TAPTestResult, self).addUnexpectedSuccess(self, test) |
| 132 | + self.not_ok(test) |
| 133 | + |
| 134 | + def printErrors(self): |
| 135 | + self.print_raw("1..%d\n" % self.testsRun) |
| 136 | + |
| 137 | + |
| 138 | +class TAPTestRunner(unittest.TextTestRunner): |
| 139 | + def __init__( |
| 140 | + self, |
| 141 | + message_log=LogMode.LogToYAML, |
| 142 | + test_output_log=LogMode.LogToDiagnostics, |
| 143 | + output_stream=sys.stdout, |
| 144 | + error_stream=sys.stderr, |
| 145 | + ): |
| 146 | + self.output_stream = output_stream |
| 147 | + self.error_stream = error_stream |
| 148 | + self.message_log = message_log |
| 149 | + self.test_output_log = test_output_log |
| 150 | + |
| 151 | + def run(self, test): |
| 152 | + result = TAPTestResult( |
| 153 | + self.output_stream, |
| 154 | + self.error_stream, |
| 155 | + self.message_log, |
| 156 | + self.test_output_log, |
| 157 | + ) |
| 158 | + test(result) |
| 159 | + result.printErrors() |
| 160 | + |
| 161 | + return result |
0 commit comments