Skip to content

Commit

Permalink
Added tests for config and arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoRoos committed Dec 9, 2024
1 parent 4cca1f9 commit 8605ff6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/tctools/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
import sys
from abc import ABC, abstractmethod
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
from pathlib import Path
from typing import List, Optional, Dict, Any
from typing import Any, Dict, List, Optional

from lxml import etree

Expand Down
54 changes: 54 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest # noqa

from tctools.common import Tool


class MyTool(Tool):
"""Dummy tool."""

CONFIG_KEY = "dummy"

@classmethod
def set_arguments(cls, parser):
super().set_arguments(parser)

parser.add_argument(
"--my-option",
action="store",
default="default-text",
)
return parser

def run(self) -> int:
return 0 # Do nothing


class TestCommon:
"""Directly test the common interface."""

def test_cli_arguments(self):
tool = MyTool("--my-option", "xyz123")
assert tool.args.my_option == "xyz123"

def test_cli_arguments_default(self):
tool = MyTool()
assert tool.args.my_option == "default-text"

def test_config_file(self, tmp_path, monkeypatch):
conf_dir = tmp_path / "project"
work_dir = conf_dir / "subdir1" / "subdir2"
work_dir.mkdir(parents=True)

conf_file = conf_dir / "tctools.toml"
conf_file.write_text(
"""[tctools.dummy]
my_option = "xyz123"
"""
)

monkeypatch.chdir(work_dir)

tool = MyTool()
assert tool.args.my_option == "xyz123"

# TODO: Add test for section priority

0 comments on commit 8605ff6

Please sign in to comment.