| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# compare.py -- High-level tests for dtrx. |
| 5 |
# Copyright © 2006-2009 Brett Smith <[email protected]>. |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by the |
| 9 |
# Free Software Foundation; either version 3 of the License, or (at your |
| 10 |
# option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 15 |
# Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along |
| 18 |
# with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
from __future__ import print_function |
| 21 |
|
| 22 |
import fcntl |
| 23 |
import os |
| 24 |
import re |
| 25 |
import struct |
| 26 |
|
| 27 |
try: |
| 28 |
import subprocess32 as subprocess |
| 29 |
except ImportError: |
| 30 |
import subprocess |
| 31 |
|
| 32 |
import sys |
| 33 |
import tempfile |
| 34 |
import termios |
| 35 |
|
| 36 |
import yaml |
| 37 |
|
| 38 |
try: |
| 39 |
set |
| 40 |
except NameError: |
| 41 |
from sets import Set as set |
| 42 |
|
| 43 |
if os.path.exists("dtrx/dtrx.py") and os.path.exists("tests"): |
| 44 |
os.chdir("tests") |
| 45 |
elif os.path.exists("../dtrx/dtrx.py") and os.path.exists("../tests"): |
| 46 |
pass |
| 47 |
else: |
| 48 |
print("ERROR: Can't run tests in this directory!") |
| 49 |
sys.exit(2) |
| 50 |
|
| 51 |
DTRX_SCRIPT = os.path.realpath("../dtrx/dtrx.py") |
| 52 |
SHELL_CMD = ["sh", "-se"] |
| 53 |
ROOT_DIR = os.path.realpath(os.curdir) |
| 54 |
NUM_TESTS = 0 |
| 55 |
|
| 56 |
|
| 57 |
class ExtractorTestError(Exception): |
| 58 |
pass |
| 59 |
|
| 60 |
|
| 61 |
class StatusWriter(object): |
| 62 |
def __init__(self): |
| 63 |
try: |
| 64 |
size = fcntl.ioctl( |
| 65 |
sys.stdout.fileno(), termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0) |
| 66 |
) |
| 67 |
except IOError: |
| 68 |
self.show = self.show_file |
| 69 |
else: |
| 70 |
self.width = struct.unpack("HHHH", size)[1] - 1 |
| 71 |
self.last_width = self.width |
| 72 |
self.show = self.show_term |
| 73 |
|
| 74 |
def show_term(self, message): |
| 75 |
sys.stdout.write(message.ljust(self.last_width) + "\r") |
| 76 |
sys.stdout.flush() |
| 77 |
self.last_width = max(self.width, len(message)) |
| 78 |
|
| 79 |
def show_file(self, message): |
| 80 |
if message: |
| 81 |
print(message) |
| 82 |
|
| 83 |
def clear(self): |
| 84 |
self.show("") |
| 85 |
|
| 86 |
|
| 87 |
class ExtractorTest(object): |
| 88 |
status_writer = StatusWriter() |
| 89 |
|
| 90 |
def __init__(self, **kwargs): |
| 91 |
global NUM_TESTS |
| 92 |
NUM_TESTS += 1 |
| 93 |
self.test_num = NUM_TESTS |
| 94 |
self.name = kwargs["name"] |
| 95 |
self.options = kwargs.get("options", "-n").split() |
| 96 |
self.filenames = kwargs.get("filenames", "").split() |
| 97 |
for key in ( |
| 98 |
"directory", |
| 99 |
"prerun", |
| 100 |
"posttest", |
| 101 |
"baseline", |
| 102 |
"error", |
| 103 |
"input", |
| 104 |
"output", |
| 105 |
"cleanup", |
| 106 |
): |
| 107 |
setattr(self, key, kwargs.get(key, None)) |
| 108 |
for key in ("grep", "antigrep"): |
| 109 |
value = kwargs.get(key, []) |
| 110 |
if isinstance(value, str): |
| 111 |
value = [value] |
| 112 |
setattr(self, key, value) |
| 113 |
if self.input and (not self.input.endswith("\n")): |
| 114 |
self.input = self.input + "\n" |
| 115 |
|
| 116 |
def start_proc(self, command, stdin=None, output=None): |
| 117 |
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=output, stderr=output) |
| 118 |
if stdin: |
| 119 |
process.stdin.write(bytes(str(stdin).encode("utf-8"))) |
| 120 |
process.stdin.close() |
| 121 |
return process |
| 122 |
|
| 123 |
def get_results(self, command, stdin=None): |
| 124 |
print("Output from %s:" % (" ".join(command),), file=self.outbuffer) |
| 125 |
self.outbuffer.flush() |
| 126 |
status = self.start_proc(command, stdin, self.outbuffer).wait(5) |
| 127 |
process = subprocess.Popen(["find"], stdout=subprocess.PIPE) |
| 128 |
output = process.stdout.read(-1).decode("ascii", errors="ignore") |
| 129 |
process.stdout.close() |
| 130 |
process.wait() |
| 131 |
return status, set(output.split("\n")) |
| 132 |
|
| 133 |
def run_script(self, key): |
| 134 |
commands = getattr(self, key) |
| 135 |
if commands is not None: |
| 136 |
if self.directory: |
| 137 |
directory_hint = "../" |
| 138 |
else: |
| 139 |
directory_hint = "" |
| 140 |
self.start_proc(SHELL_CMD + [directory_hint], commands).wait() |
| 141 |
|
| 142 |
def get_shell_results(self): |
| 143 |
self.run_script("prerun") |
| 144 |
return self.get_results(SHELL_CMD + self.filenames, self.baseline) |
| 145 |
|
| 146 |
def get_extractor_results(self): |
| 147 |
self.run_script("prerun") |
| 148 |
# run with the current python interpreter, rather than relying on the |
| 149 |
# hashbang. |
| 150 |
return self.get_results( |
| 151 |
[sys.executable, DTRX_SCRIPT] + self.options + self.filenames, self.input |
| 152 |
) |
| 153 |
|
| 154 |
def get_posttest_result(self): |
| 155 |
if not self.posttest: |
| 156 |
return 0 |
| 157 |
return self.start_proc(SHELL_CMD, self.posttest).wait() |
| 158 |
|
| 159 |
def clean(self): |
| 160 |
self.run_script("cleanup") |
| 161 |
if self.directory: |
| 162 |
target = os.path.join(ROOT_DIR, self.directory) |
| 163 |
extra_options = [] |
| 164 |
else: |
| 165 |
target = ROOT_DIR |
| 166 |
extra_options = [ |
| 167 |
"(", |
| 168 |
"(", |
| 169 |
"-type", |
| 170 |
"d", |
| 171 |
"!", |
| 172 |
"-name", |
| 173 |
"CVS", |
| 174 |
"!", |
| 175 |
"-name", |
| 176 |
".svn", |
| 177 |
")", |
| 178 |
"-or", |
| 179 |
"-name", |
| 180 |
"test-text", |
| 181 |
"-or", |
| 182 |
"-name", |
| 183 |
"test-onefile", |
| 184 |
")", |
| 185 |
] |
| 186 |
status = subprocess.call( |
| 187 |
["find", target, "-mindepth", "1", "-maxdepth", "1"] |
| 188 |
+ extra_options |
| 189 |
+ ["-exec", "rm", "-rf", "{}", ";"] |
| 190 |
) |
| 191 |
if status != 0: |
| 192 |
raise ExtractorTestError("cleanup exited with status code %s" % (status,)) |
| 193 |
|
| 194 |
def show_pass(self): |
| 195 |
self.status_writer.show("Passed %i/%i: %s" % (self.test_num, NUM_TESTS, self.name)) |
| 196 |
return "passed" |
| 197 |
|
| 198 |
def show_report(self, status, message=None): |
| 199 |
self.status_writer.clear() |
| 200 |
self.outbuffer.seek(0, 0) |
| 201 |
sys.stdout.write(self.outbuffer.read(-1)) |
| 202 |
if message is None: |
| 203 |
last_part = "" |
| 204 |
else: |
| 205 |
last_part = ": %s" % (message,) |
| 206 |
print("%s: %s%s\n" % (status, self.name, last_part)) |
| 207 |
return status.lower() |
| 208 |
|
| 209 |
def compare_results(self, actual): |
| 210 |
posttest_result = self.get_posttest_result() |
| 211 |
self.clean() |
| 212 |
status, expected = self.get_shell_results() |
| 213 |
self.clean() |
| 214 |
if expected != actual: |
| 215 |
print("Only in baseline results:", file=self.outbuffer) |
| 216 |
print("\n".join(expected.difference(actual)), file=self.outbuffer) |
| 217 |
print("Only in actual results:", file=self.outbuffer) |
| 218 |
print("\n".join(actual.difference(expected)), file=self.outbuffer) |
| 219 |
return self.show_report("FAILED") |
| 220 |
elif posttest_result != 0: |
| 221 |
print("Posttest gave status code", posttest_result, file=self.outbuffer) |
| 222 |
return self.show_report("FAILED") |
| 223 |
return self.show_pass() |
| 224 |
|
| 225 |
def have_error_mismatch(self, status): |
| 226 |
if self.error and (status == 0): |
| 227 |
return "dtrx did not return expected error" |
| 228 |
elif (not self.error) and (status != 0): |
| 229 |
return "dtrx returned error code %s" % (status,) |
| 230 |
return None |
| 231 |
|
| 232 |
def grep_output(self, output): |
| 233 |
for pattern in self.grep: |
| 234 |
if not re.search(pattern.replace(" ", "\\s+"), output, re.MULTILINE): |
| 235 |
return "output did not match %s" % (pattern) |
| 236 |
for pattern in self.antigrep: |
| 237 |
if re.search(pattern.replace(" ", "\\s+"), output, re.MULTILINE): |
| 238 |
return "output matched antigrep %s" % (self.antigrep) |
| 239 |
return None |
| 240 |
|
| 241 |
def check_output(self, output): |
| 242 |
if (self.output is not None) and (self.output.strip() != output.strip()): |
| 243 |
return "output did not match provided text:\n{}\nVS:\n{}".format( |
| 244 |
repr(self.output), repr(output) |
| 245 |
) |
| 246 |
return None |
| 247 |
|
| 248 |
def check_results(self): |
| 249 |
self.clean() |
| 250 |
status, actual = self.get_extractor_results() |
| 251 |
self.outbuffer.seek(0, 0) |
| 252 |
self.outbuffer.readline() |
| 253 |
output = self.outbuffer.read(-1) |
| 254 |
problem = ( |
| 255 |
self.have_error_mismatch(status) |
| 256 |
or self.check_output(output) |
| 257 |
or self.grep_output(output) |
| 258 |
) |
| 259 |
if problem: |
| 260 |
return self.show_report("FAILED", problem) |
| 261 |
if self.baseline is not None: |
| 262 |
return self.compare_results(actual) |
| 263 |
else: |
| 264 |
self.clean() |
| 265 |
return self.show_pass() |
| 266 |
|
| 267 |
def run(self): |
| 268 |
self.outbuffer = tempfile.TemporaryFile(mode="w+") |
| 269 |
if self.directory: |
| 270 |
os.mkdir(self.directory) |
| 271 |
os.chdir(self.directory) |
| 272 |
try: |
| 273 |
result = self.check_results() |
| 274 |
except ExtractorTestError as error: |
| 275 |
result = self.show_report("ERROR", error) |
| 276 |
self.outbuffer.close() |
| 277 |
if self.directory: |
| 278 |
os.chdir(ROOT_DIR) |
| 279 |
subprocess.call(["chmod", "-R", "700", self.directory]) |
| 280 |
subprocess.call(["rm", "-rf", self.directory]) |
| 281 |
return result |
| 282 |
|
| 283 |
|
| 284 |
class TestsRunner(object): |
| 285 |
outcomes = ["error", "failed", "passed"] |
| 286 |
|
| 287 |
def __init__(self): |
| 288 |
with open("tests.yml", "rb") as test_db: |
| 289 |
self.test_data = yaml.load( |
| 290 |
test_db.read(-1).decode("utf-8", errors="ignore"), |
| 291 |
Loader=yaml.FullLoader, |
| 292 |
) |
| 293 |
self.name_regexps = [re.compile(s) for s in sys.argv[1:]] |
| 294 |
self.tests = [ExtractorTest(**data) for data in self.test_data if self.wanted_test(data)] |
| 295 |
self.add_subdir_tests() |
| 296 |
|
| 297 |
def wanted_test(self, data): |
| 298 |
if not self.name_regexps: |
| 299 |
return True |
| 300 |
return any([r.search(data["name"]) for r in self.name_regexps]) |
| 301 |
|
| 302 |
def add_subdir_tests(self): |
| 303 |
for odata in self.test_data: |
| 304 |
if (not self.wanted_test(odata)) or "directory" in odata or ("baseline" not in odata): |
| 305 |
continue |
| 306 |
data = odata.copy() |
| 307 |
data["name"] += " in .." |
| 308 |
data["directory"] = "inside-dir" |
| 309 |
data["filenames"] = " ".join([ |
| 310 |
"../%s" % filename for filename in data.get("filenames", "").split() |
| 311 |
]) |
| 312 |
self.tests.append(ExtractorTest(**data)) |
| 313 |
|
| 314 |
def run(self): |
| 315 |
results = {} |
| 316 |
for outcome in self.outcomes: |
| 317 |
results[outcome] = 0 |
| 318 |
for test in self.tests: |
| 319 |
results[test.run()] += 1 |
| 320 |
if self.tests: |
| 321 |
self.tests[-1].status_writer.clear() |
| 322 |
print( |
| 323 |
"Totals:", |
| 324 |
", ".join(["%s %s" % (results[key], key) for key in self.outcomes]), |
| 325 |
) |
| 326 |
return (results["error"] + results["failed"]) == 0 |
| 327 |
|
| 328 |
|
| 329 |
runner = TestsRunner() |
| 330 |
if not runner.run(): |
| 331 |
sys.exit(1) |