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
//! Tests for executing script files by path and $PATH search.
//!
//! Covers: absolute path, relative path, arguments, shebang stripping,
//! missing file, directory, permission denied, exit code propagation,
//! nested paths, and $PATH search.

use bashkit::Bash;
use std::path::Path;

/// Execute script by absolute path
#[tokio::test]
async fn exec_script_by_absolute_path() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/test.sh"), b"#!/bin/bash\necho hello")
        .await
        .unwrap();
    fs.chmod(Path::new("/test.sh"), 0o755).await.unwrap();

    let result = bash.exec("/test.sh").await.unwrap();
    assert_eq!(result.stdout.trim(), "hello");
    assert_eq!(result.exit_code, 0);
}

/// Execute script without shebang line
#[tokio::test]
async fn exec_script_without_shebang() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/no_shebang.sh"), b"echo no shebang")
        .await
        .unwrap();
    fs.chmod(Path::new("/no_shebang.sh"), 0o755).await.unwrap();

    let result = bash.exec("/no_shebang.sh").await.unwrap();
    assert_eq!(result.stdout.trim(), "no shebang");
    assert_eq!(result.exit_code, 0);
}

/// Execute script with arguments ($1, $2)
#[tokio::test]
async fn exec_script_with_args() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(
        Path::new("/greet.sh"),
        b"#!/bin/bash\necho \"Hello, $1 and $2!\"",
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/greet.sh"), 0o755).await.unwrap();

    let result = bash.exec("/greet.sh world moon").await.unwrap();
    assert_eq!(result.stdout.trim(), "Hello, world and moon!");
    assert_eq!(result.exit_code, 0);
}

/// $0 is set to the script name
#[tokio::test]
async fn exec_script_dollar_zero() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/show_name.sh"), b"#!/bin/bash\necho $0")
        .await
        .unwrap();
    fs.chmod(Path::new("/show_name.sh"), 0o755).await.unwrap();

    let result = bash.exec("/show_name.sh").await.unwrap();
    assert_eq!(result.stdout.trim(), "/show_name.sh");
    assert_eq!(result.exit_code, 0);
}

/// Nonexistent file returns "No such file or directory" (exit 127)
#[tokio::test]
async fn exec_script_missing_file() {
    let mut bash = Bash::new();

    let result = bash.exec("/missing.sh").await.unwrap();
    assert!(result.stderr.contains("No such file or directory"));
    assert_eq!(result.exit_code, 127);
}

/// Directory returns "Is a directory" (exit 126)
#[tokio::test]
async fn exec_script_is_directory() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.mkdir(Path::new("/mydir"), false).await.unwrap();

    let result = bash.exec("/mydir").await.unwrap();
    assert!(result.stderr.contains("Is a directory"));
    assert_eq!(result.exit_code, 126);
}

/// Not executable returns "Permission denied" (exit 126)
#[tokio::test]
async fn exec_script_permission_denied() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/noperm.sh"), b"echo nope")
        .await
        .unwrap();
    // Default mode is 0o644 — not executable

    let result = bash.exec("/noperm.sh").await.unwrap();
    assert!(result.stderr.contains("Permission denied"));
    assert_eq!(result.exit_code, 126);
}

/// Exit code propagation from script
#[tokio::test]
async fn exec_script_exit_code_propagation() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/fail.sh"), b"#!/bin/bash\nexit 42")
        .await
        .unwrap();
    fs.chmod(Path::new("/fail.sh"), 0o755).await.unwrap();

    let result = bash.exec("/fail.sh\necho $?").await.unwrap();
    assert_eq!(result.stdout.trim(), "42");
}

/// Nested directory paths work
#[tokio::test]
async fn exec_script_nested_path() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.mkdir(Path::new("/workspace/.agents/skills/nav/scripts"), true)
        .await
        .unwrap();
    fs.write_file(
        Path::new("/workspace/.agents/skills/nav/scripts/nav.sh"),
        b"#!/bin/bash\necho \"nav: $1\"",
    )
    .await
    .unwrap();
    fs.chmod(
        Path::new("/workspace/.agents/skills/nav/scripts/nav.sh"),
        0o755,
    )
    .await
    .unwrap();

    let result = bash
        .exec("/workspace/.agents/skills/nav/scripts/nav.sh dist")
        .await
        .unwrap();
    assert_eq!(result.stdout.trim(), "nav: dist");
    assert_eq!(result.exit_code, 0);
}

/// $PATH search finds and executes script
#[tokio::test]
async fn exec_script_via_path_search() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.mkdir(Path::new("/usr/local/bin"), true).await.unwrap();
    fs.write_file(
        Path::new("/usr/local/bin/myscript"),
        b"#!/bin/bash\necho found",
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/usr/local/bin/myscript"), 0o755)
        .await
        .unwrap();

    let result = bash.exec("PATH=/usr/local/bin\nmyscript").await.unwrap();
    assert_eq!(result.stdout.trim(), "found");
    assert_eq!(result.exit_code, 0);
}

