merry-process 0.1.2

Sandboxed host-process execution (Landlock/seccomp, Seatbelt, Windows Job Objects) for Merry.
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
//! Session-scoped ownership for processes that outlive one tool call.
//!
//! `runner_for` builds a fresh runner and drops it when the call returns.
//! A child stored only on that runner would die with the call. The factory
//! and each long-lived runner therefore share this supervisor and drop it
//! with the session.

use merry_runtime::{
    BackgroundProcessControl, BackgroundProcessId, BackgroundProcessLookupError,
    BackgroundProcessOutput, BackgroundProcessState, BackgroundProcessStop,
    BackgroundProcessSummary, ProcessActionIntent, ProcessExitStatus, ProcessOutputSink,
    ProcessRunnerContext, ProcessRunnerError, ProcessRunnerOutput, ToolOutputStream,
};
use std::{
    sync::{
        Arc, Mutex, MutexGuard,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
    time::Duration,
};
use tokio::{
    io::{AsyncRead, AsyncReadExt},
    process::Child,
    task::JoinHandle,
};

/// How long a background command may run before the tool returns.
///
/// A process that exits inside this window is reported as a normal exit, so a
/// startup failure stays visible. A process that is still alive is retained.
const BACKGROUND_SETTLE: Duration = Duration::from_secs(1);

/// How long to collect output after a background command exits during settle.
///
/// A shell that backgrounds another process can leave the pipe open. Waiting
/// for EOF would block the turn again.
const BACKGROUND_EXIT_OUTPUT_WAIT: Duration = Duration::from_millis(200);

#[derive(Debug, Clone)]
pub(crate) struct BackgroundSupervisor {
    inner: Arc<SupervisorInner>,
}

#[derive(Debug)]
struct SupervisorInner {
    next_id: AtomicU64,
    processes: Mutex<Vec<RetainedProcess>>,
}

struct RetainedProcess {
    id: BackgroundProcessId,
    display_name: Option<String>,
    command: String,
    cwd: Option<String>,
    pid: u32,
    life: Life,
    child: Child,
    #[cfg(windows)]
    windows_job: Option<super::windows_job::WindowsProcessJob>,
    readers: Vec<JoinHandle<Result<(), ProcessRunnerError>>>,
    stdout: Arc<Capture>,
    stderr: Arc<Capture>,
}

enum Life {
    Running,
    Exited(i32),
    Killed,
}

impl std::fmt::Debug for RetainedProcess {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("RetainedProcess")
            .field("id", &self.id)
            .field("pid", &self.pid)
            .finish_non_exhaustive()
    }
}

impl BackgroundSupervisor {
    pub(crate) fn new() -> Self {
        Self {
            inner: Arc::new(SupervisorInner {
                next_id: AtomicU64::new(0),
                processes: Mutex::new(Vec::new()),
            }),
        }
    }

    fn mint_id(&self, explicit_background: bool) -> BackgroundProcessId {
        let number = self.inner.next_id.fetch_add(1, Ordering::Relaxed) + 1;
        let prefix = if explicit_background { "bg" } else { "proc" };
        BackgroundProcessId::new(format!("{prefix}-{number}"))
            .expect("minted retained process id is valid")
    }

    fn retain(&self, process: RetainedProcess) {
        let mut processes = self
            .inner
            .processes
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        processes.push(process);
    }
}

