1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
"""
PC-BASIC tests.basic
tests against original BASICs
(c) 2015--2023 Rob Hagemans
This file is released under the GNU GPL version 3 or later.
"""
from __future__ import print_function
import sys
import os
import shutil
import filecmp
import traceback
import time
import json
import logging
import platform
from copy import copy, deepcopy
from contextlib import contextmanager
try:
# Python 3 only
from importlib import reload
except ImportError:
pass
# process_time not in py2; clock deprecated in py3
try:
from time import process_time
except ImportError:
from time import clock as process_time
try:
import colorama
colorama.init()
except ImportError:
# only needed on Windows
# without it we still work but look a bit garbled
class colorama:
def init(): pass
# make pcbasic package accessible
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path = [os.path.join(HERE, '..', '..')] + sys.path
# copy of pythonpath for use by testing cycle
PYTHONPATH = copy(sys.path)
# test timing file
TEST_TIMES = os.path.join(HERE, '..', '_settings', 'slowtest.json')
# number of slowest tests to show or exclude
SLOWSHOW = 20
# statuses
CRASHED = 'crashed'
PASSED = 'passed'
NEWPASSED = 'newly passed'
ACCEPTED = 'accepted'
KNOWN = 'known to fail'
OLDFAILED = 'failed'
NEWFAILED = 'newly failed'
SKIPPED = 'skipped'
NONESUCH = 'not found'
# ANSI colours for test status
STATUS_COLOURS = {
CRASHED: '01;37;41',
PASSED: '00;32',
NEWPASSED: '01;32',
ACCEPTED: '00;36',
KNOWN: '00;33',
OLDFAILED: '00;31',
NEWFAILED: '01;31',
SKIPPED: '00;30',
NONESUCH: '00;31',
}
def is_same(file1, file2):
try:
return filecmp.cmp(file1, file2, shallow=False)
except EnvironmentError:
return False
@contextmanager
def suppress_stdio(do_suppress):
if not do_suppress:
yield
else:
with pcbasic.compat.stdio.quiet():
yield
class TestFrame(object):
def __init__(self, category, name, reraise, skip, loud):
self._dirname = os.path.join(HERE, category, name)
self._reraise = reraise
self.skip = testname(category, name) in skip
self._loud = loud
@contextmanager
def check_output(self):
if os.path.isdir(self._dirname) and 'PCBASIC.INI' in os.listdir(self._dirname):
self.exists = True
else:
self.exists = False
yield self
return
if self.skip:
yield self
return
self._output_dir = os.path.join(self._dirname, 'output')
self._model_dir = os.path.join(self._dirname, 'model')
self._accepted_dir = os.path.join(self._dirname, 'accepted')
self._known_dir = os.path.join(self._dirname, 'known')
self.old_fail = False
if os.path.isdir(self._output_dir):
self.old_fail = True
shutil.rmtree(self._output_dir)
os.makedirs(self._output_dir)
for filename in os.listdir(self._dirname):
if os.path.isfile(os.path.join(self._dirname, filename)):
shutil.copy(
os.path.join(self._dirname, filename),
os.path.join(self._output_dir, filename)
)
self._top = os.getcwd()
os.chdir(self._output_dir)
yield self
self.passed = True
self.accepted = os.path.isdir(self._accepted_dir)
self.known = os.path.isdir(self._known_dir)
self.failfiles = []
for path, dirs, files in os.walk(self._model_dir):
for f in files:
filename = os.path.join(path[len(self._model_dir)+1:], f)
if (
not is_same(
os.path.join(self._output_dir, filename),
os.path.join(self._model_dir, filename)
)
and not os.path.isfile(os.path.join(self._dirname, filename))
):
self.failfiles.append(filename)
self.passed = False
if not self.passed:
for path, dirs, files in os.walk(self._accepted_dir):
for f in files:
filename = os.path.join(path[len(self._accepted_dir)+1:], f)
self.accepted = (
os.path.isdir(self._accepted_dir) and
is_same(
os.path.join(self._output_dir, filename),
os.path.join(self._accepted_dir, filename)
)
)
if not self.passed and not self.accepted:
for path, dirs, files in os.walk(self._known_dir):
for f in files:
filename = os.path.join(path[len(self._known_dir)+1:], f)
self.known = (
os.path.isdir(self._known_dir) and
is_same(
os.path.join(self._output_dir, filename),
os.path.join(self._known_dir, filename)
)
)
for path, dirs, files in os.walk(self._output_dir):
for f in files:
filename = os.path.join(path[len(self._output_dir)+1:], f)
if (
not os.path.isfile(os.path.join(self._model_dir, filename))
and not os.path.isfile(os.path.join(self._dirname, filename))
):
self.failfiles.append(filename)
self.passed = False
self.accepted = self.accepted and (
os.path.isfile(os.path.join(self._accepted_dir, filename))
)
self.known = self.known and (
os.path.isfile(os.path.join(self._known_dir, filename))
)
os.chdir(self._top)
if self.passed:
try:
shutil.rmtree(self._output_dir)
shutil.rmtree(self._accepted_dir)
shutil.rmtree(self._known_dir)
except EnvironmentError:
pass
@contextmanager
def check_crash(self):
self.crash = None
try:
yield self
except Exception as e:
self.crash = e
if self._reraise:
raise
if self._loud:
traceback.print_exc()
@contextmanager
def guard(self):
with self.check_output():
with self.check_crash():
yield self
@property
def status(self):
if not self.exists:
return NONESUCH
if self.skip:
return SKIPPED
if self.crash:
return CRASHED
if self.passed:
if self.accepted or self.old_fail:
return NEWPASSED
return PASSED
if self.accepted:
return ACCEPTED
if self.known:
return KNOWN
if self.old_fail:
return OLDFAILED
return NEWFAILED
class Timer(object):
@contextmanager
def time(self):
start_time = time.time()
start_cpu = process_time()
yield self
self.wall_time = time.time() - start_time
self.cpu_time = process_time() - start_cpu
def testname(cat, name):
return '/'.join((cat, name))
def normalise(name):
if name.endswith('/'):
name = name[:-1]
#_, name = name.split(os.sep, 1)
# e.g. basic/gwbasic/TestName
try:
_dir, name = os.path.split(name)
_, category = os.path.split(_dir)
except ValueError:
category = 'gwbasic'
return category, name
def run_tests(tests, all, fast, loud, reraise, **dummy):
print('Running tests with Python', platform.python_version(), 'on', platform.platform())
if all:
dirs = [
_preset
for _preset in sorted(os.listdir(HERE))
if os.path.isdir(os.path.join(HERE, _preset))
and _preset != '__pycache__'
]
tests = [
os.path.join(_preset, _test)
for _preset in dirs
for _test in sorted(os.listdir(os.path.join(HERE, _preset)))
]
try:
with open(TEST_TIMES) as timefile:
times = dict(json.load(timefile))
except EnvironmentError:
times = {}
if fast:
# exclude slowest tests
skip = dict(sorted(times.items(), key=lambda _p: _p[1], reverse=True)[:SLOWSHOW])
else:
skip = {}
results = {}
with Timer().time() as overall_timer:
# preserve environment
startdir = os.path.abspath(os.getcwd())
save_env = deepcopy(os.environ)
# run all tests
for number, fullname in enumerate(tests):
# reset testing environment
os.chdir(startdir)
os.environ = deepcopy(save_env)
# normalise test name
category, name = normalise(fullname)
fullname = testname(category, name)
print(
'\033[00;37mRunning test {number}/{total} [{time:.2f}s] {category}/\033[01m{name} \033[00;37m.. '.format(
number=number+1, total=len(tests), time=times.get(fullname, 0),
category=category, name=name
),
end=''
)
with suppress_stdio(not loud):
with Timer().time() as timer:
with TestFrame(category, name, reraise, skip, loud).guard() as test_frame:
if test_frame.exists and not test_frame.skip:
# we need to include the output dir in the PYTHONPATH
# for it to find extension modules
sys.path = PYTHONPATH + [os.path.abspath('.')]
# run PC-BASIC
pcbasic.main('--interface=none')
# update test time
if test_frame.exists and not test_frame.skip and not test_frame.crash:
times[fullname] = timer.wall_time
# report status
results[fullname] = test_frame.status
print('\033[{colour}m{status}.\033[00;37m'.format(
colour=STATUS_COLOURS[test_frame.status],
status=test_frame.status,
))
# update stored times
with open(TEST_TIMES, 'w') as timefile:
json.dump(times, timefile)
return results, times, overall_timer
def report_results(results, times, overall_timer):
res_stat = {
_status: [_test for _test, _teststatus in results.items() if _teststatus == _status]
for _status in set(results.values())
}
print()
print(
'\033[00mRan %d tests in %.2fs (wall) %.2fs (cpu):\033[00;37m' %
(len(results), overall_timer.wall_time, overall_timer.cpu_time)
)
for status, tests in res_stat.items():
if status == PASSED:
print(' %d %s' % (len(tests), status), end='')
print('.')
for status, tests in res_stat.items():
if status != PASSED:
print(' %d %s' % (len(tests), status), end='')
print(':\n \033[%sm%s\033[00;37m.' % (
STATUS_COLOURS[status], '\n '.join(tests)
))
def run_basic_tests(**arg_dict):
# import late because of coverage
# see https://stackoverflow.com/questions/22146864/pytest-2-5-2-coverage-reports-missing-lines-which-must-have-been-processed
global pcbasic
import pcbasic
results = run_tests(**arg_dict)
report_results(*results)
print()
|