File: test_config_reader.py

package info (click to toggle)
python-maison 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 816 kB
  • sloc: python: 1,549; makefile: 9; sh: 5
file content (56 lines) | stat: -rw-r--r-- 1,598 bytes parent folder | download
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
import io
import pathlib
import typing

import pytest

from maison import config_parser
from maison import errors
from maison import typedefs


class FakePyprojectParser:
    def parse_config(self, file: typing.BinaryIO) -> typedefs.ConfigValues:
        return {"config": "pyproject"}


class FakeTomlParser:
    def parse_config(self, file: typing.BinaryIO) -> typedefs.ConfigValues:
        return {"config": "toml"}


class TestParsesConfig:
    def setup_method(self):
        self.parser = config_parser.ConfigParser()

    def test_uses_parser_by_file_path_and_stem(self):
        self.parser.register_parser(
            suffix=".toml", parser=FakePyprojectParser(), stem="pyproject"
        )

        values = self.parser.parse_config(
            file_path=pathlib.Path("path/to/pyproject.toml"),
            file=io.BytesIO(b"file"),
        )

        assert values == {"config": "pyproject"}

    def test_falls_back_to_suffix(self):
        self.parser.register_parser(
            suffix=".toml", parser=FakePyprojectParser(), stem="pyproject"
        )
        self.parser.register_parser(suffix=".toml", parser=FakeTomlParser())

        values = self.parser.parse_config(
            pathlib.Path("path/to/.acme.toml"),
            file=io.BytesIO(b"file"),
        )

        assert values == {"config": "toml"}

    def test_raises_error_if_no_parser_available(self):
        with pytest.raises(errors.UnsupportedConfigError):
            _ = self.parser.parse_config(
                pathlib.Path("path/to/.acme.toml"),
                file=io.BytesIO(b"file"),
            )