/// $PATH search skips non-executable files
#[tokio::test]
async fn path_search_skips_non_executable() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.mkdir(Path::new("/bin1"), false).await.unwrap();
    fs.mkdir(Path::new("/bin2"), false).await.unwrap();
    // /bin1/cmd exists but not executable
    fs.write_file(Path::new("/bin1/cmd"), b"echo wrong")
        .await
        .unwrap();
    // /bin2/cmd is executable
    fs.write_file(Path::new("/bin2/cmd"), b"echo right")
        .await
        .unwrap();
    fs.chmod(Path::new("/bin2/cmd"), 0o755).await.unwrap();

    let result = bash.exec("PATH=/bin1:/bin2\ncmd").await.unwrap();
    assert_eq!(result.stdout.trim(), "right");
}

/// $PATH search returns "command not found" when no match
#[tokio::test]
async fn path_search_command_not_found() {
    let mut bash = Bash::new();

    let result = bash.exec("PATH=\nnosuchcmd").await.unwrap();
    assert!(result.stderr.contains("command not found"));
    assert_eq!(result.exit_code, 127);
}

/// Command-not-found suggests similar builtins (typo detection)
#[tokio::test]
async fn command_not_found_typo_suggestion() {
    let mut bash = Bash::new();

    // "grepp" is close to "grep" (distance=1, unique match)
    let result = bash.exec("grepp test").await.unwrap();
    assert_eq!(result.exit_code, 127);
    assert!(result.stderr.contains("Did you mean"));
    assert!(result.stderr.contains("grep"));
}

/// Command-not-found hints for unavailable sandbox commands
#[tokio::test]
async fn command_not_found_sandbox_hint() {
    let mut bash = Bash::new();

    let result = bash.exec("pip install foo").await.unwrap();
    assert_eq!(result.exit_code, 127);
    assert!(result.stderr.contains("Package managers"));

    let result = bash.exec("sudo ls").await.unwrap();
    assert_eq!(result.exit_code, 127);
    assert!(result.stderr.contains("privilege"));

    // With ssh feature, ssh is a registered builtin (returns "not configured").
    // Without ssh feature, ssh is unavailable (returns "command not found").
    let result = bash.exec("ssh user@host").await.unwrap();
    #[cfg(feature = "ssh")]
    {
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("not configured"));
    }
    #[cfg(not(feature = "ssh"))]
    {
        assert_eq!(result.exit_code, 127);
        assert!(result.stderr.contains("ssh"));
    }
}

/// Completely unknown command has no suggestion
#[tokio::test]
async fn command_not_found_no_suggestion() {
    let mut bash = Bash::new();

    let result = bash.exec("zzzznonexistent").await.unwrap();
    assert_eq!(result.exit_code, 127);
    assert!(result.stderr.contains("command not found"));
    assert!(!result.stderr.contains("Did you mean"));
}

/// Script with relative path (contains /)
#[tokio::test]
async fn exec_script_relative_path() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.mkdir(Path::new("/workspace"), false).await.unwrap();
    fs.write_file(Path::new("/workspace/run.sh"), b"echo relative works")
        .await
        .unwrap();
    fs.chmod(Path::new("/workspace/run.sh"), 0o755)
        .await
        .unwrap();

    // Set cwd to /workspace so ./run.sh resolves
    let result = bash.exec("cd /workspace\n./run.sh").await.unwrap();
    assert_eq!(result.stdout.trim(), "relative works");
    assert_eq!(result.exit_code, 0);
}

/// Script calls another script by path
#[tokio::test]
async fn exec_script_calls_script() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/inner.sh"), b"#!/bin/bash\necho inner")
        .await
        .unwrap();
    fs.chmod(Path::new("/inner.sh"), 0o755).await.unwrap();

    fs.write_file(
        Path::new("/outer.sh"),
        b"#!/bin/bash\necho outer\n/inner.sh",
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/outer.sh"), 0o755).await.unwrap();

    let result = bash.exec("/outer.sh").await.unwrap();
    assert_eq!(result.stdout, "outer\ninner\n");
    assert_eq!(result.exit_code, 0);
}

/// Script written via echo/redirect then chmod +x then executed
#[tokio::test]
async fn exec_script_chmod_then_run() {
    let mut bash = Bash::new();

    let result = bash
        .exec(
            "echo '#!/bin/bash\necho script ran' > /tmp/test_exec.sh\n\
             chmod +x /tmp/test_exec.sh\n\
             /tmp/test_exec.sh",
        )
        .await
        .unwrap();
    assert_eq!(result.stdout.trim(), "script ran");
    assert_eq!(result.exit_code, 0);
}

/// $PATH search with args
#[tokio::test]
async fn path_search_with_args() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.mkdir(Path::new("/mybin"), false).await.unwrap();
    fs.write_file(Path::new("/mybin/greeter"), b"#!/bin/bash\necho \"hi $1\"")
        .await
        .unwrap();
    fs.chmod(Path::new("/mybin/greeter"), 0o755).await.unwrap();

    let result = bash.exec("PATH=/mybin\ngreeter alice").await.unwrap();
    assert_eq!(result.stdout.trim(), "hi alice");
}

