def test_search_on_github_cache_terraform_releases_does_not_cache_error_404(  # noqa: D103
        tmp_working_dir,
        terraform_releases_html_after_v0_13_0,
):
    with mock.patch("io.BytesIO", AutoclosingBytesIO):
        with pook.use():
            repo = "hashicorp/terraform"
            releases_url = "https://github.com/{}/releases?after=v0.13.0".format(
                repo)

            # volatile mocks that can only be invoked once each
            pook.get(
                releases_url,
                reply=404,
                response_headers={
                    "Status": "404 Not found",
                    "Date": formatdate(usegmt=True)
                },
                times=1,
            )
            pook.get(
                releases_url,
                reply=200,
                response_type="text/plain",
                response_body=terraform_releases_html_after_v0_13_0,
                response_headers={
                    "Status": "200 OK",
                    "ETag": 'W/"df0474ebd25f223a95926ba58e11e77b"',
                    "Cache-Control": "max-age=0, private, must-revalidate",
                    "Date": formatdate(usegmt=True),
                },
                times=1,
            )

            patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"

            # pook does not implement urllib3's retry logic so this first request will always return None during tests
            patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
            assert patch is None

            patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "14")
            assert patch == "14"

            patch = tfwrapper.search_on_github(repo, "0.12", patch_regex, "")
            assert patch == "19"

            assert pook.isdone()
            assert not pook.pending_mocks()
            assert not pook.unmatched_requests()
def test_search_on_github_provider_releases(  # noqa: D103
    requests_mock,
    provider_releases_html_after_v2_6_0,
    provider_releases_html_after_v1_3_0,
):
    from claranet_tfwrapper import GITHUB_RELEASES

    repo = "claranet/terraform-provider-gitlab"
    releases_url = GITHUB_RELEASES.format(repo)
    releases_url_after_v2_6_0 = releases_url + "?after=v2.6.0"
    releases_url_after_v1_3_0 = releases_url + "?after=v1.3.0"

    requests_mock.get(
        releases_url_after_v2_6_0,
        complete_qs=True,
        text=provider_releases_html_after_v2_6_0,
    )
    requests_mock.get(
        releases_url_after_v1_3_0,
        complete_qs=True,
        text=provider_releases_html_after_v1_3_0,
    )

    assert provider_releases_html_after_v2_6_0 == requests.get(
        releases_url_after_v2_6_0).text

    patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
    patch = tfwrapper.search_on_github(repo, "2.5", patch_regex, "")
    assert patch == "0"

    patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
    patch = tfwrapper.search_on_github(repo, "2.5", patch_regex, "0-claranet")
    assert patch == "0-claranet"

    patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
    patch = tfwrapper.search_on_github(repo, "1.2", patch_regex, "")
    assert patch == "0"

    patch_regex = r"[0-9]+(((-alpha|-beta|-rc)[0-9]+)|(?P<dev>-dev))?"
    patch = tfwrapper.search_on_github(repo, "1.2", patch_regex, "9-claranet")
    assert patch == "9-claranet"