diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-01-03 20:43:44 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-01-03 20:43:44 +0100 |
commit | 3840192d67a38604cfd6738c4f07d181a668ae68 (patch) | |
tree | 9924aa3ae6bab87abbf531891430ee07c2587c5f | |
parent | c71ebff86fa79b20388749dd4781fd96fcc5c63a (diff) | |
download | browser-extension-3840192d67a38604cfd6738c4f07d181a668ae68.tar.gz browser-extension-3840192d67a38604cfd6738c4f07d181a668ae68.zip |
facilitate testing extension's HTML files
-rwxr-xr-x | compute_scripts.awk | 10 | ||||
-rw-r--r-- | html/default_blocking_policy.js | 2 | ||||
-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 |
6 files changed, 81 insertions, 11 deletions
diff --git a/compute_scripts.awk b/compute_scripts.awk index 43d8a65..e5efc49 100755 --- a/compute_scripts.awk +++ b/compute_scripts.awk @@ -626,15 +626,17 @@ BEGIN { } function main(i, j, path, letter, dir, max_line_nr, js_deps, js_deps_count, - code, lines) { + code, tmp_lines) { output_dir = "./build" write_js_deps = false write_html_deps = false delete appended_lines[0] delete appended_lines_counts[0] + delete tmp_lines[0] delete lines[0] + delete lines_count[0] output = "" js_to_amalgamate = "" @@ -678,12 +680,12 @@ function main(i, j, path, letter, dir, max_line_nr, js_deps, js_deps_count, modes[path] = "js" - clear_array(lines) + clear_array(tmp_lines) code = ARGV[i] sub(/^[^:]+:/, "", code) - appended_lines_counts[path] = split(code, lines, "\n") + appended_lines_counts[path] = split(code, tmp_lines, "\n") for (j = appended_lines_counts[path]; j > 0; j--) - appended_lines[path,j] = lines[j] + appended_lines[path,j] = tmp_lines[j] } } else if (ARGV[i] ~ /^-(-help|h)$/ ) { print_usage() diff --git a/html/default_blocking_policy.js b/html/default_blocking_policy.js index 47435eb..dcc5d0b 100644 --- a/html/default_blocking_policy.js +++ b/html/default_blocking_policy.js @@ -72,4 +72,6 @@ async function init_default_policy_dialog() blocking_policy_span.classList.remove("hide"); } +#IF !TEST_UNIT init_default_policy_dialog(); +#ENDIF 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' |