From 3840192d67a38604cfd6738c4f07d181a668ae68 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 3 Jan 2022 20:43:44 +0100 Subject: facilitate testing extension's HTML files --- test/extension_crafting.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'test/extension_crafting.py') 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'{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 -- cgit v1.2.3