Skip to content

Commit 18a3932

Browse files
committed
SNOW-2306184: config refactor - show-config-sources tests
1 parent a0bb96c commit 18a3932

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Copyright (c) 2024 Snowflake Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
from unittest import mock
17+
18+
from snowflake.cli.api.config_provider import ALTERNATIVE_CONFIG_ENV_VAR
19+
20+
COMMAND = "show-config-sources"
21+
22+
23+
class TestHiddenLogic:
24+
"""
25+
Test the logic that determines if the command should be hidden.
26+
27+
Note: The 'hidden' parameter in Typer decorators is evaluated at module import time,
28+
so we test the logic itself rather than the runtime visibility in help output.
29+
"""
30+
31+
def test_hidden_logic_with_truthy_values(self):
32+
"""Test that the hidden logic correctly identifies truthy values."""
33+
truthy_values = ["1", "true", "yes", "on", "TRUE", "Yes", "ON"]
34+
for value in truthy_values:
35+
# This is the logic used in the command decorator
36+
is_hidden = value.lower() not in ("1", "true", "yes", "on")
37+
assert (
38+
not is_hidden
39+
), f"Value '{value}' should make command visible (not hidden)"
40+
41+
def test_hidden_logic_with_falsy_values(self):
42+
"""Test that the hidden logic correctly identifies falsy values."""
43+
falsy_values = ["", "0", "false", "no", "off", "random"]
44+
for value in falsy_values:
45+
# This is the logic used in the command decorator
46+
is_hidden = value.lower() not in ("1", "true", "yes", "on")
47+
assert is_hidden, f"Value '{value}' should make command hidden"
48+
49+
50+
class TestCommandFunctionality:
51+
"""Test that the command functions correctly when called."""
52+
53+
@mock.patch.dict(os.environ, {}, clear=True)
54+
def test_command_unavailable_without_env_var(self, runner):
55+
"""Command should indicate resolution logging is unavailable without env var."""
56+
result = runner.invoke(["helpers", COMMAND])
57+
assert result.exit_code == 0
58+
assert "Configuration resolution logging is not available" in result.output
59+
assert ALTERNATIVE_CONFIG_ENV_VAR in result.output
60+
61+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
62+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
63+
def test_command_unavailable_message_when_logging_not_available(
64+
self, mock_is_available, runner
65+
):
66+
"""Command should show unavailable message when resolution logging is not available."""
67+
mock_is_available.return_value = False
68+
result = runner.invoke(["helpers", COMMAND])
69+
assert result.exit_code == 0
70+
assert "Configuration resolution logging is not available" in result.output
71+
72+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
73+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
74+
@mock.patch("snowflake.cli.api.config_ng.explain_configuration")
75+
def test_command_shows_summary_without_arguments(
76+
self, mock_explain, mock_is_available, runner
77+
):
78+
"""Command should show configuration summary when called without arguments."""
79+
mock_is_available.return_value = True
80+
result = runner.invoke(["helpers", COMMAND])
81+
assert result.exit_code == 0
82+
mock_explain.assert_called_once_with(key=None, verbose=False)
83+
assert "Configuration resolution summary displayed above" in result.output
84+
85+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
86+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
87+
@mock.patch("snowflake.cli.api.config_ng.explain_configuration")
88+
def test_command_shows_specific_key(self, mock_explain, mock_is_available, runner):
89+
"""Command should show resolution for specific key when provided."""
90+
mock_is_available.return_value = True
91+
result = runner.invoke(["helpers", COMMAND, "account"])
92+
assert result.exit_code == 0
93+
mock_explain.assert_called_once_with(key="account", verbose=False)
94+
assert "Showing resolution for key: account" in result.output
95+
96+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
97+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
98+
@mock.patch("snowflake.cli.api.config_ng.explain_configuration")
99+
def test_command_shows_details_with_flag(
100+
self, mock_explain, mock_is_available, runner
101+
):
102+
"""Command should show detailed resolution when --show-details flag is used."""
103+
mock_is_available.return_value = True
104+
result = runner.invoke(["helpers", COMMAND, "--show-details"])
105+
assert result.exit_code == 0
106+
mock_explain.assert_called_once_with(key=None, verbose=True)
107+
assert "Configuration resolution summary displayed above" in result.output
108+
109+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
110+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
111+
@mock.patch("snowflake.cli.api.config_ng.explain_configuration")
112+
def test_command_shows_details_with_short_flag(
113+
self, mock_explain, mock_is_available, runner
114+
):
115+
"""Command should show detailed resolution when -d flag is used."""
116+
mock_is_available.return_value = True
117+
result = runner.invoke(["helpers", COMMAND, "-d"])
118+
assert result.exit_code == 0
119+
mock_explain.assert_called_once_with(key=None, verbose=True)
120+
121+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
122+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
123+
@mock.patch("snowflake.cli.api.config_ng.explain_configuration")
124+
def test_command_shows_key_with_details(
125+
self, mock_explain, mock_is_available, runner
126+
):
127+
"""Command should show detailed resolution for specific key."""
128+
mock_is_available.return_value = True
129+
result = runner.invoke(["helpers", COMMAND, "user", "--show-details"])
130+
assert result.exit_code == 0
131+
mock_explain.assert_called_once_with(key="user", verbose=True)
132+
assert "Showing resolution for key: user" in result.output
133+
134+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
135+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
136+
@mock.patch("snowflake.cli.api.config_ng.export_resolution_history")
137+
def test_command_exports_to_file_success(
138+
self, mock_export, mock_is_available, runner, tmp_path
139+
):
140+
"""Command should export resolution history to file when --export is used."""
141+
mock_is_available.return_value = True
142+
mock_export.return_value = True
143+
export_file = tmp_path / "config_debug.json"
144+
145+
result = runner.invoke(["helpers", COMMAND, "--export", str(export_file)])
146+
assert result.exit_code == 0
147+
mock_export.assert_called_once_with(export_file)
148+
assert "Resolution history exported to:" in result.output
149+
assert str(export_file) in result.output
150+
151+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
152+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
153+
@mock.patch("snowflake.cli.api.config_ng.export_resolution_history")
154+
def test_command_exports_to_file_with_short_flag(
155+
self, mock_export, mock_is_available, runner, tmp_path
156+
):
157+
"""Command should export resolution history to file when -e is used."""
158+
mock_is_available.return_value = True
159+
mock_export.return_value = True
160+
export_file = tmp_path / "debug.json"
161+
162+
result = runner.invoke(["helpers", COMMAND, "-e", str(export_file)])
163+
assert result.exit_code == 0
164+
mock_export.assert_called_once_with(export_file)
165+
166+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
167+
@mock.patch("snowflake.cli.api.config_ng.is_resolution_logging_available")
168+
@mock.patch("snowflake.cli.api.config_ng.export_resolution_history")
169+
def test_command_export_failure(
170+
self, mock_export, mock_is_available, runner, tmp_path
171+
):
172+
"""Command should show error message when export fails."""
173+
mock_is_available.return_value = True
174+
mock_export.return_value = False
175+
export_file = tmp_path / "config_debug.json"
176+
177+
result = runner.invoke(["helpers", COMMAND, "--export", str(export_file)])
178+
assert result.exit_code == 0
179+
assert "Failed to export resolution history" in result.output
180+
181+
182+
class TestCommandHelp:
183+
"""Test the command help output."""
184+
185+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
186+
def test_command_help_message(self, runner):
187+
"""Command help should display correctly."""
188+
result = runner.invoke(["helpers", COMMAND, "--help"])
189+
assert result.exit_code == 0
190+
assert "Show where configuration values come from" in result.output
191+
assert "--show-details" in result.output
192+
assert "--export" in result.output
193+
assert "Examples:" in result.output
194+
195+
@mock.patch.dict(os.environ, {ALTERNATIVE_CONFIG_ENV_VAR: "1"}, clear=True)
196+
def test_command_help_shows_key_argument(self, runner):
197+
"""Command help should show the optional key argument."""
198+
result = runner.invoke(["helpers", COMMAND, "--help"])
199+
assert result.exit_code == 0
200+
assert "KEY" in result.output or "key" in result.output.lower()
201+
assert "account" in result.output or "user" in result.output

0 commit comments

Comments
 (0)