impl Default for BackgroundSupervisor {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for SupervisorInner {
    fn drop(&mut self) {
        let processes = std::mem::take(
            self.processes
                .get_mut()
                .unwrap_or_else(|error| error.into_inner()),
        );
        for mut process in processes {
            stop_retained(&mut process);
        }
    }
}

/// Owned child process, stdio pipes, and display label passed into [`finish_process`].
pub(super) struct SpawnedProcess<Stdout, Stderr> {
    pub(super) child: Child,
    #[cfg(windows)]
    pub(super) windows_job: Option<super::windows_job::WindowsProcessJob>,
    pub(super) stdout: Stdout,
    pub(super) stderr: Stderr,
    pub(super) program_for_error: String,
}

/// Waits for a spawned process up to its background settle window or foreground
/// wait timeout, retaining it in `BackgroundSupervisor` without killing it if it
/// is still alive when the wait window elapses.
pub(super) async fn finish_process<Stdout, Stderr>(
    spawned: SpawnedProcess<Stdout, Stderr>,
    intent: ProcessActionIntent,
    context: ProcessRunnerContext,
    supervisor: BackgroundSupervisor,
) -> Result<ProcessRunnerOutput, ProcessRunnerError>
where
    Stdout: AsyncRead + Unpin + Send + 'static,
    Stderr: AsyncRead + Unpin + Send + 'static,
{
    let SpawnedProcess {
        mut child,
        #[cfg(windows)]
        windows_job,
        stdout,
        stderr,
        program_for_error,
    } = spawned;
    let explicit_background = intent.background();
    let wait_timeout_secs = (!explicit_background).then(|| intent.effective_wait_timeout_secs());
    let wait_window = match wait_timeout_secs {
        Some(secs) => Duration::from_secs(secs),
        None => BACKGROUND_SETTLE,
    };

    let stdout_capture = Arc::new(Capture::default());
    let stderr_capture = Arc::new(Capture::default());
    let live_sink = if explicit_background {
        None
    } else {
        context
            .output_sink()
            .cloned()
            .map(|sink| Arc::new(Mutex::new(Some(sink))))
    };
    let mut stdout_task = tokio::spawn(drain_capture(
        stdout,
        intent.stdout_limit_bytes(),
        Arc::clone(&stdout_capture),
        live_sink.clone(),
        ToolOutputStream::Stdout,
    ));
    let mut stderr_task = tokio::spawn(drain_capture(
        stderr,
        intent.stderr_limit_bytes(),
        Arc::clone(&stderr_capture),
        live_sink.clone(),
        ToolOutputStream::Stderr,
    ));

    let settled = tokio::select! {
        biased;
        () = context.cancellation_token().cancelled() => Settle::Cancelled,
        status = child.wait() => Settle::Exited(status),
        () = tokio::time::sleep(wait_window) => Settle::StillRunning,
    };

    match settled {
        Settle::Cancelled => {
            detach_live_sink(&live_sink);
            stdout_task.abort();
            stderr_task.abort();
            #[cfg(windows)]
            if let Some(job) = windows_job.as_ref() {
                job.terminate();
            }
            kill_child_group(&mut child);
            let _ = child.wait().await;
            Err(ProcessRunnerError::Cancelled)
        }
        Settle::Exited(status) => {
            let status = status.map_err(|source| {
                ProcessRunnerError::infrastructure(format!(
                    "failed to wait for process executable `{program_for_error}`: {source}"
                ))
            })?;
            if explicit_background {
                collect_exit_output(&mut stdout_task, &mut stderr_task).await?;
            } else {
                join_exit_output(&mut stdout_task, &mut stderr_task).await?;
            }
            detach_live_sink(&live_sink);
            finish_exited(&intent, status, &stdout_capture, &stderr_capture)
        }
        Settle::StillRunning => {
            detach_live_sink(&live_sink);
            if context.cancellation_token().is_cancelled() {
                stdout_task.abort();
                stderr_task.abort();
                #[cfg(windows)]
                if let Some(job) = windows_job.as_ref() {
                    job.terminate();
                }
                kill_child_group(&mut child);
                let _ = child.wait().await;
                return Err(ProcessRunnerError::Cancelled);
            }
            let (stdout_bytes, stdout_truncated) = stdout_capture.snapshot();
            let (stderr_bytes, stderr_truncated) = stderr_capture.snapshot();
            if let Some(message) = crate::confinement::setup_failure(&stderr_bytes) {
                stdout_task.abort();
                stderr_task.abort();
                #[cfg(windows)]
                if let Some(job) = windows_job.as_ref() {
                    job.terminate();
                }
                kill_child_group(&mut child);
                let _ = child.wait().await;
                return Err(ProcessRunnerError::infrastructure(message));
            }
            let pid = child.id();
            let command = display_command(&intent);
            let cwd = intent.cwd().map(str::to_owned);
            let mut output = ProcessRunnerOutput::from_bytes(
                &intent,
                ProcessExitStatus::Running,
                stdout_bytes,
                stdout_truncated,
                stderr_bytes,
                stderr_truncated,
            )
            .map_err(|source| ProcessRunnerError::infrastructure(source.to_string()))?;
            let Some(pid) = pid else {
                stdout_task.abort();
                stderr_task.abort();
                #[cfg(windows)]
                if let Some(job) = windows_job.as_ref() {
                    job.terminate();
                }
                kill_child_group(&mut child);
                let _ = child.wait().await;
                return Err(ProcessRunnerError::infrastructure(
                    "background process started without a pid",
                ));
            };
            let id = supervisor.mint_id(explicit_background);
            let display_name = intent
                .display_name()
                .map(str::to_owned)
                .or_else(|| fallback_display_name(&command));
            output = output
                .with_running_pid(pid)
                .with_background_process_id(id.clone());
            if let Some(secs) = wait_timeout_secs {
                output = output.with_wait_timed_out_secs(secs);
            }
            supervisor.retain(RetainedProcess {
                id,
                display_name,
                command,
                cwd,
                pid,
                life: Life::Running,
                child,
                #[cfg(windows)]
                windows_job,
                readers: vec![stdout_task, stderr_task],
                stdout: stdout_capture,
                stderr: stderr_capture,
            });
            tracing::debug!(
                pid,
                wait_timeout_secs,
                "retained still-running process in background supervisor"
            );
            Ok(output)
        }
    }
}

fn detach_live_sink(slot: &Option<Arc<Mutex<Option<ProcessOutputSink>>>>) {
    if let Some(slot) = slot {
        let mut guard = slot.lock().unwrap_or_else(|error| error.into_inner());
        *guard = None;
    }
}

async fn join_exit_output(
    stdout_task: &mut JoinHandle<Result<(), ProcessRunnerError>>,
    stderr_task: &mut JoinHandle<Result<(), ProcessRunnerError>>,
) -> Result<(), ProcessRunnerError> {
    let stdout_result = (&mut *stdout_task).await.map_err(|source| {
        ProcessRunnerError::infrastructure(format!("process stdout reader task failed: {source}"))
    });
    if let Err(error) = stdout_result {
        stderr_task.abort();
        return Err(error);
    }
    if let Err(error) = stdout_result.expect("checked above") {
        stderr_task.abort();
        return Err(error);
    }
    (&mut *stderr_task).await.map_err(|source| {
        ProcessRunnerError::infrastructure(format!("process stderr reader task failed: {source}"))
    })??;
    Ok(())
}

enum Settle {
    Cancelled,
    Exited(std::io::Result<std::process::ExitStatus>),
    StillRunning,
}

async fn collect_exit_output(
    stdout_task: &mut JoinHandle<Result<(), ProcessRunnerError>>,
    stderr_task: &mut JoinHandle<Result<(), ProcessRunnerError>>,
) -> Result<(), ProcessRunnerError> {
    let readers = async {
        (&mut *stdout_task).await.map_err(|source| {
            ProcessRunnerError::infrastructure(format!(
                "process stdout reader task failed: {source}"
            ))
        })??;
        (&mut *stderr_task).await.map_err(|source| {
            ProcessRunnerError::infrastructure(format!(
                "process stderr reader task failed: {source}"
            ))
        })??;
        Ok(())
    };
    let result = tokio::select! {
        biased;
        result = readers => result,
        () = tokio::time::sleep(BACKGROUND_EXIT_OUTPUT_WAIT) => {
            stdout_task.abort();
            stderr_task.abort();
            Ok(())
        }
    };
    if result.is_err() {
        stdout_task.abort();
        stderr_task.abort();
    }
    result
}

fn finish_exited(
    intent: &ProcessActionIntent,
    status: std::process::ExitStatus,
    stdout_capture: &Capture,
    stderr_capture: &Capture,
) -> Result<ProcessRunnerOutput, ProcessRunnerError> {
    let (stdout, stdout_truncated) = stdout_capture.snapshot();
    let (stderr, stderr_truncated) = stderr_capture.snapshot();
    if let Some(message) = crate::confinement::setup_failure(&stderr) {
        return Err(ProcessRunnerError::infrastructure(message));
    }
    let status = status
        .code()
        .map(ProcessExitStatus::Exited)
        .unwrap_or(ProcessExitStatus::DomainFailed);
    ProcessRunnerOutput::from_bytes(
        intent,
        status,
        stdout,
        stdout_truncated,
        stderr,
        stderr_truncated,
    )
    .map_err(|source| ProcessRunnerError::infrastructure(source.to_string()))
}

impl BackgroundProcessControl for BackgroundSupervisor {
    fn list(&self) -> Vec<BackgroundProcessSummary> {
        let mut processes = lock_processes(self);
        processes.iter_mut().map(summary_of).collect()
    }

