execra 0.1.0

Typed job runtime for external processes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! End-to-end smoke tests for the runtime. Uses platform shell so it can
//! run on both Windows and Unix without bundling test fixtures.

use std::io::Read;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, SystemTime};

use execra::{
    Command, Config, Context, Execra, ExitCode, Finding, Interpreter, InterpreterEvent as Ev, Job,
    JobId, JobState, Line, Outcome, Progress, RawOutputPolicy,
};

static TEST_ID: AtomicUsize = AtomicUsize::new(0);

fn test_config() -> Config {
    let id = TEST_ID.fetch_add(1, Ordering::Relaxed);
    let root = std::env::temp_dir().join(format!("execra-test-{}-{id}", std::process::id()));
    Config {
        db_path: root.join("execra.db"),
        log_dir: root.join("logs"),
        ..Config::default()
    }
}

fn echo(text: &str) -> Command {
    Command::shell(format!("echo {text}"))
}

fn fail() -> Command {
    Command::shell("exit 7")
}

fn sleep_forever() -> Command {
    #[cfg(windows)]
    {
        // `ping -t` runs until killed; -n 9999 also works and is quieter.
        Command::new("cmd").args(["/C", "ping -n 9999 127.0.0.1 > nul"])
    }
    #[cfg(not(windows))]
    {
        Command::new("sh").args(["-c", "sleep 60"])
    }
}

fn sleep_millis(ms: u64) -> Command {
    #[cfg(windows)]
    {
        Command::new("powershell").args([
            "-NoProfile",
            "-Command",
            &format!("Start-Sleep -Milliseconds {ms}"),
        ])
    }
    #[cfg(not(windows))]
    {
        Command::new("sh").args(["-c", &format!("sleep {}", ms as f64 / 1000.0)])
    }
}

fn invalid_utf8_output() -> Command {
    #[cfg(windows)]
    {
        Command::powershell("[Console]::OpenStandardOutput().Write([byte[]](255,10),0,2)")
    }
    #[cfg(not(windows))]
    {
        Command::sh("printf '\\377\\n'")
    }
}

#[tokio::test]
async fn successful_command_succeeds() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt.spawn(echo("hello")).await.unwrap();
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
}

#[tokio::test]
async fn invalid_utf8_output_is_lossy_decoded_not_dropped() {
    let rt = Execra::open(test_config()).await.unwrap();
    let mut handle = rt.spawn(invalid_utf8_output()).await.unwrap();
    let mut events = handle.subscribe();
    let mut saw_replacement = false;
    while let Some(event) = events.next().await {
        match event {
            execra::Event::OutputAppended { line, .. } if line.contains('\u{fffd}') => {
                saw_replacement = true;
            }
            execra::Event::Finalized { .. } => break,
            _ => {}
        }
    }
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
    assert!(saw_replacement, "invalid output was not surfaced lossily");
}

#[tokio::test]
async fn non_zero_exit_fails_with_nonzero_exit_reason() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt.spawn(fail()).await.unwrap();
    let outcome = handle.await;
    match outcome {
        Outcome::Failed { reason, .. } => match reason {
            execra::FailureReason::NonZeroExit { code } => assert_eq!(code, 7),
            other => panic!("expected NonZeroExit, got {other:?}"),
        },
        other => panic!("expected Failed, got {other:?}"),
    }
}

#[tokio::test]
async fn spawn_failure_yields_spawn_failed_outcome() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt
        .spawn(Command::new("definitely-not-a-real-binary-9f8e2"))
        .await
        .unwrap();
    let outcome = handle.await;
    assert!(matches!(
        outcome,
        Outcome::Failed {
            reason: execra::FailureReason::SpawnFailed { .. },
            ..
        }
    ));
}

#[tokio::test]
async fn cancelled_job_finalizes_as_cancelled() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt.spawn(sleep_forever()).await.unwrap();
    // Give the child a moment to actually start.
    tokio::time::sleep(Duration::from_millis(100)).await;
    handle.cancel().unwrap();
    let outcome = tokio::time::timeout(Duration::from_secs(10), handle)
        .await
        .expect("cancelled job should finalize quickly");
    assert!(
        matches!(outcome, Outcome::Cancelled { .. }),
        "got {outcome:?}"
    );
}

