bashkit 0.5.0

Awesomely fast virtual sandbox with bash and file system
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
//! patch - Apply unified diff patches to files
//!
//! Reads a unified diff from stdin and applies it to files in the VFS.
//!
//! Usage:
//!   patch [OPTIONS] [FILE]
//!   patch -p1 < diff.patch
//!   patch --dry-run < diff.patch
//!   patch -R < diff.patch         # reverse patch

use async_trait::async_trait;

use super::{Builtin, Context, resolve_path};
use crate::error::Result;
use crate::interpreter::ExecResult;

/// patch command - apply unified diffs
pub struct Patch;

struct PatchOptions {
    strip: usize,
    dry_run: bool,
    reverse: bool,
    target_file: Option<String>,
}

fn parse_patch_args(args: &[String]) -> PatchOptions {
    let mut opts = PatchOptions {
        strip: 0,
        dry_run: false,
        reverse: false,
        target_file: None,
    };
    let mut p = super::arg_parser::ArgParser::new(args);

    while !p.is_done() {
        if let Some(val) = p.flag_value_opt("-p") {
            opts.strip = val.parse().unwrap_or(0);
        } else if p.flag("--dry-run") {
            opts.dry_run = true;
        } else if p.flag_any(&["-R", "--reverse"]) {
            opts.reverse = true;
        } else if let Some(arg) = p.positional() {
            opts.target_file = Some(arg.to_string());
        } else {
            p.advance();
        }
    }

    opts
}

/// A single hunk from a unified diff
#[derive(Debug)]
struct Hunk {
    old_start: usize,
    #[allow(dead_code)]
    old_count: usize,
    new_start: usize,
    #[allow(dead_code)]
    new_count: usize,
    lines: Vec<HunkLine>,
}

#[derive(Debug, Clone)]
enum HunkLine {
    Context(String),
    Add(String),
    Remove(String),
}

/// A parsed file diff containing multiple hunks
#[derive(Debug)]
struct FileDiff {
    old_path: String,
    new_path: String,
    hunks: Vec<Hunk>,
}

/// Strip path components from a file path
fn strip_path(path: &str, strip: usize) -> String {
    if strip == 0 {
        return path.to_string();
    }
    let parts: Vec<&str> = path.split('/').collect();
    if strip >= parts.len() {
        parts.last().unwrap_or(&"").to_string()
    } else {
        parts[strip..].join("/")
    }
}

/// Validate that a path does not escape the working directory via `..` components.
/// Rejects any path containing a literal `..` path segment to prevent directory traversal.
fn validate_path(path: &str) -> std::result::Result<(), String> {
    for component in path.split('/') {
        if component == ".." {
            return Err(format!(
                "patch: rejecting path '{}': contains '..' traversal",
                path
            ));
        }
    }
    Ok(())
}

/// Parse unified diff text into file diffs
fn parse_unified_diff(input: &str) -> Vec<FileDiff> {
    let mut diffs = Vec::new();
    let lines: Vec<&str> = input.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        // Look for --- a/file header
        if lines[i].starts_with("--- ") && i + 1 < lines.len() && lines[i + 1].starts_with("+++ ") {
            let old_path = lines[i]
                .strip_prefix("--- ")
                .unwrap_or("")
                .split('\t')
                .next()
                .unwrap_or("")
                .to_string();
            let new_path = lines[i + 1]
                .strip_prefix("+++ ")
                .unwrap_or("")
                .split('\t')
                .next()
                .unwrap_or("")
                .to_string();
            i += 2;

            let mut hunks = Vec::new();

            // Parse hunks
            while i < lines.len() && lines[i].starts_with("@@ ") {
                if let Some(hunk) = parse_hunk_header(lines[i]) {
                    let mut hunk = hunk;
                    i += 1;

                    // Read hunk lines
                    while i < lines.len() {
                        let line = lines[i];
                        if line.starts_with("@@ ") || line.starts_with("--- ") {
                            break;
                        }
                        if let Some(rest) = line.strip_prefix('+') {
                            hunk.lines.push(HunkLine::Add(rest.to_string()));
                        } else if let Some(rest) = line.strip_prefix('-') {
                            hunk.lines.push(HunkLine::Remove(rest.to_string()));
                        } else if let Some(rest) = line.strip_prefix(' ') {
                            hunk.lines.push(HunkLine::Context(rest.to_string()));
                        } else if line == "\\ No newline at end of file" {
                            // skip
                        } else {
                            // Treat as context (some diffs omit the space prefix)
                            hunk.lines.push(HunkLine::Context(line.to_string()));
                        }
                        i += 1;
                    }
                    hunks.push(hunk);
                } else {
                    i += 1;
                }
            }

            diffs.push(FileDiff {
                old_path,
                new_path,
                hunks,
            });
        } else {
            i += 1;
        }
    }

    diffs
}

