Skip to content

Commit 4a59791

Browse files
committed
Added tests for config and arguments
1 parent 286a177 commit 4a59791

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/tctools/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import logging
22
import sys
33
from abc import ABC, abstractmethod
4-
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
4+
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
55
from pathlib import Path
6-
from typing import List, Optional, Dict, Any
6+
from typing import Any, Dict, List, Optional
77

88
from lxml import etree
99

tests/test_common.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest # noqa
2+
3+
from tctools.common import Tool
4+
5+
6+
class MyTool(Tool):
7+
"""Dummy tool."""
8+
9+
CONFIG_KEY = "dummy"
10+
11+
@classmethod
12+
def set_arguments(cls, parser):
13+
super().set_arguments(parser)
14+
15+
parser.add_argument(
16+
"--my-option",
17+
action="store",
18+
default="default-text",
19+
)
20+
return parser
21+
22+
def run(self) -> int:
23+
return 0 # Do nothing
24+
25+
26+
class TestCommon:
27+
"""Directly test the common interface."""
28+
29+
def test_cli_arguments(self):
30+
tool = MyTool("--my-option", "xyz123")
31+
assert tool.args.my_option == "xyz123"
32+
33+
def test_cli_arguments_default(self):
34+
tool = MyTool()
35+
assert tool.args.my_option == "default-text"
36+
37+
def test_config_file(self, tmp_path, monkeypatch):
38+
conf_dir = tmp_path / "project"
39+
work_dir = conf_dir / "subdir1" / "subdir2"
40+
work_dir.mkdir(parents=True)
41+
42+
conf_file = conf_dir / "tctools.toml"
43+
conf_file.write_text(
44+
"""[tctools.dummy]
45+
my_option = "xyz123"
46+
"""
47+
)
48+
49+
monkeypatch.chdir(work_dir)
50+
51+
tool = MyTool()
52+
assert tool.args.my_option == "xyz123"
53+
54+
# TODO: Add test for section priority

0 commit comments

Comments
 (0)