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. --- CHROMIUM_exports_init.js | 2 +- MOZILLA_exports_init.js | 2 +- background/main.js | 4 +-- compute_scripts.awk | 5 +-- copyright | 2 +- test/script_loader.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test_unit.py | 34 ++++++++++++++------ 7 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 test/script_loader.py diff --git a/CHROMIUM_exports_init.js b/CHROMIUM_exports_init.js index d2ca065..0e61d40 100644 --- a/CHROMIUM_exports_init.js +++ b/CHROMIUM_exports_init.js @@ -1,3 +1,3 @@ // SPDX-License-Identifier: CC0-1.0 -window.killtheweb={is_chrome: true, browser: window.chrome}; +window.haketilo_exports = {is_chrome: true, browser: window.chrome}; diff --git a/MOZILLA_exports_init.js b/MOZILLA_exports_init.js index 0015f0c..a1135e8 100644 --- a/MOZILLA_exports_init.js +++ b/MOZILLA_exports_init.js @@ -54,4 +54,4 @@ String.prototype.matchAll = String.prototype.matchAll || function(regex) { } } -window.killtheweb={is_mozilla: true, browser: this.browser}; +window.haketilo_exports = {is_mozilla: true, browser: this.browser}; diff --git a/background/main.js b/background/main.js index 9cdfb97..358d549 100644 --- a/background/main.js +++ b/background/main.js @@ -186,9 +186,9 @@ start_webRequest_operations(); const code = `\ console.warn("Hi, I'm Mr Dynamic!"); -console.debug("let's see how window.killtheweb looks like now"); +console.debug("let's see how window.haketilo_exports looks like now"); -console.log("killtheweb", window.killtheweb); +console.log("haketilo_exports", window.haketilo_exports); ` async function test_dynamic_content_scripts() diff --git a/compute_scripts.awk b/compute_scripts.awk index 123106c..2bad3c5 100644 --- a/compute_scripts.awk +++ b/compute_scripts.awk @@ -92,7 +92,8 @@ function print_imports_code(filename, i, count, import_name) { count = import_counts[filename] for (i = 1; i <= count; i++) { import_name = imports[filename,i] - printf "const %s = window.killtheweb.%s;\n", import_name, import_name + printf "const %s = window.haketilo_exports.%s;\n", + import_name, import_name } } @@ -100,7 +101,7 @@ function print_exports_code(filename, i, count, export_name) { count = export_counts[filename] for (i = 1; i <= count; i++) { export_name = exports[filename,i] - printf "window.killtheweb.%s = %s;\n", export_name, export_name + printf "window.haketilo_exports.%s = %s;\n", export_name, export_name } } diff --git a/copyright b/copyright index a238d33..c7934b7 100644 --- a/copyright +++ b/copyright @@ -79,7 +79,7 @@ Files: test/__init__.py test/test_unit.py test/default_profiles/icecat_empty/ext Copyright: 2021 Wojtek Kosior License: CC0 -Files: test/profiles.py +Files: test/profiles.py test/script_loader.py Copyright: 2021 Wojtek Kosior License: GPL-3+ Comment: Wojtek Kosior promises not to sue even in case of violations 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