88import pytest
99import yaml
1010
11- from commitizen import config , defaults , git
11+ from commitizen import cmd , config , defaults , git
1212from commitizen .config .json_config import JsonConfig
1313from commitizen .config .toml_config import TomlConfig
1414from commitizen .config .yaml_config import YAMLConfig
@@ -172,9 +172,7 @@ def test_find_git_project_root(tmpdir):
172172 assert git .find_git_project_root () is None
173173
174174
175- @pytest .mark .parametrize (
176- "config_files_manager" , defaults .CONFIG_FILES .copy (), indirect = True
177- )
175+ @pytest .mark .parametrize ("config_files_manager" , defaults .CONFIG_FILES , indirect = True )
178176def test_set_key (config_files_manager ):
179177 _conf = config .read_cfg ()
180178 _conf .set_key ("version" , "2.0.0" )
@@ -184,7 +182,7 @@ def test_set_key(config_files_manager):
184182
185183class TestReadCfg :
186184 @pytest .mark .parametrize (
187- "config_files_manager" , defaults .CONFIG_FILES . copy () , indirect = True
185+ "config_files_manager" , defaults .CONFIG_FILES , indirect = True
188186 )
189187 def test_load_conf (_ , config_files_manager ):
190188 cfg = config .read_cfg ()
@@ -239,71 +237,85 @@ def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir):
239237 with pytest .raises (ConfigFileIsEmpty ):
240238 config .read_cfg (filepath = "./not_in_root/pyproject.toml" )
241239
242- def test_warn_multiple_config_files (_ , tmpdir , capsys ):
243- """Test that a warning is issued when multiple config files exist."""
244- with tmpdir .as_cwd ():
245- # Create multiple config files
246- tmpdir .join (".cz.toml" ).write (PYPROJECT )
247- tmpdir .join (".cz.json" ).write (JSON_STR )
248240
249- # Read config
250- cfg = config .read_cfg ()
251-
252- # Check that the warning was issued
253- captured = capsys .readouterr ()
254- assert "Multiple config files detected" in captured .err
255- assert ".cz.toml" in captured .err
256- assert ".cz.json" in captured .err
257- assert "Using" in captured .err
258-
259- # Verify the correct config is loaded (first in priority order)
260- assert cfg .settings == _settings
261-
262- def test_warn_multiple_config_files_with_pyproject (_ , tmpdir , capsys ):
263- """Test warning excludes pyproject.toml from the warning message."""
241+ class TestWarnMultipleConfigFiles :
242+ @pytest .mark .parametrize (
243+ "files,expected_path,should_warn" ,
244+ [
245+ # 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)
250+ (
251+ [("pyproject.toml" , PYPROJECT ), (".cz.json" , JSON_STR )],
252+ ".cz.json" ,
253+ False ,
254+ ),
255+ (
256+ [("pyproject.toml" , PYPROJECT ), (".cz.toml" , PYPROJECT )],
257+ ".cz.toml" ,
258+ False ,
259+ ),
260+ ],
261+ )
262+ def test_warn_multiple_config_files_same_dir (
263+ _ , tmpdir , capsys , files , expected_path , should_warn
264+ ):
265+ """Test warning when multiple config files exist in same directory."""
264266 with tmpdir .as_cwd ():
265- # Create multiple config files including pyproject.toml
266- tmpdir .join ("pyproject.toml" ).write (PYPROJECT )
267- tmpdir .join (".cz.json" ).write (JSON_STR )
267+ for filename , content in files :
268+ tmpdir .join (filename ).write (content )
268269
269- # Read config - should use pyproject.toml (first in priority)
270270 cfg = config .read_cfg ()
271-
272- # No warning should be issued as only one non-pyproject config exists
273271 captured = capsys .readouterr ()
274- assert "Multiple config files detected" not in captured .err
275272
276- # Verify the correct config is loaded
277- assert cfg .settings == _settings
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
281+
282+ 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"
278286
279- def test_warn_multiple_config_files_uses_correct_one (_ , tmpdir , capsys ):
280- """Test that the correct config file is used when multiple exist."""
287+ @pytest .mark .parametrize (
288+ "config_file,content" ,
289+ [
290+ (".cz.json" , JSON_STR ),
291+ (".cz.toml" , PYPROJECT ),
292+ (".cz.yaml" , YAML_STR ),
293+ ("cz.toml" , PYPROJECT ),
294+ ("cz.json" , JSON_STR ),
295+ ("cz.yaml" , YAML_STR ),
296+ ],
297+ )
298+ def test_warn_same_filename_different_directories_with_git (
299+ _ , tmpdir , capsys , config_file , content
300+ ):
301+ """Test warning when same config filename exists in the current directory and in the git root."""
281302 with tmpdir .as_cwd ():
282- # Create .cz.json with different settings
283- json_different = """
284- {
285- "commitizen": {
286- "name": "cz_conventional_commits",
287- "version": "2.0.0"
288- }
289- }
290- """
291- tmpdir .join (".cz.json" ).write (json_different )
292- tmpdir .join (".cz.toml" ).write (PYPROJECT )
303+ cmd .run ("git init" )
293304
294- # Read config - should use pyproject.toml (first in defaults.CONFIG_FILES)
295- # But since pyproject.toml doesn't exist, .cz.toml is second in priority
296- cfg = config .read_cfg ()
305+ # Create config in git root
306+ tmpdir .join (config_file ).write (content )
297307
298- # Check that warning mentions both files
299- captured = capsys .readouterr ()
300- assert "Multiple config files detected" in captured .err
301- assert ".cz.toml" in captured .err
302- assert ".cz.json" in captured .err
308+ # Create same filename in subdirectory
309+ subdir = tmpdir .mkdir ("subdir" )
310+ subdir .join (config_file ).write (content )
303311
304- # Verify .cz.toml was used (second in priority after pyproject.toml)
305- assert cfg .settings ["name" ] == "cz_jira" # from PYPROJECT
306- assert cfg .settings ["version" ] == "1.0.0"
312+ with subdir .as_cwd ():
313+ cfg = config .read_cfg ()
314+ captured = capsys .readouterr ()
315+
316+ assert "Multiple config files detected" in captured .err
317+ assert f"Using config file: '{ config_file } '" in captured .err
318+ assert cfg .path == Path (config_file )
307319
308320 def test_no_warn_with_explicit_config_path (_ , tmpdir , capsys ):
309321 """Test that no warning is issued when user explicitly specifies config."""
@@ -323,6 +335,39 @@ def test_no_warn_with_explicit_config_path(_, tmpdir, capsys):
323335 json_cfg_expected = JsonConfig (data = JSON_STR , path = Path (".cz.json" ))
324336 assert cfg .settings == json_cfg_expected .settings
325337
338+ @pytest .mark .parametrize (
339+ "config_file, content, with_git" ,
340+ [
341+ (file , content , with_git )
342+ for file , content in [
343+ (".cz.toml" , PYPROJECT ),
344+ (".cz.json" , JSON_STR ),
345+ (".cz.yaml" , YAML_STR ),
346+ ("pyproject.toml" , PYPROJECT ),
347+ ("cz.toml" , PYPROJECT ),
348+ ("cz.json" , JSON_STR ),
349+ ("cz.yaml" , YAML_STR ),
350+ ]
351+ for with_git in [True , False ]
352+ ],
353+ )
354+ def test_no_warn_with_single_config_file (
355+ _ , tmpdir , capsys , config_file , content , with_git
356+ ):
357+ """Test that no warning is issued when user explicitly specifies config."""
358+ with tmpdir .as_cwd ():
359+ if with_git :
360+ cmd .run ("git init" )
361+
362+ tmpdir .join (config_file ).write (content )
363+
364+ cfg = config .read_cfg ()
365+ captured = capsys .readouterr ()
366+
367+ # No warning should be issued
368+ assert "Multiple config files detected" not in captured .err
369+ assert cfg .path == Path (config_file )
370+
326371
327372@pytest .mark .parametrize (
328373 "config_file, exception_string" ,
0 commit comments