Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf6a6bf

Browse files
test(conf): add test case if config file is given in argument
1 parent 00e21a0 commit bf6a6bf

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

‎commitizen/cli.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __call__(
9494
"arguments": [
9595
{
9696
"name": "--config",
97-
"help": "specify file path if config file is not in root folder",
97+
"help": "the path of configuration file",
9898
},
9999
{"name": "--debug", "action": "store_true", "help": "use debug mode"},
100100
{

‎commitizen/config/__init__.py‎

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44

55
from commitizen import defaults, git
6-
from commitizen.exceptions import ConfigFileNotFound
6+
from commitizen.exceptions import ConfigFileNotFound, ConfigFileIsEmpty
77

88
from .base_config import BaseConfig
99
from .json_config import JsonConfig
@@ -14,37 +14,23 @@
1414
def read_cfg(filepath: str | None = None) -> BaseConfig:
1515
conf = BaseConfig()
1616

17-
git_project_root = git.find_git_project_root()
18-
1917
if filepath is not None:
20-
given_cfg_path = Path(filepath)
21-
22-
if not given_cfg_path.exists():
18+
if not Path(filepath).exists():
2319
raise ConfigFileNotFound()
2420

25-
with open(given_cfg_path, "rb") as f:
26-
given_cfg_data: bytes = f.read()
27-
28-
given_cfg: TomlConfig | JsonConfig | YAMLConfig
29-
30-
if "toml" in given_cfg_path.suffix:
31-
given_cfg = TomlConfig(data=given_cfg_data, path=given_cfg_path)
32-
elif "json" in given_cfg_path.suffix:
33-
given_cfg = JsonConfig(data=given_cfg_data, path=given_cfg_path)
34-
elif "yaml" in given_cfg_path.suffix:
35-
given_cfg = YAMLConfig(data=given_cfg_data, path=given_cfg_path)
36-
37-
return given_cfg
21+
cfg_paths = (path for path in (Path(filepath),))
22+
else:
23+
git_project_root = git.find_git_project_root()
24+
cfg_search_paths = [Path(".")]
25+
if git_project_root:
26+
cfg_search_paths.append(git_project_root)
3827

39-
cfg_search_paths = [Path(".")]
40-
if git_project_root:
41-
cfg_search_paths.append(git_project_root)
28+
cfg_paths = (
29+
path / Path(filename)
30+
for path in cfg_search_paths
31+
for filename in defaults.config_files
32+
)
4233

43-
cfg_paths = (
44-
path / Path(filename)
45-
for path in cfg_search_paths
46-
for filename in defaults.config_files
47-
)
4834
for filename in cfg_paths:
4935
if not filename.exists():
5036
continue
@@ -61,7 +47,9 @@ def read_cfg(filepath: str | None = None) -> BaseConfig:
6147
elif "yaml" in filename.suffix:
6248
_conf = YAMLConfig(data=data, path=filename)
6349

64-
if _conf.is_empty_config:
50+
if filepath is not None and _conf.is_empty_config:
51+
raise ConfigFileIsEmpty()
52+
elif _conf.is_empty_config:
6553
continue
6654
else:
6755
conf = _conf

‎commitizen/exceptions.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ExitCode(enum.IntEnum):
3535
VERSION_SCHEME_UNKNOWN = 28
3636
CHANGELOG_FORMAT_UNKNOWN = 29
3737
CONFIG_FILE_NOT_FOUND = 30
38+
CONFIG_FILE_IS_EMPTY = 31
3839

3940

4041
class CommitizenException(Exception):
@@ -195,3 +196,8 @@ class ChangelogFormatUnknown(CommitizenException):
195196
class ConfigFileNotFound(CommitizenException):
196197
exit_code = ExitCode.CONFIG_FILE_NOT_FOUND
197198
message = "Cannot found the config file, please check your file path again."
199+
200+
201+
class ConfigFileIsEmpty(CommitizenException):
202+
exit_code = ExitCode.CONFIG_FILE_IS_EMPTY
203+
message = "Config file is empty, please check your file path again."

‎docs/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ For more information about the topic go to https://conventionalcommits.org/
107107

108108
optional arguments:
109109
-h, --help show this help message and exit
110-
--config specify file path if config file is not in root folder
110+
--config the path of configuration file
111111
--debug use debug mode
112112
-n NAME, --name NAME use the given commitizen (default: cz_conventional_commits)
113113
-nr NO_RAISE, --no-raise NO_RAISE

‎tests/test_conf.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import yaml
1010

1111
from commitizen import config, defaults, git
12-
from commitizen.exceptions import InvalidConfigurationError
12+
from commitizen.exceptions import InvalidConfigurationError, ConfigFileIsEmpty
1313

1414
PYPROJECT = """
1515
[tool.commitizen]
@@ -179,15 +179,15 @@ def test_load_empty_pyproject_toml_and_cz_toml_with_config(_, tmpdir):
179179
cfg = config.read_cfg()
180180
assert cfg.settings == _settings
181181

182-
def test_load_pyproject_toml_not_in_root_folder(_, tmpdir):
182+
def test_load_pyproject_toml_from_config_argument(_, tmpdir):
183183
with tmpdir.as_cwd():
184184
_not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml")
185185
_not_root_path.write(PYPROJECT)
186186

187187
cfg = config.read_cfg(filepath="./not_in_root/pyproject.toml")
188188
assert cfg.settings == _settings
189189

190-
def test_load_cz_json_not_in_root_folder(_, tmpdir):
190+
def test_load_cz_json_not_from_config_argument(_, tmpdir):
191191
with tmpdir.as_cwd():
192192
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json")
193193
_not_root_path.write(JSON_STR)
@@ -196,15 +196,23 @@ def test_load_cz_json_not_in_root_folder(_, tmpdir):
196196
json_cfg_by_class = config.JsonConfig(data=JSON_STR, path=_not_root_path)
197197
assert cfg.settings == json_cfg_by_class.settings
198198

199-
def test_load_cz_yaml_not_in_root_folder(_, tmpdir):
199+
def test_load_cz_yaml_not_from_config_argument(_, tmpdir):
200200
with tmpdir.as_cwd():
201201
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml")
202202
_not_root_path.write(YAML_STR)
203203

204204
cfg = config.read_cfg(filepath="./not_in_root/.cz.yaml")
205-
yaml_cfg_by_class = config.YAMLConfig(data=JSON_STR, path=_not_root_path)
205+
yaml_cfg_by_class = config.YAMLConfig(data=YAML_STR, path=_not_root_path)
206206
assert cfg.settings == yaml_cfg_by_class._settings
207207

208+
def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir):
209+
with tmpdir.as_cwd():
210+
_not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml")
211+
_not_root_path.write("")
212+
213+
with pytest.raises(ConfigFileIsEmpty):
214+
config.read_cfg(filepath="./not_in_root/pyproject.toml")
215+
208216

209217
class TestTomlConfig:
210218
def test_init_empty_config_content(self, tmpdir):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /