Skip to content

Commit

Permalink
Merge branch 'main' into mchok-metric-spans-instrument-nade-cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mchok authored Nov 20, 2024
2 parents 2fbdcb6 + ff0a734 commit 05d81a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/snowflake/cli/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ def _dump_config(config_and_connections: Dict):
_update_connections_toml(config_and_connections.get("connections") or {})
# to config.toml save only connections from config.toml
connections_to_save_in_config_toml = _read_config_file_toml().get("connections")
config_toml_dict["connections"] = connections_to_save_in_config_toml
if connections_to_save_in_config_toml:
config_toml_dict["connections"] = connections_to_save_in_config_toml
else:
config_toml_dict.pop("connections", None)

with SecurePath(CONFIG_MANAGER.file_path).open("w+") as fh:
dump(config_toml_dict, fh)
Expand Down
26 changes: 17 additions & 9 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,16 @@ def test_not_found_default_connection_from_evn_variable(test_root_path):
)


@pytest.mark.parametrize(
"config_file,expected_number_of_entries_in_config",
[("test_snowcli_config", 1), ("empty_snowcli_config", 0)],
)
def test_correct_updates_of_connections_on_setting_default_connection(
test_snowcli_config, snowflake_home
config_file, expected_number_of_entries_in_config, snowflake_home, request
):
from snowflake.cli.api.config import CONFIG_MANAGER

config = request.getfixturevalue(config_file)
connections_toml = snowflake_home / "connections.toml"
connections_toml.write_text(
"""[asdf_a]
Expand All @@ -242,7 +247,7 @@ def test_correct_updates_of_connections_on_setting_default_connection(
account = "asdf_b"
"""
)
config_init(test_snowcli_config)
config_init(config)
set_config_value(section=None, key="default_connection_name", value="asdf_b")

def assert_correct_connections_loaded():
Expand Down Expand Up @@ -274,7 +279,7 @@ def assert_correct_connections_loaded():
assert (
connection_toml_content.count("jwt") == 0
) # connection from config.toml isn't copied to connections.toml
with open(test_snowcli_config) as f:
with open(config) as f:
config_toml_content = f.read()
assert (
config_toml_content.count("asdf_a") == 0
Expand All @@ -283,17 +288,20 @@ def assert_correct_connections_loaded():
config_toml_content.count("asdf_b") == 1
) # only default_config_name setting, connection from connections.toml isn't copied to config.toml
assert (
config_toml_content.count("connections.full") == 1
) # connection still exists in config.toml
config_toml_content.count("connections.full")
== expected_number_of_entries_in_config
) # connection wasn't erased from config.toml
assert (
config_toml_content.count("connections.jwt") == 1
) # connection still exists in config.toml
config_toml_content.count("connections.jwt")
== expected_number_of_entries_in_config
) # connection wasn't erased from config.toml
assert (
config_toml_content.count("dummy_flag = true") == 1
config_toml_content.count("dummy_flag = true")
== expected_number_of_entries_in_config
) # other settings are not erased

# reinit config file and recheck loaded connections
config_init(test_snowcli_config)
config_init(config)
assert_correct_connections_loaded()


Expand Down
7 changes: 7 additions & 0 deletions tests/testing_utils/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ def test_snowcli_config():
yield p


@pytest.fixture(scope="function")
def empty_snowcli_config():
with _named_temporary_file(suffix=".toml") as p:
p.chmod(0o600) # Make config file private
yield p


@pytest.fixture(scope="session")
def test_root_path():
return TEST_DIR
Expand Down

0 comments on commit 05d81a9

Please sign in to comment.