Skip to content

Commit

Permalink
Update Test Harness (MetroRobots#10)
Browse files Browse the repository at this point in the history
* Update test data hash
* Refactor test temp directories and directory comparison

(with known test failure)
  • Loading branch information
DLu authored Jan 11, 2024
1 parent ededda9 commit 7f5f216
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
52 changes: 27 additions & 25 deletions test/test_from_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,22 @@
URL_TEMPLATE = 'https://github.com/DLu/roscompile_test_data/raw/{}/test_data.zip'
TEST_DATA = [
# (branch, known_hash)
('ros1', '2100c19c912c6044194e7a77dad3d002e3b22f5206b171c3f127069b87dbc662'),
('ros2', '97fa59fdc742a60e57cf17643b70eb21aa26a6967cc64d364371480665fb5635'),
('ros1', '84bf97b92d2ceb8d4369ed0d6fd8a01fabaf3006034fb7710d448b8cfe9885d9'),
('ros2', 'cf23d832e0cf56c8e160c0178af81e865c615fb330fb26604ddf031fc51a280a'),
]

linters = get_linters()


def files_match(pkg_in, pkg_out, filename):
"""Return true if the contents of the given file are the same in each package. Otherwise maybe show the diff."""
generated_contents = pkg_in.get_contents(filename).rstrip()
canonical_contents = pkg_out.get_contents(filename).rstrip()
ret = generated_contents == canonical_contents
if not ret:
print(get_diff_string(generated_contents, canonical_contents, filename))
return ret


def run_case(test_config, cases):
resources = ROSResources.get()

with cases[test_config['in']] as pkg_in:
pkg_out = cases[test_config['out']]
if test_config['in'] == test_config['out']:
pkg_out = pkg_in.copy()
else:
pkg_out = cases[test_config['out']]

root = pkg_in.root
pkg_obj = Package(root)
local_config = test_config.get('config', {})
Expand All @@ -57,22 +51,30 @@ def run_case(test_config, cases):
fne(pkg_obj)
pkg_obj.save()

folder_diff = pkg_in.compare_filesets(pkg_out)

s = '{:25} >> {:25} {}'.format(test_config['in'], test_config['out'],
','.join(test_config['functions']))
print(color_text(s, 'BLUE', bright=True))

def jp(paths):
# Join Paths
return ', '.join(map(str, paths))

assert len(folder_diff['deleted']) == 0, \
f'These files should have been deleted but weren\'t: {jp(folder_diff["deleted"])}'
assert len(folder_diff['added']) == 0, \
f'These files should have been generated but weren\'t: {jp(folder_diff["added"])}'
for filename in folder_diff['matches']:
assert files_match(pkg_in, pkg_out, filename), 'The contents of {} do not match!'.format(filename)
# Compute the differences
filenames_in = pkg_in.get_filenames()
filenames_out = pkg_out.get_filenames()
problems = []

for filename in sorted(filenames_in - filenames_out):
print(get_diff_string(pkg_in.get_contents(filename).rstrip(), '', filename))
problems.append(f"File {filename} should have been deleted but wasn't")
for filename in sorted(filenames_out - filenames_in):
print(get_diff_string('', pkg_out.get_contents(filename).rstrip(), filename))
problems.append(f"File {filename} should have been generated but wasn't")
for filename in sorted(filenames_in.intersection(filenames_out)):
contents_in = pkg_in.get_contents(filename).rstrip()
contents_out = pkg_out.get_contents(filename).rstrip()

if contents_in != contents_out:
print(get_diff_string(contents_in, contents_out, filename))
problems.append('The contents of {} do not match!'.format(filename))

assert not problems, ', '.join(problems)


parameters = []
Expand Down
26 changes: 6 additions & 20 deletions test/zip_interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import collections
import os
import pathlib
import shutil
import tempfile
import yaml
import zipfile
Expand All @@ -14,24 +13,25 @@ class ROSCompilePackageFiles:
def __init__(self, package_name, pkg_files, executables):
self.package_name = package_name
self.is_written = False
self.root = self.get_input_root()
self.tempdir = None
self.root = None
self.pkg_files = pkg_files
self.executables = executables

def copy(self):
return ROSCompilePackageFiles(self.package_name, self.pkg_files, self.executables)

def get_input_root(self):
return pathlib.Path(tempfile.gettempdir()) / self.package_name

def __enter__(self):
self.tempdir = tempfile.TemporaryDirectory()
self.root = pathlib.Path(self.tempdir.name)
self.write()
self.is_written = True
return self

def __exit__(self, a_type, value, traceback):
self.is_written = False
self.clear()
self.root = None
self.tempdir = None

def get_filenames(self):
if self.is_written:
Expand All @@ -50,17 +50,7 @@ def get_contents(self, filename):
elif filename in self.pkg_files:
return self.pkg_files[filename].replace('\r\n', '\n')

def compare_filesets(self, other_package):
in_keys = self.get_filenames()
out_keys = other_package.get_filenames()
matches = in_keys.intersection(out_keys)
missed_deletes = in_keys - out_keys
missed_generations = out_keys - in_keys
return {'matches': sorted(matches), 'deleted': sorted(missed_deletes), 'added': sorted(missed_generations)}

def write(self):
self.clear()
self.root.mkdir()
for fn, contents in self.pkg_files.items():
outfile = self.root / fn
outfile.parent.mkdir(exist_ok=True, parents=True)
Expand All @@ -69,10 +59,6 @@ def write(self):
if fn in self.executables:
set_executable(outfile, True)

def clear(self):
if self.root.exists():
shutil.rmtree(self.root)

def __repr__(self):
return self.package_name

Expand Down

0 comments on commit 7f5f216

Please sign in to comment.