tests: Move userscript importing to helpers
This commit is contained in:
parent
b265c737ce
commit
b6f2276f04
|
|
@ -26,6 +26,8 @@ import pprint
|
|||
import os.path
|
||||
import contextlib
|
||||
import pathlib
|
||||
import importlib.util
|
||||
import importlib.machinery
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -282,3 +284,20 @@ def seccomp_args(qt_flag):
|
|||
return ['--qt-flag', disable_arg] if qt_flag else ['--' + disable_arg]
|
||||
|
||||
return []
|
||||
|
||||
|
||||
def import_userscript(name):
|
||||
"""Import an userscript via importlib.
|
||||
|
||||
This is needed because userscripts don't have a .py extension and violate
|
||||
Python's module naming convention.
|
||||
"""
|
||||
repo_root = pathlib.Path(__file__).resolve().parents[2]
|
||||
script_path = repo_root / 'misc' / 'userscripts' / name
|
||||
module_name = name.replace('-', '_')
|
||||
loader = importlib.machinery.SourceFileLoader(
|
||||
module_name, str(script_path))
|
||||
spec = importlib.util.spec_from_loader(module_name, loader)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
|
|
|||
|
|
@ -20,23 +20,15 @@
|
|||
"""Tests for misc.userscripts.qute-lastpass."""
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
from importlib.machinery import SourceFileLoader
|
||||
from importlib.util import spec_from_loader, module_from_spec
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import ANY, call
|
||||
|
||||
import attr
|
||||
import pytest
|
||||
|
||||
# qute-lastpass violates naming convention and does not have .py extension
|
||||
repo_root = pathlib.Path(__file__).resolve().parents[4]
|
||||
script_path = str(repo_root / 'misc' / 'userscripts' / 'qute-lastpass')
|
||||
spec = spec_from_loader("qute_lastpass", SourceFileLoader(
|
||||
"qute_lastpass",
|
||||
script_path))
|
||||
qute_lastpass = module_from_spec(spec)
|
||||
spec.loader.exec_module(qute_lastpass)
|
||||
from helpers import utils
|
||||
|
||||
qute_lastpass = utils.import_userscript('qute-lastpass')
|
||||
|
||||
default_lpass_match = [
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue