diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/extension_crafting.py | 44 | ||||
-rw-r--r-- | test/misc_constants.py | 5 | ||||
-rw-r--r-- | test/script_loader.py | 8 | ||||
-rw-r--r-- | test/unit/test_basic.py | 23 |
4 files changed, 73 insertions, 7 deletions
diff --git a/test/extension_crafting.py b/test/extension_crafting.py index df45d26..0ffa5c8 100644 --- a/test/extension_crafting.py +++ b/test/extension_crafting.py @@ -29,6 +29,9 @@ import json import zipfile from pathlib import Path from uuid import uuid4 +from tempfile import TemporaryDirectory +import shutil +import subprocess from .misc_constants import * @@ -75,6 +78,43 @@ def manifest_template(): ] } +class ExtraHTML: + def __init__(self, html_path, append={}, wrap_into_htmldoc=True): + self.html_path = html_path + self.append = append + self.wrap_into_htmldoc = wrap_into_htmldoc + + def add_to_xpi(self, xpi, tmpdir=None): + if tmpdir is None: + with TemporaryDirectory() as tmpdir: + return self.add_to_xpi(xpi, tmpdir) + + append_flags = [] + for filename, code in self.append.items(): + append_flags.extend(['-A', f'{filename}:{code}']) + + awk = subprocess.run( + ['awk', '-f', awk_script_name, '--', *unit_test_defines, + *append_flags, '-H', self.html_path, '--write-js-deps', + '--output=files-to-copy', f'--output-dir={tmpdir}'], + stdout=subprocess.PIPE, cwd=script_root, check=True + ) + + for path in filter(None, awk.stdout.decode().split('\n')): + xpi.write(script_root / path, path) + + tmpdir = Path(tmpdir) + for path in tmpdir.rglob('*'): + relpath = str(path.relative_to(tmpdir)) + if not path.is_dir() and relpath != self.html_path: + xpi.write(path, relpath) + + with open(tmpdir / self.html_path, 'rt') as html_file: + html = html_file.read() + if self.wrap_into_htmldoc: + html = f'<!DOCTYPE html><html><body>{html}</body></html>' + xpi.writestr(self.html_path, html) + default_background_script = '' default_content_script = '' default_test_page = ''' @@ -102,7 +142,7 @@ def make_extension(destination_dir, background_script=default_background_script, content_script=default_content_script, test_page=default_test_page, - extra_files={}): + extra_files={}, extra_html=[]): manifest = manifest_template() extension_id = '{%s}' % uuid4() manifest['applications']['gecko']['id'] = extension_id @@ -120,5 +160,7 @@ def make_extension(destination_dir, if hasattr(contents, '__call__'): contents = contents() xpi.writestr(filename, contents) + for html in extra_html: + html.add_to_xpi(xpi) return destination_path diff --git a/test/misc_constants.py b/test/misc_constants.py index b3e9e32..cb7f2eb 100644 --- a/test/misc_constants.py +++ b/test/misc_constants.py @@ -30,6 +30,11 @@ Miscellaneous data that were found useful from pathlib import Path here = Path(__file__).resolve().parent +script_root = here.parent +awk_script_name = 'compute_scripts.awk' + +unit_test_defines = ['-D', 'MOZILLA', '-D', 'MV2', '-D', 'TEST', + '-D', 'UNIT_TEST', '-D', 'DEBUG'] default_firefox_binary = '/usr/lib/icecat/icecat' # The browser might be loading some globally-installed add-ons by default. They diff --git a/test/script_loader.py b/test/script_loader.py index f527d9e..07f75c7 100644 --- a/test/script_loader.py +++ b/test/script_loader.py @@ -30,9 +30,6 @@ 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) @@ -65,9 +62,8 @@ def load_script(path, code_to_add=None): append_flags = () if code_to_add is None else ('-A', ':'.join(key)) - awk = subprocess.run(['awk', '-f', str(awk_script), '--', '-D', 'MOZILLA', - '-D', 'MV2', '-D', 'TEST', '-D', 'UNIT_TEST', - '-D', 'DEBUG', *append_flags, + awk = subprocess.run(['awk', '-f', awk_script_name, '--', + *unit_test_defines, *append_flags, '--output=amalgamate-js:' + str(path)], stdout=subprocess.PIPE, cwd=script_root, check=True) script = awk.stdout.decode() diff --git a/test/unit/test_basic.py b/test/unit/test_basic.py index 2564e9d..612fe06 100644 --- a/test/unit/test_basic.py +++ b/test/unit/test_basic.py @@ -20,6 +20,7 @@ Haketilo unit tests - base import pytest from ..script_loader import load_script +from ..extension_crafting import ExtraHTML def test_driver(driver): """ @@ -55,3 +56,25 @@ def test_webextension(driver): 'return document.getElementsByTagName("h1")[0].innerText;' ) assert "Extension's options page for testing" in heading + +@pytest.mark.ext_data({ + 'extra_html': [ + ExtraHTML( + 'html/default_blocking_policy.html', + { + 'html/default_blocking_policy.js': + 'document.body.innerHTML = `ski-ba-bop-ba ${typeof by_id}`;' + } + ) + ] +}) +@pytest.mark.usefixtures('webextension') +def test_extra_html(driver): + """ + A trivial test case of the facility for loading the Haketilo's HTML files + into test WebExtension for unit-testing. + """ + driver.get(driver.execute_script('return window.location.href;') + .replace('testpage.html', 'html/default_blocking_policy.html')) + assert driver.execute_script('return document.body.innerText') == \ + 'ski-ba-bop-ba function' |