/// Parse a hunk header like @@ -1,3 +1,4 @@
fn parse_hunk_header(line: &str) -> Option<Hunk> {
    let line = line.strip_prefix("@@ ")?;
    let line = line.split(" @@").next()?;
    let parts: Vec<&str> = line.split(' ').collect();
    if parts.len() < 2 {
        return None;
    }

    let old_part = parts[0].strip_prefix('-')?;
    let new_part = parts[1].strip_prefix('+')?;

    let (old_start, old_count) = parse_range(old_part);
    let (new_start, new_count) = parse_range(new_part);

    Some(Hunk {
        old_start,
        old_count,
        new_start,
        new_count,
        lines: Vec::new(),
    })
}

fn parse_range(s: &str) -> (usize, usize) {
    if let Some((start, count)) = s.split_once(',') {
        (start.parse().unwrap_or(1), count.parse().unwrap_or(1))
    } else {
        (s.parse().unwrap_or(1), 1)
    }
}

/// Apply hunks to file content, returning the patched content.
/// If reverse is true, swaps add/remove operations.
fn apply_hunks(
    content: &str,
    hunks: &[Hunk],
    reverse: bool,
) -> std::result::Result<String, String> {
    let mut lines: Vec<String> = content.lines().map(|l| l.to_string()).collect();
    // Track if original ended with newline
    let had_trailing_newline = content.ends_with('\n') || content.is_empty();

    // Apply hunks in reverse order to preserve line numbers
    for hunk in hunks.iter().rev() {
        let start = if reverse {
            hunk.new_start
        } else {
            hunk.old_start
        };
        // Convert 1-based to 0-based
        let start_idx = if start > 0 { start - 1 } else { 0 };

        // Build expected old lines and new lines based on direction
        let mut old_lines = Vec::new();
        let mut new_lines = Vec::new();

        for hl in &hunk.lines {
            match hl {
                HunkLine::Context(l) => {
                    old_lines.push(l.clone());
                    new_lines.push(l.clone());
                }
                HunkLine::Add(l) => {
                    if reverse {
                        old_lines.push(l.clone());
                    } else {
                        new_lines.push(l.clone());
                    }
                }
                HunkLine::Remove(l) => {
                    if reverse {
                        new_lines.push(l.clone());
                    } else {
                        old_lines.push(l.clone());
                    }
                }
            }
        }

        // Verify context/old lines match (with fuzz tolerance)
        let end_idx = start_idx + old_lines.len();
        if end_idx > lines.len() {
            return Err(format!(
                "hunk at line {} does not match (file too short)",
                start
            ));
        }

        for (j, expected) in old_lines.iter().enumerate() {
            let actual_idx = start_idx + j;
            if actual_idx < lines.len() && lines[actual_idx] != *expected {
                return Err(format!(
                    "hunk at line {} does not match: expected '{}', got '{}'",
                    start, expected, lines[actual_idx]
                ));
            }
        }

        // Replace old lines with new lines
        lines.splice(start_idx..end_idx, new_lines);
    }

    let mut result = lines.join("\n");
    if had_trailing_newline && !result.is_empty() {
        result.push('\n');
    }
    Ok(result)
}

