Skip to content

Commit b5ed2b2

Browse files
chore: unit tests around protocol usage (#48)
* chore: removing unused protocol * improve naming * Add the default * Improve based on feedback
1 parent d048022 commit b5ed2b2

21 files changed

Lines changed: 104 additions & 40 deletions

slack_cli_hooks/hooks/check_update.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
import json
33
from http.client import HTTPResponse
4+
import sys
45
from types import ModuleType
56
from typing import Any, Dict, List, Optional
67
from urllib import request
@@ -99,5 +100,5 @@ def build_output(dependencies: List[ModuleType] = DEPENDENCIES) -> Dict[str, Any
99100

100101

101102
if __name__ == "__main__":
102-
PROTOCOL = build_protocol()
103+
PROTOCOL = build_protocol(argv=sys.argv)
103104
PROTOCOL.respond(json.dumps(build_output()))

slack_cli_hooks/hooks/doctor.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/usr/bin/env python
22
import json
33
import platform
4+
import sys
45

5-
from slack_cli_hooks.protocol import (
6-
Protocol,
7-
build_protocol,
8-
)
6+
from slack_cli_hooks.protocol import Protocol, build_protocol
97

108
PROTOCOL: Protocol
119

@@ -28,5 +26,5 @@
2826
}
2927

3028
if __name__ == "__main__":
31-
PROTOCOL = build_protocol()
29+
PROTOCOL = build_protocol(argv=sys.argv)
3230
PROTOCOL.respond(json.dumps(doctor_payload))

slack_cli_hooks/hooks/get_hooks.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#!/usr/bin/env python
22
import json
33
import sys
4-
from slack_cli_hooks.protocol import (
5-
Protocol,
6-
MessageBoundaryProtocol,
7-
DefaultProtocol,
8-
build_protocol,
9-
)
4+
5+
from slack_cli_hooks.protocol import DefaultProtocol, MessageBoundaryProtocol, Protocol, build_protocol
106

117
PROTOCOL: Protocol
128

@@ -30,5 +26,5 @@
3026
}
3127

3228
if __name__ == "__main__":
33-
PROTOCOL = build_protocol()
29+
PROTOCOL = build_protocol(argv=sys.argv)
3430
PROTOCOL.respond(json.dumps(hooks_payload))

slack_cli_hooks/hooks/get_manifest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
import os
33
import re
4+
import sys
45
from typing import List
56

67
from slack_cli_hooks.error import CliError
@@ -46,5 +47,5 @@ def get_manifest(working_directory: str) -> str:
4647

4748

4849
if __name__ == "__main__":
49-
PROTOCOL = build_protocol()
50+
PROTOCOL = build_protocol(argv=sys.argv)
5051
PROTOCOL.respond(get_manifest(os.getcwd()))

slack_cli_hooks/hooks/start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ def start(working_directory: str) -> None:
4040

4141

4242
if __name__ == "__main__":
43-
PROTOCOL = build_protocol()
43+
PROTOCOL = build_protocol(argv=sys.argv)
4444
start(os.getcwd())

slack_cli_hooks/protocol/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import argparse
2-
import sys
32
from typing import List
3+
44
from .default_protocol import DefaultProtocol
55
from .message_boundary_protocol import MessageBoundaryProtocol
66
from .protocol import Protocol
77

8-
__all__ = [
9-
"DefaultProtocol",
10-
"MessageBoundaryProtocol",
11-
"Protocol",
12-
]
8+
__all__ = ["DefaultProtocol", "MessageBoundaryProtocol", "Protocol"]
139

1410

15-
def build_protocol(argv: List[str] = sys.argv[1:]) -> Protocol:
11+
def build_protocol(argv: List[str]) -> Protocol:
1612
parser = argparse.ArgumentParser()
1713
parser.add_argument("--protocol", type=str, required=False)
1814
parser.add_argument("--boundary", type=str, required=False)
1915

20-
args, unknown = parser.parse_known_args(args=argv)
16+
args, unknown = parser.parse_known_args(args=argv[1:])
2117

2218
if args.protocol == MessageBoundaryProtocol.name:
2319
return MessageBoundaryProtocol(boundary=args.boundary)

slack_cli_hooks/protocol/message_boundary_protocol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
23
from .protocol import Protocol
34

45

tests/mock_protocol.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from unittest.mock import Mock
2+
3+
from slack_cli_hooks.protocol.protocol import Protocol
4+
5+
6+
def debug(self, msg: str, *args, **kwargs):
7+
"""This is a mock"""
8+
pass
9+
10+
11+
def info(self, msg: str, *args, **kwargs):
12+
"""This is a mock"""
13+
pass
14+
15+
16+
def warning(self, msg: str, *args, **kwargs):
17+
"""This is a mock"""
18+
pass
19+
20+
21+
def error(self, msg: str, *args, **kwargs):
22+
"""This is a mock"""
23+
pass
24+
25+
26+
def respond(self, data: str):
27+
"""This is a mock"""
28+
pass
29+
30+
31+
class MockProtocol(Protocol):
32+
name: str = "MockProtocol"
33+
34+
debug = Mock(spec=debug, return_value=None)
35+
info = Mock(spec=info, return_value=None)
36+
warning = Mock(spec=warning, return_value=None)
37+
error = Mock(spec=error, return_value=None)
38+
respond = Mock(spec=respond, return_value=None)

tests/mock_socket_mode_server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import json
22
import threading
33
import time
4-
from urllib.request import urlopen
5-
from urllib.error import URLError
64
from unittest import TestCase
5+
from urllib.error import URLError
6+
from urllib.request import urlopen
7+
78
from flask import Flask
89
from gevent import pywsgi
910
from geventwebsocket.handler import WebSocketHandler

tests/mock_web_api_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from http.server import HTTPServer, SimpleHTTPRequestHandler
66
from typing import Type
77
from unittest import TestCase
8-
from urllib.parse import urlparse, ParseResult
8+
from urllib.parse import ParseResult, urlparse
99

1010

1111
class MockHandler(SimpleHTTPRequestHandler):

0 commit comments

Comments
 (0)