refactor(tests): reuse CLI loader in more tests (#2571)
This commit is contained in:
committed by
GitHub
parent
ae48ea7064
commit
51e668ce60
@@ -1,25 +1,12 @@
|
|||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
import io
|
import io
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load_cli():
|
|
||||||
path = ROOT / "scripts" / "odysseus-cookbook"
|
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_cookbook_cli", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_state_set_rejects_non_object_json(tmp_path, monkeypatch, capsys):
|
def test_state_set_rejects_non_object_json(tmp_path, monkeypatch, capsys):
|
||||||
cli = _load_cli()
|
cli = load_script("odysseus-cookbook")
|
||||||
cli._STATE_PATH = tmp_path / "cookbook_state.json"
|
cli._STATE_PATH = tmp_path / "cookbook_state.json"
|
||||||
monkeypatch.setattr(cli.sys, "stdin", io.StringIO("[]"))
|
monkeypatch.setattr(cli.sys, "stdin", io.StringIO("[]"))
|
||||||
|
|
||||||
|
|||||||
@@ -4,22 +4,10 @@
|
|||||||
(e.g. None) raised TypeError once any *.log file existed. Non-strings now
|
(e.g. None) raised TypeError once any *.log file existed. Non-strings now
|
||||||
return None (no match).
|
return None (no match).
|
||||||
"""
|
"""
|
||||||
import importlib.machinery
|
from tests.helpers.cli_loader import load_script
|
||||||
import importlib.util
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load():
|
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_logs_cli", str(ROOT / "scripts" / "odysseus-logs"))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
m = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(m)
|
|
||||||
return m
|
|
||||||
|
|
||||||
|
|
||||||
def test_non_string_name_returns_none():
|
def test_non_string_name_returns_none():
|
||||||
cli = _load()
|
cli = load_script("odysseus-logs")
|
||||||
assert cli._resolve(None) is None
|
assert cli._resolve(None) is None
|
||||||
assert cli._resolve(123) is None
|
assert cli._resolve(123) is None
|
||||||
|
|||||||
@@ -1,24 +1,15 @@
|
|||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load_cli(monkeypatch):
|
def _load_cli(monkeypatch):
|
||||||
svc = types.ModuleType("services.memory.memory")
|
svc = types.ModuleType("services.memory.memory")
|
||||||
svc.MemoryManager = MagicMock()
|
svc.MemoryManager = MagicMock()
|
||||||
monkeypatch.setitem(sys.modules, "services.memory.memory", svc)
|
monkeypatch.setitem(sys.modules, "services.memory.memory", svc)
|
||||||
path = ROOT / "scripts" / "odysseus-memory"
|
return load_script("odysseus-memory")
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_memory_cli", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_memory_entries_skips_invalid_rows(monkeypatch):
|
def test_memory_entries_skips_invalid_rows(monkeypatch):
|
||||||
|
|||||||
@@ -1,19 +1,8 @@
|
|||||||
import importlib.machinery
|
from tests.helpers.cli_loader import load_script
|
||||||
import importlib.util
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
def _load_dispatcher():
|
|
||||||
path = Path(__file__).resolve().parent.parent / "scripts" / "odysseus"
|
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_dispatcher_under_test", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_runnable_subcommand_requires_executable_file(tmp_path):
|
def test_is_runnable_subcommand_requires_executable_file(tmp_path):
|
||||||
cli = _load_dispatcher()
|
cli = load_script("odysseus")
|
||||||
sub = tmp_path / "odysseus-demo"
|
sub = tmp_path / "odysseus-demo"
|
||||||
sub.write_text("#!/bin/sh\n")
|
sub.write_text("#!/bin/sh\n")
|
||||||
sub.chmod(0o644)
|
sub.chmod(0o644)
|
||||||
|
|||||||
@@ -1,24 +1,15 @@
|
|||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load_cli(monkeypatch):
|
def _load_cli(monkeypatch):
|
||||||
personal_docs = types.ModuleType("src.personal_docs")
|
personal_docs = types.ModuleType("src.personal_docs")
|
||||||
personal_docs.PersonalDocsManager = MagicMock()
|
personal_docs.PersonalDocsManager = MagicMock()
|
||||||
monkeypatch.setitem(sys.modules, "src.personal_docs", personal_docs)
|
monkeypatch.setitem(sys.modules, "src.personal_docs", personal_docs)
|
||||||
path = ROOT / "scripts" / "odysseus-personal"
|
return load_script("odysseus-personal")
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_personal_cli", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_file_rows_skips_invalid_rows(monkeypatch):
|
def test_file_rows_skips_invalid_rows(monkeypatch):
|
||||||
|
|||||||
@@ -1,19 +1,8 @@
|
|||||||
import importlib.machinery
|
from tests.helpers.cli_loader import load_script
|
||||||
import importlib.util
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
def _load_preset_cli():
|
|
||||||
path = Path(__file__).resolve().parent.parent / "scripts" / "odysseus-preset"
|
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_preset_invalid_entries", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_entry_or_fail_rejects_non_object_entries():
|
def test_entry_or_fail_rejects_non_object_entries():
|
||||||
cli = _load_preset_cli()
|
cli = load_script("odysseus-preset")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cli._entry_or_fail({"broken": "raw prompt"}, "broken")
|
cli._entry_or_fail({"broken": "raw prompt"}, "broken")
|
||||||
@@ -24,6 +13,6 @@ def test_entry_or_fail_rejects_non_object_entries():
|
|||||||
|
|
||||||
|
|
||||||
def test_entry_or_fail_returns_valid_entry():
|
def test_entry_or_fail_returns_valid_entry():
|
||||||
cli = _load_preset_cli()
|
cli = load_script("odysseus-preset")
|
||||||
|
|
||||||
assert cli._entry_or_fail({"ok": {"name": "ok"}}, "ok") == {"name": "ok"}
|
assert cli._entry_or_fail({"ok": {"name": "ok"}}, "ok") == {"name": "ok"}
|
||||||
|
|||||||
@@ -1,24 +1,10 @@
|
|||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load_cli():
|
|
||||||
path = ROOT / "scripts" / "odysseus-preset"
|
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_preset_cli", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_load_rejects_non_object_preset_store(tmp_path, capsys):
|
def test_load_rejects_non_object_preset_store(tmp_path, capsys):
|
||||||
cli = _load_cli()
|
cli = load_script("odysseus-preset")
|
||||||
cli._PATH = tmp_path / "presets.json"
|
cli._PATH = tmp_path / "presets.json"
|
||||||
cli._PATH.write_text("[]")
|
cli._PATH.write_text("[]")
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,15 @@
|
|||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load_cli(monkeypatch):
|
def _load_cli(monkeypatch):
|
||||||
svc = types.ModuleType("services.memory.skills")
|
svc = types.ModuleType("services.memory.skills")
|
||||||
svc.SkillsManager = MagicMock()
|
svc.SkillsManager = MagicMock()
|
||||||
monkeypatch.setitem(sys.modules, "services.memory.skills", svc)
|
monkeypatch.setitem(sys.modules, "services.memory.skills", svc)
|
||||||
path = ROOT / "scripts" / "odysseus-skills"
|
return load_script("odysseus-skills")
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_skills_cli", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
def test_skill_entries_skips_invalid_rows(monkeypatch):
|
def test_skill_entries_skips_invalid_rows(monkeypatch):
|
||||||
|
|||||||
@@ -1,25 +1,11 @@
|
|||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from tests.helpers.cli_loader import load_script
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _load_cli():
|
|
||||||
path = ROOT / "scripts" / "odysseus-theme"
|
|
||||||
loader = importlib.machinery.SourceFileLoader("odysseus_theme_cli", str(path))
|
|
||||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
loader.exec_module(module)
|
|
||||||
return module
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("payload", ["[]", '{"_users": []}'])
|
@pytest.mark.parametrize("payload", ["[]", '{"_users": []}'])
|
||||||
def test_load_prefs_rejects_non_object_user_store(tmp_path, capsys, payload):
|
def test_load_prefs_rejects_non_object_user_store(tmp_path, capsys, payload):
|
||||||
cli = _load_cli()
|
cli = load_script("odysseus-theme")
|
||||||
cli._USER_PREFS_PATH = tmp_path / "user_prefs.json"
|
cli._USER_PREFS_PATH = tmp_path / "user_prefs.json"
|
||||||
cli._USER_PREFS_PATH.write_text(payload)
|
cli._USER_PREFS_PATH.write_text(payload)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user