forked from ChelseaKR/tods-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
96 lines (69 loc) · 3.27 KB
/
Copy pathtest_config.py
File metadata and controls
96 lines (69 loc) · 3.27 KB
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
"""Configuration file loading and the --ignore/--config CLI options."""
from pathlib import Path
import pytest
from click.testing import CliRunner
from conftest import FIXTURES
from tods_validate.cli import main
from tods_validate.config import Config, ConfigError, load_config
def invoke(*args: str, cwd: Path | None = None):
runner = CliRunner()
if cwd is None:
return runner.invoke(main, list(args))
import contextlib
import os
@contextlib.contextmanager
def chdir(path: Path):
before = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(before)
with chdir(cwd):
return runner.invoke(main, list(args))
def test_missing_discovered_file_is_empty_config(tmp_path: Path) -> None:
assert load_config(None, start_dir=tmp_path) == Config()
def test_discovered_file_is_used(tmp_path: Path) -> None:
(tmp_path / "tods-validate.toml").write_text(
'ignore = ["TODS-W206"]\n"fail-on" = "warning"\n', encoding="utf-8"
)
config = load_config(None, start_dir=tmp_path)
assert config.ignore == ("TODS-W206",)
assert config.fail_on == "warning"
def test_unknown_key_is_an_error(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text("severity = 3\n", encoding="utf-8")
with pytest.raises(ConfigError, match="unknown setting"):
load_config(path)
def test_bad_ignore_type_is_an_error(tmp_path: Path) -> None:
path = tmp_path / "tods-validate.toml"
path.write_text('ignore = "TODS-W206"\n', encoding="utf-8")
with pytest.raises(ConfigError, match="list of rule IDs"):
load_config(path)
def test_cli_ignore_suppresses_rule_and_exit_code() -> None:
fixture = str(FIXTURES / "invalid" / "TODS-E201")
result = invoke(fixture, "--ignore", "TODS-E201")
assert result.exit_code == 0, result.output
assert "ERROR TODS-E201" not in result.output
assert "No problems found." in result.output
def test_cli_unknown_ignore_id_exits_two() -> None:
result = invoke(str(FIXTURES / "valid" / "tods"), "--ignore", "TODS-E999")
assert result.exit_code == 2
assert "TODS-E999" in result.output
def test_cli_config_file_applies(tmp_path: Path) -> None:
config = tmp_path / "policy.toml"
config.write_text('ignore = ["TODS-W101"]\n', encoding="utf-8")
result = invoke(str(FIXTURES / "invalid" / "TODS-W101"), "--config", str(config))
assert result.exit_code == 0, result.output
assert "WARNING TODS-W101" not in result.output
def test_cli_discovers_config_in_cwd(tmp_path: Path) -> None:
(tmp_path / "tods-validate.toml").write_text('"fail-on" = "warning"\n', encoding="utf-8")
result = invoke(str(FIXTURES / "invalid" / "TODS-W101"), cwd=tmp_path)
assert result.exit_code == 1 # warning now fails via config
def test_cli_flag_overrides_config(tmp_path: Path) -> None:
(tmp_path / "tods-validate.toml").write_text('"fail-on" = "warning"\n', encoding="utf-8")
result = invoke(str(FIXTURES / "invalid" / "TODS-W101"), "--fail-on", "error", cwd=tmp_path)
assert result.exit_code == 0
def test_cli_missing_config_file_exits_two(tmp_path: Path) -> None:
result = invoke(str(FIXTURES / "valid" / "tods"), "--config", str(tmp_path / "nope.toml"))
assert result.exit_code == 2