#[tokio::test]
async fn cancelling_finalized_job_is_ok() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt.spawn(echo("done")).await.unwrap();
    let id = handle.id();
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
    rt.cancel(id).unwrap();
}

#[tokio::test]
async fn max_concurrent_serializes_process_execution() {
    let mut config = test_config();
    config.max_concurrent = 1;
    let rt = Execra::open(config).await.unwrap();
    let start = std::time::Instant::now();
    let first = rt.spawn(sleep_millis(350)).await.unwrap();
    let second = rt.spawn(sleep_millis(350)).await.unwrap();
    let first = first.await;
    let second = second.await;
    assert!(matches!(first, Outcome::Succeeded { .. }), "got {first:?}");
    assert!(
        matches!(second, Outcome::Succeeded { .. }),
        "got {second:?}"
    );
    assert!(
        start.elapsed() >= Duration::from_millis(600),
        "jobs appear to have run concurrently"
    );
}

#[tokio::test]
async fn timed_out_job_fails_with_timeout_reason() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt
        .spawn(sleep_forever().timeout(Duration::from_millis(100)))
        .await
        .unwrap();
    let outcome = tokio::time::timeout(Duration::from_secs(10), handle)
        .await
        .expect("timed-out job should finalize quickly");
    assert!(
        matches!(
            outcome,
            Outcome::Failed {
                reason: execra::FailureReason::Timeout,
                ..
            }
        ),
        "got {outcome:?}"
    );
}

// ---------- Interpreter-driven flows ----------

struct CollectFinding;
impl Interpreter for CollectFinding {
    fn on_line(&mut self, _ctx: &Context, line: &Line) -> Vec<Ev> {
        if line.text.contains("ALERT") {
            return vec![Ev::Finding {
                finding: Finding::warning("test.alert", line.text.clone()),
            }];
        }
        if line.text.contains("HALF") {
            return vec![Ev::Progress {
                progress: Progress::fraction(0.5),
            }];
        }
        vec![]
    }
    fn on_exit(&mut self, _ctx: &Context, _exit: &ExitCode) -> Vec<Ev> {
        vec![Ev::Summary {
            text: "done".into(),
        }]
    }
}

#[tokio::test]
async fn interpreter_findings_persist_into_outcome() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt
        .spawn(echo("ALERT something happened").interpreter(CollectFinding))
        .await
        .unwrap();
    let outcome = handle.await;
    match outcome {
        Outcome::Succeeded { summary, findings } => {
            assert_eq!(summary.as_deref(), Some("done"));
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].code, "test.alert");
        }
        other => panic!("expected Succeeded, got {other:?}"),
    }
}

struct PanicOnce {
    fired: bool,
}
impl Interpreter for PanicOnce {
    fn on_line(&mut self, _ctx: &Context, _line: &Line) -> Vec<Ev> {
        if !self.fired {
            self.fired = true;
            panic!("interpreter blew up on first line");
        }
        vec![]
    }
    fn on_exit(&mut self, _ctx: &Context, _exit: &ExitCode) -> Vec<Ev> {
        vec![]
    }
}

#[tokio::test]
async fn interpreter_panic_does_not_kill_job() {
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt
        .spawn(echo("first line").interpreter(PanicOnce { fired: false }))
        .await
        .unwrap();
    let outcome = handle.await;
    // The process still exits 0 even though the interpreter panicked.
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
}

#[tokio::test]
async fn known_error_makes_failed_carry_known_error_reason() {
    struct ClaimKnownError;
    impl Interpreter for ClaimKnownError {
        fn on_line(&mut self, _: &Context, _: &Line) -> Vec<Ev> {
            vec![]
        }
        fn on_exit(&mut self, _: &Context, exit: &ExitCode) -> Vec<Ev> {
            // Classify the failure at exit time based on the captured code.
            // Mirrors the real-world pattern where an interpreter recognizes
            // a known failure mode after the fact.
            if !exit.is_success() {
                vec![Ev::KnownError {
                    code: "test.broken".into(),
                    message: "explicitly broken".into(),
                }]
            } else {
                vec![]
            }
        }
    }
    let rt = Execra::open(test_config()).await.unwrap();
    let handle = rt.spawn(fail().interpreter(ClaimKnownError)).await.unwrap();
    let outcome = handle.await;
    match outcome {
        Outcome::Failed {
            reason: execra::FailureReason::KnownError { code, .. },
            ..
        } => assert_eq!(code, "test.broken"),
        other => panic!("expected Failed{{KnownError}}, got {other:?}"),
    }
}

