44import sys
55from importlib .metadata import PackageNotFoundError , version
66from pathlib import Path
7+ from typing import Sequence
78
89from 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 )
0 commit comments