summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/tests/utils.py
blob: bf30a1f2a721b589938fe33bf6916961e7bd0f73 (plain)
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
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))