Files
textual/e2e_tests/sandbox_basic_test.py
Olivier Philippon 078b7c151b [e2e] The smoke tests no longer rely on the multiprocessing package
Running the scripts with this package leads to the following cryptic error in our CI, when run in macOS:
```
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
```
(and Python's docs clearly state that this `freeze_support` function has no effect when invoked on any operating system other than Windows ¯\_(ツ)_/¯ )
2022-05-03 14:45:48 +01:00

56 lines
1.4 KiB
Python

from __future__ import annotations
import shlex
import sys
import subprocess
import threading
from pathlib import Path
target_script_name = "basic"
script_time_to_live = 2.0 # in seconds
if len(sys.argv) > 1:
target_script_name = sys.argv[1]
if len(sys.argv) > 2:
script_time_to_live = float(sys.argv[2])
e2e_root = Path(__file__).parent
completed_process = None
def launch_sandbox_script(python_file_name: str) -> None:
global completed_process
command = f"{sys.executable} ./test_apps/{shlex.quote(python_file_name)}.py"
print(f"Launching command '{command}'...")
try:
completed_process = subprocess.run(
command, shell=True, check=True, capture_output=True, cwd=str(e2e_root)
)
except subprocess.CalledProcessError as err:
print(f"Process error: {err.stderr}")
raise
thread = threading.Thread(
target=launch_sandbox_script, args=(target_script_name,), daemon=True
)
thread.start()
print(
f"Launching Python script in a sub-thread; we'll wait for it for {script_time_to_live} seconds..."
)
thread.join(timeout=script_time_to_live)
print("The wait is over.")
process_still_running = completed_process is None
process_was_able_to_run_without_errors = process_still_running
if process_was_able_to_run_without_errors:
print("Python script is still running :-)")
else:
print("Python script is no longer running :-/")
sys.exit(0 if process_was_able_to_run_without_errors else 1)