|
13 | 13 | NotAGitProjectError,
|
14 | 14 | NotAllowed,
|
15 | 15 | )
|
16 |
| -from tests.utils import create_file_and_commit, wait_for_tag |
| 16 | +from tests.utils import ( |
| 17 | + create_branch, |
| 18 | + create_file_and_commit, |
| 19 | + get_current_branch, |
| 20 | + merge_branch, |
| 21 | + switch_branch, |
| 22 | + wait_for_tag, |
| 23 | +) |
17 | 24 |
|
18 | 25 |
|
19 | 26 | @pytest.mark.usefixtures("tmp_commitizen_project")
|
@@ -268,6 +275,75 @@ def test_changelog_hook_customize(mocker: MockFixture, config_customize):
|
268 | 275 | changelog_hook_mock.assert_called_with(full_changelog, full_changelog)
|
269 | 276 |
|
270 | 277 |
|
| 278 | +@pytest.mark.usefixtures("tmp_commitizen_project") |
| 279 | +def test_changelog_with_non_linear_merges_commit_order( |
| 280 | + mocker: MockFixture, config_customize |
| 281 | +): |
| 282 | + """Test that commits merged non-linearly are correctly ordered in the changelog |
| 283 | +
|
| 284 | + A typical scenario is having two branches from main like so: |
| 285 | + * feat: I will be merged first - (2023-03-01 11:35:51 +0100) | (branchB) |
| 286 | + | * feat: I will be merged second - (2023-03-01 11:35:22 +0100) | (branchA) |
| 287 | + |/ |
| 288 | + * feat: initial commit - (2023-03-01 11:34:54 +0100) | (HEAD -> main) |
| 289 | +
|
| 290 | + And merging them, for example in the reverse order they were created on would give the following: |
| 291 | + * Merge branch 'branchA' - (2023-03-01 11:42:59 +0100) | (HEAD -> main) |
| 292 | + |\ |
| 293 | + | * feat: I will be merged second - (2023-03-01 11:35:22 +0100) | (branchA) |
| 294 | + * | feat: I will be merged first - (2023-03-01 11:35:51 +0100) | (branchB) |
| 295 | + |/ |
| 296 | + * feat: initial commit - (2023-03-01 11:34:54 +0100) | |
| 297 | +
|
| 298 | + In this case we want the changelog to reflect the topological order of commits, |
| 299 | + i.e. the order in which they were merged into the main branch |
| 300 | +
|
| 301 | + So the above example should result in the following: |
| 302 | + ## Unreleased |
| 303 | +
|
| 304 | + ### Feat |
| 305 | + - I will be merged second |
| 306 | + - I will be merged first |
| 307 | + - initial commit |
| 308 | + """ |
| 309 | + changelog_hook_mock = mocker.Mock() |
| 310 | + changelog_hook_mock.return_value = "cool changelog hook" |
| 311 | + |
| 312 | + create_file_and_commit("feat: initial commit") |
| 313 | + |
| 314 | + main_branch = get_current_branch() |
| 315 | + |
| 316 | + create_branch("branchA") |
| 317 | + create_branch("branchB") |
| 318 | + |
| 319 | + switch_branch("branchA") |
| 320 | + create_file_and_commit("feat: I will be merged second") |
| 321 | + |
| 322 | + switch_branch("branchB") |
| 323 | + create_file_and_commit("feat: I will be merged first") |
| 324 | + |
| 325 | + # Note we merge branches opposite order than author_date |
| 326 | + switch_branch(main_branch) |
| 327 | + merge_branch("branchB") |
| 328 | + merge_branch("branchA") |
| 329 | + |
| 330 | + changelog = Changelog( |
| 331 | + config_customize, |
| 332 | + {"unreleased_version": None, "incremental": True, "dry_run": False}, |
| 333 | + ) |
| 334 | + mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock) |
| 335 | + changelog() |
| 336 | + full_changelog = "\ |
| 337 | +## Unreleased\n\n\ |
| 338 | +\ |
| 339 | +### Feat\n\n\ |
| 340 | +- I will be merged second\n\ |
| 341 | +- I will be merged first\n\ |
| 342 | +- initial commit\n" |
| 343 | + |
| 344 | + changelog_hook_mock.assert_called_with(full_changelog, full_changelog) |
| 345 | + |
| 346 | + |
271 | 347 | @pytest.mark.usefixtures("tmp_commitizen_project")
|
272 | 348 | def test_changelog_multiple_incremental_do_not_add_new_lines(
|
273 | 349 | mocker: MockFixture, capsys, changelog_path, file_regression
|
|
0 commit comments