Skip to content

Commit

Permalink
Enable --offline to affect checking version online (#4269)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cavcrosby committed Aug 22, 2024
1 parent 5d2d16a commit deb79f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 890
PYTEST_REQPASS: 891
steps:
- uses: actions/checkout@v4
with:
Expand Down
17 changes: 9 additions & 8 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def get_version_warning() -> str:
with open(cache_file, encoding="utf-8") as f:
data = json.load(f)

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

html_url = data["html_url"]
new_version = Version(data["tag_name"][1:]) # removing v prefix from tag
if data:
html_url = data["html_url"]
new_version = Version(data["tag_name"][1:]) # removing v prefix from tag

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

return msg
12 changes: 11 additions & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import shutil
import subprocess
import sys
import tempfile
import time
from http.client import RemoteDisconnected
from pathlib import Path

import pytest
from pytest_mock import MockerFixture

from ansiblelint.config import get_version_warning
from ansiblelint.config import get_version_warning, options
from ansiblelint.constants import RC


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


def test_get_version_warning_offline(mocker: MockerFixture) -> None:
"""Test that offline mode does not display any message."""
with tempfile.TemporaryDirectory() as temporary_directory:
# ensures a real cache_file is not loaded
mocker.patch("ansiblelint.config.CACHE_DIR", Path(temporary_directory))
options.offline = True
assert get_version_warning() == ""


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

0 comments on commit deb79f7

Please sign in to comment.