From d2f3161dac2313e4109ba2159fdc0d43e50506f8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:43:44 -0500 Subject: Test extras parsers & move imports inside those parsers --- config_file/toml_parser.py | 25 ++++++++----------------- config_file/yaml_parser.py | 27 +++++++++++---------------- tests/test_toml_parser.py | 24 ++++++++++++++++++++++++ tests/test_yaml_parser.py | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py index fc12ab3..d3cf226 100644 --- a/config_file/toml_parser.py +++ b/config_file/toml_parser.py @@ -1,27 +1,18 @@ -try: - import tomlkit - - TOML_AVAILABLE = True -except ImportError: - TOML_AVAILABLE = False - - from .base_parser import BaseParser class TomlParser(BaseParser): def __init__(self, file_contents: str): - if not TOML_AVAILABLE: + try: + import tomlkit + from tomlkit.exceptions import ParseError + + super().__init__( + file_contents=file_contents, module=tomlkit, decode_error=ParseError, + ) + except ImportError: raise ImportError( "It doesn't appear `tomlkit` is installed, but a toml " "file was attempted to be used. Install the `toml` " "extra first with `pip install config-file[toml]`." ) - - super().__init__( - file_contents=file_contents, - module=tomlkit, - # tomlkit doesn't expose it's exceptions, but - # it's parsing error is a subclass of ValueError - decode_error=ValueError, - ) diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py index 938b0d7..8d88881 100644 --- a/config_file/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -1,26 +1,21 @@ -try: - from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load - - YAML_AVAILABLE = True -except ImportError: - YAML_AVAILABLE = False - from .base_parser import BaseParser class YamlParser(BaseParser): def __init__(self, file_contents: str): - if not YAML_AVAILABLE: + try: + from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load + + yaml = YAML(typ="safe") + yaml.loads = round_trip_load + yaml.dumps = round_trip_dump + + super().__init__( + file_contents=file_contents, module=yaml, decode_error=YAMLError + ) + except ImportError: raise ImportError( "It doesn't appear `ruamel.yaml` is installed, but a yaml " "file was attempted to be used. Install the `yaml` " "extra first with `pip install config-file[yaml]`." ) - - yaml = YAML(typ="safe") - yaml.loads = round_trip_load - yaml.dumps = round_trip_dump - - super().__init__( - file_contents=file_contents, module=yaml, decode_error=YAMLError - ) diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py index e69de29..845e78e 100644 --- a/tests/test_toml_parser.py +++ b/tests/test_toml_parser.py @@ -0,0 +1,24 @@ +from unittest.mock import patch + +import pytest + +from config_file.toml_parser import TomlParser + + +def test_that_import_error_is_raised_if_no_tomlkit(): + """ + config_file.toml_parser.TomlParser + + Ensure an import error is raised if tomlkit is not + installed. + """ + with pytest.raises(ImportError) as error: + + with patch("builtins.__import__") as mock_import: + mock_import.side_effect = ImportError + TomlParser("") + + assert ( + "Install the `toml` extra first with `pip install config-file[toml]`." + in str(error) + ) diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py index e69de29..1f46692 100644 --- a/tests/test_yaml_parser.py +++ b/tests/test_yaml_parser.py @@ -0,0 +1,24 @@ +from unittest.mock import patch + +import pytest + +from config_file.yaml_parser import YamlParser + + +def test_that_import_error_is_raised_if_no_ruamelyaml(): + """ + config_file.yaml_parser.YamlParser + + Ensure an import error is raised if ruamel.yaml is not + installed. + """ + with pytest.raises(ImportError) as error: + + with patch("builtins.__import__") as mock_import: + mock_import.side_effect = ImportError + YamlParser("") + + assert ( + "Install the `yaml` extra first with `pip install config-file[yaml]`." + in str(error) + ) -- cgit v1.2.3