#[tokio::test]
async fn completed_job_survives_reopen_and_jobs_query_runs() {
    let config = test_config();
    let rt = Execra::open(config.clone()).await.unwrap();
    let handle = rt
        .spawn(
            echo("persist me")
                .label("persistent")
                .tags(vec!["persist".to_string()]),
        )
        .await
        .unwrap();
    let id = handle.id();
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );

    let reopened = Execra::open(config).await.unwrap();
    let job = reopened.job(id).await.expect("persisted job should load");
    assert_eq!(job.state, JobState::Finalized);
    assert_eq!(job.label.as_deref(), Some("persistent"));

    let jobs = reopened
        .jobs()
        .with_tag("persist")
        .run(&reopened)
        .await
        .unwrap();
    assert!(
        jobs.iter().any(|j| j.id == id),
        "persisted job missing from query"
    );
}

#[tokio::test]
async fn jobs_query_filters_by_tag_state_and_created_time() {
    let config = test_config();
    let rt = Execra::open(config).await.unwrap();
    let before = SystemTime::now() - Duration::from_secs(1);
    let keep = rt
        .spawn(echo("keep").tags(vec!["keep".to_string()]))
        .await
        .unwrap();
    let keep_id = keep.id();
    let skip = rt
        .spawn(echo("skip").tags(vec!["skip".to_string()]))
        .await
        .unwrap();
    let skip_id = skip.id();
    assert!(matches!(keep.await, Outcome::Succeeded { .. }));
    assert!(matches!(skip.await, Outcome::Succeeded { .. }));
    let after = SystemTime::now() + Duration::from_secs(1);

    let jobs = rt
        .jobs()
        .with_tag("keep")
        .with_state(JobState::Finalized)
        .created_after(before)
        .created_before(after)
        .limit(10)
        .run(&rt)
        .await
        .unwrap();
    assert!(jobs.iter().any(|j| j.id == keep_id));
    assert!(!jobs.iter().any(|j| j.id == skip_id));
}

#[tokio::test]
async fn open_marks_stranded_running_jobs_failed() {
    let config = test_config();
    let store = execra::store::Store::open(&config.db_path).await.unwrap();
    let id = JobId::new();
    let job = Job {
        id,
        command: Command::new("stranded").spec().clone(),
        created_at: std::time::SystemTime::now(),
        started_at: Some(std::time::SystemTime::now()),
        state: JobState::Running,
        current_phase: None,
        progress: Progress::Unknown,
        label: Some("stranded".into()),
        exit: None,
        outcome: None,
    };
    store.upsert_job(&job).await.unwrap();
    drop(store);

    let rt = Execra::open(config).await.unwrap();
    let job = rt.job(id).await.expect("stranded job should load");
    assert_eq!(job.state, JobState::Finalized);
    assert!(matches!(
        job.outcome,
        Some(Outcome::Failed {
            reason: execra::FailureReason::SpawnFailed { ref error },
            ..
        }) if error == "host process exited"
    ));
}

