diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-01-27 13:30:53 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-01-27 13:30:53 -0600 |
| commit | 3a5ce18e9b2d921c419805c0b01f2b40822e7ab9 (patch) | |
| tree | 51f2158cd556fedc20f0f2017510dd2843cb836b | |
| parent | Remove BREAKING (diff) | |
| download | config-file-3a5ce18e9b2d921c419805c0b01f2b40822e7ab9.zip | |
Add proper working support for custom parsers and raise test coverage
| -rw-r--r-- | CHANGELOG.md | 6 | ||||
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | config_file/config_file.py | 14 | ||||
| -rw-r--r-- | tests/test_config_file.py | 134 |
4 files changed, 149 insertions, 13 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ee64f..9c34b35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +Fixed + + - Support for using custom parsers with `ConfigFile` with the `parser` optional + argument. This was technically supported before, but it was not tested and found + to not actually use the passed in parser once tested. + Changed - The `reset()` method on `ConfigFile` is now called `restore_original()`. @@ -126,6 +126,8 @@ config.restore_original(original_file_path="~/some_other_directory/this_is_actua ## Using your own parser -> This feature is a work in-progress and has not been tested yet. - -You can still use config file, even if you don't use one of our supported configuration formats. The `ConfigFile` object swaps in the parser it needs based on the file format. However, the constructor takes in an optional `parser` argument that you can use to supply your own custom parser. The only requirement is that the parser must be a concrete implementation of `BaseParser`. +You can still use config file, even if you don't use one of our supported configuration +formats. The `ConfigFile` object swaps in the parser it needs based on the file format. +However, the constructor takes in an optional `parser` argument that you can use to +supply your own custom parser. The only requirement is that the parser must be a +concrete implementation of `BaseParser`. diff --git a/config_file/config_file.py b/config_file/config_file.py index bc05d97..397c007 100644 --- a/config_file/config_file.py +++ b/config_file/config_file.py @@ -2,7 +2,6 @@ import inspect from pathlib import Path, PurePath from shutil import copyfile -from config_file.parsers.base_parser import BaseParser from config_file.parsers.ini_parser import IniParser from config_file.utils import split_on_dot @@ -47,7 +46,7 @@ class ConfigFile: if isinstance(file_path, PurePath): file_path = str(file_path) - if isinstance(parser, BaseParser) and not inspect.isabstract(parser): + if parser is not None and not inspect.isabstract(parser): return parser(self.contents) file_type = split_on_dot(file_path, only_last_dot=True)[-1] @@ -116,15 +115,12 @@ class ConfigFile: ) if not Path(original_file_path).exists(): - raise OSError(f"The {original_file_path} file to reset to does not exist.") - - try: - Path(self.path).expanduser().unlink() - except OSError: - pass + raise OSError( + f"The {original_file_path} file to restore to does not exist." + ) + Path(self.path).expanduser().unlink() copyfile(original_file_path, self.path) - self.contents = self.__read_config_file() self.parser.parsed_content = self.parser.parse(self.contents) diff --git a/tests/test_config_file.py b/tests/test_config_file.py index 3d2a5d2..0e42d19 100644 --- a/tests/test_config_file.py +++ b/tests/test_config_file.py @@ -2,7 +2,8 @@ from pathlib import Path import pytest -from config_file.config_file import BaseParser, ConfigFile +from config_file.config_file import ConfigFile +from config_file.parsers.base_parser import BaseParser @pytest.mark.parametrize( @@ -41,6 +42,26 @@ def test_that_config_file_can_restore_the_original( @pytest.mark.parametrize( + "file_extension, specify_original_config_path", [("ini", False), ("ini", True)] +) +def test_missing_config_files_during_restore( + tmpdir, file_extension, specify_original_config_path +): + with pytest.raises(OSError): + temp_path = tmpdir / f"config.{file_extension}" + temp_path.write("") + config = ConfigFile(str(temp_path)) + + if specify_original_config_path: + specify_original_config_path = f"does_not_exist.{file_extension}" + else: + config.path = f"does_not_exist.{file_extension}" + specify_original_config_path = None + + config.restore_original(original_file_path=specify_original_config_path) + + +@pytest.mark.parametrize( "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")] ) def test_that_config_file_can_save(tmpdir, file_contents, file_name): @@ -54,6 +75,72 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name): @pytest.mark.parametrize( + "section_key, value, parse_type, file_name, file_contents", + [ + ( + "calendar.sunday_index", + 0, + True, + "config.ini", + "[calendar]\nsunday_index = 0\n\n", + ), + ( + "calendar.sunday_index", + "0", + False, + "config.ini", + "[calendar]\nsunday_index = 0\n\n", + ), + ], +) +def test_that_config_file_can_get( + tmpdir, section_key, value, parse_type, file_name, file_contents +): + config_path = tmpdir / file_name + config_path.write_text(file_contents, encoding="utf-8") + config = ConfigFile(str(config_path)) + assert config.get(section_key, parse_type=parse_type) == value + + +@pytest.mark.parametrize( + "file_extension, file_contents, section_key, expected_result", + [ + ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.sunday_index", True), + ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar.blah", False), + ("ini", "[calendar]\nsunday_index = 0\n\n", "calendar", True), + ], +) +def test_that_config_file_can_find_sections_and_keys( + tmpdir, file_extension, file_contents, section_key, expected_result +): + config_path = tmpdir / f"config.{file_extension}" + config_path.write_text(file_contents, encoding="utf-8") + config = ConfigFile(str(config_path)) + assert config.has(section_key) == expected_result + + +@pytest.mark.parametrize( + "section_key, file_name, file_contents, deleted_file_contents", + [ + ( + "calendar.sunday_index", + "config.ini", + "[calendar]\nsunday_index = 0\n\n", + "[calendar]\n\n", + ), + ], +) +def test_that_config_file_can_delete( + tmpdir, section_key, file_name, file_contents, deleted_file_contents +): + config_path = tmpdir / file_name + config_path.write_text(file_contents, encoding="utf-8") + config = ConfigFile(str(config_path)) + config.delete(section_key) + assert config.stringify() == deleted_file_contents + + +@pytest.mark.parametrize( "path, is_dir", [("invalid", False), ("invalid", True), ("invalid.conf", False)] ) def test_invalid_config_paths(tmpdir, path, is_dir): @@ -61,3 +148,48 @@ def test_invalid_config_paths(tmpdir, path, is_dir): temp_path = tmpdir / path temp_path.write("") ConfigFile(str(tmpdir)) if is_dir else ConfigFile(str(temp_path)) + + +def test_that_custom_parser_can_be_used(tmpdir): + class CustomParser(BaseParser): + def __init__(self, file_contents): + super().__init__(file_contents) + + def get(self, key, parse_type=True): + return key + + def set(self, key, value): + return key, value + + def delete(self, section_key): + return section_key + + def has(self, section_key: str) -> bool: + return True + + def stringify(self) -> str: + return str(self.parsed_content) + + def parse(self, file_contents: str): + return file_contents + + config_path = tmpdir / "config.conf" + config_path.write_text("", encoding="utf-8") + config = ConfigFile(str(config_path), parser=CustomParser) + assert isinstance(config.parser, CustomParser) + assert config.get("key") == "key" + assert config.set("key", "value") == ("key", "value") + assert config.delete("section_key") == "section_key" + assert config.has("blah") + assert config.stringify() == "" + + +def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path(): + config_name = "fjlajbkfdajfdioanfks.ini" + config_path = "~/" + config_name + + try: + Path(config_path).expanduser().touch() + assert ConfigFile(config_path).path == str(Path.home() / config_name) + finally: + Path(config_path).expanduser().unlink() |