diff --git a/slack_cli_hooks/hooks/start.py b/slack_cli_hooks/hooks/start.py index 2866d25..ca0fe72 100644 --- a/slack_cli_hooks/hooks/start.py +++ b/slack_cli_hooks/hooks/start.py @@ -4,25 +4,12 @@ import sys from slack_cli_hooks.error import CliError -from slack_cli_hooks.hooks.utils import ManagedOSEnvVars from slack_cli_hooks.protocol import Protocol, build_protocol PROTOCOL: Protocol DEFAULT_MAIN_FILE = "app.py" -SLACK_CLI_XOXB = "SLACK_CLI_XOXB" -SLACK_CLI_XAPP = "SLACK_CLI_XAPP" -SLACK_BOT_TOKEN = "SLACK_BOT_TOKEN" -SLACK_APP_TOKEN = "SLACK_APP_TOKEN" - - -def validate_env() -> None: - if not os.environ.get(SLACK_CLI_XOXB): - raise CliError(f"Missing local run bot token ({SLACK_CLI_XOXB}).") - if not os.environ.get(SLACK_CLI_XAPP): - raise CliError(f"Missing local run app token ({SLACK_CLI_XAPP}).") - def get_main_file() -> str: custom_file = os.environ.get("SLACK_APP_PATH") @@ -38,25 +25,18 @@ def get_main_path(working_directory: str) -> str: def start(working_directory: str) -> None: - validate_env() - entrypoint_path = get_main_path(working_directory) if not os.path.exists(entrypoint_path): raise CliError(f"Could not find {get_main_file()} file") parent_package = os.path.dirname(entrypoint_path) - os_env_vars = ManagedOSEnvVars(PROTOCOL) try: - os_env_vars.set_if_absent(SLACK_BOT_TOKEN, os.environ[SLACK_CLI_XOXB]) - os_env_vars.set_if_absent(SLACK_APP_TOKEN, os.environ[SLACK_CLI_XAPP]) sys.path.insert(0, parent_package) # Add parent package to sys path - runpy.run_path(entrypoint_path, run_name="__main__") finally: sys.path.remove(parent_package) - os_env_vars.clear() if __name__ == "__main__": diff --git a/slack_cli_hooks/hooks/utils/__init__.py b/slack_cli_hooks/hooks/utils/__init__.py deleted file mode 100644 index 4515687..0000000 --- a/slack_cli_hooks/hooks/utils/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .managed_os_env_vars import ManagedOSEnvVars - -__all__ = [ - "ManagedOSEnvVars", -] diff --git a/slack_cli_hooks/hooks/utils/managed_os_env_vars.py b/slack_cli_hooks/hooks/utils/managed_os_env_vars.py deleted file mode 100644 index 917da04..0000000 --- a/slack_cli_hooks/hooks/utils/managed_os_env_vars.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -from typing import List -from slack_cli_hooks.protocol import Protocol - - -class ManagedOSEnvVars: - def __init__(self, protocol: Protocol) -> None: - self._protocol = protocol - self._os_env_vars: List[str] = [] - - def set_if_absent(self, os_env_var: str, value: str) -> None: - if os_env_var in os.environ: - self._protocol.info(f"{os_env_var} environment variable detected in session, using it over the provided one!") - return - self._os_env_vars.append(os_env_var) - os.environ[os_env_var] = value - - def clear(self) -> None: - for os_env_var in self._os_env_vars: - os.environ.pop(os_env_var, None) diff --git a/tests/scenario_test/test_app/app.py b/tests/scenario_test/test_app/app.py index 342a370..cfa0358 100644 --- a/tests/scenario_test/test_app/app.py +++ b/tests/scenario_test/test_app/app.py @@ -4,9 +4,6 @@ from slack_bolt.app import App from utils import get_test_socket_mode_handler, wait_for_test_socket_connection -assert "SLACK_BOT_TOKEN" in os.environ -assert "SLACK_APP_TOKEN" in os.environ - web_client = WebClient(base_url="http://localhost:8888", token=os.environ.get("SLACK_BOT_TOKEN")) app = App(signing_secret="valid", client=web_client) diff --git a/tests/scenario_test/test_app/my_app.py b/tests/scenario_test/test_app/my_app.py index 0639a73..e5af42b 100644 --- a/tests/scenario_test/test_app/my_app.py +++ b/tests/scenario_test/test_app/my_app.py @@ -4,9 +4,6 @@ from slack_bolt.app import App from utils import get_test_socket_mode_handler, wait_for_test_socket_connection -assert "SLACK_BOT_TOKEN" in os.environ -assert "SLACK_APP_TOKEN" in os.environ - web_client = WebClient(base_url="http://localhost:8888", token=os.environ.get("SLACK_BOT_TOKEN")) app = App(signing_secret="valid", client=web_client) diff --git a/tests/scenario_test/test_start.py b/tests/scenario_test/test_start.py index e88f047..175e93b 100644 --- a/tests/scenario_test/test_start.py +++ b/tests/scenario_test/test_start.py @@ -14,16 +14,16 @@ class TestStart: def setup_method(self): self.old_os_env = remove_os_env_temporarily() - os.environ["SLACK_CLI_XOXB"] = "xoxb-valid" - os.environ["SLACK_CLI_XAPP"] = "xapp-A111-222-xyz" + os.environ["SLACK_BOT_TOKEN"] = "xoxb-valid" + os.environ["SLACK_APP_TOKEN"] = "xapp-A111-222-xyz" setup_mock_web_api_server(self) start_socket_mode_server(self, 3012) self.cwd = os.getcwd() def teardown_method(self): os.chdir(self.cwd) - os.environ.pop("SLACK_CLI_XOXB", None) - os.environ.pop("SLACK_CLI_XAPP", None) + os.environ.pop("SLACK_BOT_TOKEN", None) + os.environ.pop("SLACK_APP_TOKEN", None) os.environ.pop("SLACK_APP_PATH", None) cleanup_mock_web_api_server(self) stop_socket_mode_server(self) diff --git a/tests/slack_cli_hooks/hooks/test_start.py b/tests/slack_cli_hooks/hooks/test_start.py index c475117..1ec83ba 100644 --- a/tests/slack_cli_hooks/hooks/test_start.py +++ b/tests/slack_cli_hooks/hooks/test_start.py @@ -1,12 +1,8 @@ -import pytest import os -from slack_cli_hooks.error import CliError -from slack_cli_hooks.hooks.start import validate_env, get_main_file, get_main_path +from slack_cli_hooks.hooks.start import get_main_file, get_main_path from tests.utils import remove_os_env_temporarily, restore_os_env -SLACK_CLI_XOXB = "SLACK_CLI_XOXB" -SLACK_CLI_XAPP = "SLACK_CLI_XAPP" SLACK_APP_PATH = "SLACK_APP_PATH" @@ -17,29 +13,9 @@ def setup_method(self): self.old_os_env = remove_os_env_temporarily() def teardown_method(self): - os.environ.pop(SLACK_CLI_XOXB, None) - os.environ.pop(SLACK_CLI_XAPP, None) os.environ.pop(SLACK_APP_PATH, None) restore_os_env(self.old_os_env) - def test_validate_env(self): - os.environ[SLACK_CLI_XOXB] = "xoxb-valid" - os.environ[SLACK_CLI_XAPP] = "xapp-A111-222-xyz" - - assert validate_env() is None - - def test_validate_env_with_missing_xoxb(self): - os.environ[SLACK_CLI_XAPP] = "xapp-A111-222-xyz" - with pytest.raises(CliError) as e: - validate_env() - assert str(e.value) == f"Missing local run bot token ({SLACK_CLI_XOXB})." - - def test_validate_env_with_missing_xapp(self): - os.environ[SLACK_CLI_XOXB] = "xoxb-valid" - with pytest.raises(CliError) as e: - validate_env() - assert str(e.value) == f"Missing local run app token ({SLACK_CLI_XAPP})." - def test_get_main_file(self): assert get_main_file() == "app.py" diff --git a/tests/slack_cli_hooks/hooks/utils/__init__.py b/tests/slack_cli_hooks/hooks/utils/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/slack_cli_hooks/hooks/utils/test_managed_os_env_vars.py b/tests/slack_cli_hooks/hooks/utils/test_managed_os_env_vars.py deleted file mode 100644 index 38370f9..0000000 --- a/tests/slack_cli_hooks/hooks/utils/test_managed_os_env_vars.py +++ /dev/null @@ -1,76 +0,0 @@ -import os -from slack_cli_hooks.hooks.utils import ManagedOSEnvVars -from slack_cli_hooks.protocol import DefaultProtocol -from tests.utils import remove_os_env_temporarily, restore_os_env - -TEST_VAR = "TEST" -TEST_VAR2 = "TEST2" - - -class TestManagedOSEnvVars: - def setup_method(self): - self.old_os_env = remove_os_env_temporarily() - - def teardown_method(self): - os.environ.pop(TEST_VAR, None) - os.environ.pop(TEST_VAR2, None) - restore_os_env(self.old_os_env) - - def test_set_if_absent(self): - expected = "expected test value" - os_env_vars = ManagedOSEnvVars(DefaultProtocol()) - os_env_vars.set_if_absent(TEST_VAR, expected) - - assert TEST_VAR in os.environ - assert os.environ[TEST_VAR] == expected - - def test_set_default_does_not_overwrite(self): - expected = "default test value" - os.environ[TEST_VAR] = expected - - os_env_vars = ManagedOSEnvVars(DefaultProtocol()) - os_env_vars.set_if_absent(TEST_VAR, "nothing") - - assert TEST_VAR in os.environ - assert os.environ[TEST_VAR] == expected - - def test_clear(self): - expected = "expected test value" - os_env_vars = ManagedOSEnvVars(DefaultProtocol()) - os_env_vars.set_if_absent(TEST_VAR, expected) - - os_env_vars.clear() - - assert not (TEST_VAR in os.environ) - - def test_clear_does_not_overwrite(self): - expected = "expected test value" - os.environ[TEST_VAR] = expected - - os_env_vars = ManagedOSEnvVars(DefaultProtocol()) - os_env_vars.set_if_absent(TEST_VAR, "nothing") - - os_env_vars.clear() - - assert TEST_VAR in os.environ - assert os.environ[TEST_VAR] == expected - - def test_clear_only_clears_absent_vars(self): - expected = "expected test value" - os.environ[TEST_VAR] = expected - - os_env_vars = ManagedOSEnvVars(DefaultProtocol()) - os_env_vars.set_if_absent(TEST_VAR, "nothing") - os_env_vars.set_if_absent(TEST_VAR2, expected) - - os_env_vars.clear() - - assert TEST_VAR in os.environ - assert os.environ[TEST_VAR] == expected - assert not (TEST_VAR2 in os.environ) - - def test_no_env_var_set(self): - os_env_vars = ManagedOSEnvVars(DefaultProtocol()) - - os_env_vars.clear() - assert TEST_VAR not in os.environ