Move test to the separate file

This commit is contained in:
Dima Gerasimov 2019-06-29 19:01:22 +01:00 committed by Dmitrii Gerasimov
parent 2fa4f81968
commit a7caf6c0f5
3 changed files with 76 additions and 52 deletions

View file

@ -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(): 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')

View file

@ -7,7 +7,9 @@ import re
import json import json
from selenium import webdriver # type: ignore 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() addon_path = (Path(__file__).parent.parent / 'build').absolute()
@ -17,11 +19,13 @@ addon_path = (Path(__file__).parent.parent / 'build').absolute()
def get_webdriver(): def get_webdriver():
with TemporaryDirectory() as td: with TemporaryDirectory() as td:
profile = webdriver.FirefoxProfile(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 = webdriver.Firefox(profile, firefox_binary='/L/soft/firefox-dev/firefox/firefox')
try:
driver.install_addon(str(addon_path), temporary=True) driver.install_addon(str(addon_path), temporary=True)
yield driver yield driver
# TODO close driver? finally:
driver.close()
def open_options_page(driver): def open_options_page(driver):
@ -74,12 +78,17 @@ def trigger_grasp():
# pyautogui.locateOnScreen('/L/soft/browser-extensions/grasp/unicorn.png') # pyautogui.locateOnScreen('/L/soft/browser-extensions/grasp/unicorn.png')
print("sending hotkey!") print("sending hotkey!")
import pyautogui # type: ignore
pyautogui.hotkey('ctrl', 'alt', 'c') pyautogui.hotkey('ctrl', 'alt', 'c')
def test(): def test(tmp_path: Path):
with get_webdriver() as driver: capture_file = tmp_path / 'output.org'
change_port(driver, port='17777') 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') driver.get('https://en.wikipedia.org/wiki/Automation')
@ -87,9 +96,15 @@ def test():
# TODO select something? # TODO select something?
trigger_grasp() 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(): def main():
test() with TemporaryDirectory() as tdir:
test(Path(tdir))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

52
server/test_grasp.py Normal file
View file

@ -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()