#[async_trait]
impl Builtin for Patch {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: patch [OPTION]... [FILE]\nApply a unified diff patch to FILE(s).\n\n  -pNUM\tstrip NUM leading path components\n  --dry-run\tprint results without modifying files\n  -R, --reverse\treverse the patch\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("patch (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        let opts = parse_patch_args(ctx.args);

        let input = match ctx.stdin {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => {
                return Ok(ExecResult::err(
                    "patch: no input (expected unified diff on stdin)\n".to_string(),
                    1,
                ));
            }
        };

        let file_diffs = parse_unified_diff(&input);
        if file_diffs.is_empty() {
            return Ok(ExecResult::err(
                "patch: no valid diff found in input\n".to_string(),
                1,
            ));
        }

        let mut output = String::new();
        let mut had_error = false;

        for diff in &file_diffs {
            // Determine target file path
            let target = if let Some(ref t) = opts.target_file {
                t.clone()
            } else {
                // Use new_path for forward patches, old_path for reverse
                let raw_path = if opts.reverse {
                    &diff.new_path
                } else {
                    // Prefer new_path unless it's /dev/null (file deletion)
                    if diff.new_path == "/dev/null" {
                        &diff.old_path
                    } else {
                        &diff.new_path
                    }
                };
                let stripped = strip_path(raw_path, opts.strip);
                if let Err(e) = validate_path(&stripped) {
                    output.push_str(&format!("{}\n", e));
                    had_error = true;
                    continue;
                }
                stripped
            };

            let path = resolve_path(ctx.cwd, &target);

            // Read existing file (may not exist for new files)
            let content = if diff.old_path == "/dev/null" && !opts.reverse {
                // New file creation
                String::new()
            } else {
                match ctx.fs.read_file(&path).await {
                    Ok(bytes) => String::from_utf8_lossy(&bytes).to_string(),
                    Err(_) => {
                        // File doesn't exist - might be a new file
                        String::new()
                    }
                }
            };

            match apply_hunks(&content, &diff.hunks, opts.reverse) {
                Ok(patched) => {
                    if opts.dry_run {
                        output.push_str(&format!("checking file {}\n", target));
                    } else {
                        // Handle file deletion
                        if diff.new_path == "/dev/null" && !opts.reverse {
                            output.push_str(&format!("patching file {} (removed)\n", target));
                            ctx.fs.remove(&path, false).await?;
                        } else {
                            ctx.fs.write_file(&path, patched.as_bytes()).await?;
                            output.push_str(&format!("patching file {}\n", target));
                        }
                    }
                }
                Err(e) => {
                    output.push_str(&format!("patch: {}: {}\n", target, e));
                    had_error = true;
                }
            }
        }

        if had_error {
            Ok(ExecResult::err(output, 1))
        } else {
            Ok(ExecResult::ok(output))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fs::{FileSystem, InMemoryFs};
    use std::collections::HashMap;
    use std::path::{Path, PathBuf};
    use std::sync::Arc;

    async fn run_patch(
        args: &[&str],
        stdin: &str,
        files: &[(&str, &[u8])],
    ) -> (ExecResult, Arc<InMemoryFs>) {
        let fs = Arc::new(InMemoryFs::new());
        for (path, content) in files {
            let fs_trait = fs.clone() as Arc<dyn FileSystem>;
            fs_trait.write_file(Path::new(path), content).await.unwrap();
        }

        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let env = HashMap::new();
        let mut variables = HashMap::new();
        let mut cwd = PathBuf::from("/");
        let fs_dyn = fs.clone() as Arc<dyn FileSystem>;
        let ctx = Context {
            args: &args,
            env: &env,
            variables: &mut variables,
            cwd: &mut cwd,
            fs: fs_dyn,
            stdin: Some(stdin),
            #[cfg(feature = "http_client")]
            http_client: None,
            #[cfg(feature = "git")]
            git_client: None,
            #[cfg(feature = "ssh")]
            ssh_client: None,
            shell: None,
        };

        let result = Patch.execute(ctx).await.unwrap();
        (result, fs)
    }

    #[tokio::test]
    async fn test_patch_simple_change() {
        let diff = "\
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,3 @@
 line1
-line2
+modified
 line3
";
        let (result, fs) =
            run_patch(&["-p1"], diff, &[("/test.txt", b"line1\nline2\nline3\n")]).await;
        assert_eq!(result.exit_code, 0);
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/test.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("modified"));
        assert!(!text.contains("line2"));
    }

    #[tokio::test]
    async fn test_patch_add_lines() {
        let diff = "\
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,4 @@
 line1
+added1
+added2
 line2
";
        let (result, fs) = run_patch(&["-p1"], diff, &[("/test.txt", b"line1\nline2\n")]).await;
        assert_eq!(result.exit_code, 0);
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/test.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("added1"));
        assert!(text.contains("added2"));
    }

