Skip to content

Commit c5690c9

Browse files
branchseerclaude
andcommitted
refactor(e2e): use TerminationState enum for process state
Replace separate `timed_out` and `exit_status` variables with a unified `TerminationState` enum that better represents the three possible states: - Running (implicitly `None`) - Exited with status - Timed out Benefits: - Eliminates impossible state combinations - More idiomatic Rust with pattern matching - Clearer intent and better type safety - Loop now returns the termination state directly Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 6bc6f22 commit c5690c9

File tree

1 file changed

+28
-17
lines changed
  • crates/vite_task_bin/tests/e2e_snapshots

1 file changed

+28
-17
lines changed

crates/vite_task_bin/tests/e2e_snapshots/main.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,18 @@ async fn run_case_inner(tmpdir: &AbsolutePath, fixture_path: &Path, fixture_name
213213
// Read chunks concurrently with process wait, using select! with timeout
214214
let mut stdout_done = false;
215215
let mut stderr_done = false;
216-
let mut timed_out = false;
217-
let mut exit_status: Option<std::process::ExitStatus> = None;
216+
217+
enum TerminationState {
218+
Exited(std::process::ExitStatus),
219+
TimedOut,
220+
}
221+
// Initial state is running
222+
let mut termination_state: Option<TerminationState> = None;
218223

219224
let timeout = tokio::time::sleep(STEP_TIMEOUT);
220225
tokio::pin!(timeout);
221226

222-
loop {
227+
let termination_state = loop {
223228
let mut stdout_chunk = [0u8; 8192];
224229
let mut stderr_chunk = [0u8; 8192];
225230

@@ -238,31 +243,37 @@ async fn run_case_inner(tmpdir: &AbsolutePath, fixture_path: &Path, fixture_name
238243
Err(_) => stderr_done = true,
239244
}
240245
}
241-
result = child.wait(), if exit_status.is_none() => {
242-
exit_status = Some(result.unwrap());
246+
result = child.wait(), if termination_state.is_none() => {
247+
termination_state = Some(TerminationState::Exited(result.unwrap()));
243248
}
244-
_ = &mut timeout, if !timed_out => {
249+
_ = &mut timeout, if termination_state.is_none() => {
245250
// Timeout - kill the process
246251
let _ = child.kill().await;
247-
timed_out = true;
252+
termination_state = Some(TerminationState::TimedOut);
248253
}
249254
}
250255

251256
// Exit conditions:
252257
// 1. Process exited and all output drained
253258
// 2. Timed out and all output drained (after kill, pipes close)
254-
if (exit_status.is_some() || timed_out) && stdout_done && stderr_done {
255-
break;
259+
if let Some(termination_state) = &termination_state
260+
&& stdout_done
261+
&& stderr_done
262+
{
263+
break termination_state;
256264
}
257-
}
265+
};
258266

259267
// Format output
260-
if timed_out {
261-
e2e_outputs.push_str("[timeout]");
262-
} else if let Some(status) = exit_status {
263-
let exit_code = status.code().unwrap_or(-1);
264-
if exit_code != 0 {
265-
e2e_outputs.push_str(format!("[{}]", exit_code).as_str());
268+
match termination_state {
269+
TerminationState::TimedOut => {
270+
e2e_outputs.push_str("[timeout]");
271+
}
272+
TerminationState::Exited(status) => {
273+
let exit_code = status.code().unwrap_or(-1);
274+
if exit_code != 0 {
275+
e2e_outputs.push_str(format!("[{}]", exit_code).as_str());
276+
}
266277
}
267278
}
268279

@@ -277,7 +288,7 @@ async fn run_case_inner(tmpdir: &AbsolutePath, fixture_path: &Path, fixture_name
277288
e2e_outputs.push('\n');
278289

279290
// Skip remaining steps if timed out
280-
if timed_out {
291+
if matches!(termination_state, TerminationState::TimedOut) {
281292
break;
282293
}
283294
}

0 commit comments

Comments
 (0)