diff --git a/docker/tag_latest_release.sh b/docker/tag_latest_release.sh index 362ce4f47ee1..6d99d9dc08ae 100755 --- a/docker/tag_latest_release.sh +++ b/docker/tag_latest_release.sh @@ -46,7 +46,7 @@ git_show_ref () { get_latest_tag_list () { if [[ "$TEST_ENV" == "true" ]] then - echo "(tag: 2.1.0, apache/2.1test)" + echo "${TEST_LATEST_TAG_LIST:-(tag: 2.1.0, apache/2.1test)}" else result=$(git show-ref --tags --dereference latest | awk '{print $2}' | xargs git show --pretty=tformat:%d -s | grep tag:) echo "${result}" @@ -140,7 +140,7 @@ TAGS=($(split_string "$LATEST_TAGS" " ")) compare_result="" # Iterate through the tags of the latest release -for tag in $TAGS +for tag in $(printf '%s\n' "${TAGS[@]}" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) do if [[ $tag == "latest" ]]; then continue diff --git a/scripts/tag_latest_release.sh b/scripts/tag_latest_release.sh index 362ce4f47ee1..6d99d9dc08ae 100755 --- a/scripts/tag_latest_release.sh +++ b/scripts/tag_latest_release.sh @@ -46,7 +46,7 @@ git_show_ref () { get_latest_tag_list () { if [[ "$TEST_ENV" == "true" ]] then - echo "(tag: 2.1.0, apache/2.1test)" + echo "${TEST_LATEST_TAG_LIST:-(tag: 2.1.0, apache/2.1test)}" else result=$(git show-ref --tags --dereference latest | awk '{print $2}' | xargs git show --pretty=tformat:%d -s | grep tag:) echo "${result}" @@ -140,7 +140,7 @@ TAGS=($(split_string "$LATEST_TAGS" " ")) compare_result="" # Iterate through the tags of the latest release -for tag in $TAGS +for tag in $(printf '%s\n' "${TAGS[@]}" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) do if [[ $tag == "latest" ]]; then continue diff --git a/tests/unit_tests/fixtures/bash_mock.py b/tests/unit_tests/fixtures/bash_mock.py index f2512d1270cc..7c3e2b94bc98 100644 --- a/tests/unit_tests/fixtures/bash_mock.py +++ b/tests/unit_tests/fixtures/bash_mock.py @@ -20,13 +20,18 @@ class BashMock: @staticmethod - def tag_latest_release(tag): + def tag_latest_release(tag, latest_tag_list=None): bash_command = f"./scripts/tag_latest_release.sh {tag} --dry-run" + env = {"TEST_ENV": "true"} + if latest_tag_list is not None: + # Overrides the mocked `git show --pretty=%d` decoration that the + # script reads for the commit currently tagged `latest`. + env["TEST_LATEST_TAG_LIST"] = latest_tag_list result = subprocess.run( # noqa: S602 bash_command, shell=True, capture_output=True, text=True, - env={"TEST_ENV": "true"}, + env=env, ) return result diff --git a/tests/unit_tests/scripts/tag_latest_release_test.py b/tests/unit_tests/scripts/tag_latest_release_test.py index 15e33978a62e..214f1105ea7b 100644 --- a/tests/unit_tests/scripts/tag_latest_release_test.py +++ b/tests/unit_tests/scripts/tag_latest_release_test.py @@ -62,3 +62,49 @@ def test_tag_latest_release(tag, expected_output): ) assert expected_output in result.stdout + + +@pytest.mark.parametrize( + "tag, expected_output", + [ + ("2.1.1", "This release tag 2.1.1 is newer than the latest."), + ("1.0.0", "This release tag 1.0.0 is older than the latest."), + ("2.1.0", "Versions are equal"), + ], +) +def test_tag_latest_release_reads_every_decoration(tag, expected_output): + """ + `git show --pretty=%d` decorates a commit with branches as well as tags, so + the version tag is not necessarily the first entry in the list. Every entry + has to be considered, not just the first one. + """ + result = BashMock.tag_latest_release( + tag, latest_tag_list="(HEAD -> master, tag: latest, tag: 2.1.0)" + ) + + assert "The current release with the latest tag is version 2.1.0" in result.stdout + assert expected_output in result.stdout + + +@pytest.mark.parametrize( + "tag, expected_output", + [ + ("2.5.0", "This release tag 2.5.0 is older than the latest."), + ("3.0.1", "This release tag 3.0.1 is newer than the latest."), + ("3.0.0", "Versions are equal"), + ], +) +def test_tag_latest_release_compares_against_the_highest_tag(tag, expected_output): + """ + A release commit can carry more than one version tag. Only the highest one + describes what `latest` currently points at, so that is the version the + candidate is compared against -- comparing against a lower tag as well would + let an older release claim to be newer and move `latest` backwards. + """ + result = BashMock.tag_latest_release( + tag, latest_tag_list="(tag: latest, tag: 3.0.0, tag: 2.1.0)" + ) + + assert "The current release with the latest tag is version 3.0.0" in result.stdout + assert "version 2.1.0" not in result.stdout + assert expected_output in result.stdout