rust-bash 0.3.0

A sandboxed bash interpreter for AI Agents with a virtual filesystem
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
#![cfg(feature = "native-fs")]
//! Integration tests for filesystem backends through the `RustBash` API.
//!
//! These tests exercise OverlayFs, ReadWriteFs, and MountableFs end-to-end,
//! verifying that each backend works correctly when wired into the shell
//! via `RustBashBuilder::fs()`.

use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;

use rust_bash::{
    InMemoryFs, MountableFs, OverlayFs, ReadWriteFs, RustBash, RustBashBuilder, VirtualFs,
};

// ── Helpers ────────────────────────────────────────────────────────

fn assert_stdout(shell: &mut RustBash, cmd: &str, expected: &str) {
    let r = shell.exec(cmd).unwrap();
    assert_eq!(
        r.stdout, expected,
        "command `{cmd}` produced unexpected stdout"
    );
}

// ── OverlayFs integration ──────────────────────────────────────────

#[test]
fn overlay_reads_from_real_directory() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("hello.txt"), b"disk content\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /hello.txt", "disk content\n");
}

#[test]
fn overlay_writes_stay_in_memory() {
    let tmp = tempfile::tempdir().unwrap();
    let file = tmp.path().join("data.txt");
    std::fs::write(&file, b"original\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo modified > /data.txt").unwrap();
    assert_stdout(&mut shell, "cat /data.txt", "modified\n");

    // Disk file unchanged
    let disk = std::fs::read_to_string(&file).unwrap();
    assert_eq!(disk, "original\n");
}

#[test]
fn overlay_new_files_do_not_touch_disk() {
    let tmp = tempfile::tempdir().unwrap();
    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo hello > /newfile.txt").unwrap();
    assert_stdout(&mut shell, "cat /newfile.txt", "hello\n");

    assert!(!tmp.path().join("newfile.txt").exists());
}

#[test]
fn overlay_delete_hides_lower_file() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("gone.txt"), b"bye").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("rm /gone.txt").unwrap();
    let r = shell.exec("cat /gone.txt").unwrap();
    assert_ne!(r.exit_code, 0);

    // Disk file still exists
    assert!(tmp.path().join("gone.txt").exists());
}

#[test]
fn overlay_mkdir_and_write_in_new_dir() {
    let tmp = tempfile::tempdir().unwrap();
    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell
        .exec("mkdir -p /sub/dir && echo ok > /sub/dir/f.txt")
        .unwrap();
    assert_stdout(&mut shell, "cat /sub/dir/f.txt", "ok\n");
    assert!(!tmp.path().join("sub").exists());
}

#[test]
fn overlay_append_copies_up_from_lower() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("log.txt"), b"line1\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo line2 >> /log.txt").unwrap();
    assert_stdout(&mut shell, "cat /log.txt", "line1\nline2\n");

    // Disk file unchanged
    let disk = std::fs::read_to_string(tmp.path().join("log.txt")).unwrap();
    assert_eq!(disk, "line1\n");
}

#[test]
fn overlay_ls_merges_layers() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("lower.txt"), b"lo").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo hi > /upper.txt").unwrap();
    let r = shell.exec("ls / | sort").unwrap();
    assert!(r.stdout.contains("lower.txt"), "missing lower.txt in ls");
    assert!(r.stdout.contains("upper.txt"), "missing upper.txt in ls");
}

#[test]
fn overlay_pipeline_with_real_files() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("words.txt"), b"apple\nbanana\ncherry\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /words.txt | wc -l | tr -d ' '", "3\n");
    assert_stdout(&mut shell, "grep an /words.txt", "banana\n");
}

// ── ReadWriteFs integration ────────────────────────────────────────

#[test]
fn readwrite_reads_and_writes_real_files() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();
    std::fs::create_dir_all(root.join("home")).unwrap();

    let rwfs = ReadWriteFs::with_root(&root).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(rwfs))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo real > /home/test.txt").unwrap();
    assert_stdout(&mut shell, "cat /home/test.txt", "real\n");

    // Verify it actually hit disk
    let disk = std::fs::read_to_string(root.join("home/test.txt")).unwrap();
    assert_eq!(disk, "real\n");
}

#[test]
fn readwrite_restricted_root_prevents_escape() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();
    std::fs::create_dir_all(&root).unwrap();

    let rwfs = ReadWriteFs::with_root(&root).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(rwfs))
        .cwd("/")
        .build()
        .unwrap();

    let r = shell.exec("cat /../../../etc/passwd").unwrap();
    assert_ne!(r.exit_code, 0, "should fail to escape root");
}

#[test]
fn readwrite_pipeline_with_real_files() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();
    std::fs::write(root.join("nums.txt"), b"3\n1\n2\n").unwrap();

    let rwfs = ReadWriteFs::with_root(&root).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(rwfs))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "sort /nums.txt", "1\n2\n3\n");
}

