Skip to content
Open
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
52 changes: 23 additions & 29 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,26 +370,23 @@ def test_no_warn_with_single_config_file(


@pytest.mark.parametrize(
"config_file, exception_string",
"config_file",
[
(".cz.toml", r"\.cz\.toml"),
("cz.toml", r"cz\.toml"),
("pyproject.toml", r"pyproject\.toml"),
".cz.toml",
"cz.toml",
"pyproject.toml",
],
ids=[".cz.toml", "cz.toml", "pyproject.toml"],
)
class TestTomlConfig:
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
def test_init_empty_config_content(self, tmpdir, config_file):
path = tmpdir.mkdir("commitizen").join(config_file)
toml_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, config_file, exception_string
):
def test_init_empty_config_content_with_existing_content(self, tmpdir, config_file):
existing_content = "[tool.black]\nline-length = 88\n"

path = tmpdir.mkdir("commitizen").join(config_file)
Expand All @@ -400,63 +397,60 @@ def test_init_empty_config_content_with_existing_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, config_file, exception_string
):
def test_init_with_invalid_config_content(self, tmpdir, config_file):
existing_content = "invalid toml content"
path = tmpdir.mkdir("commitizen").join(config_file)

with pytest.raises(InvalidConfigurationError, match=exception_string):
with pytest.raises(InvalidConfigurationError) as excinfo:
TomlConfig(data=existing_content, path=path)
assert config_file in str(excinfo.value)


@pytest.mark.parametrize(
"config_file, exception_string",
"config_file",
[
(".cz.json", r"\.cz\.json"),
("cz.json", r"cz\.json"),
".cz.json",
"cz.json",
],
ids=[".cz.json", "cz.json"],
)
class TestJsonConfig:
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
def test_init_empty_config_content(self, tmpdir, config_file):
path = tmpdir.mkdir("commitizen").join(config_file)
json_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, config_file, exception_string
):
def test_init_with_invalid_config_content(self, tmpdir, config_file):
existing_content = "invalid json content"
path = tmpdir.mkdir("commitizen").join(config_file)

with pytest.raises(InvalidConfigurationError, match=exception_string):
with pytest.raises(InvalidConfigurationError) as excinfo:
JsonConfig(data=existing_content, path=path)
assert config_file in str(excinfo.value)


@pytest.mark.parametrize(
"config_file, exception_string",
"config_file",
[
(".cz.yaml", r"\.cz\.yaml"),
("cz.yaml", r"cz\.yaml"),
".cz.yaml",
"cz.yaml",
],
ids=[".cz.yaml", "cz.yaml"],
)
class TestYamlConfig:
def test_init_empty_config_content(self, tmpdir, config_file, exception_string):
def test_init_empty_config_content(self, tmpdir, config_file):
path = tmpdir.mkdir("commitizen").join(config_file)
yaml_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, config_file, exception_string):
def test_init_with_invalid_content(self, tmpdir, config_file):
existing_content = "invalid: .cz.yaml: content: maybe?"
path = tmpdir.mkdir("commitizen").join(config_file)

with pytest.raises(InvalidConfigurationError, match=exception_string):
with pytest.raises(InvalidConfigurationError) as excinfo:
YAMLConfig(data=existing_content, path=path)
assert config_file in str(excinfo.value)