click-rs 1.0.1

A Rust port of Python's Click library for creating command-line interfaces
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
//! Testing utilities for click-rs applications.
//!
//! This module provides utilities for testing CLI applications built with click-rs.
//! The main component is [`CliRunner`], which allows invoking commands with captured
//! output for assertions.
//!
//! # Reference
//!
//! Based on Python Click's `testing.py`.
//!
//! # Example
//!
//! ```rust
//! use click::testing::CliRunner;
//! use click::command::Command;
//!
//! let cmd = Command::new("hello")
//!     .callback(|_ctx| {
//!         // Your command logic here
//!         Ok(())
//!     })
//!     .build();
//!
//! let runner = CliRunner::new();
//! let result = runner.invoke(&cmd, &[]);
//!
//! assert_eq!(result.exit_code, 0);
//! assert!(result.is_success());
//! ```

use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::{self, Read, Write};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use std::thread;

use crate::group::CommandLike;
use encoding_rs::Encoding;

#[derive(Debug)]
enum CaptureOutcome {
    Returned(Result<(), crate::ClickError>),
    Panicked(Box<dyn std::any::Any + Send + 'static>),
}

fn panic_message(panic: &(dyn std::any::Any + Send + 'static)) -> String {
    if let Some(s) = panic.downcast_ref::<&str>() {
        (*s).to_string()
    } else if let Some(s) = panic.downcast_ref::<String>() {
        s.clone()
    } else {
        "panic".to_string()
    }
}

fn restore_env(saved_env: &[(String, Option<String>)]) {
    for (key, value) in saved_env {
        match value {
            Some(v) => env::set_var(key, v),
            None => env::remove_var(key),
        }
    }
}

/// A global lock used to serialize stdio redirection during output capture.
///
/// Output capture redirects process-global file descriptors, which is not safe
/// to do concurrently from multiple threads. The lock prevents tests (and other
/// concurrent invocations) from corrupting each other's stdio streams.
#[cfg(any(unix, windows))]
static IO_CAPTURE_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

#[cfg(any(unix, windows))]
fn capture_lock() -> std::sync::MutexGuard<'static, ()> {
    IO_CAPTURE_LOCK
        .get_or_init(|| Mutex::new(()))
        .lock()
        .expect("IO capture lock poisoned")
}

#[cfg(unix)]
fn run_with_capture<F>(input: &str, f: F) -> io::Result<(CaptureOutcome, Vec<u8>, Vec<u8>)>
where
    F: FnOnce() -> Result<(), crate::ClickError>,
{
    use nix::unistd::{dup, dup2_stderr, dup2_stdin, dup2_stdout};

    let _lock = capture_lock();

    // Pipes for stdout/stderr/stdin capture (os_pipe: safe, cross-platform).
    let (out_reader_pipe, out_writer_pipe) = os_pipe::pipe()?;
    let (err_reader_pipe, err_writer_pipe) = os_pipe::pipe()?;
    let (in_reader_pipe, in_writer_pipe) = os_pipe::pipe()?;

    // Save original fds (nix::dup returns OwnedFd with RAII cleanup).
    let saved_stdin = dup(io::stdin()).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
    let saved_stdout = dup(io::stdout()).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
    let saved_stderr = dup(io::stderr()).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

    // Write input then close stdin write end (PipeWriter Drop closes fd).
    {
        let mut writer = in_writer_pipe;
        let _ = writer.write_all(input.as_bytes());
    }

    // Redirect stdio using nix safe wrappers.
    let redir_ok = dup2_stdin(&in_reader_pipe)
        .and_then(|_| dup2_stdout(&out_writer_pipe))
        .and_then(|_| dup2_stderr(&err_writer_pipe))
        .is_ok();

    // Close pipe ends that have been duped into fd 0/1/2 (RAII Drop).
    drop(in_reader_pipe);
    drop(out_writer_pipe);
    drop(err_writer_pipe);

    if !redir_ok {
        let _ = dup2_stdin(&saved_stdin);
        let _ = dup2_stdout(&saved_stdout);
        let _ = dup2_stderr(&saved_stderr);
        return Err(io::Error::new(
            io::ErrorKind::Other,
            "failed to redirect stdio",
        ));
    }

    // Reader threads use PipeReader directly (implements Read, no unsafe needed).
    let out_thread = thread::spawn(move || {
        let mut buf = Vec::new();
        let mut reader = out_reader_pipe;
        let _ = reader.read_to_end(&mut buf);
        buf
    });
    let err_thread = thread::spawn(move || {
        let mut buf = Vec::new();
        let mut reader = err_reader_pipe;
        let _ = reader.read_to_end(&mut buf);
        buf
    });

    let old_panic_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let unwind = catch_unwind(AssertUnwindSafe(f));
    std::panic::set_hook(old_panic_hook);

    let _ = io::stdout().flush();
    let _ = io::stderr().flush();

    // Restore stdio (OwnedFd auto-closes via Drop after dup2).
    let _ = dup2_stdin(&saved_stdin);
    let _ = dup2_stdout(&saved_stdout);
    let _ = dup2_stderr(&saved_stderr);

    let stdout_bytes = out_thread.join().unwrap_or_default();
    let stderr_bytes = err_thread.join().unwrap_or_default();

    let outcome = match unwind {
        Ok(r) => CaptureOutcome::Returned(r),
        Err(panic) => CaptureOutcome::Panicked(panic),
    };

    Ok((outcome, stdout_bytes, stderr_bytes))
}

#[cfg(windows)]
fn run_with_capture<F>(input: &str, f: F) -> io::Result<(CaptureOutcome, Vec<u8>, Vec<u8>)>
where
    F: FnOnce() -> Result<(), crate::ClickError>,
{
    use std::os::windows::io::AsRawHandle;
    use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
    use windows_sys::Win32::System::Console::{
        GetStdHandle, SetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
    };

    let _lock = capture_lock();

    // Pipes for stdout/stderr/stdin capture (os_pipe: safe, cross-platform).
    let (out_reader_pipe, out_writer_pipe) = os_pipe::pipe()?;
    let (err_reader_pipe, err_writer_pipe) = os_pipe::pipe()?;
    let (in_reader_pipe, in_writer_pipe) = os_pipe::pipe()?;

    // SAFETY: GetStdHandle is a read-only query of the process-global stdio table.
    // The returned handles are borrowed (not owned) and we only use them to
    // restore the original state later. IO_CAPTURE_LOCK serializes access.
    let (saved_in, saved_out, saved_err) = unsafe {
        (
            GetStdHandle(STD_INPUT_HANDLE),
            GetStdHandle(STD_OUTPUT_HANDLE),
            GetStdHandle(STD_ERROR_HANDLE),
        )
    };

    if saved_in == 0
        || saved_out == 0
        || saved_err == 0
        || saved_in == INVALID_HANDLE_VALUE
        || saved_out == INVALID_HANDLE_VALUE
        || saved_err == INVALID_HANDLE_VALUE
    {
        return Err(io::Error::new(io::ErrorKind::Other, "GetStdHandle failed"));
    }

    // Write input then close stdin write end (PipeWriter Drop closes handle).
    {
        let mut writer = in_writer_pipe;
        let _ = writer.write_all(input.as_bytes());
    }

    // SAFETY: SetStdHandle replaces the process-global stdio handles. This is
    // serialized by IO_CAPTURE_LOCK, and we restore the original handles in all
    // exit paths (including panics) before releasing the lock.
    let redir_ok = unsafe {
        SetStdHandle(STD_INPUT_HANDLE, in_reader_pipe.as_raw_handle() as _) != 0
            && SetStdHandle(STD_OUTPUT_HANDLE, out_writer_pipe.as_raw_handle() as _) != 0
            && SetStdHandle(STD_ERROR_HANDLE, err_writer_pipe.as_raw_handle() as _) != 0
    };

    if !redir_ok {
        // SAFETY: Restoring original handles after failed redirect.
        unsafe {
            let _ = SetStdHandle(STD_INPUT_HANDLE, saved_in);
            let _ = SetStdHandle(STD_OUTPUT_HANDLE, saved_out);
            let _ = SetStdHandle(STD_ERROR_HANDLE, saved_err);
        }
        return Err(io::Error::last_os_error());
    }

    // Reader threads use PipeReader directly (implements Read, no unsafe needed).
    let out_thread = thread::spawn(move || {
        let mut buf = Vec::new();
        let mut reader = out_reader_pipe;
        let _ = reader.read_to_end(&mut buf);
        buf
    });
    let err_thread = thread::spawn(move || {
        let mut buf = Vec::new();
        let mut reader = err_reader_pipe;
        let _ = reader.read_to_end(&mut buf);
        buf
    });

    let old_panic_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let unwind = catch_unwind(AssertUnwindSafe(f));
    std::panic::set_hook(old_panic_hook);

    let _ = io::stdout().flush();
    let _ = io::stderr().flush();

    // SAFETY: Restoring original handles and dropping redirected pipe ends
    // to signal EOF to reader threads.
    unsafe {
        let _ = SetStdHandle(STD_INPUT_HANDLE, saved_in);
        let _ = SetStdHandle(STD_OUTPUT_HANDLE, saved_out);
        let _ = SetStdHandle(STD_ERROR_HANDLE, saved_err);
    }

    // Close redirected pipe ends to signal EOF to readers (safe RAII Drop).
    drop(out_writer_pipe);
    drop(err_writer_pipe);
    drop(in_reader_pipe);

    let stdout_bytes = out_thread.join().unwrap_or_default();
    let stderr_bytes = err_thread.join().unwrap_or_default();

    let outcome = match unwind {
        Ok(r) => CaptureOutcome::Returned(r),
        Err(panic) => CaptureOutcome::Panicked(panic),
    };

    Ok((outcome, stdout_bytes, stderr_bytes))
}

#[cfg(not(any(unix, windows)))]
fn run_with_capture<F>(_input: &str, f: F) -> io::Result<(CaptureOutcome, Vec<u8>, Vec<u8>)>
where
    F: FnOnce() -> Result<(), crate::ClickError>,
{
    let unwind = catch_unwind(AssertUnwindSafe(f));
    let outcome = match unwind {
        Ok(r) => CaptureOutcome::Returned(r),
        Err(panic) => CaptureOutcome::Panicked(panic),
    };
    Ok((outcome, Vec::new(), Vec::new()))
}

// =============================================================================
// CliRunner
// =============================================================================

/// A test runner for CLI applications.
///
/// `CliRunner` provides a controlled environment for testing CLI commands.
/// It captures stdout, stderr, and tracks exit codes.
///
/// # Example
///
/// ```rust
/// use click::testing::CliRunner;
/// use click::command::Command;
///
/// let cmd = Command::new("greet")
///     .callback(|ctx| {
///         let default_name = "World".to_string();
///         let name = ctx.get_param::<String>("name").unwrap_or(&default_name);
///         // Use name for greeting
///         Ok(())
///     })
///     .build();
///
/// let runner = CliRunner::new();
/// let result = runner.invoke(&cmd, &[]);
///
/// assert_eq!(result.exit_code, 0);
/// ```
#[derive(Debug, Clone, Default)]
pub struct CliRunner {
    /// Environment variables to set during invocation.
    env: HashMap<String, String>,

