Skip to content

Commit cd066a2

Browse files
committed
Fix --version behavior to handle remote disconnect (#4269)
1 parent 1dbb602 commit cd066a2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/ansiblelint/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import warnings
1212
from dataclasses import dataclass, field
1313
from functools import lru_cache
14+
from http.client import HTTPException
1415
from importlib.metadata import PackageNotFoundError, distribution, version
1516
from pathlib import Path
1617
from typing import Any
@@ -330,7 +331,7 @@ def get_version_warning() -> str:
330331
data = json.load(url)
331332
with open(cache_file, "w", encoding="utf-8") as f:
332333
json.dump(data, f)
333-
except (URLError, HTTPError) as exc: # pragma: no cover
334+
except (URLError, HTTPError, HTTPException) as exc: # pragma: no cover
334335
_logger.debug(
335336
"Unable to fetch latest version from %s due to: %s",
336337
release_url,

test/test_main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import sys
77
import time
8+
from http.client import RemoteDisconnected
89
from pathlib import Path
910

1011
import pytest
@@ -92,6 +93,15 @@ def test_get_version_warning_no_pip(mocker: MockerFixture) -> None:
9293
assert get_version_warning() == ""
9394

9495

96+
def test_get_version_warning_remote_disconnect(mocker: MockerFixture) -> None:
97+
"""Test that we can handle remote disconnect when fetching release url."""
98+
mocker.patch("urllib.request.urlopen", side_effect=RemoteDisconnected)
99+
try:
100+
get_version_warning()
101+
except RemoteDisconnected:
102+
pytest.fail("Failed to handle a remote disconnect")
103+
104+
95105
@pytest.mark.parametrize(
96106
("lintable"),
97107
(

0 commit comments

Comments
 (0)