Skip to content

Commit 38e1dd7

Browse files
Snow 2106756 move upgrade warning to stderr (#2313)
* change upgrade message to warning * fix unit tests * update release notes
1 parent 53455e2 commit 38e1dd7

File tree

3 files changed

+46
-48
lines changed

3 files changed

+46
-48
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Added `--encryption` flag to `snow stage create` command defining the type of encryption for all files on the stage.
2424

2525
## Fixes and improvements
26+
* Upgrade message is printed to stderr.
2627

2728
# v3.8.0
2829

src/snowflake/cli/_app/version_check.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
22
import time
3+
from warnings import warn
34

45
import requests
56
from packaging.version import Version
67
from snowflake.cli.__about__ import VERSION
7-
from snowflake.cli.api.console import cli_console
8+
from snowflake.cli.api.cli_global_context import get_cli_context
89
from snowflake.cli.api.secure_path import SecurePath
910
from snowflake.connector.config_manager import CONFIG_MANAGER
1011

@@ -21,8 +22,8 @@ def get_new_version_msg() -> str | None:
2122

2223
def show_new_version_banner_callback(msg):
2324
def _callback(*args, **kwargs):
24-
if msg:
25-
cli_console.message(msg)
25+
if msg and not get_cli_context().silent:
26+
warn(msg)
2627

2728
return _callback
2829

tests/app/test_version_check.py

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,65 @@
22
from itertools import cycle
33
from unittest.mock import patch
44

5+
import pytest
56
from packaging.version import Version
67
from requests import Response
78
from snowflake.cli._app.version_check import _VersionCache, get_new_version_msg
89
from snowflake.cli.api.secure_path import SecurePath
910

10-
11-
@patch("snowflake.cli._app.version_check.VERSION", "1.0.0")
12-
@patch(
11+
_WARNING_MESSAGE = (
12+
"New version of Snowflake CLI available. Newest: 2.0.0, current: 1.0.0"
13+
)
14+
_PATCH_VERSION = ["snowflake.cli._app.version_check.VERSION", "1.0.0"]
15+
_PATCH_LAST_VERSION = [
1316
"snowflake.cli._app.version_check._VersionCache.get_last_version",
1417
lambda _: Version("2.0.0"),
15-
)
16-
def test_banner_shows_up_in_help(build_runner):
17-
runner = build_runner()
18-
result = runner.invoke(["--help"])
19-
msg = "New version of Snowflake CLI available. Newest: 2.0.0, current: 1.0.0"
20-
assert msg in result.output
18+
]
2119

2220

23-
@patch("snowflake.cli._app.version_check.VERSION", "1.0.0")
24-
@patch(
25-
"snowflake.cli._app.version_check._VersionCache.get_last_version",
26-
lambda _: Version("2.0.0"),
27-
)
28-
def test_banner_shows_up_in_command_invocation(build_runner):
29-
runner = build_runner()
30-
result = runner.invoke(["connection", "set-default", "default"])
31-
msg = "New version of Snowflake CLI available. Newest: 2.0.0, current: 1.0.0"
32-
assert msg in result.output
21+
@pytest.fixture
22+
def warning_is_thrown():
23+
with pytest.warns(UserWarning, match=_WARNING_MESSAGE):
24+
yield
3325

3426

35-
@patch("snowflake.cli._app.version_check.VERSION", "1.0.0")
36-
@patch(
37-
"snowflake.cli._app.version_check._VersionCache.get_last_version",
38-
lambda _: Version("2.0.0"),
39-
)
40-
def test_banner_do_not_shows_up_if_silent(build_runner):
41-
runner = build_runner()
42-
result = runner.invoke(["connection", "set-default", "default", "--silent"])
43-
msg = "New version of Snowflake CLI available. Newest: 2.0.0, current: 1.0.0"
44-
assert msg not in result.output
27+
@pytest.fixture
28+
def warning_is_not_thrown():
29+
with pytest.warns() as recorded_warnings:
30+
yield
31+
for warning in recorded_warnings:
32+
assert _WARNING_MESSAGE not in str(warning.message)
33+
34+
35+
@patch(*_PATCH_VERSION)
36+
@patch(*_PATCH_LAST_VERSION) # type: ignore
37+
def test_banner_shows_up_in_help(build_runner, warning_is_thrown):
38+
build_runner().invoke(["--help"])
39+
40+
41+
@patch(*_PATCH_VERSION)
42+
@patch(*_PATCH_LAST_VERSION) # type: ignore
43+
def test_banner_shows_up_in_command_invocation(build_runner, warning_is_thrown):
44+
build_runner().invoke(["connection", "set-default", "default"])
45+
46+
47+
@patch(*_PATCH_VERSION)
48+
@patch(*_PATCH_LAST_VERSION) # type: ignore
49+
def test_banner_do_not_shows_up_if_silent(build_runner, warning_is_not_thrown):
50+
build_runner().invoke(["connection", "set-default", "default", "--silent"])
4551

4652

4753
@patch("snowflake.cli._app.version_check._VersionCache._read_latest_version")
4854
def test_version_check_exception_are_handled_safely(
49-
mock_read_latest_version, build_runner
55+
mock_read_latest_version, build_runner, warning_is_not_thrown
5056
):
5157
mock_read_latest_version.side_effect = Exception("Error")
52-
runner = build_runner()
53-
result = runner.invoke(["connection", "set-default", "default"])
54-
55-
msg = "New version of Snowflake CLI available. Newest: 2.0.0, current: 1.0.0"
58+
result = build_runner().invoke(["connection", "set-default", "default"])
5659
assert result.exit_code == 0
57-
assert msg not in result.output
5860

5961

60-
@patch("snowflake.cli._app.version_check.VERSION", "1.0.0")
61-
@patch(
62-
"snowflake.cli._app.version_check._VersionCache.get_last_version",
63-
lambda _: Version("2.0.0"),
64-
)
62+
@patch(*_PATCH_VERSION)
63+
@patch(*_PATCH_LAST_VERSION) # type: ignore
6564
def test_get_new_version_msg_message_if_new_version_available():
6665
msg = get_new_version_msg()
6766
assert (
@@ -70,7 +69,7 @@ def test_get_new_version_msg_message_if_new_version_available():
7069
)
7170

7271

73-
@patch("snowflake.cli._app.version_check.VERSION", "1.0.0")
72+
@patch(*_PATCH_VERSION)
7473
@patch(
7574
"snowflake.cli._app.version_check._VersionCache.get_last_version", lambda _: None
7675
)
@@ -79,10 +78,7 @@ def test_get_new_version_msg_does_not_show_message_if_no_new_version():
7978

8079

8180
@patch("snowflake.cli._app.version_check.VERSION", "3.0.0")
82-
@patch(
83-
"snowflake.cli._app.version_check._VersionCache.get_last_version",
84-
lambda _: Version("2.0.0"),
85-
)
81+
@patch(*_PATCH_LAST_VERSION) # type: ignore
8682
def test_new_version_banner_does_not_show_message_if_local_version_is_newer():
8783
assert get_new_version_msg() is None
8884

0 commit comments

Comments
 (0)