Skip to content

Commit ec46a4c

Browse files
committed
fix(config): include pyproject.toml in multi config file warning
1 parent 8283081 commit ec46a4c

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

commitizen/config/__init__.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,16 @@ def read_cfg(filepath: str | None = None) -> BaseConfig:
4444
raise ConfigFileIsEmpty()
4545
return conf
4646

47-
config_candidate_paths = _resolve_config_paths()
48-
49-
# Check for multiple config files and warn the user
50-
config_candidates_exclude_pyproject = [
51-
path for path in config_candidate_paths if path.name != "pyproject.toml"
47+
config_candidates = [
48+
(conf, path)
49+
for path in _resolve_config_paths()
50+
if not (conf := _create_config_from_path(path)).is_empty_config
5251
]
5352

54-
for config_candidate_path in config_candidate_paths:
55-
conf = _create_config_from_path(config_candidate_path)
56-
if not conf.is_empty_config:
57-
if len(config_candidates_exclude_pyproject) > 1:
58-
out.warn(
59-
f"Multiple config files detected: {', '.join(map(str, config_candidates_exclude_pyproject))}. "
60-
f"Using config file: '{config_candidate_path}'."
61-
)
62-
return conf
53+
if len(config_candidates) > 1:
54+
out.warn(
55+
f"Multiple config files detected: {', '.join(str(path) for _, path in config_candidates)}. "
56+
f"Using config file: '{config_candidates[0][1]}'."
57+
)
6358

64-
return BaseConfig()
59+
return config_candidates[0][0] if config_candidates else BaseConfig()

tests/test_conf.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,25 @@ def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir):
240240

241241
class TestWarnMultipleConfigFiles:
242242
@pytest.mark.parametrize(
243-
"files,expected_path,should_warn",
243+
"files,expected_path",
244244
[
245245
# Same directory, different file types
246-
([(".cz.toml", PYPROJECT), (".cz.json", JSON_STR)], ".cz.toml", True),
247-
([(".cz.json", JSON_STR), (".cz.yaml", YAML_STR)], ".cz.json", True),
248-
([(".cz.toml", PYPROJECT), (".cz.yaml", YAML_STR)], ".cz.toml", True),
249-
# With pyproject.toml (excluded from warning)
246+
([(".cz.toml", PYPROJECT), (".cz.json", JSON_STR)], ".cz.toml"),
247+
([(".cz.json", JSON_STR), (".cz.yaml", YAML_STR)], ".cz.json"),
248+
([(".cz.toml", PYPROJECT), (".cz.yaml", YAML_STR)], ".cz.toml"),
249+
# With pyproject.toml
250250
(
251251
[("pyproject.toml", PYPROJECT), (".cz.json", JSON_STR)],
252252
".cz.json",
253-
False,
254253
),
255254
(
256255
[("pyproject.toml", PYPROJECT), (".cz.toml", PYPROJECT)],
257256
".cz.toml",
258-
False,
259257
),
260258
],
261259
)
262260
def test_warn_multiple_config_files_same_dir(
263-
_, tmpdir, capsys, files, expected_path, should_warn
261+
_, tmpdir, capsys, files, expected_path
264262
):
265263
"""Test warning when multiple config files exist in same directory."""
266264
with tmpdir.as_cwd():
@@ -270,19 +268,12 @@ def test_warn_multiple_config_files_same_dir(
270268
cfg = config.read_cfg()
271269
captured = capsys.readouterr()
272270

273-
if should_warn:
274-
assert "Multiple config files detected" in captured.err
275-
assert "Using" in captured.err
276-
for filename, _ in files:
277-
if filename != "pyproject.toml":
278-
assert filename in captured.err
279-
else:
280-
assert "Multiple config files detected" not in captured.err
271+
assert "Multiple config files detected" in captured.err
272+
for filename, _ in files:
273+
assert filename in captured.err
274+
assert f"Using config file: '{expected_path}'" in captured.err
281275

282276
assert cfg.path == Path(expected_path)
283-
# Verify config loaded correctly (name and version match expected)
284-
assert cfg.settings["name"] == "cz_jira"
285-
assert cfg.settings["version"] == "1.0.0"
286277

287278
@pytest.mark.parametrize(
288279
"config_file,content",

0 commit comments

Comments
 (0)