|
9 | 9 | from semantic_release.cli.commands.main import main
|
10 | 10 | from semantic_release.cli.config import RawConfig
|
11 | 11 |
|
12 |
| -from tests.const import GENERATE_CONFIG_SUBCMD, MAIN_PROG_NAME |
| 12 | +from tests.const import GENERATE_CONFIG_SUBCMD, MAIN_PROG_NAME, VERSION_SUBCMD |
| 13 | +from tests.fixtures.repos import repo_w_no_tags_angular_commits |
13 | 14 | from tests.util import assert_successful_exit_code
|
14 | 15 |
|
15 | 16 | if TYPE_CHECKING:
|
| 17 | + from pathlib import Path |
16 | 18 | from typing import Any
|
17 | 19 |
|
18 | 20 | from click.testing import CliRunner
|
19 | 21 |
|
| 22 | + from tests.fixtures.example_project import ExProjectDir |
| 23 | + |
20 | 24 |
|
21 | 25 | @pytest.fixture
|
22 | 26 | def raw_config_dict() -> dict[str, Any]:
|
23 | 27 | return RawConfig().model_dump(mode="json", exclude_none=True)
|
24 | 28 |
|
25 | 29 |
|
26 | 30 | @pytest.mark.parametrize("args", [(), ("--format", "toml"), ("--format", "TOML")])
|
| 31 | +@pytest.mark.usefixtures(repo_w_no_tags_angular_commits.__name__) |
27 | 32 | def test_generate_config_toml(
|
28 |
| - cli_runner: CliRunner, args: tuple[str], raw_config_dict: dict[str, Any] |
| 33 | + cli_runner: CliRunner, |
| 34 | + args: tuple[str], |
| 35 | + raw_config_dict: dict[str, Any], |
| 36 | + example_project_dir: ExProjectDir, |
29 | 37 | ):
|
| 38 | + # Setup: Generate the expected configuration as a TOML string |
30 | 39 | expected_config_as_str = tomlkit.dumps(
|
31 | 40 | {"semantic_release": raw_config_dict}
|
32 | 41 | ).strip()
|
33 | 42 |
|
| 43 | + # Act: Print the generated configuration to stdout |
34 | 44 | cli_cmd = [MAIN_PROG_NAME, GENERATE_CONFIG_SUBCMD, *args]
|
35 |
| - |
36 | 45 | result = cli_runner.invoke(main, cli_cmd[1:])
|
37 | 46 |
|
| 47 | + # Evaluate: Check that the command ran successfully and that the output matches the expected configuration |
38 | 48 | assert_successful_exit_code(result, cli_cmd)
|
39 | 49 | assert expected_config_as_str == result.output.strip()
|
40 | 50 |
|
| 51 | + # Setup: Write the generated configuration to a file |
| 52 | + config_file = "releaserc.toml" |
| 53 | + example_project_dir.joinpath(config_file).write_text(result.output) |
| 54 | + |
| 55 | + # Act: Validate that the generated config is a valid configuration for PSR |
| 56 | + cli_cmd = [ |
| 57 | + MAIN_PROG_NAME, |
| 58 | + "--noop", |
| 59 | + "--strict", |
| 60 | + "-c", |
| 61 | + config_file, |
| 62 | + VERSION_SUBCMD, |
| 63 | + "--print", |
| 64 | + ] |
| 65 | + result = cli_runner.invoke(main, cli_cmd[1:]) |
| 66 | + |
| 67 | + # Evaluate: Check that the version command in noop mode ran successfully |
| 68 | + # which means PSR loaded the configuration successfully |
| 69 | + assert_successful_exit_code(result, cli_cmd) |
| 70 | + |
41 | 71 |
|
42 | 72 | @pytest.mark.parametrize("args", [("--format", "json"), ("--format", "JSON")])
|
| 73 | +@pytest.mark.usefixtures(repo_w_no_tags_angular_commits.__name__) |
43 | 74 | def test_generate_config_json(
|
44 |
| - cli_runner: CliRunner, args: tuple[str], raw_config_dict: dict[str, Any] |
| 75 | + cli_runner: CliRunner, |
| 76 | + args: tuple[str], |
| 77 | + raw_config_dict: dict[str, Any], |
| 78 | + example_project_dir: ExProjectDir, |
45 | 79 | ):
|
| 80 | + # Setup: Generate the expected configuration as a JSON string |
46 | 81 | expected_config_as_str = json.dumps(
|
47 | 82 | {"semantic_release": raw_config_dict}, indent=4
|
48 | 83 | ).strip()
|
49 | 84 |
|
| 85 | + # Act: Print the generated configuration to stdout |
50 | 86 | cli_cmd = [MAIN_PROG_NAME, GENERATE_CONFIG_SUBCMD, *args]
|
51 |
| - |
52 | 87 | result = cli_runner.invoke(main, cli_cmd[1:])
|
53 | 88 |
|
| 89 | + # Evaluate: Check that the command ran successfully and that the output matches the expected configuration |
54 | 90 | assert_successful_exit_code(result, cli_cmd)
|
55 | 91 | assert expected_config_as_str == result.output.strip()
|
56 | 92 |
|
| 93 | + # Setup: Write the generated configuration to a file |
| 94 | + config_file = "releaserc.json" |
| 95 | + example_project_dir.joinpath(config_file).write_text(result.output) |
57 | 96 |
|
| 97 | + # Act: Validate that the generated config is a valid configuration for PSR |
| 98 | + cli_cmd = [ |
| 99 | + MAIN_PROG_NAME, |
| 100 | + "--noop", |
| 101 | + "--strict", |
| 102 | + "-c", |
| 103 | + config_file, |
| 104 | + VERSION_SUBCMD, |
| 105 | + "--print", |
| 106 | + ] |
| 107 | + result = cli_runner.invoke(main, cli_cmd[1:]) |
| 108 | + |
| 109 | + # Evaluate: Check that the version command in noop mode ran successfully |
| 110 | + # which means PSR loaded the configuration successfully |
| 111 | + assert_successful_exit_code(result, cli_cmd) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.usefixtures(repo_w_no_tags_angular_commits.__name__) |
58 | 115 | def test_generate_config_pyproject_toml(
|
59 |
| - cli_runner: CliRunner, raw_config_dict: dict[str, Any] |
| 116 | + cli_runner: CliRunner, |
| 117 | + raw_config_dict: dict[str, Any], |
| 118 | + example_pyproject_toml: Path, |
60 | 119 | ):
|
| 120 | + # Setup: Generate the expected configuration as a TOML string according to PEP 518 |
61 | 121 | expected_config_as_str = tomlkit.dumps(
|
62 | 122 | {"tool": {"semantic_release": raw_config_dict}}
|
63 | 123 | ).strip()
|
64 | 124 |
|
| 125 | + # Setup: Remove any current configuration from pyproject.toml |
| 126 | + pyproject_config = tomlkit.loads(example_pyproject_toml.read_text(encoding="utf-8")) |
| 127 | + pyproject_config.get("tool", {}).pop("semantic_release", None) |
| 128 | + example_pyproject_toml.write_text(tomlkit.dumps(pyproject_config)) |
| 129 | + |
| 130 | + # Act: Print the generated configuration to stdout |
65 | 131 | cli_cmd = [
|
66 | 132 | MAIN_PROG_NAME,
|
67 | 133 | GENERATE_CONFIG_SUBCMD,
|
68 | 134 | "--format",
|
69 | 135 | "toml",
|
70 | 136 | "--pyproject",
|
71 | 137 | ]
|
72 |
| - |
73 | 138 | result = cli_runner.invoke(main, cli_cmd[1:])
|
74 | 139 |
|
| 140 | + # Evaluate: Check that the command ran successfully and that the output matches the expected configuration |
75 | 141 | assert_successful_exit_code(result, cli_cmd)
|
76 | 142 | assert expected_config_as_str == result.output.strip()
|
| 143 | + |
| 144 | + # Setup: Write the generated configuration to a file |
| 145 | + example_pyproject_toml.write_text( |
| 146 | + str.join( |
| 147 | + "\n\n", |
| 148 | + [ |
| 149 | + example_pyproject_toml.read_text(encoding="utf-8").strip(), |
| 150 | + result.output, |
| 151 | + ], |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + # Act: Validate that the generated config is a valid configuration for PSR |
| 156 | + cli_cmd = [MAIN_PROG_NAME, "--noop", "--strict", VERSION_SUBCMD, "--print"] |
| 157 | + result = cli_runner.invoke(main, cli_cmd[1:]) |
| 158 | + |
| 159 | + # Evaluate: Check that the version command in noop mode ran successfully |
| 160 | + # which means PSR loaded the configuration successfully |
| 161 | + assert_successful_exit_code(result, cli_cmd) |
0 commit comments