    /// Environment variables to unset during invocation.
    env_unset: Vec<String>,

    /// Whether to echo input to output.
    echo_stdin: bool,

    /// Whether to mix stderr into stdout.
    mix_stderr: bool,

    /// Whether to capture panics as a failure instead of re-panicking.
    ///
    /// When enabled (default), panics inside the invoked command are caught and returned as
    /// `InvokeResult { exit_code: 1, exception_message: Some(...) }`.
    catch_panics: bool,

    /// Output charset for decoding captured bytes.
    charset: String,

}

impl CliRunner {
    /// Create a new CLI runner with default settings.
    ///
    /// # Example
    ///
    /// ```rust
    /// use click::testing::CliRunner;
    ///
    /// let runner = CliRunner::new();
    /// ```
    pub fn new() -> Self {
        Self {
            env: HashMap::new(),
            env_unset: Vec::new(),
            echo_stdin: false,
            mix_stderr: true,
            catch_panics: true,
            charset: "utf-8".to_string(),
        }
    }

    /// Set an environment variable for the invocation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use click::testing::CliRunner;
    ///
    /// let runner = CliRunner::new()
    ///     .env("MY_VAR", "value");
    /// ```
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.insert(key.into(), value.into());
        self
    }

    /// Unset an environment variable for the invocation.
    pub fn env_unset(mut self, key: impl Into<String>) -> Self {
        self.env_unset.push(key.into());
        self
    }

