summaryrefslogtreecommitdiff
path: root/scss/tests/conftest.py
blob: d507cfcbbd17db742517224c484238d1199167e6 (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
"""py.test plugin configuration."""

import glob
import os.path
import re

FILES_DIR = os.path.relpath(os.path.join(os.path.dirname(__file__), 'files'))

test_file_pairs = None  # edited below
def pytest_configure(config):
    """Scan for test files.  Done here because other hooks tend to run once
    *per test*, and there's no reason to do this work more than once.
    """
    global test_file_pairs

    scss_files = glob.glob(os.path.join(FILES_DIR, '*/*.scss'))
    test_file_filter = config.getoption('test_file_filter')
    if test_file_filter:
        file_filters = [
            re.compile(filt)
            for filt in test_file_filter.split(',')
        ]

        filtered_scss_files = []
        for fn in scss_files:
            relfn = os.path.relpath(fn, FILES_DIR)
            for rx in file_filters:
                if rx.search(relfn):
                    filtered_scss_files.append(fn)
                    break

        scss_files = filtered_scss_files

    test_file_pairs = [(fn, fn[:-5] + '.css') for fn in scss_files]

def pytest_generate_tests(metafunc):
    """Inject the test files as parametrizations.

    Relies on the command-line option `--test-file-filter`, set by the root
    conftest.py.
    """

    if 'scss_file_pair' in metafunc.fixturenames:
        ids = [scss_fn for (scss_fn, css_fn) in test_file_pairs]
        metafunc.parametrize('scss_file_pair', test_file_pairs, ids=ids)