    fn read(
        &self,
        id: &BackgroundProcessId,
    ) -> Result<BackgroundProcessOutput, BackgroundProcessLookupError> {
        let mut processes = lock_processes(self);
        let Some(process) = processes.iter_mut().find(|process| &process.id == id) else {
            return Err(missing(id, &processes));
        };
        let (stdout, stdout_truncated) = process.stdout.snapshot();
        let (stderr, stderr_truncated) = process.stderr.snapshot();
        Ok(BackgroundProcessOutput::new(
            summary_of(process),
            String::from_utf8_lossy(&stdout),
            String::from_utf8_lossy(&stderr),
            stdout_truncated,
            stderr_truncated,
        ))
    }

    fn stop(
        &self,
        id: &BackgroundProcessId,
    ) -> Result<BackgroundProcessStop, BackgroundProcessLookupError> {
        let mut processes = lock_processes(self);
        let Some(process) = processes.iter_mut().find(|process| &process.id == id) else {
            return Err(missing(id, &processes));
        };
        refresh(process);
        if !matches!(process.life, Life::Running) {
            return Ok(BackgroundProcessStop::new(summary_of(process), false));
        }
        process.life = Life::Killed;
        kill_tree(Some(process.pid));
        let _ = process.child.start_kill();
        reap_child(&mut process.child);
        for reader in &process.readers {
            reader.abort();
        }
        Ok(BackgroundProcessStop::new(summary_of(process), true))
    }
}

fn lock_processes(supervisor: &BackgroundSupervisor) -> MutexGuard<'_, Vec<RetainedProcess>> {
    supervisor
        .inner
        .processes
        .lock()
        .unwrap_or_else(|error| error.into_inner())
}