    /// Clear all custom environment settings.
    pub fn env_clear(mut self) -> Self {
        self.env.clear();
        self.env_unset.clear();
        self
    }

    /// Set whether to echo stdin to output.
    pub fn echo_stdin(mut self, echo: bool) -> Self {
        self.echo_stdin = echo;
        self
    }

    /// Set whether to mix stderr into stdout.
    ///
    /// When true (default), `InvokeResult.output` contains `stdout + stderr`.
    /// The `InvokeResult.stderr` field is always populated with captured stderr.
    pub fn mix_stderr(mut self, mix: bool) -> Self {
        self.mix_stderr = mix;
        self
    }

    /// Set whether panics should be captured as a failure instead of re-panicking.
    pub fn catch_panics(mut self, catch: bool) -> Self {
        self.catch_panics = catch;
        self
    }

    /// Set the charset used to decode captured output.
    ///
    /// Defaults to `"utf-8"`. Unknown labels fall back to UTF-8.
    pub fn charset(mut self, charset: impl Into<String>) -> Self {
        self.charset = charset.into();
        self
    }

    /// Invoke a command and capture the result.
    ///
    /// # Arguments
    ///
    /// * `cmd` - The command to invoke
    /// * `args` - Command-line arguments
    ///
    /// # Example
    ///
    /// ```rust
    /// use click::testing::CliRunner;
    /// use click::command::Command;
    ///
    /// let cmd = Command::new("hello")
    ///     .callback(|_| {
    ///         println!("Hello!");
    ///         Ok(())
    ///     })
    ///     .build();
    ///
    /// let result = CliRunner::new().invoke(&cmd, &[]);
    /// assert_eq!(result.exit_code, 0);
    /// ```
    pub fn invoke(&self, cmd: &dyn CommandLike, args: &[&str]) -> InvokeResult {
        self.invoke_with_input(cmd, args, None)
    }