/// Script $# shows argument count
#[tokio::test]
async fn exec_script_dollar_hash() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/count.sh"), b"#!/bin/bash\necho $#")
        .await
        .unwrap();
    fs.chmod(Path::new("/count.sh"), 0o755).await.unwrap();

    let result = bash.exec("/count.sh a b c").await.unwrap();
    assert_eq!(result.stdout.trim(), "3");
}

/// Script $@ shows all arguments
#[tokio::test]
async fn exec_script_dollar_at() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/all.sh"), b"#!/bin/bash\necho $@")
        .await
        .unwrap();
    fs.chmod(Path::new("/all.sh"), 0o755).await.unwrap();

    let result = bash.exec("/all.sh x y z").await.unwrap();
    assert_eq!(result.stdout.trim(), "x y z");
}

/// Functions defined in parent are NOT visible in subprocess script execution
#[tokio::test]
async fn exec_script_functions_not_inherited() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(
        Path::new("/check_func.sh"),
        b"#!/bin/bash\nif declare -f helper > /dev/null 2>&1; then\n  echo found\nelse\n  echo not found\nfi",
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/check_func.sh"), 0o755).await.unwrap();

    // Define a function in parent, then run subprocess script
    bash.exec("helper() { echo from_parent; }").await.unwrap();
    let result = bash.exec("/check_func.sh").await.unwrap();
    assert_eq!(result.stdout.trim(), "not found");
}

/// `declare -f nonexistent` should return exit code 1
#[tokio::test]
async fn declare_f_nonexistent_function_returns_1() {
    let mut bash = Bash::new();
    let result = bash.exec("declare -f no_such_func").await.unwrap();
    assert_eq!(result.exit_code, 1);
}

/// `declare -f existing_func` should print definition and return exit code 0
#[tokio::test]
async fn declare_f_existing_function_prints_definition() {
    let mut bash = Bash::new();
    bash.exec("myfunc() { echo hello; }").await.unwrap();
    let result = bash.exec("declare -f myfunc").await.unwrap();
    assert_eq!(result.exit_code, 0);
    assert!(result.stdout.contains("myfunc"));
}

/// `declare -f` with no args lists all functions
#[tokio::test]
async fn declare_f_no_args_lists_all_functions() {
    let mut bash = Bash::new();
    bash.exec("foo() { echo a; }").await.unwrap();
    bash.exec("bar() { echo b; }").await.unwrap();
    let result = bash.exec("declare -f").await.unwrap();
    assert_eq!(result.exit_code, 0);
    assert!(result.stdout.contains("foo"));
    assert!(result.stdout.contains("bar"));
}

/// Issue #842: $? should be 0 at start of VFS script subprocess, even when
/// parent had non-zero exit code. Subprocess isolation must reset last_exit_code
/// to match real bash subprocess behavior.
#[tokio::test]
async fn exec_vfs_script_initial_exit_code_is_zero() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(Path::new("/check_exit.sh"), b"#!/bin/bash\necho $?\n")
        .await
        .unwrap();
    fs.chmod(Path::new("/check_exit.sh"), 0o755).await.unwrap();

    // Parent has non-zero exit code, then run VFS script
    let result = bash.exec("false; /check_exit.sh").await.unwrap();
    assert_eq!(
        result.stdout.trim(),
        "0",
        "VFS script subprocess should start with $?=0 (like real bash); got stdout={:?}",
        result.stdout
    );
}

/// Issue #842: `set -euo pipefail` in VFS script should not trigger false failure
/// even after a prior non-zero exit code in the parent shell.
#[tokio::test]
async fn exec_vfs_script_set_e_after_prior_failure() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(
        Path::new("/test.sh"),
        br#"#!/bin/bash
set -euo pipefail
MY_VAR="hello"
echo "${MY_VAR}"
"#,
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/test.sh"), 0o755).await.unwrap();

    let result = bash.exec("false; /test.sh").await.unwrap();
    assert_eq!(
        result.exit_code, 0,
        "VFS script with set -e should succeed after prior failure; stdout={:?}, stderr={:?}",
        result.stdout, result.stderr
    );
    assert_eq!(result.stdout.trim(), "hello");
}

/// Issue #842: Nested VFS scripts with set -e and command substitution.
#[tokio::test]
async fn exec_vfs_script_set_e_nested_scripts() {
    let mut bash = Bash::new();
    let fs = bash.fs();

    fs.write_file(
        Path::new("/inner.sh"),
        br#"#!/bin/bash
set -euo pipefail
echo "inner_output"
"#,
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/inner.sh"), 0o755).await.unwrap();

    fs.write_file(
        Path::new("/outer.sh"),
        br#"#!/bin/bash
set -euo pipefail
RESULT="$(/inner.sh)"
echo "${RESULT}"
"#,
    )
    .await
    .unwrap();
    fs.chmod(Path::new("/outer.sh"), 0o755).await.unwrap();

    let result = bash.exec("/outer.sh").await.unwrap();
    assert_eq!(
        result.exit_code, 0,
        "Nested VFS scripts with set -e should work; stdout={:?}, stderr={:?}",
        result.stdout, result.stderr
    );
    assert_eq!(result.stdout.trim(), "inner_output");
}