test for server

This commit is contained in:
Dima Gerasimov 2019-06-29 16:18:09 +01:00 committed by Dmitrii Gerasimov
parent c981231c76
commit 6c5153b4fe
2 changed files with 47 additions and 1 deletions

View file

@ -58,6 +58,7 @@ jobs:
# TODO preserve apt cache.. # TODO preserve apt cache..
- run: sudo apt install python3-pip - run: sudo apt install python3-pip
- run: python3 -m pip install --user mypy pylint pytest - run: python3 -m pip install --user mypy pylint pytest
- run: sudo apt install httpie # only used for tests
- run: - run:
name: pylint name: pylint

View file

@ -1,4 +1,4 @@
#!/usr/bin/python3 #!/usr/bin/env python3
import argparse import argparse
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
import json import json
@ -122,6 +122,7 @@ def run(port: str, capture_path: str, template: str):
logger.info(f"Starting httpd on port {port}") logger.info(f"Starting httpd on port {port}")
httpd.serve_forever() httpd.serve_forever()
def setup_parser(p): def setup_parser(p):
p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension') p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension')
p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into') p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into')
@ -129,6 +130,50 @@ def setup_parser(p):
{as_org.__doc__} {as_org.__doc__}
""") """)
def test_server(tmp_path: Path):
from subprocess import Popen, check_call
from time import sleep
from contextlib import contextmanager
# ugh.
@contextmanager
def killmepls(*args, **kwargs):
with Popen(*args, **kwargs) as p:
try:
yield p
finally:
p.kill()
cfile = tmp_path / 'test-capture.org'
PORT = '17777'
def send(url: str, title: str):
# TODO eh, not sure if there is anything easier than httpie
check_call([
'http', '--ignore-stdin', 'POST', f'http://localhost:{PORT}',
f'url={url}',
f'title={title}',
'selection=null',
'comment=null',
'tag_str=null',
])
with killmepls([
__file__, '--port', PORT, '--path', cfile,
]) as s:
sleep(0.5) # startup
send(url='twitter.com', title='Twitter')
sleep(0.5)
assert 'twitter.com' in cfile.read_text()
# TODO er... wonder how nulls are converted into Nones..
# assert 'null' not in cfile.read_text()
def main(): def main():
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')