From 463e6830faf5bb81474ac55cf95eed6ae68cc684 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 1 Dec 2021 14:02:42 +0100 Subject: facilitate testing javascript functions Haketilo's .js files can now be loaded together with their dependencies and executed on a page opened in a selenium-driven Firefox instance. --- test/script_loader.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_unit.py | 34 +++++++++++++++------ 2 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 test/script_loader.py (limited to 'test') diff --git a/test/script_loader.py b/test/script_loader.py new file mode 100644 index 0000000..22196c3 --- /dev/null +++ b/test/script_loader.py @@ -0,0 +1,84 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +""" +Loading of parts of Haketilo source for testing in browser +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021 Wojtek Kosior +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use this code in a +# proprietary program, I am not going to enforce this in court. + +from pathlib import Path +import subprocess, re + +from .misc_constants import * + +script_root = here.parent +awk_script = script_root / 'compute_scripts.awk' + +def make_relative_path(path): + path = Path(path) + + if path.is_absolute(): + path = path.relative_to(script_root) + + return path + +"""Used to ignore hidden files and emacs auto-save files.""" +script_name_regex = re.compile(r'^[^.#].*\.js$') + +def available_scripts(directory): + for script in directory.rglob('*.js'): + if script_name_regex.match(script.name): + yield script + +def get_wrapped_script(script_path): + if script_path == 'exports_init.js': + with open(script_root / 'MOZILLA_exports_init.js') as script: + return script.read() + + awk = subprocess.run(['awk', '-f', str(awk_script), 'wrapped_code', + str(script_path)], + stdout=subprocess.PIPE, cwd=script_root, check=True) + + return awk.stdout.decode() + +def load_script(path, import_dirs): + """ + `path` and `import_dirs` are .js file path and a list of directory paths, + respectively. They may be absolute or specified relative to Haketilo's + project directory. + + Return a string containing script from `path` together with all other + scripts it depends on, wrapped in the same way Haketilo's build system wraps + them, with imports properly satisfied. + """ + path = make_relative_path(path) + + import_dirs = [make_relative_path(dir) for dir in import_dirs] + available = [s for dir in import_dirs for s in available_scripts(dir)] + + awk = subprocess.run(['awk', '-f', str(awk_script), 'script_dependencies', + str(path), *[str(s) for s in available]], + stdout=subprocess.PIPE, cwd=script_root, check=True) + + output = awk.stdout.decode() + + return '\n'.join([get_wrapped_script(path) for path in output.split()]) diff --git a/test/test_unit.py b/test/test_unit.py index 50a80df..ce46f88 100644 --- a/test/test_unit.py +++ b/test/test_unit.py @@ -19,23 +19,39 @@ Haketilo unit tests # CC0 1.0 Universal License for more details. import pytest -from .profiles import firefox_safe_mode -from .server import do_an_internet +from .profiles import firefox_safe_mode +from .server import do_an_internet +from .script_loader import load_script -@pytest.fixture +@pytest.fixture(scope="module") def proxy(): httpd = do_an_internet() yield httpd httpd.shutdown() -@pytest.fixture +@pytest.fixture(scope="module") def driver(proxy): with firefox_safe_mode() as driver: yield driver driver.quit() -def test_basic(driver): - driver.get('https://gotmyowndoma.in') - element = driver.find_element_by_tag_name('title') - title = driver.execute_script('return arguments[0].innerText;', element) - assert "Schrodinger's Document" in title +def test_proxy(driver): + """ + A trivial test case that verifies mocked web pages served by proxy can be + accessed by the browser driven. + """ + for proto in ['http://', 'https://']: + driver.get(proto + 'gotmyowndoma.in') + element = driver.find_element_by_tag_name('title') + title = driver.execute_script('return arguments[0].innerText;', element) + assert "Schrodinger's Document" in title + +def test_script_loader(driver): + """ + A trivial test case that verifies Haketilo's .js files can be properly + loaded into a test page together with their dependencies. + """ + driver.get('http://gotmyowndoma.in') + driver.execute_script(load_script('common/stored_types.js', ['common'])) + get_var_prefix = 'return window.haketilo_exports.TYPE_PREFIX.VAR;' + assert driver.execute_script(get_var_prefix) == '_' -- cgit v1.2.3