#[test]
fn readwrite_subshell_writes_are_visible() {
    // ReadWriteFs has no subshell isolation — writes go to the real FS
    // and are visible in the parent shell.
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();

    let rwfs = ReadWriteFs::with_root(&root).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(rwfs))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo before > /rw.txt").unwrap();
    shell.exec("(echo after > /rw.txt)").unwrap();
    // Unlike OverlayFs/MountableFs, ReadWriteFs subshell writes ARE visible
    assert_stdout(&mut shell, "cat /rw.txt", "after\n");
}

// ── MountableFs integration ────────────────────────────────────────

#[test]
fn mountable_routes_to_correct_backend() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo hello > /file.txt").unwrap();
    assert_stdout(&mut shell, "cat /file.txt", "hello\n");
}

#[test]
fn mountable_overlay_at_mount_point() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("real.txt"), b"from disk\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mountable = MountableFs::new()
        .mount("/", Arc::new(InMemoryFs::new()))
        .mount("/project", Arc::new(overlay));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /project/real.txt", "from disk\n");

    // Write through mount stays in memory
    shell.exec("echo overlay > /project/real.txt").unwrap();
    let disk = std::fs::read_to_string(tmp.path().join("real.txt")).unwrap();
    assert_eq!(disk, "from disk\n");
}

#[test]
fn mountable_multiple_backends() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("src.rs"), b"fn main() {}\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mountable = MountableFs::new()
        .mount("/", Arc::new(InMemoryFs::new()))
        .mount("/project", Arc::new(overlay))
        .mount("/tmp", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    // InMemory root
    shell.exec("echo root > /root.txt").unwrap();
    assert_stdout(&mut shell, "cat /root.txt", "root\n");

    // Overlay /project
    assert_stdout(&mut shell, "cat /project/src.rs", "fn main() {}\n");

    // Separate InMemory /tmp
    shell.exec("echo temp > /tmp/work.txt").unwrap();
    assert_stdout(&mut shell, "cat /tmp/work.txt", "temp\n");
}

#[test]
fn mountable_cross_mount_copy() {
    let mountable = MountableFs::new()
        .mount("/", Arc::new(InMemoryFs::new()))
        .mount("/other", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo data > /file.txt").unwrap();
    shell.exec("cp /file.txt /other/copy.txt").unwrap();
    assert_stdout(&mut shell, "cat /other/copy.txt", "data\n");
}

#[test]
fn mountable_readwrite_at_mount_point() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();
    std::fs::create_dir_all(root.join("data")).unwrap();
    std::fs::write(root.join("data/info.txt"), b"real info\n").unwrap();

    let rwfs = ReadWriteFs::with_root(&root).unwrap();
    let mountable = MountableFs::new()
        .mount("/", Arc::new(InMemoryFs::new()))
        .mount("/real", Arc::new(rwfs));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /real/data/info.txt", "real info\n");
}

#[test]
fn mountable_with_builder_files() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .files(HashMap::from([(
            "/seed.txt".to_string(),
            b"seeded\n".to_vec(),
        )]))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /seed.txt", "seeded\n");
}

#[test]
fn mountable_env_and_cwd() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .env(HashMap::from([(
            "MYVAR".to_string(),
            "value123".to_string(),
        )]))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("mkdir -p /work && cd /work").unwrap();
    assert_stdout(&mut shell, "echo $MYVAR", "value123\n");
    assert_stdout(&mut shell, "pwd", "/work\n");
}

// ── Subshell isolation tests ───────────────────────────────────────

#[test]
fn subshell_writes_dont_leak_inmemoryfs() {
    let mut shell = RustBashBuilder::new().cwd("/").build().unwrap();

    shell.exec("echo before > /file.txt").unwrap();
    shell.exec("(echo after > /file.txt)").unwrap();
    let r = shell.exec("cat /file.txt").unwrap();
    assert_eq!(r.stdout, "before\n");
}

#[test]
fn subshell_writes_dont_leak_overlay() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("base.txt"), b"base\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo before > /file.txt").unwrap();
    shell.exec("(echo after > /file.txt)").unwrap();
    assert_stdout(&mut shell, "cat /file.txt", "before\n");
}

#[test]
fn subshell_writes_dont_leak_mountable() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo before > /file.txt").unwrap();
    shell.exec("(echo after > /file.txt)").unwrap();
    assert_stdout(&mut shell, "cat /file.txt", "before\n");
}

#[test]
fn subshell_new_files_dont_leak_overlay() {
    let tmp = tempfile::tempdir().unwrap();
    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("(echo secret > /leak.txt)").unwrap();
    let r = shell.exec("cat /leak.txt").unwrap();
    assert_ne!(r.exit_code, 0, "subshell file should not leak");
}

#[test]
fn subshell_new_files_dont_leak_mountable() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("(echo secret > /leak.txt)").unwrap();
    let r = shell.exec("cat /leak.txt").unwrap();
    assert_ne!(r.exit_code, 0, "subshell file should not leak");
}

