Skip to content

Commit 52cec64

Browse files
committed
Enable --offline to affect checking version online (#4269)
A conditional has been added to see if data is empty to prevent KeyError exceptions from happening because a cache file may not exist when using the --offline flag. This prevents data from ever being populated.
1 parent 454b807 commit 52cec64

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
env:
7373
# Number of expected test passes, safety measure for accidental skip of
7474
# tests. Update value if you add/remove tests.
75-
PYTEST_REQPASS: 890
75+
PYTEST_REQPASS: 891
7676
steps:
7777
- uses: actions/checkout@v4
7878
with:

src/ansiblelint/config.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def get_version_warning() -> str:
322322
with open(cache_file, encoding="utf-8") as f:
323323
data = json.load(f)
324324

325-
if refresh or not data:
325+
if not options.offline and (refresh or not data):
326326
release_url = (
327327
"https://api.github.com/repos/ansible/ansible-lint/releases/latest"
328328
)
@@ -339,13 +339,14 @@ def get_version_warning() -> str:
339339
)
340340
return ""
341341

342-
html_url = data["html_url"]
343-
new_version = Version(data["tag_name"][1:]) # removing v prefix from tag
342+
if data:
343+
html_url = data["html_url"]
344+
new_version = Version(data["tag_name"][1:]) # removing v prefix from tag
344345

345-
if current_version > new_version:
346-
msg = "[dim]You are using a pre-release version of ansible-lint.[/]"
347-
elif current_version < new_version:
348-
msg = f"""[warning]A new release of ansible-lint is available: [red]{current_version}[/] → [green][link={html_url}]{new_version}[/][/][/]"""
349-
msg += f" Upgrade by running: [info]{pip}[/]"
346+
if current_version > new_version:
347+
msg = "[dim]You are using a pre-release version of ansible-lint.[/]"
348+
elif current_version < new_version:
349+
msg = f"""[warning]A new release of ansible-lint is available: [red]{current_version}[/] → [green][link={html_url}]{new_version}[/][/][/]"""
350+
msg += f" Upgrade by running: [info]{pip}[/]"
350351

351352
return msg

test/test_main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
import shutil
55
import subprocess
66
import sys
7+
import tempfile
78
import time
89
from http.client import RemoteDisconnected
910
from pathlib import Path
1011

1112
import pytest
1213
from pytest_mock import MockerFixture
1314

14-
from ansiblelint.config import get_version_warning
15+
from ansiblelint.config import get_version_warning, options
1516
from ansiblelint.constants import RC
1617

1718

@@ -102,6 +103,15 @@ def test_get_version_warning_remote_disconnect(mocker: MockerFixture) -> None:
102103
pytest.fail("Failed to handle a remote disconnect")
103104

104105

106+
def test_get_version_warning_offline(mocker: MockerFixture) -> None:
107+
"""Test that offline mode does not display any message."""
108+
with tempfile.TemporaryDirectory() as temporary_directory:
109+
# ensures a real cache_file is not loaded
110+
mocker.patch("ansiblelint.config.CACHE_DIR", Path(temporary_directory))
111+
options.offline = True
112+
assert get_version_warning() == ""
113+
114+
105115
@pytest.mark.parametrize(
106116
("lintable"),
107117
(

0 commit comments

Comments
 (0)