    /// Invoke a command with simulated stdin input.
    ///
    /// # Arguments
    ///
    /// * `cmd` - The command to invoke
    /// * `args` - Command-line arguments
    /// * `input` - Optional stdin input
    ///
    /// # Example
    ///
    /// ```rust
    /// use click::testing::CliRunner;
    /// use click::command::Command;
    ///
    /// let cmd = Command::new("echo")
    ///     .callback(|_| Ok(()))
    ///     .build();
    ///
    /// let result = CliRunner::new().invoke_with_input(&cmd, &[], Some("test input"));
    /// ```
    pub fn invoke_with_input(
        &self,
        cmd: &dyn CommandLike,
        args: &[&str],
        input: Option<&str>,
    ) -> InvokeResult {
        // Save current environment
        let saved_env: Vec<(String, Option<String>)> = self
            .env
            .keys()
            .chain(self.env_unset.iter())
            .map(|k| (k.clone(), env::var(k).ok()))
            .collect();

        // Set test environment
        for (key, value) in &self.env {
            env::set_var(key, value);
        }
        for key in &self.env_unset {
            env::remove_var(key);
        }

        // Convert args to owned strings
        let args_owned: Vec<String> = args.iter().map(|s| s.to_string()).collect();

        // Always provide a stdin stream (empty by default) to avoid hanging on interactive reads.
        let input_str = input.unwrap_or("");

        let (outcome, stdout_bytes, stderr_bytes) = match run_with_capture(input_str, || {
            // Run in a child thread so Rust's test harness output capture (thread-local) doesn't
            // intercept stdout/stderr before our OS-level redirection does.
            thread::scope(|s| {
                let handle = s.spawn(|| cmd.main(args_owned));
                match handle.join() {
                    Ok(r) => r,
                    Err(panic) => std::panic::resume_unwind(panic),
                }
            })
        }) {
            Ok(v) => v,
            Err(e) => {
                restore_env(&saved_env);
                return InvokeResult::new(1, String::new(), String::new(), Some(e.to_string()));
            }
        };

        // Determine exit code and exception message
        let (exit_code, exception_message) = match outcome {
            CaptureOutcome::Returned(r) => match &r {
                Ok(()) => (0, None),
                Err(e) => (e.exit_code(), Some(e.to_string())),
            },
            CaptureOutcome::Panicked(panic) => {
                if !self.catch_panics {
                    restore_env(&saved_env);
                    std::panic::resume_unwind(panic);
                }
                (1, Some(format!("panic: {}", panic_message(&*panic))))
            }
        };

        let label = self.charset.trim().to_lowercase();
        let encoding = Encoding::for_label(label.as_bytes())
            .or_else(|| {
                if label == "latin-1" || label == "latin1" {
                    Encoding::for_label(b"iso-8859-1")
                } else {
                    None
                }
            })
            .unwrap_or(encoding_rs::UTF_8);
        let mut stdout = encoding.decode(&stdout_bytes).0.into_owned();
        let stderr = encoding.decode(&stderr_bytes).0.into_owned();

        // Echo stdin into stdout like Python Click's CliRunner when enabled.
        if self.echo_stdin && !input_str.is_empty() {
            stdout = format!("{}{}", input_str, stdout);
        }

        let output = if self.mix_stderr {
            format!("{}{}", stdout, stderr)
        } else {
            stdout
        };

        // Restore environment
        restore_env(&saved_env);

        InvokeResult::new(exit_code, output, stderr, exception_message)
    }

