Skip to content

Commit 5ccf915

Browse files
committed
impl: testing script
1 parent 10d5141 commit 5ccf915

4 files changed

Lines changed: 49 additions & 16 deletions

File tree

ocean_runner/entrypoint.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from importlib.metadata import PackageNotFoundError, version
66
from pathlib import Path
7+
from typing import Sequence
78

89
from pydantic import BaseModel, Field
910

@@ -54,27 +55,43 @@ def get_algorithm(module_path: str) -> Algorithm | None:
5455
return None
5556

5657

57-
def run_algorithm(config: CLIRunnerConfig) -> None:
58+
def setup_environment(base_dir: Path) -> None:
59+
abs_base = base_dir.resolve()
5860
# Set the "base_dir" environment variable for oceanprotocol_job_details to load data from
59-
abs_base = config.base_dir.resolve()
6061
os.environ["base_dir"] = str(abs_base)
6162

6263
# Ensure RW directories exist locally so the algorithm doesn't crash
6364
(abs_base / "outputs").mkdir(parents=True, exist_ok=True)
6465
(abs_base / "logs").mkdir(parents=True, exist_ok=True)
6566

67+
68+
def run_algorithm(config: CLIRunnerConfig) -> None:
69+
setup_environment(config.base_dir)
70+
6671
algorithm = get_algorithm(config.module)
6772

6873
if isinstance(algorithm, Algorithm):
6974
print(f"Launching algorithm from {config.module}")
70-
print(f"Base Directory: {abs_base}")
75+
print(f"Base Directory: {config.base_dir}")
7176
algorithm()
7277
else:
7378
print(f"No algorithm instance found in {config.module}", file=sys.stderr)
7479
sys.exit(1)
7580

7681

77-
def get_config() -> CLIRunnerConfig:
82+
def run_tests(config: CLIRunnerConfig, args: Sequence[str]) -> None:
83+
import pytest
84+
85+
print(f"Preparing Test Environment at: {config.base_dir.resolve()}")
86+
setup_environment(config.base_dir)
87+
88+
sys.path.append(str(Path.cwd()))
89+
90+
exit_code = pytest.main([*args])
91+
sys.exit(exit_code)
92+
93+
94+
def get_config(args: Sequence[str]) -> CLIRunnerConfig:
7895
parser = argparse.ArgumentParser(description="Ocean Runner CLI Entrypoint")
7996
parser.add_argument(
8097
"module",
@@ -88,14 +105,32 @@ def get_config() -> CLIRunnerConfig:
88105
default="../_data",
89106
help="The base data directory path",
90107
)
91-
args = parser.parse_args()
92108

93-
return CLIRunnerConfig.model_validate(vars(args))
109+
return CLIRunnerConfig.model_validate(vars(parser.parse_args(args)))
94110

95111

96-
def main() -> None:
112+
def setup(args: Sequence[str]) -> CLIRunnerConfig:
97113
print(f"--- Ocean Runner CLI v{get_version()} ---")
98114

99-
config = get_config()
115+
return get_config(args)
100116

117+
118+
def main() -> None:
119+
config = setup(sys.argv)
101120
run_algorithm(config)
121+
122+
123+
def main_test() -> None:
124+
try:
125+
sep_index = sys.argv.index("--")
126+
wrapper_args = sys.argv[1:sep_index]
127+
pytest_argv = sys.argv[sep_index + 1 :]
128+
except ValueError:
129+
# If '--' is not present, everything is a wrapper arg
130+
wrapper_args = sys.argv[1:]
131+
pytest_argv = []
132+
133+
final_args = pytest_argv if pytest_argv else ["tests"]
134+
135+
config = setup(wrapper_args)
136+
run_tests(config, final_args)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Issues = "https://github.com/AgrospAI/ocean-runner/issues"
2828

2929
[project.scripts]
3030
ocean-execute = "ocean_runner.entrypoint:main"
31+
ocean-test = "ocean_runner.entrypoint:main_test"
3132

3233
[tool.pytest.ini_options]
3334
log_level = "INFO"

tests/test_entrypoint.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from importlib.metadata import PackageNotFoundError
32
from unittest.mock import MagicMock, patch
43

@@ -24,12 +23,10 @@ def __call__(self):
2423

2524
def test_config_validation_custom_args():
2625
"""Test that CLIRunnerConfig accepts custom module paths."""
27-
with patch.object(
28-
sys, "argv", ["ocean-execute", "custom.path", "--base-dir", "./_data"]
29-
):
30-
config = get_config()
31-
assert config.module == "custom.path"
32-
assert config.base_dir.exists()
26+
27+
config = get_config(["custom.path", "--base-dir", "./_data"])
28+
assert config.module == "custom.path"
29+
assert config.base_dir.exists()
3330

3431

3532
@patch("importlib.import_module")

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)