summaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: f2eb28ef8b2f9d97cb6feb01b65f3e345fb31a30 (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
# -*- coding: utf-8 -*-

"""Helpers for testing."""

import io
import os

import pytest

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


@pytest.fixture()
def filepath():
    """Returns full file path for test files."""

    def make_filepath(filename):
        # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
        # Alternate solution is to use parametrization `indirect=True`
        # http://stackoverflow.com/a/33879151
        # Syntax is noisy and requires specific variable names
        return os.path.join(FILES_DIR, filename)

    return make_filepath


@pytest.fixture()
def load_file(filepath):
    """Opens filename with encoding and return its contents."""

    def make_load_file(filename, encoding='utf-8'):
        # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
        # Alternate solution is to use parametrization `indirect=True`
        # http://stackoverflow.com/a/33879151
        # Syntax is noisy and requires specific variable names
        # And seems to be limited to only 1 argument.
        with io.open(filepath(filename), encoding=encoding) as f:
            return f.read().strip()

    return make_load_file


@pytest.fixture()
def get_stream(filepath):
    def make_stream(filename, encoding='utf-8'):
        return io.open(filepath(filename), encoding=encoding)

    return make_stream