File: test_config.py

package info (click to toggle)
python-sphinx-chango 0.5.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,776 kB
  • sloc: python: 4,909; javascript: 74; makefile: 23
file content (90 lines) | stat: -rw-r--r-- 3,809 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
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
#  SPDX-FileCopyrightText: 2024-present Hinrich Mahler <chango@mahlerhome.de>
#
#  SPDX-License-Identifier: MIT
from contextlib import nullcontext
from pathlib import Path

import pytest
from click import UsageError

from tests.auxil.files import PROJECT_ROOT_PATH, data_path, temporary_chdir
from tests.cli.conftest import ReuseCliRunner


class TestConfig:
    @pytest.mark.parametrize(
        "path",
        [
            None,
            data_path("config/pyproject.toml"),
            data_path("config/pyproject.toml").relative_to(Path.cwd(), walk_up=True),
            data_path("config"),
        ],
        ids=["None", "absolute", "relative", "directory"],
    )
    def test_show_path_selection(self, cli: ReuseCliRunner, path):
        with temporary_chdir(data_path("config")) if path is None else nullcontext():
            args = ["config", "--path", str(path), "show"] if path else ["config", "show"]

            result = cli.invoke(args)
            assert result.check_exit_code(0)

            assert str(data_path("config/pyproject.toml")) in result.stdout
            assert 'sys_path = "/abs/sys_path"' in result.stdout
            assert (
                'chango_instance = { name= "name", module = "module", package = "package" }'
                in result.stdout
            )

    def test_show_path_not_found(self, cli: ReuseCliRunner):
        path = Path("nonexistent").absolute()
        result = cli.invoke(["config", "--path", str(path), "show"])
        assert result.check_exit_code(UsageError.exit_code)
        assert f"File not found: {path!s}" in result.stderr

    def test_show_invalid_toml(self, cli: ReuseCliRunner, tmp_path):
        path = tmp_path / "pyproject.toml"
        path.write_text("invalid toml")

        with temporary_chdir(tmp_path):
            result = cli.invoke(["config", "--path", str(path), "show"])
            assert result.check_exit_code(UsageError.exit_code)
            assert "Failed to parse the configuration file" in result.stderr

    def test_show_no_chango_config(self, cli: ReuseCliRunner, tmp_path):
        path = tmp_path / "pyproject.toml"
        path.write_text("[tool.other]")

        with temporary_chdir(tmp_path):
            result = cli.invoke(["config", "--path", str(path), "show"])
            assert result.check_exit_code(UsageError.exit_code)
            assert "No configuration found for chango" in result.stderr

    def test_validate_invalid_chango_config(self, cli: ReuseCliRunner, tmp_path):
        path = tmp_path / "pyproject.toml"
        path.write_text("[tool.chango]\nsys_path = 42")

        with temporary_chdir(tmp_path):
            result = cli.invoke(["config", "--path", str(path), "validate"])
            assert result.check_exit_code(UsageError.exit_code)
            assert f"Validation of config file at {path!s} failed:" in result.stderr

    def test_validate_import_error(self, cli: ReuseCliRunner, tmp_path):
        path = tmp_path / "pyproject.toml"
        path.write_text(
            "[tool.chango]\nsys_path = 'sys_path'\nchango_instance = { name= 'name', module = "
            "'module', package = 'package' }"
        )

        with temporary_chdir(tmp_path):
            result = cli.invoke(["config", "--path", str(path), "validate"])
            assert result.check_exit_code(UsageError.exit_code)
            assert "importing the ChanGo instance failed" in result.stderr

    def test_validate_success(self, cli: ReuseCliRunner):
        with temporary_chdir(PROJECT_ROOT_PATH):
            result = cli.invoke(["config", "validate"])
            assert result.check_exit_code(0)
            assert "The configuration in" in result.stdout
            assert str(PROJECT_ROOT_PATH / "pyproject.toml") in result.stdout
            assert "valid" in result.stdout