test for server
This commit is contained in:
parent
c981231c76
commit
6c5153b4fe
|
@ -58,6 +58,7 @@ jobs:
|
|||
# TODO preserve apt cache..
|
||||
- run: sudo apt install python3-pip
|
||||
- run: python3 -m pip install --user mypy pylint pytest
|
||||
- run: sudo apt install httpie # only used for tests
|
||||
|
||||
- run:
|
||||
name: pylint
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/python3
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
import json
|
||||
|
@ -122,6 +122,7 @@ def run(port: str, capture_path: str, template: str):
|
|||
logger.info(f"Starting httpd on port {port}")
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
def setup_parser(p):
|
||||
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')
|
||||
|
@ -129,6 +130,50 @@ def setup_parser(p):
|
|||
{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():
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
|
||||
|
||||
|
|
Loading…
Reference in a new issue