aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_config_file_path.py
blob: e5ec5ba3f0072f62f73e54277e9edfcfd01d6ea2 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from pathlib import Path

import pytest

from config_file.config_file_path import ConfigFilePath
from config_file.parsers.ini_parser import IniParser
from config_file.parsers.json_parser import JsonParser
from config_file.parsers.toml_parser import TomlParser
from config_file.parsers.yaml_parser import YamlParser


def test_config_file_path_is_a_path():
    """
    config_file.config_file_path.ConfigFilePath

    Ensure that the ConfigFilePath object is an instance
    of Path from pathlib.
    """
    assert isinstance(ConfigFilePath("."), Path)


def test_config_file_path_is_able_to_read_the_contents(template_file):
    """
    config_file.config_file_path.ConfigFilePath

    Ensure that the contents retrieved are the same
    as the contents of the file.
    """
    template = template_file("ini", "default")
    path = ConfigFilePath(template)

    assert path.contents == template.read_text()


def test_config_file_path_is_able_to_calculate_the_original_path():
    """
    config_file.config_file_path.ConfigFilePath

    Ensure that the calculated original configuration
    path includes a "original" between the filename and
    extension.
    """
    path = ConfigFilePath("path-existing-does-not-matter.txt")

    assert str(path.original_path) == "path-existing-does-not-matter.original.txt"


def test_config_file_path_is_able_to_retrieve_the_extension():
    """
    config_file.config_file_path.ConfigFilePath

    Ensure that the retrieved extension is correct
    for the given filepath and that it does not retrieve
    the dot (.) from the extension.
    """
    path = ConfigFilePath("some-file.txt")

    assert path.extension == "txt"


@pytest.mark.parametrize(
    "template_file_type, expected_parser_type",
    [
        ("ini", IniParser),
        ("json", JsonParser),
        ("yaml", YamlParser),
        ("toml", TomlParser),
    ],
)
def test_config_file_path_decides_on_the_correct_parser(
    template_file, template_file_type, expected_parser_type
):
    """
    config_file.config_file_path.ConfigFilePath

    Ensure that a filepath with an extension that is supported
    retrieves the correct parser for it.
    """
    template = template_file(template_file_type, "default")
    path = ConfigFilePath(template)

    assert isinstance(path.file_parser, expected_parser_type)


@pytest.mark.parametrize("filename", ["some-file", "some-file.unsupported-extension"])
def test_config_file_path_raises_value_error_on_parser_if_invalid_extension(filename):
    """
    config_file.config_file_path.ConfigFilePath

    Ensure that a ValueError is raised if we have a
    filepath with no extension or a filepath with an
    unsupported extension.
    """
    path = ConfigFilePath(filename)

    with pytest.raises(ValueError):
        path.file_parser