Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions slack_cli_hooks/hooks/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}).")
Comment on lines -20 to -24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No harm in removing this IMO since an error will eventually be caught in the app if these variables are expected! I imagine this guard was to ensure hooks are called from the CLI but I'm honestly not sure 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly this was additional safeguard that was originally put in but we don't need it anymore



def get_main_file() -> str:
custom_file = os.environ.get("SLACK_APP_PATH")
Expand All @@ -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__":
Expand Down
5 changes: 0 additions & 5 deletions slack_cli_hooks/hooks/utils/__init__.py

This file was deleted.

20 changes: 0 additions & 20 deletions slack_cli_hooks/hooks/utils/managed_os_env_vars.py

This file was deleted.

3 changes: 0 additions & 3 deletions tests/scenario_test/test_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions tests/scenario_test/test_app/my_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/scenario_test/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 1 addition & 25 deletions tests/slack_cli_hooks/hooks/test_start.py
Original file line number Diff line number Diff line change
@@ -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"


Expand All @@ -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"

Expand Down
Empty file.
76 changes: 0 additions & 76 deletions tests/slack_cli_hooks/hooks/utils/test_managed_os_env_vars.py

This file was deleted.