aboutsummaryrefslogtreecommitdiffstats
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-07 20:25:00 -0500
committerGitHub <noreply@github.com>2020-08-07 20:25:00 -0500
commit05dba35cab34c2524b945da0164327b7b8a1159d (patch)
treee1f263a316839c0d32a413c120a3c92426f36817 /tests/conftest.py
parentCreate python-package-ci.yml (diff)
parentFix ConfigFile default (diff)
downloadconfig-file-05dba35cab34c2524b945da0164327b7b8a1159d.zip
Merge pull request #24 from eugenetriguba/dev
v0.11.0
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..3ec303e
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,68 @@
+from pathlib import Path
+from typing import Callable, Tuple, Type, Union
+
+import pytest
+
+from config_file import ConfigFile
+from config_file.abstract_parser import AbstractParser
+from config_file.config_file_path import ConfigFilePath
+
+
+@pytest.fixture
+def tmp_file(tmp_path) -> Callable[[str, str], Path]:
+ def func(file_name: str, file_contents: str) -> Path:
+ file = tmp_path / file_name
+ file.touch()
+ file.write_text(file_contents, encoding="utf-8")
+ return file
+
+ return func
+
+
+@pytest.fixture
+def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str], Path]:
+ template_dir = Path(__file__).parent / "templates"
+
+ def func(file_type: str, template_name: str = "default") -> Path:
+ file_name = f"{template_name}.{file_type}"
+ template = template_dir / file_type / file_name
+
+ return tmp_file(file_name, template.read_text(encoding="utf-8"))
+
+ return func
+
+
+@pytest.fixture
+def template_original_file() -> Callable[[Path], Path]:
+ def func(template_path: Union[str, Path]):
+ if isinstance(template_path, str):
+ template_path = Path(template_path)
+
+ original_path = ConfigFilePath(template_path).original_path
+ original_path.touch()
+ original_path.write_text(template_path.read_text(encoding="utf-8"))
+
+ return original_path
+
+ return func
+
+
+@pytest.fixture
+def template_and_parser(
+ template_file: Callable[[str, str], Path]
+) -> Callable[[str, str], Tuple[Path, Type[AbstractParser]]]:
+ def func(
+ file_type: str, template_name: str = "default"
+ ) -> Tuple[Path, Type[AbstractParser]]:
+ test_file = template_file(file_type, template_name)
+ return test_file, ConfigFile(test_file)._ConfigFile__parser
+
+ return func
+
+
+@pytest.fixture
+def templated_parser(template_and_parser) -> Callable[[str, str], Type[AbstractParser]]:
+ def func(file_type: str, template_name: str = "default") -> Type[AbstractParser]:
+ return template_and_parser(file_type, template_name)[1]
+
+ return func