baraddur 0.1.3

Project-agnostic file watcher that surfaces issues before CI
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
use baraddur::config::{Config, OnFailureConfig, OutputConfig, Step, WatchConfig};
use baraddur::output::Display;
use baraddur::pipeline;
use baraddur::pipeline::StepResult;

/// Test display that records all lifecycle events for assertion.
#[derive(Default)]
struct RecordingDisplay {
    events: Vec<String>,
}

impl Display for RecordingDisplay {
    fn run_started(&mut self, step_names: &[String]) {
        self.events
            .push(format!("run_started:{}", step_names.join(",")));
    }

    fn step_running(&mut self, name: &str) {
        self.events.push(format!("running:{name}"));
    }

    fn step_finished(&mut self, r: &StepResult) {
        self.events
            .push(format!("finished:{}:{}", r.name, r.success));
    }

    fn steps_skipped(&mut self, names: &[String]) {
        for name in names {
            self.events.push(format!("skipped:{name}"));
        }
    }

    fn run_cancelled(&mut self) {
        self.events.push("run_cancelled".into());
    }

    fn run_finished(&mut self, _results: &[StepResult]) {
        self.events.push("run_finished".into());
    }
}

fn make_config(steps: Vec<Step>) -> Config {
    Config {
        watch: WatchConfig {
            extensions: vec!["rs".into()],
            debounce_ms: 1000,
            ignore: vec![],
        },
        output: OutputConfig::default(),
        on_failure: OnFailureConfig::default(),
        steps,
    }
}

// ── Sequential behavior ──────────────────────────────────────────────────────

