From 1bdb86ae5a41c04e9aac745520ba3e2291205b6f Mon Sep 17 00:00:00 2001 From: malachov Date: Tue, 11 Jan 2022 18:48:13 +0100 Subject: [PATCH 1/2] Ignoring files prefix and use only files implemented --- eel/__init__.py | 16 ++++++++++++++-- tests/__init__.py | 0 tests/data/init_test/ignore_test.html | 22 ++++++++++++++++++++++ tests/unit/test_eel.py | 16 +++++++++++++--- 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/data/init_test/ignore_test.html diff --git a/eel/__init__.py b/eel/__init__.py index a630e1e1..890d9c1d 100644 --- a/eel/__init__.py +++ b/eel/__init__.py @@ -101,17 +101,29 @@ def decorator(function): ) -def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm', - '.xhtml', '.vue'], js_result_timeout=10000): +def init( + path, + allowed_extensions=[".js", ".html", ".txt", ".htm", ".xhtml", ".vue"], + exclude_prefix=[], + js_result_timeout=10000, + use_only_files=None, +): global root_path, _js_functions, _js_result_timeout root_path = _get_real_path(path) js_functions = set() for root, _, files in os.walk(root_path): for name in files: + + if use_only_files and name not in use_only_files: + continue + if not any(name.endswith(ext) for ext in allowed_extensions): continue + if exclude_prefix and any(name.startswith(exc) for exc in exclude_prefix): + continue + try: with open(os.path.join(root, name), encoding='utf-8') as file: contents = file.read() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/init_test/ignore_test.html b/tests/data/init_test/ignore_test.html new file mode 100644 index 00000000..a0a85e3f --- /dev/null +++ b/tests/data/init_test/ignore_test.html @@ -0,0 +1,22 @@ + + + + + + + + + + + +

Testing if some js file can be ignored from parsing.

+ + + \ No newline at end of file diff --git a/tests/unit/test_eel.py b/tests/unit/test_eel.py index 5a5b3223..c26b9d85 100644 --- a/tests/unit/test_eel.py +++ b/tests/unit/test_eel.py @@ -24,6 +24,16 @@ def test_exposed_js_functions(js_code, expected_matches): def test_init(): """Test eel.init() against a test directory and ensure that all JS functions are in the global _js_functions.""" eel.init(path=INIT_DIR) - result = eel._js_functions.sort() - functions = ['show_log', 'js_random', 'show_log_alt', 'say_hello_js'].sort() - assert result == functions, f'Expected {functions} (found: {result}) in {INIT_DIR}' + result = eel._js_functions + functions = ["show_log", "js_random", "ignore_test", "show_log_alt", "say_hello_js"] + assert set(result) == set(functions), f"Expected {functions} (found: {result}) in {INIT_DIR}" + + eel.init(path=INIT_DIR, exclude_prefix="ignore") + result = eel._js_functions + functions = ["show_log", "js_random", "show_log_alt", "say_hello_js"] + assert set(result) == set(functions), f"Expected {functions} (found: {result}) in {INIT_DIR}" + + eel.init(path=INIT_DIR, use_only_files=["hello.html"]) + result = eel._js_functions + functions = ["js_random", "say_hello_js"] + assert set(result) == set(functions), f"Expected {functions} (found: {result}) in {INIT_DIR}" \ No newline at end of file From 80af71e74f0f841ff841b0eee515e1034aba9234 Mon Sep 17 00:00:00 2001 From: malachov Date: Fri, 6 Jan 2023 21:53:29 +0100 Subject: [PATCH 2/2] Change param name and parametrize tests --- eel/__init__.py | 4 ++-- tests/unit/test_eel.py | 23 ++++++++--------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/eel/__init__.py b/eel/__init__.py index 83a919a7..ac03ee21 100644 --- a/eel/__init__.py +++ b/eel/__init__.py @@ -104,8 +104,8 @@ def decorator(function): def init( path, allowed_extensions=[".js", ".html", ".txt", ".htm", ".xhtml", ".vue"], - exclude_prefix=[], js_result_timeout=10000, + exclude_file_prefixes=None, use_only_files=None, ): global root_path, _js_functions, _js_result_timeout @@ -121,7 +121,7 @@ def init( if not any(name.endswith(ext) for ext in allowed_extensions): continue - if exclude_prefix and any(name.startswith(exc) for exc in exclude_prefix): + if exclude_file_prefixes and any(name.startswith(exc) for exc in exclude_file_prefixes): continue try: diff --git a/tests/unit/test_eel.py b/tests/unit/test_eel.py index c26b9d85..ef0d24ad 100644 --- a/tests/unit/test_eel.py +++ b/tests/unit/test_eel.py @@ -20,20 +20,13 @@ def test_exposed_js_functions(js_code, expected_matches): matches = eel.EXPOSED_JS_FUNCTIONS.parseString(js_code).asList() assert matches == expected_matches, f'Expected {expected_matches} (found: {matches}) in: {js_code}' - -def test_init(): +@pytest.mark.parametrize('kwargs, exposed_functions', [ + ({"path": INIT_DIR}, ["show_log", "js_random", "ignore_test", "show_log_alt", "say_hello_js"]), + ({"path": INIT_DIR, "exclude_file_prefixes": ["ignore"]}, ["show_log", "js_random", "show_log_alt", "say_hello_js"]), + ({"path": INIT_DIR, "use_only_files": ["hello.html"]}, ["js_random", "say_hello_js"]), +]) +def test_init_file_excluding(kwargs, exposed_functions): """Test eel.init() against a test directory and ensure that all JS functions are in the global _js_functions.""" - eel.init(path=INIT_DIR) - result = eel._js_functions - functions = ["show_log", "js_random", "ignore_test", "show_log_alt", "say_hello_js"] - assert set(result) == set(functions), f"Expected {functions} (found: {result}) in {INIT_DIR}" - - eel.init(path=INIT_DIR, exclude_prefix="ignore") - result = eel._js_functions - functions = ["show_log", "js_random", "show_log_alt", "say_hello_js"] - assert set(result) == set(functions), f"Expected {functions} (found: {result}) in {INIT_DIR}" - - eel.init(path=INIT_DIR, use_only_files=["hello.html"]) + eel.init(**kwargs) result = eel._js_functions - functions = ["js_random", "say_hello_js"] - assert set(result) == set(functions), f"Expected {functions} (found: {result}) in {INIT_DIR}" \ No newline at end of file + assert set(result) == set(exposed_functions), f"Expected {exposed_functions} (found: {result}) in {INIT_DIR}"