File: test_config.py

package info (click to toggle)
python-semantic-release 10.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 3,112 kB
  • sloc: python: 36,523; sh: 340; makefile: 156
file content (465 lines) | stat: -rw-r--r-- 14,726 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from __future__ import annotations

import os
import shutil
import sys
from pathlib import Path, PurePosixPath
from re import compile as regexp
from typing import TYPE_CHECKING
from unittest import mock

import pytest
import tomlkit
from pydantic import RootModel, ValidationError
from urllib3.util.url import parse_url

import semantic_release
from semantic_release.cli.config import (
    BranchConfig,
    ChangelogConfig,
    ChangelogOutputFormat,
    GlobalCommandLineOptions,
    HvcsClient,
    RawConfig,
    RuntimeContext,
    _known_hvcs,
)
from semantic_release.cli.util import load_raw_config_file
from semantic_release.commit_parser.conventional import ConventionalCommitParserOptions
from semantic_release.commit_parser.emoji import EmojiParserOptions
from semantic_release.commit_parser.scipy import ScipyParserOptions
from semantic_release.commit_parser.tag import TagParserOptions
from semantic_release.const import DEFAULT_COMMIT_AUTHOR
from semantic_release.enums import LevelBump
from semantic_release.errors import ParserLoadError

from tests.fixtures.repos import repo_w_no_tags_conventional_commits
from tests.util import (
    CustomParserOpts,
    CustomParserWithNoOpts,
    CustomParserWithOpts,
    IncompleteCustomParser,
)

if TYPE_CHECKING:
    from typing import Any

    from tests.fixtures.example_project import ExProjectDir, UpdatePyprojectTomlFn
    from tests.fixtures.git_repo import BuildRepoFn, BuiltRepoResult, CommitConvention


@pytest.mark.parametrize(
    "patched_os_environ, remote_config, expected_token",
    [
        (
            {"GH_TOKEN": "mytoken"},
            {"type": HvcsClient.GITHUB.value},
            "mytoken",
        ),
        (
            {"GITLAB_TOKEN": "mytoken"},
            {"type": HvcsClient.GITLAB.value},
            "mytoken",
        ),
        (
            {"GITEA_TOKEN": "mytoken"},
            {"type": HvcsClient.GITEA.value},
            "mytoken",
        ),
        (
            # default not provided -> means Github
            {"GH_TOKEN": "mytoken"},
            {},
            "mytoken",
        ),
        (
            {"CUSTOM_TOKEN": "mytoken"},
            {"type": HvcsClient.GITHUB.value, "token": {"env": "CUSTOM_TOKEN"}},
            "mytoken",
        ),
    ],
)
def test_load_hvcs_default_token(
    patched_os_environ: dict[str, str],
    remote_config: dict[str, Any],
    expected_token: str,
):
    with mock.patch.dict(os.environ, patched_os_environ, clear=True):
        raw_config = RawConfig.model_validate(
            {
                "remote": remote_config,
            }
        )

    assert expected_token == raw_config.remote.token


@pytest.mark.parametrize("remote_config", [{"type": "nonexistent"}])
def test_invalid_hvcs_type(remote_config: dict[str, Any]):
    with pytest.raises(ValidationError) as excinfo:
        RawConfig.model_validate(
            {
                "remote": remote_config,
            }
        )
    assert "remote.type" in str(excinfo.value)


