From a47bdd04fd121bc675fb57a374f91cc528529523 Mon Sep 17 00:00:00 2001 From: rockleona <34214497+rockleona@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:44:30 +0800 Subject: [PATCH] test(conf): add test case if config file is given in argument --- commitizen/cli.py | 2 +- commitizen/config/__init__.py | 44 +++++++++++++---------------------- commitizen/exceptions.py | 6 +++++ docs/README.md | 2 +- tests/test_conf.py | 18 ++++++++++---- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index d96d67585..cf3d6c5ee 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -94,7 +94,7 @@ def __call__( "arguments": [ { "name": "--config", - "help": "specify file path if config file is not in root folder", + "help": "the path of configuration file", }, {"name": "--debug", "action": "store_true", "help": "use debug mode"}, { diff --git a/commitizen/config/__init__.py b/commitizen/config/__init__.py index 883c0ded3..a9395fca7 100644 --- a/commitizen/config/__init__.py +++ b/commitizen/config/__init__.py @@ -3,7 +3,7 @@ from pathlib import Path from commitizen import defaults, git -from commitizen.exceptions import ConfigFileNotFound +from commitizen.exceptions import ConfigFileNotFound, ConfigFileIsEmpty from .base_config import BaseConfig from .json_config import JsonConfig @@ -14,37 +14,23 @@ def read_cfg(filepath: str | None = None) -> BaseConfig: conf = BaseConfig() - git_project_root = git.find_git_project_root() - if filepath is not None: - given_cfg_path = Path(filepath) - - if not given_cfg_path.exists(): + if not Path(filepath).exists(): raise ConfigFileNotFound() - with open(given_cfg_path, "rb") as f: - given_cfg_data: bytes = f.read() - - given_cfg: TomlConfig | JsonConfig | YAMLConfig - - if "toml" in given_cfg_path.suffix: - given_cfg = TomlConfig(data=given_cfg_data, path=given_cfg_path) - elif "json" in given_cfg_path.suffix: - given_cfg = JsonConfig(data=given_cfg_data, path=given_cfg_path) - elif "yaml" in given_cfg_path.suffix: - given_cfg = YAMLConfig(data=given_cfg_data, path=given_cfg_path) - - return given_cfg + cfg_paths = (path for path in (Path(filepath),)) + else: + git_project_root = git.find_git_project_root() + cfg_search_paths = [Path(".")] + if git_project_root: + cfg_search_paths.append(git_project_root) - cfg_search_paths = [Path(".")] - if git_project_root: - cfg_search_paths.append(git_project_root) + cfg_paths = ( + path / Path(filename) + for path in cfg_search_paths + for filename in defaults.config_files + ) - cfg_paths = ( - path / Path(filename) - for path in cfg_search_paths - for filename in defaults.config_files - ) for filename in cfg_paths: if not filename.exists(): continue @@ -61,7 +47,9 @@ def read_cfg(filepath: str | None = None) -> BaseConfig: elif "yaml" in filename.suffix: _conf = YAMLConfig(data=data, path=filename) - if _conf.is_empty_config: + if filepath is not None and _conf.is_empty_config: + raise ConfigFileIsEmpty() + elif _conf.is_empty_config: continue else: conf = _conf diff --git a/commitizen/exceptions.py b/commitizen/exceptions.py index 52c300a93..9cb151768 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -35,6 +35,7 @@ class ExitCode(enum.IntEnum): VERSION_SCHEME_UNKNOWN = 28 CHANGELOG_FORMAT_UNKNOWN = 29 CONFIG_FILE_NOT_FOUND = 30 + CONFIG_FILE_IS_EMPTY = 31 class CommitizenException(Exception): @@ -195,3 +196,8 @@ class ChangelogFormatUnknown(CommitizenException): class ConfigFileNotFound(CommitizenException): exit_code = ExitCode.CONFIG_FILE_NOT_FOUND message = "Cannot found the config file, please check your file path again." + + +class ConfigFileIsEmpty(CommitizenException): + exit_code = ExitCode.CONFIG_FILE_IS_EMPTY + message = "Config file is empty, please check your file path again." diff --git a/docs/README.md b/docs/README.md index 882282659..8c7dc51e4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -107,7 +107,7 @@ For more information about the topic go to https://conventionalcommits.org/ optional arguments: -h, --help show this help message and exit - --config specify file path if config file is not in root folder + --config the path of configuration file --debug use debug mode -n NAME, --name NAME use the given commitizen (default: cz_conventional_commits) -nr NO_RAISE, --no-raise NO_RAISE diff --git a/tests/test_conf.py b/tests/test_conf.py index 8d2a0233d..786f12b36 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -9,7 +9,7 @@ import yaml from commitizen import config, defaults, git -from commitizen.exceptions import InvalidConfigurationError +from commitizen.exceptions import InvalidConfigurationError, ConfigFileIsEmpty PYPROJECT = """ [tool.commitizen] @@ -179,7 +179,7 @@ def test_load_empty_pyproject_toml_and_cz_toml_with_config(_, tmpdir): cfg = config.read_cfg() assert cfg.settings == _settings - def test_load_pyproject_toml_not_in_root_folder(_, tmpdir): + def test_load_pyproject_toml_from_config_argument(_, tmpdir): with tmpdir.as_cwd(): _not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml") _not_root_path.write(PYPROJECT) @@ -187,7 +187,7 @@ def test_load_pyproject_toml_not_in_root_folder(_, tmpdir): cfg = config.read_cfg(filepath="./not_in_root/pyproject.toml") assert cfg.settings == _settings - def test_load_cz_json_not_in_root_folder(_, tmpdir): + def test_load_cz_json_not_from_config_argument(_, tmpdir): with tmpdir.as_cwd(): _not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json") _not_root_path.write(JSON_STR) @@ -196,15 +196,23 @@ def test_load_cz_json_not_in_root_folder(_, tmpdir): json_cfg_by_class = config.JsonConfig(data=JSON_STR, path=_not_root_path) assert cfg.settings == json_cfg_by_class.settings - def test_load_cz_yaml_not_in_root_folder(_, tmpdir): + def test_load_cz_yaml_not_from_config_argument(_, tmpdir): with tmpdir.as_cwd(): _not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml") _not_root_path.write(YAML_STR) cfg = config.read_cfg(filepath="./not_in_root/.cz.yaml") - yaml_cfg_by_class = config.YAMLConfig(data=JSON_STR, path=_not_root_path) + yaml_cfg_by_class = config.YAMLConfig(data=YAML_STR, path=_not_root_path) assert cfg.settings == yaml_cfg_by_class._settings + def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir): + with tmpdir.as_cwd(): + _not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml") + _not_root_path.write("") + + with pytest.raises(ConfigFileIsEmpty): + config.read_cfg(filepath="./not_in_root/pyproject.toml") + class TestTomlConfig: def test_init_empty_config_content(self, tmpdir):