|
112 | 112 | "extras": {}, |
113 | 113 | "breaking_change_exclamation_in_title": False, |
114 | 114 | "message_length_limit": 0, |
| 115 | + "strict_config": False, |
115 | 116 | } |
116 | 117 |
|
117 | 118 | _new_settings: dict[str, Any] = { |
|
152 | 153 | "extras": {}, |
153 | 154 | "breaking_change_exclamation_in_title": False, |
154 | 155 | "message_length_limit": 0, |
| 156 | + "strict_config": False, |
155 | 157 | } |
156 | 158 |
|
157 | 159 |
|
@@ -497,3 +499,125 @@ def test_init_with_invalid_content(self, tmp_path, config_file): |
497 | 499 | with pytest.raises(InvalidConfigurationError) as excinfo: |
498 | 500 | YAMLConfig(data=existing_content, path=path) |
499 | 501 | assert config_file in str(excinfo.value) |
| 502 | + |
| 503 | + |
| 504 | +class TestUnknownConfigKeys: |
| 505 | + """Validate handling of unknown keys in the commitizen section.""" |
| 506 | + |
| 507 | + @pytest.mark.parametrize( |
| 508 | + ("config_file", "content_template"), |
| 509 | + [ |
| 510 | + ( |
| 511 | + "pyproject.toml", |
| 512 | + '[tool.commitizen]\nname = "cz_conventional_commits"\n{extra}\n', |
| 513 | + ), |
| 514 | + ( |
| 515 | + ".cz.toml", |
| 516 | + '[tool.commitizen]\nname = "cz_conventional_commits"\n{extra}\n', |
| 517 | + ), |
| 518 | + ( |
| 519 | + ".cz.json", |
| 520 | + '{{"commitizen": {{"name": "cz_conventional_commits"{extra}}}}}', |
| 521 | + ), |
| 522 | + ( |
| 523 | + ".cz.yaml", |
| 524 | + "commitizen:\n name: cz_conventional_commits\n{extra}\n", |
| 525 | + ), |
| 526 | + ], |
| 527 | + ) |
| 528 | + def test_warns_on_unknown_keys_by_default( |
| 529 | + self, tmp_path, monkeypatch, capsys, config_file, content_template |
| 530 | + ): |
| 531 | + monkeypatch.chdir(tmp_path) |
| 532 | + if config_file == ".cz.json": |
| 533 | + extra = ', "update_changelog_on_bumb": true, "another_typo": 1' |
| 534 | + elif config_file == ".cz.yaml": |
| 535 | + extra = " update_changelog_on_bumb: true\n another_typo: 1" |
| 536 | + else: |
| 537 | + extra = "update_changelog_on_bumb = true\nanother_typo = 1" |
| 538 | + (tmp_path / config_file).write_text(content_template.format(extra=extra)) |
| 539 | + |
| 540 | + cfg = config.read_cfg() |
| 541 | + captured = capsys.readouterr() |
| 542 | + |
| 543 | + assert "Unknown commitizen configuration keys" in captured.err |
| 544 | + assert "'another_typo'" in captured.err |
| 545 | + assert "'update_changelog_on_bumb'" in captured.err |
| 546 | + # The unknown keys are still loaded into settings (back-compat) but flagged. |
| 547 | + assert cfg.settings["name"] == "cz_conventional_commits" |
| 548 | + |
| 549 | + @pytest.mark.parametrize( |
| 550 | + ("config_file", "content"), |
| 551 | + [ |
| 552 | + ( |
| 553 | + "pyproject.toml", |
| 554 | + "[tool.commitizen]\n" |
| 555 | + 'name = "cz_conventional_commits"\n' |
| 556 | + "strict_config = true\n" |
| 557 | + "update_changelog_on_bumb = true\n", |
| 558 | + ), |
| 559 | + ( |
| 560 | + ".cz.json", |
| 561 | + json.dumps( |
| 562 | + { |
| 563 | + "commitizen": { |
| 564 | + "name": "cz_conventional_commits", |
| 565 | + "strict_config": True, |
| 566 | + "update_changelog_on_bumb": True, |
| 567 | + } |
| 568 | + } |
| 569 | + ), |
| 570 | + ), |
| 571 | + ( |
| 572 | + ".cz.yaml", |
| 573 | + "commitizen:\n" |
| 574 | + " name: cz_conventional_commits\n" |
| 575 | + " strict_config: true\n" |
| 576 | + " update_changelog_on_bumb: true\n", |
| 577 | + ), |
| 578 | + ], |
| 579 | + ) |
| 580 | + def test_raises_on_unknown_keys_when_strict( |
| 581 | + self, tmp_path, monkeypatch, config_file, content |
| 582 | + ): |
| 583 | + monkeypatch.chdir(tmp_path) |
| 584 | + (tmp_path / config_file).write_text(content) |
| 585 | + |
| 586 | + with pytest.raises(InvalidConfigurationError) as excinfo: |
| 587 | + config.read_cfg() |
| 588 | + assert "update_changelog_on_bumb" in str(excinfo.value) |
| 589 | + |
| 590 | + @pytest.mark.parametrize( |
| 591 | + ("config_file", "content_template"), |
| 592 | + [ |
| 593 | + ( |
| 594 | + "pyproject.toml", |
| 595 | + '[tool.commitizen]\nname = "cz_conventional_commits"\n{extra}', |
| 596 | + ), |
| 597 | + ( |
| 598 | + ".cz.json", |
| 599 | + '{{"commitizen": {{"name": "cz_conventional_commits"{extra}}}}}', |
| 600 | + ), |
| 601 | + ( |
| 602 | + ".cz.yaml", |
| 603 | + "commitizen:\n name: cz_conventional_commits\n{extra}", |
| 604 | + ), |
| 605 | + ], |
| 606 | + ) |
| 607 | + def test_no_warning_for_known_keys( |
| 608 | + self, tmp_path, monkeypatch, capsys, config_file, content_template |
| 609 | + ): |
| 610 | + monkeypatch.chdir(tmp_path) |
| 611 | + if config_file == ".cz.json": |
| 612 | + extra = ', "update_changelog_on_bump": true' |
| 613 | + elif config_file == ".cz.yaml": |
| 614 | + extra = " update_changelog_on_bump: true" |
| 615 | + else: |
| 616 | + extra = "update_changelog_on_bump = true" |
| 617 | + |
| 618 | + (tmp_path / config_file).write_text(content_template.format(extra=extra)) |
| 619 | + |
| 620 | + config.read_cfg() |
| 621 | + captured = capsys.readouterr() |
| 622 | + |
| 623 | + assert "Unknown commitizen configuration" not in captured.err |
0 commit comments