|
17 | 17 | InvalidRunnerStateError,
|
18 | 18 | RpcError,
|
19 | 19 | WorkerDispatcher,
|
| 20 | + _safe_exception_message, |
20 | 21 | import_and_run_function,
|
21 | 22 | )
|
22 | 23 |
|
@@ -71,19 +72,33 @@ def test_raises_if_used_before_started(runner: WorkerDispatcher):
|
71 | 72 | runner.run(interface.get_plans)
|
72 | 73 |
|
73 | 74 |
|
74 |
| -def test_error_on_runner_setup(runner: WorkerDispatcher, mock_subprocess: Mock): |
75 |
| - error_message = "Intentional start_worker exception" |
76 |
| - environment_id = uuid.uuid4() |
77 |
| - expected_state = EnvironmentResponse( |
78 |
| - environment_id=environment_id, |
79 |
| - initialized=False, |
80 |
| - error_message=error_message, |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "message", |
| 77 | + [ |
| 78 | + None, |
| 79 | + "", |
| 80 | + " ", |
| 81 | + "Intentional start_worker exception", |
| 82 | + ], |
| 83 | +) |
| 84 | +def test_using_safe_exception_message_copes_with_all_message_types_on_runner_setup( |
| 85 | + runner: WorkerDispatcher, mock_subprocess: Mock, message: str | None |
| 86 | +): |
| 87 | + try: |
| 88 | + raise Exception() if message is None else Exception(message) |
| 89 | + except Exception as e: |
| 90 | + expected_state = EnvironmentResponse( |
| 91 | + environment_id=uuid.uuid4(), |
| 92 | + initialized=False, |
| 93 | + error_message=_safe_exception_message(e), |
| 94 | + ) |
| 95 | + mock_subprocess.apply.side_effect = ( |
| 96 | + Exception() if message is None else Exception(message) |
81 | 97 | )
|
82 |
| - mock_subprocess.apply.side_effect = Exception(error_message) |
83 | 98 |
|
84 |
| - # Calling reload here instead of start also indirectly |
85 |
| - # tests that stop() doesn't raise if there is no error message |
86 |
| - # and the runner is not yet initialised |
| 99 | + # Calling reload here instead of start also indirectly tests |
| 100 | + # that stop() doesn't raise if there is no error message and the |
| 101 | + # runner is not yet initialised. |
87 | 102 | runner.reload()
|
88 | 103 | state = runner.state
|
89 | 104 | expected_state.environment_id = state.environment_id
|
@@ -116,7 +131,9 @@ def test_can_reload_after_an_error(pool_mock: MagicMock):
|
116 | 131 | runner.start()
|
117 | 132 | current_env = runner.state.environment_id
|
118 | 133 | assert runner.state == EnvironmentResponse(
|
119 |
| - environment_id=current_env, initialized=False, error_message="invalid code" |
| 134 | + environment_id=current_env, |
| 135 | + initialized=False, |
| 136 | + error_message="SyntaxError: invalid code", |
120 | 137 | )
|
121 | 138 |
|
122 | 139 | runner.reload()
|
|
0 commit comments