2
2
from itertools import cycle
3
3
from unittest .mock import patch
4
4
5
+ import pytest
5
6
from packaging .version import Version
6
7
from requests import Response
7
8
from snowflake .cli ._app .version_check import _VersionCache , get_new_version_msg
8
9
from snowflake .cli .api .secure_path import SecurePath
9
10
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 = [
13
16
"snowflake.cli._app.version_check._VersionCache.get_last_version" ,
14
17
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
+ ]
21
19
22
20
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
33
25
34
26
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" ])
45
51
46
52
47
53
@patch ("snowflake.cli._app.version_check._VersionCache._read_latest_version" )
48
54
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
50
56
):
51
57
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" ])
56
59
assert result .exit_code == 0
57
- assert msg not in result .output
58
60
59
61
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
65
64
def test_get_new_version_msg_message_if_new_version_available ():
66
65
msg = get_new_version_msg ()
67
66
assert (
@@ -70,7 +69,7 @@ def test_get_new_version_msg_message_if_new_version_available():
70
69
)
71
70
72
71
73
- @patch ("snowflake.cli._app.version_check.VERSION" , "1.0.0" )
72
+ @patch (* _PATCH_VERSION )
74
73
@patch (
75
74
"snowflake.cli._app.version_check._VersionCache.get_last_version" , lambda _ : None
76
75
)
@@ -79,10 +78,7 @@ def test_get_new_version_msg_does_not_show_message_if_no_new_version():
79
78
80
79
81
80
@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
86
82
def test_new_version_banner_does_not_show_message_if_local_version_is_newer ():
87
83
assert get_new_version_msg () is None
88
84
0 commit comments