xx 2.5.4

A collection of useful Rust macros and small functions.
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
//! Process execution utilities
//!
//! This module provides convenient functions and builders for executing external processes
//! with better ergonomics than the standard library's `std::process` module.
//!
//! ## Features
//!
//! - Simple shell command execution with `sh()`
//! - Builder pattern for complex command construction
//! - Automatic stdout/stderr capture options
//! - Enhanced error messages that include the command that failed
//!
//! ## Examples
//!
//! ### Simple shell command
//!
//! ```rust,no_run
//! use xx::process;
//!
//! # fn main() -> xx::XXResult<()> {
//! // Run a shell command and get stdout as a string
//! let output = process::sh("echo hello world")?;
//! assert_eq!(output.trim(), "hello world");
//! # Ok(())
//! # }
//! ```
//!
//! ### Command builder
//!
//! ```rust,no_run
//! use xx::process;
//!
//! # fn main() -> xx::XXResult<()> {
//! // Build a command with arguments
//! let output = process::cmd("git", &["status", "--short"])
//!     .read()?;
//!
//! // Capture stdout and stderr separately
//! let result = process::cmd("make", &["test"])
//!     .stdout_capture()
//!     .stderr_capture()
//!     .run()?;
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::Arc;
use std::thread;
use std::{ffi::OsString, fmt, io, process::Output};

type LineHandler = dyn Fn(&str) + Send + Sync + 'static;

use duct::IntoExecutablePath;

use crate::{XXError, XXResult};

pub fn sh(script: &str) -> XXResult<String> {
    let output = Command::new("sh")
        .arg("-c")
        .arg(script)
        .stdin(std::process::Stdio::inherit())
        .stderr(std::process::Stdio::inherit())
        .output()
        .map_err(|err| XXError::ProcessError(err, format!("sh -c {script}")))?;

    check_status(output.status)
        .map_err(|err| XXError::ProcessError(err, format!("sh -c {script}")))?;
    let stdout = String::from_utf8(output.stdout).expect("stdout is not utf-8");
    Ok(stdout)
}

pub fn check_status(status: ExitStatus) -> io::Result<()> {
    if status.success() {
        return Ok(());
    }
    let msg = if let Some(code) = status.code() {
        format!("exited with code {code}")
    } else {
        "terminated by signal".to_string()
    };
    Err(io::Error::other(msg))
}

#[derive(Default)]
pub struct XXExpression {
    program: OsString,
    args: Vec<OsString>,
    stdout_capture: bool,
    stderr_capture: bool,
    stdout_handler: Option<Arc<LineHandler>>,
    stderr_handler: Option<Arc<LineHandler>>,
    env_vars: HashMap<OsString, OsString>,
    env_clear: bool,
    cwd: Option<PathBuf>,
    stdin_data: Option<Vec<u8>>,
    unchecked: bool,
}

pub fn cmd<T, U>(program: T, args: U) -> XXExpression
where
    T: IntoExecutablePath,
    U: IntoIterator,
    U::Item: Into<OsString>,
{
    let program = program.to_executable();
    let args = args.into_iter().map(|arg| arg.into()).collect::<Vec<_>>();
    XXExpression {
        program,
        args,
        ..Default::default()
    }
}

impl XXExpression {
    pub fn stdout_capture(mut self) -> Self {
        self.stdout_capture = true;
        self
    }

    pub fn stderr_capture(mut self) -> Self {
        self.stderr_capture = true;
        self
    }

    pub fn arg(mut self, arg: impl Into<OsString>) -> Self {
        self.args.push(arg.into());
        self
    }

    pub fn args(mut self, args: impl IntoIterator<Item = impl Into<OsString>>) -> Self {
        self.args.extend(args.into_iter().map(|arg| arg.into()));
        self
    }

