summaryrefslogtreecommitdiff
path: root/gerbonara/tests/utils.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2022-01-30 20:11:38 +0100
committerjaseg <git@jaseg.de>2022-01-30 20:11:38 +0100
commitc3ca4f95bd59f69d45e582a4149327f57a360760 (patch)
tree5f43c61a261698e2f671b5238a7aa9a71a0f6d23 /gerbonara/tests/utils.py
parent259a56186820923c78a5688f59bd8249cf958b5f (diff)
downloadgerbonara-c3ca4f95bd59f69d45e582a4149327f57a360760.tar.gz
gerbonara-c3ca4f95bd59f69d45e582a4149327f57a360760.tar.bz2
gerbonara-c3ca4f95bd59f69d45e582a4149327f57a360760.zip
Rename gerbonara/gerber package to just gerbonara
Diffstat (limited to 'gerbonara/tests/utils.py')
-rw-r--r--gerbonara/tests/utils.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/gerbonara/tests/utils.py b/gerbonara/tests/utils.py
new file mode 100644
index 0000000..a3da40b
--- /dev/null
+++ b/gerbonara/tests/utils.py
@@ -0,0 +1,81 @@
+
+import pytest
+import functools
+import tempfile
+import re
+import shutil
+from contextlib import contextmanager
+from pathlib import Path
+
+from PIL import Image
+import pytest
+
+fail_dir = Path('gerbonara_test_failures')
+reference_path = lambda reference: Path(__file__).parent / 'resources' / str(reference)
+to_gerbv_svg_units = lambda val, unit='mm': val*72 if unit == 'inch' else val/25.4*72
+
+def path_test_name(request):
+ """ Create a slug suitable for use in file names from the test's nodeid """
+ module, _, test_name = request.node.nodeid.rpartition('::')
+ _test, _, test_name = test_name.partition('_')
+ test_name, _, _ext = test_name.partition('.')
+ return re.sub(r'[^\w\d]', '_', test_name)
+
+@pytest.fixture
+def print_on_error(request):
+ messages = []
+
+ def register_print(*args, sep=' ', end='\n'):
+ nonlocal messages
+ messages.append(sep.join(str(arg) for arg in args) + end)
+
+ yield register_print
+
+ if request.node.rep_call.failed:
+ for msg in messages:
+ print(msg, end='')
+
+@pytest.fixture
+def tmpfile(request):
+ registered = []
+
+ def register_tempfile(name, suffix):
+ nonlocal registered
+ f = tempfile.NamedTemporaryFile(suffix=suffix)
+ registered.append((name, suffix, f))
+ return Path(f.name)
+
+ yield register_tempfile
+
+ if request.node.rep_call.failed:
+ fail_dir.mkdir(exist_ok=True)
+ test_name = path_test_name(request)
+ for name, suffix, tmp in registered:
+ slug = re.sub(r'[^\w\d]+', '_', name.lower())
+ perm_path = fail_dir / f'failure_{test_name}_{slug}{suffix}'
+ shutil.copy(tmp.name, perm_path)
+ print(f'{name} saved to {perm_path}')
+
+ for _name, _suffix, tmp in registered:
+ tmp.close()
+
+@pytest.fixture
+def reference(request, print_on_error):
+ ref = request.param
+ if isinstance(ref, tuple):
+ ref, args = ref
+ ref = reference_path(ref)
+ yield ref, args
+
+ else:
+ ref = reference_path(request.param)
+ yield ref
+
+ print_on_error(f'Reference file: {ref}')
+
+def filter_syntax_warnings(fun):
+ a = pytest.mark.filterwarnings('ignore:.*Deprecated.*statement found.*:DeprecationWarning')
+ b = pytest.mark.filterwarnings('ignore::SyntaxWarning')
+ return a(b(fun))
+
+