From ff0a734f3b25cca1c176dff48851260eed8e3927 Mon Sep 17 00:00:00 2001 From: Jan Sikorski <132985823+sfc-gh-jsikorski@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:20:28 +0100 Subject: [PATCH] Fix for connections.toml with empty config.toml (#1875) Solution --- src/snowflake/cli/api/config.py | 5 ++++- tests/test_config.py | 26 +++++++++++++++++--------- tests/testing_utils/fixtures.py | 7 +++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/snowflake/cli/api/config.py b/src/snowflake/cli/api/config.py index 657f361f6f..b6e26a68d7 100644 --- a/src/snowflake/cli/api/config.py +++ b/src/snowflake/cli/api/config.py @@ -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) diff --git a/tests/test_config.py b/tests/test_config.py index f08ef2207c..582af03354 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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] @@ -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(): @@ -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 @@ -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() diff --git a/tests/testing_utils/fixtures.py b/tests/testing_utils/fixtures.py index fabed4d215..ee731735b1 100644 --- a/tests/testing_utils/fixtures.py +++ b/tests/testing_utils/fixtures.py @@ -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