    pub fn run(&self) -> XXResult<Output> {
        debug!("$ {self}");
        if self.stdout_handler.is_some() || self.stderr_handler.is_some() {
            // Inline streaming behavior previously provided by `run_streaming`
            let mut cmd = Command::new(&self.program);
            cmd.args(&self.args)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped());

            // Handle stdin
            if self.stdin_data.is_some() {
                cmd.stdin(Stdio::piped());
            } else {
                cmd.stdin(Stdio::inherit());
            }

            // Handle environment
            if self.env_clear {
                cmd.env_clear();
            }
            for (k, v) in &self.env_vars {
                cmd.env(k, v);
            }

            // Handle working directory
            if let Some(cwd) = &self.cwd {
                cmd.current_dir(cwd);
            }

            let mut child = cmd
                .spawn()
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;

            // Write stdin data in a separate thread to avoid deadlock when combining
            // large stdin with stdout/stderr handlers. Without this, if stdin data
            // exceeds the pipe buffer (~64KB) and the child fills its stdout buffer
            // before consuming stdin, both parent and child would block.
            let stdin_handle = self.stdin_data.clone().and_then(|stdin_data| {
                child.stdin.take().map(|mut stdin| {
                    thread::spawn(move || {
                        use std::io::Write;
                        let _ = stdin.write_all(&stdin_data);
                    })
                })
            });

            let mut stdout = child
                .stdout
                .take()
                .ok_or_else(|| io::Error::other("failed to capture stdout"))
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;
            let mut stderr = child
                .stderr
                .take()
                .ok_or_else(|| io::Error::other("failed to capture stderr"))
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;

            let out_h = self.stdout_handler.clone();
            let stdout_handle = thread::spawn(move || {
                let mut reader = io::BufReader::new(&mut stdout);
                let mut line = String::with_capacity(1024);
                loop {
                    line.clear();
                    match reader.read_line(&mut line) {
                        Ok(0) => break,
                        Ok(_) => {
                            if line.ends_with('\n') {
                                line.pop();
                                if line.ends_with('\r') {
                                    line.pop();
                                }
                            } else if line.ends_with('\r') {
                                line.pop();
                            }
                            // this can be removed in rust 1.88
                            #[allow(clippy::collapsible_if)]
                            if !line.is_empty() {
                                if let Some(h) = &out_h {
                                    (h)(&line);
                                }
                            }
                        }
                        Err(_) => break,
                    }
                }
            });

            let err_h = self.stderr_handler.clone();
            let stderr_handle = thread::spawn(move || {
                let mut reader = io::BufReader::new(&mut stderr);
                let mut line = String::with_capacity(1024);
                loop {
                    line.clear();
                    match reader.read_line(&mut line) {
                        Ok(0) => break,
                        Ok(_) => {
                            if line.ends_with('\n') {
                                line.pop();
                                if line.ends_with('\r') {
                                    line.pop();
                                }
                            } else if line.ends_with('\r') {
                                line.pop();
                            }
                            // this can be removed in rust 1.88
                            #[allow(clippy::collapsible_if)]
                            if !line.is_empty() {
                                if let Some(h) = &err_h {
                                    (h)(&line);
                                }
                            }
                        }
                        Err(_) => break,
                    }
                }
            });

            let status = child
                .wait()
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;

            if let Some(h) = stdin_handle {
                let _ = h.join();
            }
            let _ = stdout_handle.join();
            let _ = stderr_handle.join();

            if !self.unchecked {
                check_status(status).map_err(|err| XXError::ProcessError(err, self.to_string()))?;
            }
            return Ok(Output {
                status,
                stdout: vec![],
                stderr: vec![],
            });
        }
        let expr = self.build_expr();
        expr.run()
            .map_err(|err| XXError::ProcessError(err, self.to_string()))
    }

    pub fn read(&self) -> XXResult<String> {
        debug!("$ {self}");
        if self.stdout_handler.is_some() || self.stderr_handler.is_some() {
            let mut cmd = Command::new(&self.program);
            cmd.args(&self.args)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped());

            // Handle stdin
            if self.stdin_data.is_some() {
                cmd.stdin(Stdio::piped());
            } else {
                cmd.stdin(Stdio::inherit());
            }

            // Handle environment
            if self.env_clear {
                cmd.env_clear();
            }
            for (k, v) in &self.env_vars {
                cmd.env(k, v);
            }

            // Handle working directory
            if let Some(cwd) = &self.cwd {
                cmd.current_dir(cwd);
            }

            let mut child = cmd
                .spawn()
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;

            // Write stdin data in a separate thread to avoid deadlock (see run() for details)
            let stdin_handle = self.stdin_data.clone().and_then(|stdin_data| {
                child.stdin.take().map(|mut stdin| {
                    thread::spawn(move || {
                        use std::io::Write;
                        let _ = stdin.write_all(&stdin_data);
                    })
                })
            });

            let mut stderr = child
                .stderr
                .take()
                .ok_or_else(|| io::Error::other("failed to capture stderr"))
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;

            // Drain stderr on a background thread, invoking handler if present
            let err_h = self.stderr_handler.clone();
            let stderr_handle = thread::spawn(move || {
                let mut reader = io::BufReader::new(&mut stderr);
                let mut line = String::with_capacity(1024);
                loop {
                    line.clear();
                    match reader.read_line(&mut line) {
                        Ok(0) => break,
                        Ok(_) => {
                            if line.ends_with('\n') {
                                line.pop();
                                if line.ends_with('\r') {
                                    line.pop();
                                }
                            } else if line.ends_with('\r') {
                                line.pop();
                            }
                            // this can be removed in rust 1.88
                            #[allow(clippy::collapsible_if)]
                            if !line.is_empty() {
                                if let Some(h) = &err_h {
                                    (h)(&line);
                                }
                            }
                        }
                        Err(_) => break,
                    }
                }
            });

            // Read stdout line-by-line in the current thread, optionally emitting handler,
            // while reconstructing the full stdout for return
            let mut stdout = child
                .stdout
                .take()
                .ok_or_else(|| io::Error::other("failed to capture stdout"))
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;
            let out_h = self.stdout_handler.clone();
            let mut reader = io::BufReader::new(&mut stdout);
            let mut acc = String::new();
            let mut line = String::with_capacity(1024);
            loop {
                line.clear();
                match reader.read_line(&mut line) {
                    Ok(0) => break,
                    Ok(_) => {
                        let mut had_nl = false;
                        if line.ends_with('\n') {
                            had_nl = true;
                            line.pop();
                            if line.ends_with('\r') {
                                line.pop();
                            }
                        } else if line.ends_with('\r') {
                            line.pop();
                        }
                        if !line.is_empty() {
                            if let Some(h) = &out_h {
                                (h)(&line);
                            }
                            acc.push_str(&line);
                        }
                        if had_nl {
                            acc.push('\n');
                        }
                    }
                    Err(_) => break,
                }
            }

            let status = child
                .wait()
                .map_err(|err| XXError::ProcessError(err, self.to_string()))?;
            if let Some(h) = stdin_handle {
                let _ = h.join();
            }
            let _ = stderr_handle.join();
            if !self.unchecked {
                check_status(status).map_err(|err| XXError::ProcessError(err, self.to_string()))?;
            }
            // Match duct's `read()` behavior: trim a single trailing newline
            if acc.ends_with('\n') {
                let _ = acc.pop();
            }
            return Ok(acc);
        }
        let expr = self.build_expr();
        expr.read()
            .map_err(|err| XXError::ProcessError(err, self.to_string()))
    }

    // run_streaming removed; streaming logic is now handled inline in `run()`

    /// Register a line-by-line stdout handler. When set, `run()` will stream output lines
    /// to this handler instead of capturing stdout.
    pub fn on_stdout_line<F>(mut self, handler: F) -> Self
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        self.stdout_handler = Some(Arc::new(handler));
        self
    }

    /// Register a line-by-line stderr handler. When set, `run()` will stream error lines
    /// to this handler instead of capturing stderr.
    pub fn on_stderr_line<F>(mut self, handler: F) -> Self
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        self.stderr_handler = Some(Arc::new(handler));
        self
    }

    /// Set an environment variable for this command
    /// # Example
    /// ```
    /// use xx::process;
    /// let output = process::cmd("sh", ["-c", "echo $MY_VAR"])
    ///     .env("MY_VAR", "hello")
    ///     .read()
    ///     .unwrap();
    /// assert_eq!(output, "hello");
    /// ```
    pub fn env<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<OsString>,
        V: Into<OsString>,
    {
        self.env_vars.insert(key.into(), value.into());
        self
    }

    /// Set multiple environment variables for this command
    /// # Example
    /// ```
    /// use xx::process;
    /// use std::collections::HashMap;
    /// let mut env = HashMap::new();
    /// env.insert("VAR1", "value1");
    /// env.insert("VAR2", "value2");
    /// let output = process::cmd("sh", ["-c", "echo $VAR1 $VAR2"])
    ///     .envs(env)
    ///     .read()
    ///     .unwrap();
    /// assert_eq!(output, "value1 value2");
    /// ```
    pub fn envs<I, K, V>(mut self, vars: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<OsString>,
        V: Into<OsString>,
    {
        for (k, v) in vars {
            self.env_vars.insert(k.into(), v.into());
        }
        self
    }

    /// Clear all environment variables before running (start with empty environment)
    /// # Example
    /// ```
    /// use xx::process;
    /// let output = process::cmd("sh", ["-c", "echo ${PATH:-empty}"])
    ///     .env_clear()
    ///     .env("PATH", "/bin:/usr/bin")
    ///     .read()
    ///     .unwrap();
    /// assert_eq!(output, "/bin:/usr/bin");
    /// ```
    pub fn env_clear(mut self) -> Self {
        self.env_clear = true;
        self
    }

    /// Set the working directory for this command
    /// # Example
    /// ```
    /// use xx::process;
    /// let output = process::cmd("pwd", Vec::<&str>::new())
    ///     .cwd("/tmp")
    ///     .read()
    ///     .unwrap();
    /// assert!(output.contains("tmp"));
    /// ```
    pub fn cwd<P: AsRef<Path>>(mut self, dir: P) -> Self {
        self.cwd = Some(dir.as_ref().to_path_buf());
        self
    }

    /// Provide stdin data as bytes
    /// # Example
    /// ```
    /// use xx::process;
    /// let output = process::cmd("cat", Vec::<&str>::new())
    ///     .stdin_bytes(b"hello world")
    ///     .read()
    ///     .unwrap();
    /// assert_eq!(output, "hello world");
    /// ```
    pub fn stdin_bytes<B: AsRef<[u8]>>(mut self, data: B) -> Self {
        self.stdin_data = Some(data.as_ref().to_vec());
        self
    }

    /// Provide stdin data from a file
    /// # Example
    /// ```no_run
    /// use xx::process;
    /// let output = process::cmd("cat", Vec::<&str>::new())
    ///     .stdin_file("input.txt")
    ///     .unwrap()
    ///     .read()
    ///     .unwrap();
    /// ```
    pub fn stdin_file<P: AsRef<Path>>(mut self, path: P) -> XXResult<Self> {
        let path = path.as_ref();
        let data =
            std::fs::read(path).map_err(|err| XXError::FileError(err, path.to_path_buf()))?;
        self.stdin_data = Some(data);
        Ok(self)
    }

    /// Don't check the exit status (allow non-zero exit codes)
    ///
    /// By default, `run()` and `read()` return an error if the process exits
    /// with a non-zero status. This method disables that check.
    ///
    /// # Example
    /// ```
    /// use xx::process;
    /// // This would normally error because false exits with code 1
    /// let output = process::cmd("false", Vec::<&str>::new())
    ///     .unchecked()
    ///     .run()
    ///     .unwrap();
    /// assert!(!output.status.success());
    /// ```
    pub fn unchecked(mut self) -> Self {
        self.unchecked = true;
        self
    }

    fn build_expr(&self) -> duct::Expression {
        let mut expr = duct::cmd(self.program.clone(), self.args.clone());
        if self.stdout_capture {
            expr = expr.stdout_capture();
        }
        if self.stderr_capture {
            expr = expr.stderr_capture();
        }
        if self.env_clear {
            expr = expr.full_env(self.env_vars.clone());
        } else {
            for (k, v) in &self.env_vars {
                expr = expr.env(k, v);
            }
        }
        if let Some(cwd) = &self.cwd {
            expr = expr.dir(cwd);
        }
        if let Some(stdin_data) = &self.stdin_data {
            expr = expr.stdin_bytes(stdin_data.clone());
        }
        if self.unchecked {
            expr = expr.unchecked();
        }
        expr
    }
}

