Skip to content

Commit 0085203

Browse files
committed
Improves test-patch update
Patches now are detected more easily. The count is carried out correctly and the test-file is updated in a standard way.
1 parent 43d0c3d commit 0085203

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

tests/functional/test.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class TempTestFile(object):
5858
Executes a system command which creates a temporary file and
5959
ensures file deletion upon return.
6060
"""
61-
6261
def __init__(self, func, fname, keep_file=False):
6362
""" Initializes the context. The flag dont_remove will only be taken into account
6463
if the System command execution was successful (returns 0)
@@ -87,7 +86,7 @@ def __exit__(self, type_, value, traceback):
8786
if self.error_level or not self.keep_file: # command failure or remove file?
8887
try:
8988
os.unlink(self.fname)
90-
except OSError:
89+
except (OSError, FileNotFoundError):
9190
pass # Ok. It might be that the wasn't created
9291

9392

@@ -114,7 +113,7 @@ def get_file_lines(filename: str, ignore_regexp=None, replace_regexp=None,
114113
"""
115114
from src.api.utils import open_file
116115
with open_file(filename, 'rt', 'utf-8') as f:
117-
lines = [x for x in f]
116+
lines = [x.rstrip('\r\n') for x in f]
118117

119118
if ignore_regexp is not None:
120119
r = re.compile(ignore_regexp)
@@ -125,7 +124,7 @@ def get_file_lines(filename: str, ignore_regexp=None, replace_regexp=None,
125124
lines = [x.replace(replace_what, replace_with, 1) if r.search(x) else x for x in lines]
126125

127126
if strip_blanks:
128-
lines = [x.rstrip(' \t') for x in lines if x.rstrip()]
127+
lines = [x.rstrip(' \t') for x in lines if x.rstrip(' \t')]
129128

130129
return lines
131130

@@ -241,7 +240,7 @@ def updateTest(tfname: str, pattern_, strip_blanks: bool = True):
241240
lines = get_file_lines(tfname, replace_regexp=pattern_, replace_what=ZXBASIC_ROOT,
242241
replace_with=_original_root, strip_blanks=strip_blanks)
243242
with src.api.utils.open_file(tfname, 'wt', encoding='utf-8') as f:
244-
f.write(''.join(lines))
243+
f.write('\n'.join(lines))
245244

246245

247246
@src.api.utils.timeout(_timeout)
@@ -430,21 +429,24 @@ def testFiles(file_list, cmdline_args=None):
430429
_msg('FAIL\n')
431430

432431

433-
def upgradeTest(fileList, f3diff):
432+
def upgradeTest(fileList: List[str], f3diff: str):
434433
""" Run against the list of files, and a 3rd file containing the diff.
435434
If the diff between file1 and file2 are the same as file3, then the
436435
.asm file is patched.
437436
"""
437+
global COUNTER
438438

439-
def normalizeDiff(diff):
439+
def normalizeDiff(diff: List[str]) -> List[str]:
440440
diff = [x.strip(' \t') for x in diff]
441441

442442
reHEADER = re.compile(r'[-+]{3}')
443443
while diff and reHEADER.match(diff[0]):
444444
diff = diff[1:]
445445

446+
O1 = O2 = 0
446447
first = True
447448
reHUNK = re.compile(r'@@ [-+](\d+)(,\d+)? [-+](\d+)(,\d+)? @@')
449+
448450
for i in range(len(diff)):
449451
line = diff[i]
450452
if line[:7] in ('-#line ', '+#line '):
@@ -465,7 +467,9 @@ def normalizeDiff(diff):
465467

466468
return diff
467469

468-
fdiff = open(f3diff).readlines()
470+
with open(f3diff, 'rt', encoding='utf-8') as patch_file:
471+
fdiff = [line.rstrip('\n') for line in patch_file]
472+
469473
fdiff = normalizeDiff(fdiff)
470474

471475
for fname in fileList:
@@ -486,7 +490,7 @@ def normalizeDiff(diff):
486490
pass
487491
continue
488492

489-
lines = []
493+
lines: List[str] = []
490494
is_same_file(fname1, tfname, ignore_regexp=FILTER, diff=lines)
491495
lines = normalizeDiff(lines)
492496

@@ -495,13 +499,18 @@ def normalizeDiff(diff):
495499
x = x.strip()
496500
y = y.strip()
497501
c = '=' if x == y else '!'
498-
_msg('"%s"%s"%s"\n' % (x.strip(), c, y.strip()))
502+
_msg('"%s" %s "%s"\n' % (x.strip(), c, y.strip()))
499503
os.unlink(tfname)
500504
continue # Not the same diff
501505

502-
os.unlink(fname1)
503-
os.rename(tfname, fname1)
506+
lines = get_file_lines(tfname, replace_regexp=FILTER, replace_what=ZXBASIC_ROOT,
507+
replace_with=_original_root)
508+
with src.api.utils.open_file(fname1, 'wt', encoding='utf-8') as f:
509+
f.write('\n'.join(lines))
510+
511+
os.unlink(tfname)
504512
_msg("\rTest: %s (%s) updated\n" % (fname, fname1))
513+
COUNTER += 1
505514

506515

507516
def set_temp_dir(tmp_dir=None):

0 commit comments

Comments
 (0)