    /// Invoke a command within an isolated filesystem.
    ///
    /// This creates a temporary directory and runs the command there.
    ///
    /// # Arguments
    ///
    /// * `cmd` - The command to invoke
    /// * `args` - Command-line arguments
    ///
    /// # Example
    ///
    /// ```rust
    /// use click::testing::CliRunner;
    /// use click::command::Command;
    ///
    /// let cmd = Command::new("test").build();
    /// let result = CliRunner::new().invoke_isolated(&cmd, &[]);
    /// ```
    pub fn invoke_isolated(&self, cmd: &dyn CommandLike, args: &[&str]) -> InvokeResult {
        let _isolated = IsolatedFilesystem::new().expect("Failed to create isolated filesystem");
        self.invoke(cmd, args)
    }
}

// =============================================================================
// InvokeResult
// =============================================================================

/// The result of invoking a command through [`CliRunner`].
///
/// Contains the exit code, captured output, and any exception that occurred.
#[derive(Debug, Clone)]
pub struct InvokeResult {
    /// The exit code from the command (0 for success).
    pub exit_code: i32,

    /// Captured stdout (and stderr if `mix_stderr` is true).
    pub output: String,

    /// Captured stderr (always captured, even if mixed into `output`).
    pub stderr: String,

    /// The error message if an exception occurred.
    pub exception_message: Option<String>,
}

impl InvokeResult {
    /// Create a new InvokeResult for testing purposes.
    #[doc(hidden)]
    pub fn new(
        exit_code: i32,
        output: String,
        stderr: String,
        exception_message: Option<String>,
    ) -> Self {
        Self {
            exit_code,
            output,
            stderr,
            exception_message,
        }
    }

    /// Check if the command succeeded (exit code 0).
    pub fn is_success(&self) -> bool {
        self.exit_code == 0
    }

    /// Check if the command failed (exit code != 0).
    pub fn is_failure(&self) -> bool {
        self.exit_code != 0
    }

    /// Get the output lines.
    pub fn output_lines(&self) -> Vec<&str> {
        self.output.lines().collect()
    }

    /// Check if the output contains a substring.
    pub fn output_contains(&self, substring: &str) -> bool {
        self.output.contains(substring)
    }

    /// Check if the stderr contains a substring.
    pub fn stderr_contains(&self, substring: &str) -> bool {
        self.stderr.contains(substring)
    }

    /// Get the combined output (stdout + stderr).
    ///
    /// If `mix_stderr` was true, this is the same as `output`.
    pub fn combined_output(&self) -> String {
        if self.stderr.is_empty() {
            return self.output.clone();
        }

        // If output already includes stderr (mix_stderr mode), avoid duplication.
        if self.output.ends_with(&self.stderr) {
            return self.output.clone();
        }

        format!("{}{}", self.output, self.stderr)
    }
}

