Skip to content

Commit

Permalink
fixup! Missing log directory when disabled save logs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek committed Jul 12, 2024
1 parent f9ef647 commit 3302e7c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/snowflake/cli/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ def config_init(config_file: Optional[Path]):
Initializes the app configuration. Config provided via cli flag takes precedence.
If config file does not exist we create an empty one.
"""
from snowflake.cli.app.loggers import create_initial_loggers

if config_file:
CONFIG_MANAGER.file_path = config_file
else:
_check_default_config_files_permissions()
if not CONFIG_MANAGER.file_path.exists():
_initialise_config(CONFIG_MANAGER.file_path)
_read_config_file()
create_initial_loggers()


def add_connection(name: str, connection_config: ConnectionConfig):
Expand Down
2 changes: 0 additions & 2 deletions src/snowflake/cli/app/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from snowflake.cli.api.output.formats import OutputFormat
from snowflake.cli.api.output.types import CollectionResult
from snowflake.cli.api.secure_path import SecurePath
from snowflake.cli.app import loggers
from snowflake.cli.app.api_impl.plugin.plugin_config_provider_impl import (
PluginConfigProviderImpl,
)
Expand Down Expand Up @@ -89,7 +88,6 @@ def _commands_registration_callback(value: bool):
@_commands_registration.before
def _config_init_callback(configuration_file: Optional[Path]):
config_init(configuration_file)
loggers.create_initial_loggers()


@_commands_registration.before
Expand Down
7 changes: 4 additions & 3 deletions src/snowflake/cli/app/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
from typing import Any, Dict, List

import typer
from snowflake.cli.api.config import (
get_logs_config,
)
from snowflake.cli.api.exceptions import InvalidLogsConfiguration
from snowflake.cli.api.secure_path import SecurePath
from snowflake.connector.errors import ConfigSourceError
Expand Down Expand Up @@ -103,6 +100,10 @@ def _remove_underscore_prefixes_from_keys(d: Dict[str, Any]) -> None:

class FileLogsConfig:
def __init__(self, debug: bool) -> None:
from snowflake.cli.api.config import (
get_logs_config,
)

config = get_logs_config()

self.path: SecurePath = SecurePath(config["path"])
Expand Down
17 changes: 11 additions & 6 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ def _setup_config_and_logs(
)
config_path.chmod(0o700)

# Make sure we start without any leftovers
shutil.rmtree(logs_path, ignore_errors=True)
clean_logging_handlers()

# Setup loggers
config_init(config_path)
loggers.create_loggers(verbose=verbose, debug=debug)
assert len(_list_handlers()) == (2 if save_logs else 1)

yield logs_path
shutil.rmtree(logs_path)
shutil.rmtree(logs_path, ignore_errors=True)

return _setup_config_and_logs

Expand All @@ -98,7 +103,7 @@ def _list_handlers():
return logging.getLogger("snowflake.cli").handlers


def _get_logs_file(logs_path: Path) -> Path:
def get_logs_file(logs_path: Path) -> Path:
return next(logs_path.iterdir())


Expand All @@ -114,11 +119,11 @@ def assert_log_level(log_messages: str, expected_level: str) -> None:


def assert_file_log_level(logs_path: Path, expected_level: str) -> None:
assert_log_level(_get_logs_file(logs_path).read_text(), expected_level)
assert_log_level(get_logs_file(logs_path).read_text(), expected_level)


def assert_log_is_empty(logs_path: Path) -> None:
assert _get_logs_file(logs_path).read_text() == ""
assert get_logs_file(logs_path).read_text() == ""


def test_logs_section_appears_in_fresh_config_file(temp_dir):
Expand All @@ -145,7 +150,7 @@ def test_default_logs_location_is_created_automatically(setup_config_and_logs):
def test_logs_can_be_turned_off_by_config(setup_config_and_logs):
with setup_config_and_logs(save_logs=False) as logs_path:
print_log_messages()
assert_log_is_empty(logs_path)
assert not logs_path.exists()


def test_logs_path_is_configurable(setup_config_and_logs):
Expand Down Expand Up @@ -209,4 +214,4 @@ def test_incorrect_log_level_in_config(setup_config_and_logs):
def test_log_files_permissions(setup_config_and_logs):
with setup_config_and_logs(save_logs=True) as logs_path:
print_log_messages()
assert_file_permissions_are_strict(_get_logs_file(logs_path))
assert_file_permissions_are_strict(get_logs_file(logs_path))

0 comments on commit 3302e7c

Please sign in to comment.