Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config_files): add suport for "cz.toml" config file #1130

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Settings(TypedDict, total=False):
"cz.json",
".cz.yaml",
"cz.yaml",
"cz.toml",
]
encoding: str = "utf-8"

Expand Down
4 changes: 2 additions & 2 deletions docs/commands/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ regarding if the file is present or not in `version_files`.

Some examples

`pyproject.toml` or `.cz.toml`
`pyproject.toml`, `.cz.toml` or `cz.toml`

```toml
[tool.commitizen]
Expand Down Expand Up @@ -441,7 +441,7 @@ defaults to: `bump: version $current_version → $new_version`

Some examples

`pyproject.toml` or `.cz.toml`
`pyproject.toml`, `.cz.toml` or `cz.toml`

```toml
[tool.commitizen]
Expand Down
6 changes: 3 additions & 3 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ Provide extra variables to the changelog template. [Read more][template-customiz

## Configuration file

### pyproject.toml or .cz.toml
### pyproject.toml, .cz.toml or cz.toml

Default and recommended configuration format for a project.
For a **python** project, we recommend adding an entry to your `pyproject.toml`.
You can also create a `.cz.toml` file at the root of your project folder.
You can also create a `.cz.toml` or `cz.toml` file at the root of your project folder.

Example configuration:

Expand Down Expand Up @@ -339,7 +339,7 @@ Commitizen provides some version providers for some well known formats:
!!! note
The `scm` provider is meant to be used with `setuptools-scm` or any packager `*-scm` plugin.

An example in your `.cz.toml` would look like this:
An example in your `.cz.toml` or `cz.toml` would look like this:

```toml
[tool.commitizen]
Expand Down
2 changes: 1 addition & 1 deletion docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ They differ a bit in design, not sure if cz-js does any of this, but these are s
- create custom rules, version bumps and changelog generation, by default we use the popular conventional commits (I think cz-js allows this).
- single package, install one thing and it will work (cz-js is a monorepo, but you have to install different dependencies AFAIK)
- pre-commit integration
- works on any language project, as long as you create the `.cz.toml` file.
- works on any language project, as long as you create the `.cz.toml` or `cz.toml` file.

Where do they cross paths?

Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The assistant utility will help you set up everything
cz init
```

Alternatively, create a file `.cz.toml` in your project's directory.
Alternatively, create a file `.cz.toml` or `cz.toml` in your project's directory.

```toml
[tool.commitizen]
Expand Down
65 changes: 48 additions & 17 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,63 +214,94 @@ def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir):
config.read_cfg(filepath="./not_in_root/pyproject.toml")


@pytest.mark.parametrize(
"config_file, exception_string",
[
(".cz.toml", r"\.cz\.toml"),
("cz.toml", r"cz\.toml"),
("pyproject.toml", r"pyproject\.toml"),
],
ids=[".cz.toml", "cz.toml", "pyproject.toml"],
)
class TestTomlConfig:
def test_init_empty_config_content(self, tmpdir):
path = tmpdir.mkdir("commitizen").join(".cz.toml")
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
path = tmpdir.mkdir("commitizen").join(config_file)
toml_config = config.TomlConfig(data="", path=path)
toml_config.init_empty_config_content()

with open(path, encoding="utf-8") as toml_file:
assert toml_file.read() == "[tool.commitizen]\n"

def test_init_empty_config_content_with_existing_content(self, tmpdir):
def test_init_empty_config_content_with_existing_content(
self, tmpdir, config_file, exception_string
):
existing_content = "[tool.black]\n" "line-length = 88\n"

path = tmpdir.mkdir("commitizen").join(".cz.toml")
path = tmpdir.mkdir("commitizen").join(config_file)
path.write(existing_content)
toml_config = config.TomlConfig(data="", path=path)
toml_config.init_empty_config_content()

with open(path, encoding="utf-8") as toml_file:
assert toml_file.read() == existing_content + "\n[tool.commitizen]\n"

def test_init_with_invalid_config_content(self, tmpdir):
def test_init_with_invalid_config_content(
self, tmpdir, config_file, exception_string
):
existing_content = "invalid toml content"
path = tmpdir.mkdir("commitizen").join(".cz.toml")
path = tmpdir.mkdir("commitizen").join(config_file)

with pytest.raises(InvalidConfigurationError, match=r"\.cz\.toml"):
with pytest.raises(InvalidConfigurationError, match=exception_string):
config.TomlConfig(data=existing_content, path=path)


@pytest.mark.parametrize(
"config_file, exception_string",
[
(".cz.json", r"\.cz\.json"),
("cz.json", r"cz\.json"),
],
ids=[".cz.json", "cz.json"],
)
class TestJsonConfig:
def test_init_empty_config_content(self, tmpdir):
path = tmpdir.mkdir("commitizen").join(".cz.json")
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
path = tmpdir.mkdir("commitizen").join(config_file)
json_config = config.JsonConfig(data="{}", path=path)
json_config.init_empty_config_content()

with open(path, encoding="utf-8") as json_file:
assert json.load(json_file) == {"commitizen": {}}

def test_init_with_invalid_config_content(self, tmpdir):
def test_init_with_invalid_config_content(
self, tmpdir, config_file, exception_string
):
existing_content = "invalid json content"
path = tmpdir.mkdir("commitizen").join(".cz.json")
path = tmpdir.mkdir("commitizen").join(config_file)

with pytest.raises(InvalidConfigurationError, match=r"\.cz\.json"):
with pytest.raises(InvalidConfigurationError, match=exception_string):
config.JsonConfig(data=existing_content, path=path)


@pytest.mark.parametrize(
"config_file, exception_string",
[
(".cz.yaml", r"\.cz\.yaml"),
("cz.yaml", r"cz\.yaml"),
],
ids=[".cz.yaml", "cz.yaml"],
)
class TestYamlConfig:
def test_init_empty_config_content(self, tmpdir):
path = tmpdir.mkdir("commitizen").join(".cz.yaml")
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
path = tmpdir.mkdir("commitizen").join(config_file)
yaml_config = config.YAMLConfig(data="{}", path=path)
yaml_config.init_empty_config_content()

with open(path) as yaml_file:
assert yaml.safe_load(yaml_file) == {"commitizen": {}}

def test_init_with_invalid_content(self, tmpdir):
def test_init_with_invalid_content(self, tmpdir, config_file, exception_string):
existing_content = "invalid: .cz.yaml: content: maybe?"
path = tmpdir.mkdir("commitizen").join(".cz.yaml")
path = tmpdir.mkdir("commitizen").join(config_file)

with pytest.raises(InvalidConfigurationError, match=r"\.cz\.yaml"):
with pytest.raises(InvalidConfigurationError, match=exception_string):
config.YAMLConfig(data=existing_content, path=path)
Loading