@pytest.mark.parametrize(
    "commit_parser, expected_parser_opts",
    [
        (
            None,
            RootModel(ConventionalCommitParserOptions()).model_dump(),
        ),  # default not provided -> means conventional
        ("conventional", RootModel(ConventionalCommitParserOptions()).model_dump()),
        ("emoji", RootModel(EmojiParserOptions()).model_dump()),
        ("scipy", RootModel(ScipyParserOptions()).model_dump()),
        ("tag", RootModel(TagParserOptions()).model_dump()),
        (f"{CustomParserWithNoOpts.__module__}:{CustomParserWithNoOpts.__name__}", {}),
        (
            f"{CustomParserWithOpts.__module__}:{CustomParserWithOpts.__name__}",
            RootModel(CustomParserOpts()).model_dump(),
        ),
    ],
)
def test_load_default_parser_opts(
    commit_parser: str | None, expected_parser_opts: dict[str, Any]
):
    raw_config = RawConfig.model_validate(
        # Since TOML does not support NoneTypes, we need to not include the key
        {"commit_parser": commit_parser} if commit_parser else {}
    )
    assert expected_parser_opts == raw_config.commit_parser_options


def test_load_user_defined_parser_opts():
    user_defined_opts = {
        "allowed_tags": ["foo", "bar", "baz"],
        "minor_tags": ["bar"],
        "patch_tags": ["baz"],
        "default_bump_level": LevelBump.PATCH.value,
    }
    raw_config = RawConfig.model_validate(
        {
            "commit_parser": "conventional",
            "commit_parser_options": user_defined_opts,
        }
    )
    assert user_defined_opts == raw_config.commit_parser_options


@pytest.mark.parametrize("commit_parser", [""])
def test_invalid_commit_parser_value(commit_parser: str):
    with pytest.raises(ValidationError) as excinfo:
        RawConfig.model_validate(
            {
                "commit_parser": commit_parser,
            }
        )
    assert "commit_parser" in str(excinfo.value)


def test_default_toml_config_valid(example_project_dir: ExProjectDir):
    default_config_file = example_project_dir / "default.toml"

    default_config_file.write_text(
        tomlkit.dumps(RawConfig().model_dump(mode="json", exclude_none=True))
    )

    written = default_config_file.read_text(encoding="utf-8")
    loaded = tomlkit.loads(written).unwrap()
    # Check that we can load it correctly
    parsed = RawConfig.model_validate(loaded)
    assert parsed
    # Check the re-loaded internal representation is sufficient
    # There is an issue with BaseModel.__eq__ that means
    # comparing directly doesn't work with parsed.dict(); this
    # is because of how tomlkit parsed toml


@pytest.mark.parametrize(
    "mock_env, expected_author",
    [
        ({}, DEFAULT_COMMIT_AUTHOR),
        ({"GIT_COMMIT_AUTHOR": "foo <foo>"}, "foo <foo>"),
    ],
)
@pytest.mark.usefixtures(repo_w_no_tags_conventional_commits.__name__)
def test_commit_author_configurable(
    example_pyproject_toml: Path,
    mock_env: dict[str, str],
    expected_author: str,
    change_to_ex_proj_dir: None,
):
    content = tomlkit.loads(example_pyproject_toml.read_text(encoding="utf-8")).unwrap()

    with mock.patch.dict(os.environ, mock_env):
        raw = RawConfig.model_validate(content)
        runtime = RuntimeContext.from_raw_config(
            raw=raw,
            global_cli_options=GlobalCommandLineOptions(),
        )
        resulting_author = (
            f"{runtime.commit_author.name} <{runtime.commit_author.email}>"
        )
        assert expected_author == resulting_author


def test_load_valid_runtime_config(
    build_configured_base_repo: BuildRepoFn,
    example_project_dir: ExProjectDir,
    example_pyproject_toml: Path,
    update_pyproject_toml: UpdatePyprojectTomlFn,
    change_to_ex_proj_dir: None,
):
    build_configured_base_repo(example_project_dir)

    # Wipe out any existing configuration options
    update_pyproject_toml(f"tool.{semantic_release.__name__}", {})

    runtime_ctx = RuntimeContext.from_raw_config(
        RawConfig.model_validate(load_raw_config_file(example_pyproject_toml)),
        global_cli_options=GlobalCommandLineOptions(),
    )

    # TODO: add more validation
    assert runtime_ctx