#[tokio::test]
async fn raw_output_policy_controls_flat_log_files() {
    let persist_config = test_config();
    let rt = Execra::open(persist_config.clone()).await.unwrap();
    let handle = rt.spawn(echo("raw line")).await.unwrap();
    let id = handle.id();
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
    let log = std::fs::read_to_string(persist_config.log_dir.join(format!("{id}.log"))).unwrap();
    assert!(log.contains("[stdout] raw line"));

    let mut gzip_config = test_config();
    gzip_config.raw_output = RawOutputPolicy::PersistGzipOnFinalize;
    let rt = Execra::open(gzip_config.clone()).await.unwrap();
    let handle = rt.spawn(echo("gzip line")).await.unwrap();
    let id = handle.id();
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
    assert!(!gzip_config.log_dir.join(format!("{id}.log")).exists());
    let gz = std::fs::File::open(gzip_config.log_dir.join(format!("{id}.log.gz"))).unwrap();
    let mut decoder = flate2::read::GzDecoder::new(gz);
    let mut log = String::new();
    decoder.read_to_string(&mut log).unwrap();
    assert!(log.contains("[stdout] gzip line"));

    let mut disabled_config = test_config();
    disabled_config.raw_output = RawOutputPolicy::Disabled;
    let rt = Execra::open(disabled_config.clone()).await.unwrap();
    let handle = rt.spawn(echo("drop line")).await.unwrap();
    let id = handle.id();
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
    assert!(!disabled_config.log_dir.join(format!("{id}.log")).exists());
}

#[tokio::test]
async fn memory_only_raw_output_streams_live_without_flat_log() {
    let mut config = test_config();
    config.raw_output = RawOutputPolicy::MemoryOnly;
    let rt = Execra::open(config.clone()).await.unwrap();
    let mut handle = rt.spawn(echo("memory line")).await.unwrap();
    let id = handle.id();
    let mut events = handle.subscribe();
    let mut saw_output = false;
    while let Some(event) = events.next().await {
        match event {
            execra::Event::OutputAppended { line, .. } if line.contains("memory line") => {
                saw_output = true;
            }
            execra::Event::Finalized { .. } => break,
            _ => {}
        }
    }
    let outcome = handle.await;
    assert!(
        matches!(outcome, Outcome::Succeeded { .. }),
        "got {outcome:?}"
    );
    assert!(saw_output);
    assert!(!config.log_dir.join(format!("{id}.log")).exists());
}

#[tokio::test]
async fn higher_persisted_schema_version_is_rejected() {
    let config = test_config();
    let store = execra::store::Store::open(&config.db_path).await.unwrap();
    let id = JobId::new();
    let job = Job {
        id,
        command: Command::new("versioned").spec().clone(),
        created_at: SystemTime::now(),
        started_at: None,
        state: JobState::Finalized,
        current_phase: None,
        progress: Progress::Unknown,
        label: None,
        exit: None,
        outcome: Some(Outcome::Succeeded {
            summary: None,
            findings: vec![],
        }),
    };
    store.upsert_job(&job).await.unwrap();
    drop(store);

    let conn = rusqlite::Connection::open(&config.db_path).unwrap();
    conn.execute("UPDATE jobs SET schema_version = 1", [])
        .unwrap();
    drop(conn);

    let store = execra::store::Store::open(&config.db_path).await.unwrap();
    let err = store.load_job(id).await.unwrap_err();
    assert!(matches!(
        err,
        execra::store::StoreError::UnsupportedSchemaVersion {
            found: 1,
            supported: 0
        }
    ));
}

#[test]
fn command_hide_window_flag_is_stored() {
    assert!(Command::new("cmd").hide_window(true).spec().hide_window);
}

#[test]
fn command_helpers_build_platform_shells() {
    let shell = Command::shell("echo helper");
    #[cfg(windows)]
    {
        assert_eq!(shell.spec().program, "cmd");
        assert_eq!(shell.spec().args, vec!["/C", "echo helper"]);
        assert!(Command::new("cmd").spec().hide_window);
    }
    #[cfg(not(windows))]
    {
        assert_eq!(shell.spec().program, "sh");
        assert_eq!(shell.spec().args, vec!["-c", "echo helper"]);
        assert!(!Command::new("sh").spec().hide_window);
    }

    assert_eq!(
        Command::powershell("Write-Host ok").spec().program,
        "powershell"
    );
    assert_eq!(Command::pwsh("Write-Host ok").spec().program, "pwsh");
    assert_eq!(Command::cmd("echo ok").spec().args, vec!["/C", "echo ok"]);
    assert_eq!(Command::sh("echo ok").spec().args, vec!["-c", "echo ok"]);
}