    #[tokio::test]
    async fn test_patch_remove_lines() {
        let diff = "\
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,1 @@
 line1
-line2
-line3
";
        let (result, fs) =
            run_patch(&["-p1"], diff, &[("/test.txt", b"line1\nline2\nline3\n")]).await;
        assert_eq!(result.exit_code, 0);
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/test.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert_eq!(text.trim(), "line1");
    }

    #[tokio::test]
    async fn test_patch_dry_run() {
        let diff = "\
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 line1
-line2
+changed
";
        let (result, fs) = run_patch(
            &["--dry-run", "-p1"],
            diff,
            &[("/test.txt", b"line1\nline2\n")],
        )
        .await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("checking file"));
        // File should NOT be modified
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/test.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("line2"));
    }

    #[tokio::test]
    async fn test_patch_reverse() {
        // First apply forward
        let diff = "\
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 line1
-original
+changed
";
        // File has "changed", we reverse-apply to get back "original"
        let (result, fs) =
            run_patch(&["-R", "-p1"], diff, &[("/test.txt", b"line1\nchanged\n")]).await;
        assert_eq!(result.exit_code, 0);
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/test.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("original"));
    }

    #[tokio::test]
    async fn test_patch_strip_path() {
        assert_eq!(strip_path("a/b/c.txt", 0), "a/b/c.txt");
        assert_eq!(strip_path("a/b/c.txt", 1), "b/c.txt");
        assert_eq!(strip_path("a/b/c.txt", 2), "c.txt");
        assert_eq!(strip_path("a/b/c.txt", 5), "c.txt");
    }

    #[tokio::test]
    async fn test_patch_no_input() {
        let (result, _fs) = run_patch(&[], "", &[]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("no input"));
    }

    #[tokio::test]
    async fn test_patch_invalid_diff() {
        let (result, _fs) = run_patch(&[], "this is not a diff\n", &[]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("no valid diff"));
    }

    #[tokio::test]
    async fn test_patch_hunk_mismatch() {
        let diff = "\
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 line1
-wrong_content
+changed
";
        let (result, _fs) =
            run_patch(&["-p1"], diff, &[("/test.txt", b"line1\nactual_content\n")]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("does not match"));
    }

    #[tokio::test]
    async fn test_patch_new_file() {
        let diff = "\
--- /dev/null
+++ b/newfile.txt
@@ -0,0 +1,2 @@
+hello
+world
";
        let (result, fs) = run_patch(&["-p1"], diff, &[]).await;
        assert_eq!(result.exit_code, 0);
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/newfile.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("hello"));
        assert!(text.contains("world"));
    }

    #[tokio::test]
    async fn test_patch_target_file_override() {
        let diff = "\
--- a/original.txt
+++ b/original.txt
@@ -1,2 +1,2 @@
 line1
-old
+new
";
        let (result, fs) = run_patch(
            &["-p1", "target.txt"],
            diff,
            &[("/target.txt", b"line1\nold\n")],
        )
        .await;
        assert_eq!(result.exit_code, 0);
        let fs_trait = fs as Arc<dyn FileSystem>;
        let content = fs_trait.read_file(Path::new("/target.txt")).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("new"));
    }

    #[tokio::test]
    async fn test_parse_hunk_header() {
        let hunk = parse_hunk_header("@@ -1,3 +1,4 @@").unwrap();
        assert_eq!(hunk.old_start, 1);
        assert_eq!(hunk.old_count, 3);
        assert_eq!(hunk.new_start, 1);
        assert_eq!(hunk.new_count, 4);
    }

    #[tokio::test]
    async fn test_parse_hunk_header_single_line() {
        let hunk = parse_hunk_header("@@ -5 +5,2 @@").unwrap();
        assert_eq!(hunk.old_start, 5);
        assert_eq!(hunk.old_count, 1);
        assert_eq!(hunk.new_start, 5);
        assert_eq!(hunk.new_count, 2);
    }

    // --- Security tests for path traversal (issue #989) ---

    #[tokio::test]
    async fn test_patch_rejects_path_traversal() {
        let diff = "\
--- a/../../../etc/passwd
+++ b/../../../etc/passwd
@@ -1,1 +1,1 @@
-root:x:0:0:root:/root:/bin/bash
+pwned
";
        let (result, _fs) = run_patch(&["-p1"], diff, &[]).await;
        assert_eq!(result.exit_code, 1);
        assert!(
            result.stderr.contains(".."),
            "error should mention path traversal: {}",
            result.stderr
        );
    }

    #[tokio::test]
    async fn test_patch_rejects_embedded_dotdot() {
        let diff = "\
--- a/foo/../../secret.txt
+++ b/foo/../../secret.txt
@@ -1,1 +1,1 @@
-old
+new
";
        let (result, _fs) = run_patch(&["-p1"], diff, &[("/secret.txt", b"old\n")]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains(".."));
    }

    #[tokio::test]
    async fn test_patch_allows_clean_path_after_strip() {
        let diff = "\
--- a/main.rs
+++ b/main.rs
@@ -1,1 +1,1 @@
-old
+new
";
        let (result, _fs) = run_patch(&["-p1"], diff, &[("/main.rs", b"old\n")]).await;
        assert_eq!(result.exit_code, 0);
    }

    #[tokio::test]
    async fn test_strip_path_preserves_dotdot() {
        assert_eq!(
            strip_path("a/../../../etc/passwd", 1),
            "../../../etc/passwd"
        );
    }

    #[tokio::test]
    async fn test_validate_path_rejects_dotdot() {
        assert!(validate_path("../../../etc/passwd").is_err());
        assert!(validate_path("foo/../../bar").is_err());
        assert!(validate_path("..").is_err());
    }

    #[tokio::test]
    async fn test_validate_path_allows_clean() {
        assert!(validate_path("foo/bar/baz.txt").is_ok());
        assert!(validate_path("file.txt").is_ok());
        assert!(validate_path("a/b/c").is_ok());
    }

    #[tokio::test]
    async fn test_patch_delete_file_removes_from_vfs() {
        // Create a file, then apply a delete patch (new_path = /dev/null)
        let diff = "--- a/to_delete.txt\n\
                     +++ /dev/null\n\
                     @@ -1,2 +0,0 @@\n\
                     -hello\n\
                     -world\n";
        let (result, fs) =
            run_patch(&["-p1"], diff, &[("/to_delete.txt", b"hello\nworld\n")]).await;
        assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
        // File should be actually removed, not just emptied
        let fs_dyn = fs as Arc<dyn FileSystem>;
        assert!(
            !fs_dyn.exists(Path::new("/to_delete.txt")).await.unwrap(),
            "deleted file should not exist in VFS"
        );
    }
}