@pytest.mark.parametrize(
    "commit_parser",
    [
        # Module:Class string
        f"{CustomParserWithNoOpts.__module__}:{CustomParserWithNoOpts.__name__}",
        f"{CustomParserWithOpts.__module__}:{CustomParserWithOpts.__name__}",
        # File path module:Class string
        f"{CustomParserWithNoOpts.__module__.replace('.', '/')}.py:{CustomParserWithNoOpts.__name__}",
        f"{CustomParserWithOpts.__module__.replace('.', '/')}.py:{CustomParserWithOpts.__name__}",
    ],
)
def test_load_valid_runtime_config_w_custom_parser(
    commit_parser: CommitConvention,
    build_configured_base_repo: BuildRepoFn,
    example_project_dir: ExProjectDir,
    example_pyproject_toml: Path,
    change_to_ex_proj_dir: None,
    request: pytest.FixtureRequest,
):
    fake_sys_modules = {**sys.modules}

    if ".py" in commit_parser:
        module_filepath = Path(commit_parser.split(":")[0])
        module_filepath.parent.mkdir(parents=True, exist_ok=True)
        module_filepath.parent.joinpath("__init__.py").touch()
        shutil.copy(
            src=str(request.config.rootpath / module_filepath),
            dst=str(module_filepath),
        )
        fake_sys_modules.pop(
            str(Path(module_filepath).with_suffix("")).replace(os.sep, ".")
        )

    build_configured_base_repo(
        example_project_dir,
        commit_type=commit_parser,
    )

    with mock.patch.dict(sys.modules, fake_sys_modules, clear=True):
        assert RuntimeContext.from_raw_config(
            RawConfig.model_validate(load_raw_config_file(example_pyproject_toml)),
            global_cli_options=GlobalCommandLineOptions(),
        )


@pytest.mark.parametrize(
    "commit_parser",
    [
        # Non-existant module
        "tests.missing_module:CustomParser",
        # Non-existant class
        f"{CustomParserWithOpts.__module__}:MissingCustomParser",
        # Incomplete class implementation
        f"{IncompleteCustomParser.__module__}:{IncompleteCustomParser.__name__}",
        # Non-existant module file
        "tests/missing_module.py:CustomParser",
        # Non-existant class in module file
        f"{CustomParserWithOpts.__module__.replace('.', '/')}.py:MissingCustomParser",
        # Incomplete class implementation in module file
        f"{IncompleteCustomParser.__module__.replace('.', '/')}.py:{IncompleteCustomParser.__name__}",
    ],
)
def test_load_invalid_custom_parser(
    commit_parser: str,
    build_configured_base_repo: BuildRepoFn,
    example_project_dir: ExProjectDir,
    example_pyproject_toml: Path,
    update_pyproject_toml: UpdatePyprojectTomlFn,
    pyproject_toml_config_option_parser: str,
    change_to_ex_proj_dir: None,
):
    build_configured_base_repo(example_project_dir)

    # Wipe out any existing configuration options
    update_pyproject_toml(f"{pyproject_toml_config_option_parser}_options", {})

    # Insert invalid custom parser string into configuration
    update_pyproject_toml(pyproject_toml_config_option_parser, commit_parser)

    with pytest.raises(ParserLoadError):
        RuntimeContext.from_raw_config(
            RawConfig.model_validate(load_raw_config_file(example_pyproject_toml)),
            global_cli_options=GlobalCommandLineOptions(),
        )


def test_branch_config_with_plain_wildcard():
    branch_config = BranchConfig(
        match="*",
    )
    assert branch_config.match == ".*"


@pytest.mark.parametrize(
    "invalid_regex",
    [
        "*abc",
        "[a-z",
        "(.+",
        "{2,3}",
        "a{3,2}",
    ],
)
def test_branch_config_with_invalid_regex(invalid_regex: str):
    with pytest.raises(ValidationError):
        BranchConfig(
            match=invalid_regex,
        )