fn missing(
    id: &BackgroundProcessId,
    processes: &[RetainedProcess],
) -> BackgroundProcessLookupError {
    BackgroundProcessLookupError::new(
        id.clone(),
        processes.iter().map(|process| process.id.clone()).collect(),
    )
}

fn summary_of(process: &mut RetainedProcess) -> BackgroundProcessSummary {
    refresh(process);
    BackgroundProcessSummary::new(
        process.id.clone(),
        process.command.clone(),
        process.cwd.clone(),
        match process.life {
            Life::Running => BackgroundProcessState::Running { pid: process.pid },
            Life::Exited(code) => BackgroundProcessState::Exited { code },
            Life::Killed => BackgroundProcessState::Killed,
        },
    )
    .with_display_name(process.display_name.clone())
}

fn refresh(process: &mut RetainedProcess) {
    if !matches!(process.life, Life::Running) {
        return;
    }
    match process.child.try_wait() {
        Ok(Some(status)) => {
            process.life = Life::Exited(status.code().unwrap_or(-1));
        }
        Ok(None) => {}
        Err(_) => {
            process.life = Life::Exited(-1);
        }
    }
}

const DISPLAY_COMMAND_LIMIT: usize = 240;
const FALLBACK_DISPLAY_NAME_LIMIT: usize = 24;

fn fallback_display_name(command: &str) -> Option<String> {
    let first_segment = command
        .split([';', '\n', '|'])
        .next()
        .unwrap_or(command)
        .split("&&")
        .next()
        .unwrap_or(command)
        .trim();
    let tokens: Vec<&str> = first_segment.split_whitespace().take(2).collect();
    if tokens.is_empty() {
        return None;
    }
    let candidate = tokens.join(" ");
    if candidate.chars().count() <= FALLBACK_DISPLAY_NAME_LIMIT {
        Some(candidate)
    } else {
        let mut truncated: String = candidate
            .chars()
            .take(FALLBACK_DISPLAY_NAME_LIMIT.saturating_sub(1))
            .collect();
        truncated.push('…');
        Some(truncated)
    }
}

fn display_command(intent: &ProcessActionIntent) -> String {
    let raw = unwrap_shell_display_command(intent.argv());
    if raw.chars().count() <= DISPLAY_COMMAND_LIMIT {
        raw
    } else {
        let mut truncated = raw.chars().take(DISPLAY_COMMAND_LIMIT).collect::<String>();
        truncated.push_str("...");
        truncated
    }
}

fn unwrap_shell_display_command(argv: &[String]) -> String {
    match argv {
        [_, no_profile, non_interactive, flag, script]
            if no_profile.eq_ignore_ascii_case("-NoProfile")
                && non_interactive.eq_ignore_ascii_case("-NonInteractive")
                && flag.eq_ignore_ascii_case("-Command") =>
        {
            script.clone()
        }
        [_, no_profile, flag, script]
            if no_profile.eq_ignore_ascii_case("-NoProfile")
                && flag.eq_ignore_ascii_case("-Command") =>
        {
            script.clone()
        }
        [_, flag, script]
            if matches!(flag.as_str(), "-lc" | "-c") || flag.eq_ignore_ascii_case("/C") =>
        {
            script.clone()
        }
        _ => argv.join(" "),
    }
}