#[tokio::test]
async fn sequential_stops_at_first_failure() {
    let cfg = make_config(vec![
        Step {
            name: "first".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "second".into(),
            cmd: "false".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "third".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    // first passes, second fails, third is skipped.
    assert_eq!(results.len(), 2);
    assert!(results[0].success);
    assert!(!results[1].success);

    assert!(display.events.contains(&"skipped:third".to_string()));
    assert!(
        !display
            .events
            .iter()
            .any(|e| e.starts_with("running:third"))
    );
}

#[tokio::test]
async fn sequential_all_pass() {
    let cfg = make_config(vec![
        Step {
            name: "a".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "b".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "c".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    assert_eq!(results.len(), 3);
    assert!(results.iter().all(|r| r.success));
}

// ── Parallel execution ───────────────────────────────────────────────────────

#[tokio::test]
async fn parallel_steps_all_run() {
    let cfg = make_config(vec![
        Step {
            name: "a".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
        Step {
            name: "b".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
        Step {
            name: "c".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    assert_eq!(results.len(), 3);
    assert!(results.iter().all(|r| r.success));

    // All three should have been marked running before any finished.
    let events = &display.events;
    let running_indices: Vec<usize> = events
        .iter()
        .enumerate()
        .filter(|(_, e)| e.starts_with("running:"))
        .map(|(i, _)| i)
        .collect();
    let first_finished = events
        .iter()
        .position(|e| e.starts_with("finished:"))
        .unwrap();
    assert!(
        running_indices.iter().all(|&i| i < first_finished),
        "all steps should be marked running before any finish"
    );
}

#[tokio::test]
async fn parallel_stage_runs_all_even_if_one_fails() {
    let cfg = make_config(vec![
        Step {
            name: "pass".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
        Step {
            name: "fail".into(),
            cmd: "false".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    // Both steps ran — even though one failed.
    assert_eq!(results.len(), 2);
    assert!(results.iter().any(|r| r.success));
    assert!(results.iter().any(|r| !r.success));
}

#[tokio::test]
async fn parallel_wall_clock_is_max_not_sum() {
    // Two steps that each sleep 0.3s. If parallel, wall clock should be
    // ~0.3s, not ~0.6s. Allow generous margin for CI.
    let cfg = make_config(vec![
        Step {
            name: "slow_a".into(),
            cmd: "sleep 0.3".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
        Step {
            name: "slow_b".into(),
            cmd: "sleep 0.3".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let start = std::time::Instant::now();
    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();
    let elapsed = start.elapsed();

    assert_eq!(results.len(), 2);
    assert!(
        elapsed.as_secs_f64() < 0.55,
        "parallel steps took {:.2}s — expected under 0.55s",
        elapsed.as_secs_f64()
    );
}

// ── Mixed stages ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn mixed_stages_sequential_then_parallel() {
    let cfg = make_config(vec![
        Step {
            name: "seq".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "par_a".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
        Step {
            name: "par_b".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    assert_eq!(results.len(), 3);
    assert!(results.iter().all(|r| r.success));

    let events = &display.events;
    // seq must finish before par_a/par_b start running.
    let seq_finished = events
        .iter()
        .position(|e| e == "finished:seq:true")
        .unwrap();
    let par_a_running = events.iter().position(|e| e == "running:par_a").unwrap();
    assert!(seq_finished < par_a_running);
}

#[tokio::test]
async fn stage_failure_skips_subsequent_stages() {
    let cfg = make_config(vec![
        Step {
            name: "fail".into(),
            cmd: "false".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "skip_a".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
        Step {
            name: "skip_b".into(),
            cmd: "true".into(),
            parallel: true,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    // Only the failing step ran.
    assert_eq!(results.len(), 1);
    assert!(!results[0].success);

    assert!(display.events.contains(&"skipped:skip_a".to_string()));
    assert!(display.events.contains(&"skipped:skip_b".to_string()));
}

// ── Output capture ───────────────────────────────────────────────────────────

#[tokio::test]
async fn captures_stdout_and_stderr_on_failure() {
    let cfg = make_config(vec![Step {
        name: "noisyfail".into(),
        cmd: "sh -c 'echo out; echo err >&2; exit 1'".into(),
        parallel: false,
        if_changed: Vec::new(),
    }]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    assert_eq!(results.len(), 1);
    assert!(!results[0].success);
    assert!(results[0].stdout.contains("out"));
    assert!(results[0].stderr.contains("err"));
}

// ── Path-based filtering ─────────────────────────────────────────────────────

#[tokio::test]
async fn trigger_excludes_steps_with_no_glob_matches() {
    use std::path::PathBuf;

    let cfg = make_config(vec![
        Step {
            name: "rust".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: vec!["**/*.rs".into()],
        },
        Step {
            name: "ts".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: vec!["**/*.ts".into()],
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    // Only a .ts file changed — the rust step should be excluded entirely.
    let trigger = vec![PathBuf::from("src/app.ts")];
    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, Some(&trigger), None)
        .await
        .unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].name, "ts");
    assert!(
        !display.events.iter().any(|e| e.contains("rust")),
        "rust step should not appear in events"
    );
}

#[tokio::test]
async fn files_template_substitutes_matched_paths() {
    use std::path::PathBuf;

    // `printf %s {files}` writes the substituted paths to stdout — easy to
    // assert on.
    let cfg = make_config(vec![Step {
        name: "echo".into(),
        cmd: "printf %s {files}".into(),
        parallel: false,
        if_changed: vec!["**/*.rs".into()],
    }]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let trigger = vec![PathBuf::from("src/a.rs"), PathBuf::from("README.md")];
    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, Some(&trigger), None)
        .await
        .unwrap();

    assert_eq!(results.len(), 1);
    // Only the .rs path should have been substituted.
    assert_eq!(results[0].stdout, "src/a.rs");
}

#[tokio::test]
async fn initial_run_runs_all_steps_ignoring_if_changed() {
    let cfg = make_config(vec![Step {
        name: "rust".into(),
        cmd: "true".into(),
        parallel: false,
        if_changed: vec!["**/*.rs".into()],
    }]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    // Initial run = trigger is None. Step must run despite if_changed.
    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, None)
        .await
        .unwrap();

    assert_eq!(results.len(), 1);
    assert!(results[0].success);
}

#[tokio::test]
async fn only_steps_narrows_to_named_subset() {
    // Simulates the browse-mode `f` key: rerun only steps that previously failed.
    let cfg = make_config(vec![
        Step {
            name: "a".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "b".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
        Step {
            name: "c".into(),
            cmd: "true".into(),
            parallel: false,
            if_changed: Vec::new(),
        },
    ]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let only = vec!["b".to_string()];
    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, Some(&only))
        .await
        .unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].name, "b");
    // The other steps shouldn't appear in the recorded events.
    assert!(
        !display
            .events
            .iter()
            .any(|e| e.ends_with(":a") || e.ends_with(":c")),
        "only step `b` should appear; got: {:?}",
        display.events
    );
}

#[tokio::test]
async fn only_steps_empty_runs_nothing() {
    // Edge: pressing `f` after an all-pass run gives an empty filter; pipeline
    // should run zero steps but not error.
    let cfg = make_config(vec![Step {
        name: "a".into(),
        cmd: "true".into(),
        parallel: false,
        if_changed: Vec::new(),
    }]);
    let mut display = RecordingDisplay::default();
    let cwd = std::env::current_dir().unwrap();

    let only: Vec<String> = Vec::new();
    let results = pipeline::run_pipeline(&cfg, &cwd, &mut display, None, None, Some(&only))
        .await
        .unwrap();

    assert!(results.is_empty());
}