aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_config_file.py
blob: 1daba6fc714165c86a1c49e8b368ec767aa66e28 (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
from pathlib import Path

import pytest

from config_file.config_file import BaseParser, ConfigFile


@pytest.mark.parametrize(
    "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
)
def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_name):
    temp = tmp_path / file_name
    temp.write_text(file_contents)

    config = ConfigFile(temp)
    assert config.path == str(temp)
    assert config.contents == temp.read_text()
    assert isinstance(config.parser, BaseParser)


@pytest.mark.parametrize(
    "file_contents, file_name, original_file_name",
    [("[calendar]\nsunday_index = 0\n\n", "config.ini", "config.original.ini")],
)
def test_that_config_file_can_reset(
    tmpdir, file_contents, file_name, original_file_name
):
    config_path = tmpdir / file_name
    config_path.write_text(file_contents, encoding="utf-8")

    original_config_path = tmpdir / original_file_name
    original_config_path.write_text(file_contents, encoding="utf-8")

    config = ConfigFile(str(config_path))
    config.set("calendar.sunday_index", 0)
    config.reset()
    assert config.stringify() == ConfigFile(str(original_config_path)).stringify()
    assert config_path.read_text(encoding="utf-8") == original_config_path.read_text(
        encoding="utf-8"
    )


@pytest.mark.parametrize(
    "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
)
def test_that_config_file_can_save(tmpdir, file_contents, file_name):
    config_path = tmpdir / file_name
    config_path.write_text(file_contents, encoding="utf-8")

    config = ConfigFile(str(config_path))
    config.set("calendar.sunday_index", 1)
    config.save()
    assert config.stringify() == Path(config_path).read_text(encoding="utf-8")


@pytest.mark.parametrize(
    "path, is_dir", [("invalid", False), ("invalid", True), ("invalid.conf", False)]
)
def test_invalid_config_paths(tmpdir, path, is_dir):
    with pytest.raises(ValueError):
        temp_path = tmpdir / path
        temp_path.write("")
        ConfigFile(str(tmpdir)) if is_dir else ConfigFile(str(temp_path))