// =============================================================================
// IsolatedFilesystem
// =============================================================================

/// A temporary filesystem context for isolated testing.
///
/// Creates a temporary directory that is automatically cleaned up when dropped.
/// The current working directory is changed to the temporary directory for the
/// duration of the test.
///
/// # Example
///
/// ```rust
/// use click::testing::IsolatedFilesystem;
/// use std::fs;
///
/// {
///     let isolated = IsolatedFilesystem::new().unwrap();
///     let path = isolated.path();
///
///     // Create files in the isolated directory
///     fs::write(path.join("test.txt"), "hello").unwrap();
///
///     assert!(path.join("test.txt").exists());
/// }
/// // Directory is automatically cleaned up here
/// ```
#[derive(Debug)]
pub struct IsolatedFilesystem {
    /// The temporary directory path.
    path: PathBuf,
    /// The original working directory to restore on drop.
    original_cwd: PathBuf,
}

impl IsolatedFilesystem {
    /// Generate a unique suffix for directory names to avoid test interference.
    fn unique_suffix() -> u64 {
        use std::sync::atomic::{AtomicU64, Ordering};
        use std::time::{SystemTime, UNIX_EPOCH};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let count = COUNTER.fetch_add(1, Ordering::SeqCst);
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos() as u64;
        timestamp ^ count
    }

    /// Get current directory, falling back to temp dir if current dir is invalid.
    ///
    /// This can happen when running tests in parallel and another test
    /// deletes the directory that was the current working directory.
    fn get_current_dir_safe() -> io::Result<PathBuf> {
        match env::current_dir() {
            Ok(cwd) => Ok(cwd),
            Err(_) => {
                // Fall back to temp dir if current dir is invalid
                let temp = env::temp_dir();
                let _ = env::set_current_dir(&temp);
                Ok(temp)
            }
        }
    }

    /// Create a new isolated filesystem.
    ///
    /// This creates a temporary directory and changes to it.
    pub fn new() -> io::Result<Self> {
        let original_cwd = Self::get_current_dir_safe()?;
        let path = env::temp_dir().join(format!(
            "click_test_{}_{}",
            std::process::id(),
            Self::unique_suffix()
        ));

        // Create the directory
        fs::create_dir_all(&path)?;

        // Change to it
        env::set_current_dir(&path)?;

        Ok(Self { path, original_cwd })
    }

    /// Create an isolated filesystem with a specific name.
    pub fn with_name(name: &str) -> io::Result<Self> {
        let original_cwd = Self::get_current_dir_safe()?;
        let path = env::temp_dir().join(format!(
            "click_test_{}_{}_{}",
            name,
            std::process::id(),
            Self::unique_suffix()
        ));

        // Create the directory fresh
        fs::create_dir_all(&path)?;
        env::set_current_dir(&path)?;

        Ok(Self { path, original_cwd })
    }

    /// Get the path to the temporary directory.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Create a file in the isolated filesystem.
    pub fn create_file(&self, name: &str, content: &str) -> io::Result<PathBuf> {
        let file_path = self.path.join(name);
        if let Some(parent) = file_path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(&file_path, content)?;
        Ok(file_path)
    }

    /// Create a directory in the isolated filesystem.
    pub fn create_dir(&self, name: &str) -> io::Result<PathBuf> {
        let dir_path = self.path.join(name);
        fs::create_dir_all(&dir_path)?;
        Ok(dir_path)
    }

    /// Read a file from the isolated filesystem.
    pub fn read_file(&self, name: &str) -> io::Result<String> {
        fs::read_to_string(self.path.join(name))
    }

    /// Check if a file exists in the isolated filesystem.
    pub fn file_exists(&self, name: &str) -> bool {
        self.path.join(name).exists()
    }

    /// List files in the isolated filesystem.
    pub fn list_files(&self) -> io::Result<Vec<String>> {
        let mut files = Vec::new();
        for entry in fs::read_dir(&self.path)? {
            let entry = entry?;
            if let Some(name) = entry.file_name().to_str() {
                files.push(name.to_string());
            }
        }
        files.sort();
        Ok(files)
    }
}

impl Drop for IsolatedFilesystem {
    fn drop(&mut self) {
        // Restore original working directory
        let _ = env::set_current_dir(&self.original_cwd);

        // Clean up the temporary directory
        let _ = fs::remove_dir_all(&self.path);
    }
}

