diff options
Diffstat (limited to 'test/script_loader.py')
-rw-r--r-- | test/script_loader.py | 55 |
1 files changed, 15 insertions, 40 deletions
diff --git a/test/script_loader.py b/test/script_loader.py index 8f30944..edf8143 100644 --- a/test/script_loader.py +++ b/test/script_loader.py @@ -41,54 +41,29 @@ def make_relative_path(path): return path -"""Used to ignore hidden files and emacs auto-save files.""" -script_name_regex = re.compile(r'^[^.#].*\.js$') +script_cache = {} -def available_scripts(directory): - for script in directory.rglob('*.js'): - if script_name_regex.match(script.name): - yield script - -def wrapped_script(script_path, wrap_partially=True): - if script_path == 'exports_init.js': - if not (script_root / 'exports_init.js').exists(): - subprocess.run([str(script_root / 'write_exports_init.sh'), - 'mozilla', '.', 'default_settings.json'], - cwd=script_root, check=True) - - with open(script_root / 'exports_init.js') as script: - return script.read() - - 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() - -def load_script(path, import_dirs): +def load_script(path): """ - `path` and `import_dirs` are .js file path and a list of directory paths, - respectively. They may be absolute or specified relative to Haketilo's - project directory. + `path` is a .js file path in Haketilo sources. It may be absolute or + specified relative to Haketilo's project directory. Return a string containing script from `path` together with all other - scripts it depends. Dependencies are wrapped in the same way Haketilo's + scripts it depends on. 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 + its code is executed in global scope instead of within an anonymous function + and imported variables are defined with `let` instead of `const` to allow + a dependency to be substituted by a mocked value. """ path = make_relative_path(path) + if str(path) in script_cache: + return script_cache[str(path)] - import_dirs = [make_relative_path(dir) for dir in import_dirs] - available = [s for dir in import_dirs for s in available_scripts(dir)] - - awk = subprocess.run(['awk', '-f', str(awk_script), 'script_dependencies', - str(path), *[str(s) for s in available]], + awk = subprocess.run(['awk', '-f', str(awk_script), '--', '-D', 'MOZILLA', + '-D', 'MV2', '--output=amalgamate-js:' + str(path)], stdout=subprocess.PIPE, cwd=script_root, check=True) + script = awk.stdout.decode() + script_cache[str(path)] = script - 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(texts) + return script |