Skip to content

Commit 083bf2d

Browse files
fix(customize): resolve info_path from config directory
Assisted By: AI
1 parent 0839f9a commit 083bf2d

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎commitizen/cz/customize/customize.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,8 @@ def schema(self) -> str:
100100

101101
def info(self) -> str:
102102
if info_path := self.custom_settings.get("info_path"):
103-
return Path(info_path).read_text(encoding=self.config.settings["encoding"])
103+
path = Path(info_path)
104+
if not path.is_absolute() and self.config.path is not None:
105+
path = self.config.path.parent / path
106+
return path.read_text(encoding=self.config.settings["encoding"])
104107
return self.custom_settings.get("info") or ""

‎docs/customization/config_file.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Example:
158158
| `example` | `str` | `""` | (OPTIONAL) Provide an example to help understand the style. Used by `cz example`. |
159159
| `schema` | `str` | `""` | (OPTIONAL) Show the schema used. Used by `cz schema`. |
160160
| `schema_pattern` | `str` | `""` | (OPTIONAL) The regular expression used to do commit message validation. Used by `cz check`. |
161-
| `info_path` | `str` | `""` | (OPTIONAL) The path to the file that contains explanation of the commit rules. Used by `cz info`. If not provided `cz info`, will load `info` instead. |
161+
| `info_path` | `str` | `""` | (OPTIONAL) The path to the file that contains explanation of the commit rules. Relative paths are resolved from the configuration file directory. Used by `cz info`. If not provided `cz info`, will load `info` instead. |
162162
| `info` | `str` | `""` | (OPTIONAL) Explanation of the commit rules. Used by `cz info`. |
163163
| `bump_map` | `dict` | `None` | (OPTIONAL) Dictionary mapping the extracted information to a `SemVer` increment type (`MAJOR`, `MINOR`, `PATCH`). In JSON and YAML configs, `null` keeps a matched rule from bumping the version. |
164164
| `bump_pattern` | `str` | `None` | (OPTIONAL) Regex to extract information from commit (subject and body) |

‎tests/test_cz_customize.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,24 @@ def test_info_with_info_path(tmp_path, monkeypatch, config_info):
655655
assert "Test info" in cz.info()
656656

657657

658+
def test_info_with_info_path_outside_cwd(tmp_path, monkeypatch, config_info):
659+
"""Regression test for resolving info_path from the config file directory."""
660+
# Arrange
661+
config_dir = tmp_path / "project"
662+
config_dir.mkdir()
663+
(config_dir / "info.txt").write_text("Test info")
664+
config_info.path = config_dir / config_info.path.name
665+
work_dir = tmp_path / "subdirectory"
666+
work_dir.mkdir()
667+
monkeypatch.chdir(work_dir)
668+
669+
# Act
670+
info = CustomizeCommitsCz(config_info).info()
671+
672+
# Assert
673+
assert "Test info" in info
674+
675+
658676
def test_info_without_info(config_without_info):
659677
cz = CustomizeCommitsCz(config_without_info)
660678
assert cz.info() == ""

0 commit comments

Comments
 (0)