diff --git a/CHANGELOG.md b/CHANGELOG.md index c7d4813..f2d89f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Make ubiconfig.utils.config_validation.validate_config publicly available via ubiconfig.validate_config +### Fixed +- Fix LocalLoader couldn't get right version if the argument version of load() is None. + ## [v2.1.0] - 2019-10-23 ### Fixed diff --git a/setup.py b/setup.py index 746400d..1f04bf9 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def get_requirements(): setup( name="ubi-config", - version="2.1.0", + version="2.1.1", author="", author_email="", packages=find_packages(exclude=["tests", "tests.*"]), diff --git a/tests/ubiconfig/test_ubi.py b/tests/ubiconfig/test_ubi.py index 04aea04..a42b518 100644 --- a/tests/ubiconfig/test_ubi.py +++ b/tests/ubiconfig/test_ubi.py @@ -181,9 +181,10 @@ def test_load_all_with_error_config( def test_load_from_local(): - loader = ubi.get_loader(TEST_DATA_DIR) + path = os.path.join(TEST_DATA_DIR, "configs/ubi7.1") + loader = ubi.get_loader(path) # loads relative to given path - config = loader.load("configs/ubi7.1/rhel-atomic-host.yaml") + config = loader.load("rhel-atomic-host.yaml") assert isinstance(config, UbiConfig) assert config.version == "7.1" @@ -195,13 +196,13 @@ def test_load_from_local_decimal_integrity(): def test_load_from_nonyaml(tmpdir): - somefile = tmpdir.join("some-file.txt") + somefile = tmpdir.mkdir("ubi7").join("some-file.txt") somefile.write("[oops, this is not valid yaml") loader = ubi.get_loader(str(tmpdir)) with pytest.raises(yaml.YAMLError): - loader.load("some-file.txt") + loader.load("ubi7/some-file.txt") def test_load_local_failed_validation(): @@ -220,6 +221,13 @@ def test_load_all_from_local(): assert isinstance(configs[0], UbiConfig) +def test_load_from_directory_not_named_after_ubi(): + with patch("os.path.isdir"): + loader = ubi.get_loader("./ubi7.1a") + with pytest.raises(ValueError): + config = loader.load("file") + + def test_load_all_from_local_with_error_configs(): loader = ubi.get_loader(TEST_DATA_DIR) configs = loader.load_all() diff --git a/ubiconfig/_impl/loaders/local.py b/ubiconfig/_impl/loaders/local.py index 6cbf344..a5196eb 100644 --- a/ubiconfig/_impl/loaders/local.py +++ b/ubiconfig/_impl/loaders/local.py @@ -1,5 +1,6 @@ import logging import os +import re import yaml from jsonschema.exceptions import ValidationError @@ -40,16 +41,22 @@ def load(self, file_name, version=None): If version is None, we should get it from its path. """ - if version is None: - # get version form path, such as configs/ubi7.1/config.yaml, get - # ubi7.1 - version = os.path.basename(os.path.dirname(file_name)) - if not self._isroot: file_path = os.path.join(self._path, file_name) else: file_path = file_name + if version is None: + # get version from path, such as configs/ubi7.1/config.yaml, get + # ubi7.1. + version = os.path.basename(os.path.dirname(os.path.abspath(file_path))) + + if not re.search(r"ubi[0-9]{1}\.?([0-9]{0,2})$", version): + raise ValueError( + "Expect directories named in format ubi[0-9]{1}.?([0-9]{0,2})$', but got %s" + % version + ) + LOG.info("Loading configuration file locally: %s", file_path) with open(file_path, "r") as f: @@ -94,7 +101,7 @@ def _get_local_files_map(self): ] if conf_files: # if there's yaml files, then it must under some version directory - version = os.path.basename(root) + version = os.path.basename(os.path.abspath(root)) ver_files_map.setdefault(version, []).extend(conf_files) # the result map is as {'version': ['file1', 'file2', ..]}