|
| 1 | +# (C) Datadog, Inc. 2026-present |
| 2 | +# All rights reserved |
| 3 | +# Licensed under a 3-clause BSD style license (see LICENSE) |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ddev.cli.release.changelog.show import _extract_version_section |
| 9 | +from ddev.utils.fs import Path |
| 10 | +from tests.helpers.git import ClonedRepo |
| 11 | +from tests.helpers.runner import CliRunner |
| 12 | + |
| 13 | +CHANGELOG_BODY = """\ |
| 14 | +# CHANGELOG - ddev |
| 15 | +
|
| 16 | +<!-- towncrier release notes start --> |
| 17 | +
|
| 18 | +## 16.1.1 / 2026-04-29 |
| 19 | +
|
| 20 | +***Fixed***: |
| 21 | +
|
| 22 | +* Bumped datadog_checks_dev to version 38.0.0. Fixes dependency issues. ([#23516](https://github.com/DataDog/integrations-core/pull/23516)) |
| 23 | +
|
| 24 | +## 16.1.0 / 2026-04-29 |
| 25 | +
|
| 26 | +***Added***: |
| 27 | +
|
| 28 | +* Add async GitHub API client. ([#22734](https://github.com/DataDog/integrations-core/pull/22734)) |
| 29 | +* Use uv as the installer for hatch test environments. ([#23497](https://github.com/DataDog/integrations-core/pull/23497)) |
| 30 | +
|
| 31 | +## 1.0.10 / 2024-01-01 |
| 32 | +
|
| 33 | +***Fixed***: |
| 34 | +
|
| 35 | +* Older release that should not match against 1.0.1. ([#1](https://github.com/DataDog/integrations-core/pull/1)) |
| 36 | +
|
| 37 | +## 1.0.1 / 2024-01-02 |
| 38 | +
|
| 39 | +***Fixed***: |
| 40 | +
|
| 41 | +* Patch release. ([#2](https://github.com/DataDog/integrations-core/pull/2)) |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def ddev_changelog(repo_with_towncrier: ClonedRepo) -> Path: |
| 47 | + changelog = repo_with_towncrier.path / 'ddev' / 'CHANGELOG.md' |
| 48 | + changelog.write_text(CHANGELOG_BODY) |
| 49 | + return changelog |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize( |
| 53 | + 'version, expected_substring, forbidden_substring', |
| 54 | + [ |
| 55 | + pytest.param( |
| 56 | + '16.1.1', |
| 57 | + '* Bumped datadog_checks_dev to version 38.0.0', |
| 58 | + '## 16.1.0', |
| 59 | + id='middle_section', |
| 60 | + ), |
| 61 | + pytest.param( |
| 62 | + '16.1.0', |
| 63 | + '* Add async GitHub API client.', |
| 64 | + '## 16.1.1', |
| 65 | + id='multi_bullet_section', |
| 66 | + ), |
| 67 | + pytest.param( |
| 68 | + '1.0.1', |
| 69 | + '* Patch release.', |
| 70 | + '* Older release', |
| 71 | + id='strict_match_does_not_substring_into_1_0_10', |
| 72 | + ), |
| 73 | + ], |
| 74 | +) |
| 75 | +def test_extract_version_section_returns_expected_block( |
| 76 | + ddev_changelog: Path, version: str, expected_substring: str, forbidden_substring: str |
| 77 | +): |
| 78 | + section = _extract_version_section(ddev_changelog, version) |
| 79 | + |
| 80 | + assert section is not None |
| 81 | + assert expected_substring in section |
| 82 | + assert forbidden_substring not in section |
| 83 | + assert not section.startswith('\n') |
| 84 | + assert not section.endswith('\n') |
| 85 | + |
| 86 | + |
| 87 | +def test_extract_version_section_returns_none_for_missing_version(ddev_changelog: Path): |
| 88 | + assert _extract_version_section(ddev_changelog, '99.99.99') is None |
| 89 | + |
| 90 | + |
| 91 | +def test_show_prints_section_to_stdout(ddev: CliRunner, ddev_changelog: Path, helpers): |
| 92 | + result = ddev('release', 'changelog', 'show', 'ddev', '16.1.1') |
| 93 | + |
| 94 | + assert result.exit_code == 0, result.output |
| 95 | + output = helpers.remove_trailing_spaces(result.output) |
| 96 | + assert 'Fixed:' in output |
| 97 | + assert 'Bumped datadog_checks_dev to version 38.0.0' in output |
| 98 | + assert '16.1.0' not in output |
| 99 | + |
| 100 | + |
| 101 | +def test_show_writes_to_file(ddev: CliRunner, ddev_changelog: Path, tmp_path: Path): |
| 102 | + output_file = tmp_path / 'release-notes.md' |
| 103 | + |
| 104 | + result = ddev('release', 'changelog', 'show', 'ddev', '16.1.0', '--file', str(output_file)) |
| 105 | + |
| 106 | + assert result.exit_code == 0, result.output |
| 107 | + assert f'Wrote changelog section for ddev 16.1.0 to {output_file}' in result.output |
| 108 | + contents = output_file.read_text() |
| 109 | + assert '* Add async GitHub API client.' in contents |
| 110 | + assert '## 16.1.1' not in contents |
| 111 | + |
| 112 | + |
| 113 | +def test_show_creates_missing_parent_directories(ddev: CliRunner, ddev_changelog: Path, tmp_path: Path): |
| 114 | + output_file = tmp_path / 'nested' / 'sub' / 'release-notes.md' |
| 115 | + |
| 116 | + result = ddev('release', 'changelog', 'show', 'ddev', '16.1.1', '--file', str(output_file)) |
| 117 | + |
| 118 | + assert result.exit_code == 0, result.output |
| 119 | + assert output_file.is_file() |
| 120 | + assert '* Bumped datadog_checks_dev to version 38.0.0' in output_file.read_text() |
| 121 | + |
| 122 | + |
| 123 | +def test_show_strict_version_matching_against_substring(ddev: CliRunner, ddev_changelog: Path, helpers): |
| 124 | + result = ddev('release', 'changelog', 'show', 'ddev', '1.0.1') |
| 125 | + |
| 126 | + assert result.exit_code == 0, result.output |
| 127 | + output = helpers.remove_trailing_spaces(result.output) |
| 128 | + assert 'Patch release.' in output |
| 129 | + assert 'Older release' not in output |
| 130 | + |
| 131 | + |
| 132 | +def test_show_aborts_when_version_missing(ddev: CliRunner, ddev_changelog: Path): |
| 133 | + result = ddev('release', 'changelog', 'show', 'ddev', '99.99.99') |
| 134 | + |
| 135 | + assert result.exit_code != 0 |
| 136 | + assert 'No changelog section found for ddev version 99.99.99' in result.output |
| 137 | + |
| 138 | + |
| 139 | +def test_show_aborts_when_changelog_missing(ddev: CliRunner, repo_with_towncrier: ClonedRepo): |
| 140 | + changelog = repo_with_towncrier.path / 'ddev' / 'CHANGELOG.md' |
| 141 | + if changelog.is_file(): |
| 142 | + changelog.unlink() |
| 143 | + |
| 144 | + result = ddev('release', 'changelog', 'show', 'ddev', '1.0.0') |
| 145 | + |
| 146 | + assert result.exit_code != 0 |
| 147 | + assert 'Changelog not found' in result.output |
| 148 | + |
| 149 | + |
| 150 | +def test_show_aborts_for_unknown_target(ddev: CliRunner, repo_with_towncrier: ClonedRepo): |
| 151 | + result = ddev('release', 'changelog', 'show', 'definitely_not_an_integration', '1.0.0') |
| 152 | + |
| 153 | + assert result.exit_code != 0 |
| 154 | + assert 'Unknown target: definitely_not_an_integration' in result.output |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize( |
| 158 | + 'args, expected_message', |
| 159 | + [ |
| 160 | + pytest.param([], "Missing argument 'TARGET'", id='no_args'), |
| 161 | + pytest.param(['ddev'], "Missing argument 'VERSION'", id='target_only'), |
| 162 | + ], |
| 163 | +) |
| 164 | +def test_show_argument_errors(ddev: CliRunner, repo_with_towncrier: ClonedRepo, args: list[str], expected_message: str): |
| 165 | + result = ddev('release', 'changelog', 'show', *args) |
| 166 | + |
| 167 | + assert result.exit_code != 0 |
| 168 | + assert expected_message in result.output |
0 commit comments