1+ import sys
12from importlib .metadata import PackageNotFoundError
3+ from pathlib import Path
24from unittest .mock import MagicMock , patch
35
46import pytest
911 get_config ,
1012 get_version ,
1113 main ,
14+ main_test ,
15+ run_tests ,
1216)
1317from ocean_runner .runner import Algorithm
1418
@@ -29,6 +33,12 @@ def test_config_validation_custom_args():
2933 assert config .base_dir .exists ()
3034
3135
36+ @patch ("pathlib.Path.exists" , return_value = False )
37+ def test_config_validation_raises_if_bad_path (mock_exists ):
38+ with pytest .raises (FileNotFoundError ):
39+ get_config (["custom.path" , "--base-dir" , "./_data" ])
40+
41+
3242@patch ("importlib.import_module" )
3343def test_get_algorithm_success (mock_import ):
3444 """Test that get_algorithm correctly finds an Algorithm instance."""
@@ -57,7 +67,7 @@ def test_get_algorithm_no_instance(mock_import):
5767@patch ("ocean_runner.entrypoint.get_algorithm" )
5868@patch (
5969 "ocean_runner.entrypoint.get_config" ,
60- return_value = CLIRunnerConfig (module = "test.mod" , base_dir = "./_data" ),
70+ return_value = CLIRunnerConfig (module = "test.mod" , base_dir = Path ( "./_data" ) ),
6171)
6272def test_main_execution_flow (mock_get_config , mock_get_algo ):
6373 """Test the full main loop triggers the algorithm call."""
@@ -74,7 +84,7 @@ def test_main_execution_flow(mock_get_config, mock_get_algo):
7484@patch ("ocean_runner.entrypoint.get_algorithm" , return_value = None )
7585@patch (
7686 "ocean_runner.entrypoint.get_config" ,
77- return_value = CLIRunnerConfig (module = "bad.mod" , base_dir = "./_data" ),
87+ return_value = CLIRunnerConfig (module = "bad.mod" , base_dir = Path ( "./_data" ) ),
7888)
7989def test_main_failure_exit (mock_get_config , mock_get_algo ):
8090 """Test that the runner exits with code 1 if no algorithm is found."""
@@ -91,3 +101,78 @@ def test_version(mock_version):
91101@patch ("ocean_runner.entrypoint.version" , side_effect = PackageNotFoundError ())
92102def test_version_failing (mock_version ):
93103 assert "unknown" in get_version ()
104+
105+
106+ @patch ("importlib.import_module" , side_effect = ImportError ())
107+ def test_import_algorithm_error (mock_import , capsys ):
108+ result = get_algorithm ("test" )
109+
110+ captured = capsys .readouterr ()
111+
112+ assert result is None
113+ assert "Error loading" in captured .err
114+
115+
116+ def test_import_not_in_path ():
117+ fake_cwd = "/fake/path"
118+
119+ with patch ("os.getcwd" , return_value = fake_cwd ):
120+ with patch ("sys.path" , []):
121+ get_algorithm ("some.module" )
122+
123+ assert fake_cwd in sys .path
124+
125+
126+ @patch ("sys.exit" )
127+ @patch ("pytest.main" , return_value = 0 )
128+ @patch ("ocean_runner.entrypoint.setup_environment" )
129+ def test_run_tests (mock_setup_env , mock_pytest_main , mock_exit , capsys ):
130+ config = MagicMock ()
131+ config .base_dir = Path ("/tmp/project" )
132+
133+ args = ["-k" , "test_something" ]
134+
135+ run_tests (config , args )
136+
137+ # Check environment setup
138+ mock_setup_env .assert_called_once_with (config .base_dir )
139+
140+ # Check pytest execution
141+ mock_pytest_main .assert_called_once_with (args )
142+
143+ # Check exit called with pytest result
144+ mock_exit .assert_called_once_with (0 )
145+
146+ # Check printed message
147+ captured = capsys .readouterr ()
148+ assert "Preparing Test Environment at:" in captured .out
149+
150+
151+ @patch ("ocean_runner.entrypoint.run_tests" )
152+ @patch ("ocean_runner.entrypoint.setup" )
153+ def test_main_test_with_separator (mock_setup , mock_run_tests ):
154+ mock_config = MagicMock ()
155+ mock_setup .return_value = mock_config
156+
157+ test_argv = ["prog" , "--config" , "dev" , "--" , "-k" , "test_api" ]
158+
159+ with patch ("sys.argv" , test_argv ):
160+ main_test ()
161+
162+ mock_setup .assert_called_once_with (["--config" , "dev" ])
163+ mock_run_tests .assert_called_once_with (mock_config , ["-k" , "test_api" ])
164+
165+
166+ @patch ("ocean_runner.entrypoint.run_tests" )
167+ @patch ("ocean_runner.entrypoint.setup" )
168+ def test_main_test_without_separator (mock_setup , mock_run_tests ):
169+ mock_config = MagicMock ()
170+ mock_setup .return_value = mock_config
171+
172+ test_argv = ["prog" , "--config" , "dev" ]
173+
174+ with patch ("sys.argv" , test_argv ):
175+ main_test ()
176+
177+ mock_setup .assert_called_once_with (["--config" , "dev" ])
178+ mock_run_tests .assert_called_once_with (mock_config , ["tests" ])
0 commit comments