summaryrefslogtreecommitdiff
path: root/test/extension_crafting.py
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-01-03 20:43:44 +0100
committerWojtek Kosior <koszko@koszko.org>2022-01-03 20:43:44 +0100
commit3840192d67a38604cfd6738c4f07d181a668ae68 (patch)
tree9924aa3ae6bab87abbf531891430ee07c2587c5f /test/extension_crafting.py
parentc71ebff86fa79b20388749dd4781fd96fcc5c63a (diff)
downloadbrowser-extension-3840192d67a38604cfd6738c4f07d181a668ae68.tar.gz
browser-extension-3840192d67a38604cfd6738c4f07d181a668ae68.zip
facilitate testing extension's HTML files
Diffstat (limited to 'test/extension_crafting.py')
-rw-r--r--test/extension_crafting.py44
1 files changed, 43 insertions, 1 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