-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into jsikorski/1163968-Pydantic-PoC-for-Streamlit
- Loading branch information
Showing
9 changed files
with
214 additions
and
10 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from enum import Enum, unique | ||
from typing import NamedTuple | ||
|
||
from snowflake.cli.api.config import ( | ||
FEATURE_FLAGS_SECTION_PATH, | ||
get_config_bool_value, | ||
get_env_variable_name, | ||
) | ||
|
||
|
||
class BooleanFlag(NamedTuple): | ||
name: str | ||
default: bool = False | ||
|
||
|
||
@unique | ||
class FeatureFlagMixin(Enum): | ||
def is_enabled(self) -> bool: | ||
return get_config_bool_value( | ||
*FEATURE_FLAGS_SECTION_PATH, | ||
key=self.value.name.lower(), | ||
default=self.value.default, | ||
) | ||
|
||
def is_disabled(self): | ||
return not self.is_enabled() | ||
|
||
def env_variable(self): | ||
return get_env_variable_name(*FEATURE_FLAGS_SECTION_PATH, key=self.value.name) | ||
|
||
|
||
@unique | ||
class FeatureFlag(FeatureFlagMixin): | ||
ENABLE_STREAMLIT_EMBEDDED_STAGE = BooleanFlag( | ||
"ENABLE_STREAMLIT_EMBEDDED_STAGE", False | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from click import ClickException | ||
from snowflake.cli.api.feature_flags import BooleanFlag, FeatureFlagMixin | ||
|
||
|
||
class _TestFlags(FeatureFlagMixin): | ||
# Intentional inconsistency between constant and the enum name to make sure there's no strict relation | ||
ENABLED_BY_DEFAULT = BooleanFlag("ENABLED_DEFAULT", True) | ||
DISABLED_BY_DEFAULT = BooleanFlag("DISABLED_DEFAULT", False) | ||
NON_BOOLEAN = BooleanFlag("NON_BOOLEAN", "xys") # type: ignore | ||
|
||
|
||
def test_flag_value_has_to_be_boolean(): | ||
with pytest.raises(ClickException): | ||
_TestFlags.NON_BOOLEAN.is_enabled() | ||
|
||
|
||
def test_flag_is_enabled(): | ||
assert _TestFlags.ENABLED_BY_DEFAULT.is_enabled() is True | ||
assert _TestFlags.ENABLED_BY_DEFAULT.is_disabled() is False | ||
|
||
|
||
def test_flag_is_disabled(): | ||
assert _TestFlags.DISABLED_BY_DEFAULT.is_enabled() is False | ||
assert _TestFlags.DISABLED_BY_DEFAULT.is_disabled() is True | ||
|
||
|
||
def test_flag_env_variable_value(): | ||
assert ( | ||
_TestFlags.ENABLED_BY_DEFAULT.env_variable() | ||
== "SNOWFLAKE_CLI_FEATURES_ENABLED_DEFAULT" | ||
) | ||
assert ( | ||
_TestFlags.DISABLED_BY_DEFAULT.env_variable() | ||
== "SNOWFLAKE_CLI_FEATURES_DISABLED_DEFAULT" | ||
) | ||
|
||
|
||
@mock.patch("snowflake.cli.api.config.get_config_value") | ||
@pytest.mark.parametrize("value_from_config", [True, False]) | ||
def test_flag_from_config_file(mock_get_config_value, value_from_config): | ||
mock_get_config_value.return_value = value_from_config | ||
|
||
assert _TestFlags.DISABLED_BY_DEFAULT.is_enabled() is value_from_config | ||
mock_get_config_value.assert_called_once_with( | ||
"cli", "features", key="disabled_default", default=False | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("value_from_env", ["1", "true", "True", "TRUE", "TruE"]) | ||
def test_flag_is_enabled_from_env_var(value_from_env): | ||
with mock.patch.dict( | ||
"os.environ", {"SNOWFLAKE_CLI_FEATURES_DISABLED_DEFAULT": value_from_env} | ||
): | ||
assert _TestFlags.DISABLED_BY_DEFAULT.is_enabled() is True | ||
|
||
|
||
@pytest.mark.parametrize("value_from_env", ["0", "false", "False", "FALSE", "FaLse"]) | ||
def test_flag_is_disabled_from_env_var(value_from_env): | ||
with mock.patch.dict( | ||
"os.environ", {"SNOWFLAKE_CLI_FEATURES_ENABLED_DEFAULT": value_from_env} | ||
): | ||
assert _TestFlags.ENABLED_BY_DEFAULT.is_enabled() is False |
19 changes: 19 additions & 0 deletions
19
tests/plugin/__snapshots__/test_override_by_external_plugins.ambr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# serializer version: 1 | ||
# name: test_corrupted_config_raises_human_friendly_error[[corrupted0] | ||
''' | ||
╭─ Error ──────────────────────────────────────────────────────────────────────╮ | ||
│ Configuration file seems to be corrupted. Unexpected end of file at line 1 │ | ||
│ col 10 │ | ||
╰──────────────────────────────────────────────────────────────────────────────╯ | ||
|
||
''' | ||
# --- | ||
# name: test_corrupted_config_raises_human_friendly_error[[corrupted1] | ||
''' | ||
╭─ Error ──────────────────────────────────────────────────────────────────────╮ | ||
│ Configuration file seems to be corrupted. Unexpected end of file at line 1 │ | ||
│ col 10 │ | ||
╰──────────────────────────────────────────────────────────────────────────────╯ | ||
|
||
''' | ||
# --- |