impl fmt::Display for XXExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}",
            self.program.to_string_lossy(),
            self.args
                .iter()
                .map(|arg| arg.to_string_lossy())
                .collect::<Vec<_>>()
                .join(" ")
        )
    }
}

mod tests {
    #[allow(unused_imports)]
    use super::*;
    #[allow(unused_imports)]
    use std::sync::{Arc, Mutex};

    #[test]
    fn test_cmd() {
        let expr = cmd("echo", ["hello", "world"]).stdout_capture();
        let output = expr.run().unwrap();
        assert!(output.status.success());
        assert_eq!(output.stdout, b"hello world\n");
    }

    #[test]
    fn test_cmd_read() {
        let expr = cmd("echo", ["hello"]).arg("world").args(["foo", "bar"]);
        let output = expr.read().unwrap();
        assert_eq!(output, "hello world foo bar");
    }

    #[test]
    fn test_line_handlers_capture_stdout_and_stderr_lines() {
        // Use sh to emit interleaved stdout/stderr lines
        let script = r#"
            printf 'o1\n';
            printf 'e1\n' 1>&2;
            printf 'o2\n';
            printf 'e2\n' 1>&2;
        "#;
        let out_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
        let err_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));

        let out_clone = out_lines.clone();
        let err_clone = err_lines.clone();

        let output = cmd("sh", ["-c", script])
            .on_stdout_line(move |line| out_clone.lock().unwrap().push(line.to_string()))
            .on_stderr_line(move |line| err_clone.lock().unwrap().push(line.to_string()))
            .run()
            .unwrap();
        assert!(output.status.success());

        let mut out = out_lines.lock().unwrap().clone();
        let mut err = err_lines.lock().unwrap().clone();
        out.sort();
        err.sort();
        assert_eq!(out, vec!["o1", "o2"]);
        assert_eq!(err, vec!["e1", "e2"]);
    }

    #[test]
    fn test_line_handlers_propagate_nonzero_exit() {
        // Emit some output and then exit non-zero
        let script = r#"
            printf 'ok\n';
            printf 'bad\n' 1>&2;
            exit 3;
        "#;
        let res = cmd("sh", ["-c", script])
            .on_stdout_line(|_| {})
            .on_stderr_line(|_| {})
            .run();
        assert!(res.is_err());
        let err = format!("{}", res.unwrap_err());
        assert!(err.contains("sh -c"));
    }

    #[test]
    fn test_line_handlers_handle_partial_last_line() {
        // Emit lines without trailing newline at the end
        let script = r#"
            printf 'a1\n';
            printf 'b1' 1>&2;
        "#;
        let out_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
        let err_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
        let out_clone = out_lines.clone();
        let err_clone = err_lines.clone();
        let output = cmd("sh", ["-c", script])
            .on_stdout_line(move |line| out_clone.lock().unwrap().push(line.to_string()))
            .on_stderr_line(move |line| err_clone.lock().unwrap().push(line.to_string()))
            .run()
            .unwrap();
        assert!(output.status.success());
        assert_eq!(out_lines.lock().unwrap().as_slice(), ["a1"]);
        assert_eq!(err_lines.lock().unwrap().as_slice(), ["b1"]);
    }

    #[test]
    fn test_line_handlers_trim_crlf() {
        // Ensure CRLF endings are normalized before handler invocation
        let script = r#"
            printf 'x1\r\n';
            printf 'y1\r\n' 1>&2;
        "#;
        let out_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
        let err_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
        let out_clone = out_lines.clone();
        let err_clone = err_lines.clone();
        let output = cmd("sh", ["-c", script])
            .on_stdout_line(move |line| out_clone.lock().unwrap().push(line.to_string()))
            .on_stderr_line(move |line| err_clone.lock().unwrap().push(line.to_string()))
            .run()
            .unwrap();
        assert!(output.status.success());
        assert_eq!(out_lines.lock().unwrap().as_slice(), ["x1"]);
        assert_eq!(err_lines.lock().unwrap().as_slice(), ["y1"]);
    }

    #[test]
    fn test_read_with_handlers_returns_full_stdout_and_invokes_handlers() {
        let script = r#"
            printf 'l1\n';
            printf 'l2\n';
        "#;
        let lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
        let lines_clone = lines.clone();
        let out = cmd("sh", ["-c", script])
            .on_stdout_line(move |line| lines_clone.lock().unwrap().push(line.to_string()))
            .read()
            .unwrap();
        assert_eq!(out, "l1\nl2");
        assert_eq!(lines.lock().unwrap().as_slice(), ["l1", "l2"]);
    }

    #[test]
    fn test_read_without_handlers_trims_trailing_newline() {
        let script = r#"
            printf 'a\n';
            printf 'b\n';
        "#;
        let out = cmd("sh", ["-c", script]).read().unwrap();
        assert_eq!(out, "a\nb");
    }

    #[test]
    fn test_env() {
        let out = cmd("sh", ["-c", "echo $TEST_VAR"])
            .env("TEST_VAR", "hello")
            .read()
            .unwrap();
        assert_eq!(out, "hello");
    }

    #[test]
    fn test_envs() {
        let mut vars = std::collections::HashMap::new();
        vars.insert("VAR1", "a");
        vars.insert("VAR2", "b");
        let out = cmd("sh", ["-c", "echo $VAR1 $VAR2"])
            .envs(vars)
            .read()
            .unwrap();
        assert_eq!(out, "a b");
    }

    #[test]
    fn test_cwd() {
        let out = cmd("pwd", Vec::<&str>::new()).cwd("/tmp").read().unwrap();
        // Handle macOS /private/tmp symlink
        assert!(out.contains("tmp"));
    }

    #[test]
    fn test_stdin_bytes() {
        let out = cmd("cat", Vec::<&str>::new())
            .stdin_bytes(b"hello stdin")
            .read()
            .unwrap();
        assert_eq!(out, "hello stdin");
    }

    #[test]
    fn test_unchecked() {
        // Without unchecked, this would error
        let output = cmd("false", Vec::<&str>::new()).unchecked().run().unwrap();
        assert!(!output.status.success());
    }

    #[test]
    fn test_unchecked_read() {
        // Without unchecked, this would error
        let output = cmd("sh", ["-c", "echo hello; exit 1"])
            .unchecked()
            .read()
            .unwrap();
        assert_eq!(output, "hello");
    }
}