summaryrefslogtreecommitdiff
path: root/test/script_loader.py
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-11-24 15:53:00 +0100
committerWojtek Kosior <koszko@koszko.org>2021-12-01 21:06:28 +0100
commit93dd73600e91eb19e11f5ca57f9429a85cf0150f (patch)
tree1e90890a39798f6cd9a1c0886d1234ccc187f5b3 /test/script_loader.py
parent463e6830faf5bb81474ac55cf95eed6ae68cc684 (diff)
downloadbrowser-extension-93dd73600e91eb19e11f5ca57f9429a85cf0150f.tar.gz
browser-extension-93dd73600e91eb19e11f5ca57f9429a85cf0150f.zip
improve unit testing approach
Unit tests were moved to their own subdirectory. Fixtures common to many unit tests were moved to test/unit/conftest.py. A facility to execute scripts in page's global scope was added. A workaround was employed to present information about errors in injected scripts. Sample unit tests for regexes in common/patterns.js were added.
Diffstat (limited to 'test/script_loader.py')
-rw-r--r--test/script_loader.py21
1 files changed, 13 insertions, 8 deletions
diff --git a/test/script_loader.py b/test/script_loader.py
index 22196c3..15269c7 100644
--- a/test/script_loader.py
+++ b/test/script_loader.py
@@ -49,14 +49,15 @@ def available_scripts(directory):
if script_name_regex.match(script.name):
yield script
-def get_wrapped_script(script_path):
+def wrapped_script(script_path, wrap_partially=True):
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)
+ command = 'partially_wrapped_code' if wrap_partially else 'wrapped_code'
+ awk_command = ['awk', '-f', str(awk_script), command, str(script_path)]
+ awk = subprocess.run(awk_command, stdout=subprocess.PIPE, cwd=script_root,
+ check=True)
return awk.stdout.decode()
@@ -67,8 +68,10 @@ def load_script(path, import_dirs):
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.
+ scripts it depends. Dependencies are wrapped in the same way Haketilo's
+ build system wraps them, with imports properly satisfied. The main script
+ being loaded is wrapped partially - it also has its imports satisfied, but
+ its code is not placed inside an anonymous function, so the
"""
path = make_relative_path(path)
@@ -79,6 +82,8 @@ def load_script(path, import_dirs):
str(path), *[str(s) for s in available]],
stdout=subprocess.PIPE, cwd=script_root, check=True)
- output = awk.stdout.decode()
+ to_load = awk.stdout.decode().split()
+ texts = [wrapped_script(path, wrap_partially=(i == len(to_load) - 1))
+ for i, path in enumerate(to_load)]
- return '\n'.join([get_wrapped_script(path) for path in output.split()])
+ return '\n'.join(texts)