@pytest.mark.parametrize(
    "valid_patterns",
    [
        # Single entry
        [r"chore(?:\([^)]*?\))?: .+"],
        # Multiple entries
        [r"^\d+\.\d+\.\d+", r"Initial [Cc]ommit.*"],
    ],
)
def test_changelog_config_with_valid_exclude_commit_patterns(valid_patterns: list[str]):
    assert ChangelogConfig.model_validate(
        {
            "exclude_commit_patterns": valid_patterns,
        }
    )


@pytest.mark.parametrize(
    "invalid_patterns, index_of_invalid_pattern",
    [
        # Single entry, single incorrect
        (["*abc"], 0),
        # Two entries, second incorrect
        ([".*", "[a-z"], 1),
        # Two entries, first incorrect
        (["(.+", ".*"], 0),
    ],
)
def test_changelog_config_with_invalid_exclude_commit_patterns(
    invalid_patterns: list[str],
    index_of_invalid_pattern: int,
):
    with pytest.raises(
        ValidationError,
        match=regexp(
            str.join(
                "",
                [
                    r".*\bexclude_commit_patterns\[",
                    str(index_of_invalid_pattern),
                    r"\]: Invalid regular expression",
                ],
            ),
        ),
    ):
        ChangelogConfig.model_validate(
            {
                "exclude_commit_patterns": invalid_patterns,
            }
        )


@pytest.mark.parametrize(
    "output_format, insertion_flag",
    [
        (
            ChangelogOutputFormat.MARKDOWN.value,
            "<!-- version list -->",
        ),
        (
            ChangelogOutputFormat.RESTRUCTURED_TEXT.value,
            f"..{os.linesep}    version list",
        ),
    ],
)
def test_changelog_config_default_insertion_flag(
    output_format: str,
    insertion_flag: str,
):
    changelog_config = ChangelogConfig.model_validate(
        {
            "default_templates": {
                "output_format": output_format,
            }
        }
    )

    assert changelog_config.insertion_flag == insertion_flag


@pytest.mark.parametrize(
    "hvcs_type",
    [k.value for k in _known_hvcs],
)
def test_git_remote_url_w_insteadof_alias(
    repo_w_initial_commit: BuiltRepoResult,
    example_pyproject_toml: Path,
    example_git_https_url: str,
    hvcs_type: str,
    update_pyproject_toml: UpdatePyprojectTomlFn,
):
    expected_url = parse_url(example_git_https_url)
    repo_name_suffix = PurePosixPath(expected_url.path or "").name
    insteadof_alias = "psr_test_insteadof"
    insteadof_value = expected_url.url.replace(repo_name_suffix, "")
    repo = repo_w_initial_commit["repo"]

    with repo.config_writer() as cfg:
        # Setup: define the insteadOf replacement value
        cfg.add_value(f'url "{insteadof_value}"', "insteadof", f"{insteadof_alias}:")

        # Setup: set the remote URL with an insteadOf alias
        cfg.set_value('remote "origin"', "url", f"{insteadof_alias}:{repo_name_suffix}")

    # Setup: set each supported HVCS client type
    update_pyproject_toml("tool.semantic_release.remote.type", hvcs_type)

    # Act: load the configuration (in clear environment)
    with mock.patch.dict(os.environ, {}, clear=True):
        # Essentially the same as CliContextObj._init_runtime_ctx()
        project_config = tomlkit.loads(
            example_pyproject_toml.read_text(encoding="utf-8")
        ).unwrap()

        runtime = RuntimeContext.from_raw_config(
            raw=RawConfig.model_validate(
                project_config.get("tool", {}).get("semantic_release", {}),
            ),
            global_cli_options=GlobalCommandLineOptions(),
        )

        # Trigger a function that calls helpers.parse_git_url()
        actual_url = runtime.hvcs_client.remote_url(use_token=False)

    # Evaluate: the remote URL should be the full URL
    assert expected_url.url == actual_url