// =============================================================================
// EchoingStdin (for simulated input)
// =============================================================================

/// A wrapper that echoes input to output as it's read.
#[derive(Debug)]
pub struct EchoingStdin<R: Read, W: Write> {
    input: R,
    output: W,
}

impl<R: Read, W: Write> EchoingStdin<R, W> {
    /// Create a new echoing stdin wrapper.
    pub fn new(input: R, output: W) -> Self {
        Self { input, output }
    }
}

impl<R: Read, W: Write> Read for EchoingStdin<R, W> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.input.read(buf)?;
        if n > 0 {
            self.output.write_all(&buf[..n])?;
        }
        Ok(n)
    }
}

// =============================================================================
// Test Utilities
// =============================================================================

/// Create a mock context for testing.
///
/// This is useful when you need to test callback functions in isolation.
pub fn make_test_context(info_name: &str) -> crate::context::Context {
    crate::context::ContextBuilder::new()
        .info_name(info_name)
        .build()
}

/// Assert that a result indicates success.
#[macro_export]
macro_rules! assert_success {
    ($result:expr) => {
        assert!(
            $result.is_success(),
            "Expected success but got exit code {} with output:\n{}",
            $result.exit_code,
            $result.combined_output()
        );
    };
}

/// Assert that a result indicates failure.
#[macro_export]
macro_rules! assert_failure {
    ($result:expr) => {
        assert!(
            $result.is_failure(),
            "Expected failure but got success with output:\n{}",
            $result.output
        );
    };
    ($result:expr, $code:expr) => {
        assert_eq!(
            $result.exit_code,
            $code,
            "Expected exit code {} but got {} with output:\n{}",
            $code,
            $result.exit_code,
            $result.combined_output()
        );
    };
}

