From a7caf6c0f5e0825cf2cf4be4851b04c6affa4ee4 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 29 Jun 2019 19:01:22 +0100 Subject: [PATCH] Move test to the separate file --- server/grasp_server.py | 43 ---------------------------------- server/test_all.py | 33 +++++++++++++++++++-------- server/test_grasp.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 server/test_grasp.py diff --git a/server/grasp_server.py b/server/grasp_server.py index df2530d..e681aa8 100755 --- a/server/grasp_server.py +++ b/server/grasp_server.py @@ -131,49 +131,6 @@ def setup_parser(p): """) -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') diff --git a/server/test_all.py b/server/test_all.py index 6bb5d64..954f8bc 100755 --- a/server/test_all.py +++ b/server/test_all.py @@ -7,7 +7,9 @@ import re import json from selenium import webdriver # type: ignore -import pyautogui # type: ignore + + +from test_grasp import grasp_test_server addon_path = (Path(__file__).parent.parent / 'build').absolute() @@ -17,11 +19,13 @@ addon_path = (Path(__file__).parent.parent / 'build').absolute() def get_webdriver(): with TemporaryDirectory() as td: profile = webdriver.FirefoxProfile(td) + # use firefox from here to test https://www.mozilla.org/en-GB/firefox/developer/ driver = webdriver.Firefox(profile, firefox_binary='/L/soft/firefox-dev/firefox/firefox') - driver.install_addon(str(addon_path), temporary=True) - - yield driver - # TODO close driver? + try: + driver.install_addon(str(addon_path), temporary=True) + yield driver + finally: + driver.close() def open_options_page(driver): @@ -74,12 +78,17 @@ def trigger_grasp(): # pyautogui.locateOnScreen('/L/soft/browser-extensions/grasp/unicorn.png') print("sending hotkey!") + import pyautogui # type: ignore pyautogui.hotkey('ctrl', 'alt', 'c') -def test(): - with get_webdriver() as driver: - change_port(driver, port='17777') +def test(tmp_path: Path): + capture_file = tmp_path / 'output.org' + port = '17777' + template = '* [[%:link][%:description]]' + + with grasp_test_server(capture_file=capture_file, template=template, port=port), get_webdriver() as driver: + change_port(driver, port=port) driver.get('https://en.wikipedia.org/wiki/Automation') @@ -87,9 +96,15 @@ def test(): # TODO select something? trigger_grasp() + sleep(1) # just in case + # TODO come up with a better test and involve other template parameters + assert capture_file.read_text() == '* [[https://en.wikipedia.org/wiki/Automation][Automation - Wikipedia]]' + def main(): - test() + with TemporaryDirectory() as tdir: + test(Path(tdir)) + if __name__ == '__main__': main() diff --git a/server/test_grasp.py b/server/test_grasp.py new file mode 100644 index 0000000..72a0756 --- /dev/null +++ b/server/test_grasp.py @@ -0,0 +1,52 @@ +from contextlib import contextmanager +from pathlib import Path +from subprocess import Popen, check_call +from time import sleep + + +# ugh. +@contextmanager +def killmepls(*args, **kwargs): + with Popen(*args, **kwargs) as p: + try: + yield p + finally: + p.kill() + + +def grasp_test_server(capture_file: Path, port: str, template=None): + server = str((Path(__file__).parent / 'grasp_server.py').absolute()) + cmdline = [ + server, '--port', port, '--path', str(capture_file), + ] + if template is not None: + cmdline.extend(['--template', template]) + return killmepls(cmdline) + + +def test_server(tmp_path: Path): + 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 grasp_test_server(capture_file=cfile, port=PORT): + 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() +