From bd568b733e41ace018c5169487cb7909c86fdfeb Mon Sep 17 00:00:00 2001 From: Conner Crosby Date: Tue, 27 Aug 2024 04:12:15 -0400 Subject: [PATCH] Enable --offline to affect checking version online (#4283) --- .github/workflows/tox.yml | 2 +- src/ansiblelint/config.py | 17 +++++++++-------- test/test_main.py | 12 +++++++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 572b8a08fd..8366aa2c40 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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: diff --git a/src/ansiblelint/config.py b/src/ansiblelint/config.py index 989df9a6d9..e472cc7e49 100644 --- a/src/ansiblelint/config.py +++ b/src/ansiblelint/config.py @@ -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" ) @@ -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 diff --git a/test/test_main.py b/test/test_main.py index e0e9a6707d..d2e48dc060 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -4,6 +4,7 @@ import shutil import subprocess import sys +import tempfile import time from http.client import RemoteDisconnected from pathlib import Path @@ -11,7 +12,7 @@ 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 @@ -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"), (