Skip to content

Commit

Permalink
Fix --version behavior to handle remote disconnect (ansible#4269)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby committed Jul 27, 2024
1 parent 1dbb602 commit 9e3f16e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import warnings
from dataclasses import dataclass, field
from functools import lru_cache
from http.client import HTTPException
from importlib.metadata import PackageNotFoundError, distribution, version
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -330,7 +331,7 @@ def get_version_warning() -> str:
data = json.load(url)
with open(cache_file, "w", encoding="utf-8") as f:
json.dump(data, f)
except (URLError, HTTPError) as exc: # pragma: no cover
except (URLError, HTTPError, HTTPException) as exc: # pragma: no cover
_logger.debug(
"Unable to fetch latest version from %s due to: %s",
release_url,
Expand Down
10 changes: 10 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import sys
import time
from http.client import RemoteDisconnected
from pathlib import Path

import pytest
Expand Down Expand Up @@ -92,6 +93,15 @@ def test_get_version_warning_no_pip(mocker: MockerFixture) -> None:
assert get_version_warning() == ""


def test_get_version_warning_remote_disconnect(mocker: MockerFixture) -> None:
"""Test that we can handle remote disconnect when fetching release url."""
mocker.patch("urllib.request.urlopen", side_effect=RemoteDisconnected)
try:
get_version_warning()
except RemoteDisconnected:
pytest.fail("failed to handle a remote disconnect")


@pytest.mark.parametrize(
("lintable"),
(
Expand Down

0 comments on commit 9e3f16e

Please sign in to comment.