fn stop_retained(process: &mut RetainedProcess) {
    for reader in &process.readers {
        reader.abort();
    }
    #[cfg(windows)]
    if let Some(job) = process.windows_job.as_ref() {
        job.terminate();
    }
    reap_child(&mut process.child);
}

pub(super) fn kill_child_group(child: &mut Child) {
    kill_tree(child.id());
    let _ = child.start_kill();
}

/// Reaps a killed child so its pid does not remain a zombie.
///
/// Drop cannot await. `SIGKILL` is bounded by a short poll of `try_wait`.
fn reap_child(child: &mut Child) {
    kill_child_group(child);
    for _ in 0..20 {
        match child.try_wait() {
            Ok(Some(_)) | Err(_) => return,
            Ok(None) => std::thread::sleep(Duration::from_millis(5)),
        }
    }
}

fn kill_tree(pid: Option<u32>) {
    #[cfg(unix)]
    {
        let Some(pid) = pid else {
            return;
        };
        let Some(pid) = rustix::process::Pid::from_raw(pid as rustix::process::RawPid) else {
            return;
        };
        let _ = rustix::process::kill_process_group(pid, rustix::process::Signal::KILL);
    }
    #[cfg(not(unix))]
    {
        let _ = pid;
    }
}

#[derive(Debug, Default)]
struct Capture {
    bytes: Mutex<Vec<u8>>,
    truncated: AtomicBool,
}

impl Capture {
    fn snapshot(&self) -> (Vec<u8>, bool) {
        let bytes = self
            .bytes
            .lock()
            .unwrap_or_else(|error| error.into_inner())
            .clone();
        (bytes, self.truncated.load(Ordering::Relaxed))
    }
}

async fn drain_capture<R>(
    mut reader: R,
    limit: usize,
    capture: Arc<Capture>,
    live_sink: Option<Arc<Mutex<Option<ProcessOutputSink>>>>,
    stream: ToolOutputStream,
) -> Result<(), ProcessRunnerError>
where
    R: AsyncRead + Unpin,
{
    let mut scratch = [0_u8; 8192];
    loop {
        let count = reader.read(&mut scratch).await.map_err(|source| {
            ProcessRunnerError::infrastructure(format!("failed to read process output: {source}"))
        })?;
        if count == 0 {
            break;
        }
        if let Some(slot) = &live_sink {
            let guard = slot.lock().unwrap_or_else(|error| error.into_inner());
            if let Some(sink) = guard.as_ref() {
                sink.emit(stream, &scratch[..count]);
            }
        }
        let mut bytes = capture
            .bytes
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        if bytes.len() < limit {
            let remaining = limit - bytes.len();
            let kept = count.min(remaining);
            bytes.extend_from_slice(&scratch[..kept]);
            if kept < count {
                capture.truncated.store(true, Ordering::Relaxed);
            }
        } else {
            capture.truncated.store(true, Ordering::Relaxed);
        }
    }
    Ok(())
}

#[cfg(test)]
mod display_tests {
    use super::unwrap_shell_display_command;

    #[test]
    fn unwrap_shell_display_command_handles_unix_and_windows_wrappers() {
        assert_eq!(
            unwrap_shell_display_command(&[
                "bash".to_owned(),
                "-lc".to_owned(),
                "cargo test".to_owned(),
            ]),
            "cargo test"
        );
        assert_eq!(
            unwrap_shell_display_command(&[
                "pwsh".to_owned(),
                "-NoProfile".to_owned(),
                "-NonInteractive".to_owned(),
                "-Command".to_owned(),
                "cargo test".to_owned(),
            ]),
            "cargo test"
        );
        assert_eq!(
            unwrap_shell_display_command(&[
                "powershell.exe".to_owned(),
                "-NoProfile".to_owned(),
                "-Command".to_owned(),
                "Get-ChildItem".to_owned(),
            ]),
            "Get-ChildItem"
        );
        assert_eq!(
            unwrap_shell_display_command(&[
                "cmd.exe".to_owned(),
                "/C".to_owned(),
                "dir".to_owned(),
            ]),
            "dir"
        );
    }
}