Streamlines imports and app start
This commit is contained in:
104
tests/unit/test_cli_lazy_imports.py
Normal file
104
tests/unit/test_cli_lazy_imports.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
import unittest
|
||||
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[2]
|
||||
SRC_ROOT = REPO_ROOT / "src"
|
||||
HEAVY_MODULES = [
|
||||
"ffx.configuration_controller",
|
||||
"ffx.database",
|
||||
"ffx.ffx_app",
|
||||
"ffx.ffx_controller",
|
||||
"ffx.file_properties",
|
||||
"ffx.tmdb_controller",
|
||||
]
|
||||
|
||||
|
||||
class CliLazyImportTests(unittest.TestCase):
|
||||
def run_python(self, code: str) -> dict:
|
||||
completed = subprocess.run(
|
||||
[sys.executable, "-c", code],
|
||||
capture_output=True,
|
||||
cwd=REPO_ROOT,
|
||||
text=True,
|
||||
)
|
||||
if completed.returncode != 0:
|
||||
self.fail(
|
||||
"Python helper failed\n"
|
||||
f"STDOUT:\n{completed.stdout}\n"
|
||||
f"STDERR:\n{completed.stderr}"
|
||||
)
|
||||
return json.loads(completed.stdout)
|
||||
|
||||
def test_importing_cli_keeps_runtime_modules_unloaded(self):
|
||||
result = self.run_python(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
import json
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, {str(SRC_ROOT)!r})
|
||||
|
||||
import ffx.cli
|
||||
|
||||
print(json.dumps({{
|
||||
"modules": {{
|
||||
module_name: module_name in sys.modules
|
||||
for module_name in {HEAVY_MODULES!r}
|
||||
}},
|
||||
}}))
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
self.assertTrue(
|
||||
all(not is_loaded for is_loaded in result["modules"].values()),
|
||||
result["modules"],
|
||||
)
|
||||
|
||||
def test_lightweight_configure_workstation_command_stays_light(self):
|
||||
result = self.run_python(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
import json
|
||||
import sys
|
||||
from click.testing import CliRunner
|
||||
|
||||
sys.path.insert(0, {str(SRC_ROOT)!r})
|
||||
|
||||
import ffx.cli
|
||||
|
||||
runner = CliRunner()
|
||||
invoke_result = runner.invoke(
|
||||
ffx.cli.ffx,
|
||||
["--dry-run", "configure_workstation", "--check"],
|
||||
)
|
||||
if invoke_result.exit_code != 0:
|
||||
raise SystemExit(invoke_result.output)
|
||||
|
||||
print(json.dumps({{
|
||||
"output": invoke_result.output,
|
||||
"modules": {{
|
||||
module_name: module_name in sys.modules
|
||||
for module_name in {HEAVY_MODULES!r}
|
||||
}},
|
||||
}}))
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
self.assertIn("configure_workstation.sh --check", result["output"])
|
||||
self.assertTrue(
|
||||
all(not is_loaded for is_loaded in result["modules"].values()),
|
||||
result["modules"],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user