#[test]
fn subshell_deletes_dont_leak_overlay() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("keep.txt"), b"keep\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("(rm /keep.txt)").unwrap();
    assert_stdout(&mut shell, "cat /keep.txt", "keep\n");
}

#[test]
fn subshell_deletes_dont_leak_mountable() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo keep > /keep.txt").unwrap();
    shell.exec("(rm /keep.txt)").unwrap();
    assert_stdout(&mut shell, "cat /keep.txt", "keep\n");
}

#[test]
fn command_substitution_isolation_overlay() {
    let tmp = tempfile::tempdir().unwrap();
    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo original > /cs.txt").unwrap();
    shell
        .exec("X=$(echo modified > /cs.txt && cat /cs.txt)")
        .unwrap();
    assert_stdout(&mut shell, "cat /cs.txt", "original\n");
}

#[test]
fn command_substitution_isolation_mountable() {
    let mountable = MountableFs::new().mount("/", Arc::new(InMemoryFs::new()));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    shell.exec("echo original > /cs.txt").unwrap();
    shell
        .exec("X=$(echo modified > /cs.txt && cat /cs.txt)")
        .unwrap();
    assert_stdout(&mut shell, "cat /cs.txt", "original\n");
}

#[test]
fn subshell_isolation_with_mountable_overlay_mount() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("project.txt"), b"disk\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mountable = MountableFs::new()
        .mount("/", Arc::new(InMemoryFs::new()))
        .mount("/project", Arc::new(overlay));
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(mountable))
        .cwd("/")
        .build()
        .unwrap();

    // Write through mountable to overlay mount
    shell.exec("echo parent > /project/file.txt").unwrap();
    shell.exec("(echo child > /project/file.txt)").unwrap();
    assert_stdout(&mut shell, "cat /project/file.txt", "parent\n");

    // Disk untouched
    let disk = std::fs::read_to_string(tmp.path().join("project.txt")).unwrap();
    assert_eq!(disk, "disk\n");
}

// ── Builder .files() with custom backends ──────────────────────────

#[test]
fn builder_files_work_with_overlay() {
    let tmp = tempfile::tempdir().unwrap();
    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .files(HashMap::from([(
            "/seed/data.txt".to_string(),
            b"seeded content\n".to_vec(),
        )]))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /seed/data.txt", "seeded content\n");
    // Seed file only in memory, not on disk
    assert!(!tmp.path().join("seed/data.txt").exists());
}

#[test]
fn builder_files_work_with_readwrite() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();
    let rwfs = ReadWriteFs::with_root(&root).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(rwfs))
        .files(HashMap::from([(
            "/init.txt".to_string(),
            b"initialized\n".to_vec(),
        )]))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(&mut shell, "cat /init.txt", "initialized\n");
    // ReadWriteFs writes to disk
    let disk = std::fs::read_to_string(root.join("init.txt")).unwrap();
    assert_eq!(disk, "initialized\n");
}

// ── Control flow with backends ─────────────────────────────────────

#[test]
fn for_loop_with_overlay() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), b"A\n").unwrap();
    std::fs::write(tmp.path().join("b.txt"), b"B\n").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    let r = shell
        .exec("for f in /a.txt /b.txt; do cat $f; done")
        .unwrap();
    assert_eq!(r.stdout, "A\nB\n");
}

#[test]
fn if_test_with_overlay_file() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("exists.txt"), b"yes").unwrap();

    let overlay = OverlayFs::new(tmp.path()).unwrap();
    let mut shell = RustBashBuilder::new()
        .fs(Arc::new(overlay))
        .cwd("/")
        .build()
        .unwrap();

    assert_stdout(
        &mut shell,
        "if [ -f /exists.txt ]; then echo found; fi",
        "found\n",
    );
    assert_stdout(
        &mut shell,
        "if [ -f /nope.txt ]; then echo found; else echo missing; fi",
        "missing\n",
    );
}

// ── deep_clone via the public VirtualFs trait ──────────────────────

#[test]
fn overlay_deep_clone_is_independent() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("base.txt"), b"base\n").unwrap();

    let overlay = Arc::new(OverlayFs::new(tmp.path()).unwrap());
    overlay
        .write_file(Path::new("/upper.txt"), b"original")
        .unwrap();

    let cloned = overlay.deep_clone();
    cloned
        .write_file(Path::new("/upper.txt"), b"modified")
        .unwrap();

    // Original unchanged
    let content = overlay.read_file(Path::new("/upper.txt")).unwrap();
    assert_eq!(content, b"original");
}

#[test]
fn mountable_deep_clone_is_independent() {
    let mountable = Arc::new(MountableFs::new().mount("/", Arc::new(InMemoryFs::new())));
    mountable
        .write_file(Path::new("/file.txt"), b"original")
        .unwrap();

    let cloned = mountable.deep_clone();
    cloned
        .write_file(Path::new("/file.txt"), b"modified")
        .unwrap();

    let content = mountable.read_file(Path::new("/file.txt")).unwrap();
    assert_eq!(content, b"original");
}