/// Assert that the output contains a substring.
#[macro_export]
macro_rules! assert_output_contains {
    ($result:expr, $substring:expr) => {
        assert!(
            $result.output_contains($substring),
            "Expected output to contain '{}' but got:\n{}",
            $substring,
            $result.output
        );
    };
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::command::Command;

    #[test]
    fn test_cli_runner_new() {
        let runner = CliRunner::new();
        assert!(runner.env.is_empty());
        assert!(runner.env_unset.is_empty());
    }

    #[test]
    fn test_cli_runner_env() {
        let runner = CliRunner::new()
            .env("TEST_VAR", "test_value")
            .env("ANOTHER", "value");

        assert_eq!(runner.env.get("TEST_VAR"), Some(&"test_value".to_string()));
        assert_eq!(runner.env.get("ANOTHER"), Some(&"value".to_string()));
    }

    #[test]
    fn test_cli_runner_env_unset() {
        let runner = CliRunner::new().env("KEEP", "value").env_unset("REMOVE");

        assert_eq!(runner.env.len(), 1);
        assert_eq!(runner.env_unset.len(), 1);
    }

    #[test]
    fn test_cli_runner_env_clear() {
        let runner = CliRunner::new()
            .env("VAR1", "val1")
            .env("VAR2", "val2")
            .env_unset("VAR3")
            .env_clear();

        assert!(runner.env.is_empty());
        assert!(runner.env_unset.is_empty());
    }

    #[test]
    fn test_invoke_simple_command() {
        let cmd = Command::new("test").callback(|_ctx| Ok(())).build();

        let runner = CliRunner::new();
        let result = runner.invoke(&cmd, &[]);

        assert_eq!(result.exit_code, 0);
        assert!(result.exception_message.is_none());
    }

    #[test]
    fn test_invoke_failing_command() {
        let cmd = Command::new("fail")
            .callback(|_ctx| Err(crate::ClickError::usage("test error")))
            .build();

        let runner = CliRunner::new();
        let result = runner.invoke(&cmd, &[]);

        assert_eq!(result.exit_code, 2); // Usage errors have exit code 2
        assert!(result.exception_message.is_some());
    }

    #[test]
    fn test_invoke_result_is_success() {
        let result = InvokeResult::new(0, String::new(), String::new(), None);

        assert!(result.is_success());
        assert!(!result.is_failure());
    }

    #[test]
    fn test_invoke_result_is_failure() {
        let result = InvokeResult::new(1, String::new(), String::new(), None);

        assert!(!result.is_success());
        assert!(result.is_failure());
    }

    #[test]
    fn test_invoke_result_output_contains() {
        let result = InvokeResult::new(0, "Hello, World!".to_string(), String::new(), None);

        assert!(result.output_contains("Hello"));
        assert!(result.output_contains("World"));
        assert!(!result.output_contains("Goodbye"));
    }

    #[test]
    fn test_invoke_result_output_lines() {
        let result = InvokeResult::new(0, "line1\nline2\nline3".to_string(), String::new(), None);

        let lines = result.output_lines();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "line1");
        assert_eq!(lines[1], "line2");
        assert_eq!(lines[2], "line3");
    }

    #[test]
    fn test_invoke_result_combined_output() {
        let result = InvokeResult::new(0, "stdout".to_string(), "stderr".to_string(), None);

        assert_eq!(result.combined_output(), "stdoutstderr");
    }

    #[test]
    fn test_isolated_filesystem() {
        let isolated = IsolatedFilesystem::new().unwrap();
        let iso_path = isolated.path().to_path_buf();

        // The isolated directory exists
        assert!(iso_path.exists());
        assert!(iso_path.starts_with(env::temp_dir()));

        // Create a file
        isolated.create_file("test.txt", "hello").unwrap();
        assert!(isolated.file_exists("test.txt"));
        assert_eq!(isolated.read_file("test.txt").unwrap(), "hello");

        // Drop the filesystem explicitly
        drop(isolated);

        // After drop, the directory is cleaned up
        // Note: We don't test current_dir restoration because it's racy with parallel tests
        assert!(
            !iso_path.exists(),
            "temp directory should be cleaned up after drop"
        );
    }

    #[test]
    fn test_isolated_filesystem_with_name() {
        let isolated = IsolatedFilesystem::with_name("custom").unwrap();
        let path = isolated.path();

        assert!(path.to_string_lossy().contains("custom"));
    }

    #[test]
    fn test_isolated_filesystem_create_dir() {
        let isolated = IsolatedFilesystem::new().unwrap();

        let dir_path = isolated.create_dir("subdir").unwrap();
        assert!(dir_path.exists());
        assert!(dir_path.is_dir());
    }

    #[test]
    fn test_isolated_filesystem_list_files() {
        let isolated = IsolatedFilesystem::new().unwrap();

        isolated.create_file("a.txt", "").unwrap();
        isolated.create_file("b.txt", "").unwrap();
        isolated.create_file("c.txt", "").unwrap();

        let files = isolated.list_files().unwrap();
        assert_eq!(files, vec!["a.txt", "b.txt", "c.txt"]);
    }

    #[test]
    fn test_isolated_filesystem_nested_file() {
        let isolated = IsolatedFilesystem::new().unwrap();

        isolated
            .create_file("dir/nested/file.txt", "content")
            .unwrap();
        assert!(isolated.file_exists("dir/nested/file.txt"));
        assert_eq!(
            isolated.read_file("dir/nested/file.txt").unwrap(),
            "content"
        );
    }

    #[test]
    fn test_make_test_context() {
        let ctx = make_test_context("test");
        assert_eq!(ctx.info_name(), Some("test"));
    }

    #[test]
    fn test_echoing_stdin() {
        let input = b"hello";
        let mut output = Vec::new();

        {
            let mut echoing = EchoingStdin::new(&input[..], &mut output);
            let mut buf = [0u8; 10];
            let n = echoing.read(&mut buf).unwrap();
            assert_eq!(n, 5);
            assert_eq!(&buf[..n], b"hello");
        }

        assert_eq!(&output, b"hello");
    }

    #[test]
    fn test_cli_runner_mix_stderr() {
        let runner = CliRunner::new().mix_stderr(false);
        assert!(!runner.mix_stderr);

        let runner = CliRunner::new().mix_stderr(true);
        assert!(runner.mix_stderr);
    }

    #[test]
    fn test_cli_runner_echo_stdin() {
        let runner = CliRunner::new().echo_stdin(true);
        assert!(runner.echo_stdin);
    }
}