bashkit 0.20.1

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
//! Git Integration Tests
//!
//! Tests for git builtin functionality (Phase 1: local operations).

#![cfg(feature = "git")]

use bashkit::{Bash, GitConfig};

/// Helper to create a bash instance with git configured
fn create_git_bash() -> Bash {
    Bash::builder()
        .git(GitConfig::new().author("Test User", "test@example.com"))
        .build()
}

mod init {
    use super::*;

    #[tokio::test]
    async fn test_git_init_creates_repository() {
        let mut bash = create_git_bash();

        let result = bash.exec("git init /repo").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("Initialized empty Git repository"));
    }

    #[tokio::test]
    async fn test_git_init_in_current_directory() {
        let mut bash = create_git_bash();

        // Create and cd to directory first
        let result = bash
            .exec("mkdir -p /myrepo && cd /myrepo && git init")
            .await
            .unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("Initialized empty Git repository"));
    }

    #[tokio::test]
    async fn test_git_init_reinitialize() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash.exec("git init /repo").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(
            result
                .stdout
                .contains("Reinitialized existing Git repository")
        );
    }
}

mod config {
    use super::*;

    #[tokio::test]
    async fn test_git_config_get_user_name() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash.exec("cd /repo && git config user.name").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout.trim(), "Test User");
    }

    #[tokio::test]
    async fn test_git_config_get_user_email() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash
            .exec("cd /repo && git config user.email")
            .await
            .unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout.trim(), "test@example.com");
    }

    #[tokio::test]
    async fn test_git_config_set() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        bash.exec("cd /repo && git config user.name 'New Name'")
            .await
            .unwrap();
        let result = bash.exec("cd /repo && git config user.name").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout.trim(), "New Name");
    }

    #[tokio::test]
    async fn test_git_config_not_in_repo() {
        let mut bash = create_git_bash();

        let result = bash.exec("cd /tmp && git config user.name").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("not a git repository"));
    }
}

mod add_commit {
    use super::*;

    #[tokio::test]
    async fn test_git_add_single_file() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'hello' > test.txt")
            .await
            .unwrap();
        let result = bash.exec("cd /repo && git add test.txt").await.unwrap();
        assert_eq!(result.exit_code, 0);

        let status = bash.exec("cd /repo && git status").await.unwrap();
        assert!(status.stdout.contains("test.txt"));
        assert!(status.stdout.contains("Changes to be committed"));
    }

    #[tokio::test]
    async fn test_git_add_all() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'a' > a.txt && echo 'b' > b.txt")
            .await
            .unwrap();
        let result = bash.exec("cd /repo && git add .").await.unwrap();
        assert_eq!(result.exit_code, 0);

        let status = bash.exec("cd /repo && git status").await.unwrap();
        assert!(status.stdout.contains("a.txt"));
        assert!(status.stdout.contains("b.txt"));
    }

    #[tokio::test]
    async fn test_git_commit_with_message() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'hello' > test.txt && git add test.txt")
            .await
            .unwrap();
        let result = bash
            .exec("cd /repo && git commit -m 'Initial commit'")
            .await
            .unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("[master"));
        assert!(result.stdout.contains("Initial commit"));
    }

    #[tokio::test]
    async fn test_git_commit_nothing_to_commit() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash
            .exec("cd /repo && git commit -m 'Empty'")
            .await
            .unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("nothing to commit"));
    }

    #[tokio::test]
    async fn test_git_commit_requires_message() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'x' > x.txt && git add x.txt")
            .await
            .unwrap();
        let result = bash.exec("cd /repo && git commit").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("requires a value"));
    }
}

mod status {
    use super::*;

    #[tokio::test]
    async fn test_git_status_clean() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash.exec("cd /repo && git status").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("On branch master"));
        assert!(result.stdout.contains("nothing to commit"));
    }

    #[tokio::test]
    async fn test_git_status_untracked() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'x' > new.txt")
            .await
            .unwrap();
        let result = bash.exec("cd /repo && git status").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("Untracked files"));
        assert!(result.stdout.contains("new.txt"));
    }

    #[tokio::test]
    async fn test_git_status_staged() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'x' > new.txt && git add new.txt")
            .await
            .unwrap();
        let result = bash.exec("cd /repo && git status").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("Changes to be committed"));
        assert!(result.stdout.contains("new.txt"));
    }

    #[tokio::test]
    async fn test_git_status_not_in_repo() {
        let mut bash = create_git_bash();

        let result = bash.exec("cd /tmp && git status").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("not a git repository"));
    }
}

mod log {
    use super::*;

    #[tokio::test]
    async fn test_git_log_after_commit() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo && echo 'x' > x.txt && git add x.txt && git commit -m 'First'").await.unwrap();
        let result = bash.exec("cd /repo && git log").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("commit"));
        assert!(result.stdout.contains("Author: Test User"));
        assert!(result.stdout.contains("First"));
    }

    #[tokio::test]
    async fn test_git_log_multiple_commits() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo").await.unwrap();
        bash.exec("cd /repo && echo 'a' > a.txt && git add a.txt && git commit -m 'First'")
            .await
            .unwrap();
        bash.exec("cd /repo && echo 'b' > b.txt && git add b.txt && git commit -m 'Second'")
            .await
            .unwrap();

        let result = bash.exec("cd /repo && git log").await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("First"));
        assert!(result.stdout.contains("Second"));
    }

    #[tokio::test]
    async fn test_git_log_limit() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo && cd /repo").await.unwrap();
        bash.exec("cd /repo && echo 'a' > a.txt && git add a.txt && git commit -m 'First'")
            .await
            .unwrap();
        bash.exec("cd /repo && echo 'b' > b.txt && git add b.txt && git commit -m 'Second'")
            .await
            .unwrap();
        bash.exec("cd /repo && echo 'c' > c.txt && git add c.txt && git commit -m 'Third'")
            .await
            .unwrap();

        let result = bash.exec("cd /repo && git log -n 1").await.unwrap();
        assert_eq!(result.exit_code, 0);
        // Should only show the most recent commit
        assert!(result.stdout.contains("Third"));
        // First commit should not appear with -n 1
        let first_count = result.stdout.matches("First").count();
        assert_eq!(first_count, 0);
    }

    #[tokio::test]
    async fn test_git_log_no_commits() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash.exec("cd /repo && git log").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("does not have any commits"));
    }
}

mod workflow {
    use super::*;

    #[tokio::test]
    async fn test_full_workflow() {
        let mut bash = create_git_bash();

        // Initialize repository
        let result = bash.exec("git init /project").await.unwrap();
        assert_eq!(result.exit_code, 0);

        // Create files
        bash.exec("cd /project && echo '# My Project' > README.md")
            .await
            .unwrap();
        bash.exec("cd /project && echo 'fn main() {}' > main.rs")
            .await
            .unwrap();

        // Check status shows untracked
        let status = bash.exec("cd /project && git status").await.unwrap();
        assert!(status.stdout.contains("Untracked files"));

        // Add files
        let result = bash.exec("cd /project && git add .").await.unwrap();
        assert_eq!(result.exit_code, 0);

        // Check status shows staged
        let status = bash.exec("cd /project && git status").await.unwrap();
        assert!(status.stdout.contains("Changes to be committed"));

        // Commit
        let result = bash
            .exec("cd /project && git commit -m 'Initial project setup'")
            .await
            .unwrap();
        assert_eq!(result.exit_code, 0);

        // Status should be clean
        let status = bash.exec("cd /project && git status").await.unwrap();
        assert!(status.stdout.contains("nothing to commit"));

        // Log shows commit
        let log = bash.exec("cd /project && git log").await.unwrap();
        assert!(log.stdout.contains("Initial project setup"));
    }
}

mod error_handling {
    use super::*;

    #[tokio::test]
    async fn test_git_unknown_command() {
        let mut bash = create_git_bash();

        let result = bash.exec("git unknown").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("is not a git command"));
    }

    #[tokio::test]
    async fn test_git_no_subcommand() {
        let mut bash = create_git_bash();

        let result = bash.exec("git").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("usage: git"));
    }

    #[tokio::test]
    async fn test_git_add_nothing() {
        let mut bash = create_git_bash();

        bash.exec("git init /repo").await.unwrap();
        let result = bash.exec("cd /repo && git add").await.unwrap();
        // git add with no args should warn but not fail
        assert!(result.stderr.contains("Nothing specified"));
    }
}

mod not_configured {
    use bashkit::Bash;

    #[tokio::test]
    async fn test_git_not_configured() {
        // Create bash without git configuration
        let mut bash = Bash::new();

        let result = bash.exec("git init /repo").await.unwrap();
        assert_ne!(result.exit_code, 0);
        assert!(result.stderr.contains("not configured"));
    }
}

#[tokio::test]
async fn git_init_options_are_parsed_not_taken_as_directory() {
    let mut bash = create_git_bash();
    let r = bash
        .exec("git init -q /repo; echo \"[$?]\"; ls /repo/.git/HEAD; git init -b main /r2 >/dev/null; cat /r2/.git/HEAD; git init --initial-branch=dev -q /r3; cat /r3/.git/HEAD; ls /home/user")
        .await
        .unwrap();
    assert_eq!(
        r.stdout,
        "[0]\n/repo/.git/HEAD\nref: refs/heads/main\nref: refs/heads/dev\n"
    );
    let r = bash.exec("git init --bare /x").await.unwrap();
    assert_eq!(r.exit_code, 129);
    let r = bash.exec("git init -b 'a b' /y").await.unwrap();
    assert_eq!(r.exit_code, 128);
}

/// Stage-all shorthands agents reach for first (`add -A`, `commit -am`).
mod stage_all {
    use super::*;

    #[tokio::test]
    async fn test_git_add_all_flags_stage_everything() {
        for flag in ["-A", "--all"] {
            let mut bash = create_git_bash();
            let script = format!(
                "mkdir -p /r/d && cd /r && git init -q && echo a > f && echo b > d/g && \
                 git add {flag} && git commit -m init && git ls-files"
            );
            let result = bash.exec(&script).await.unwrap();
            assert_eq!(result.exit_code, 0, "{flag}: {}", result.stderr);
            assert!(result.stdout.contains("d/g"), "{flag}: {}", result.stdout);
            assert!(result.stdout.contains("f\n"), "{flag}: {}", result.stdout);
        }
    }

    #[tokio::test]
    async fn test_git_commit_am_stages_tracked_files_only() {
        let mut bash = create_git_bash();
        let result = bash
            .exec(
                "mkdir -p /r && cd /r && git init -q && echo a > f && git add . && \
                 git commit -q -m init && echo b >> f && echo n > untracked && \
                 git commit -am 'fix: y'",
            )
            .await
            .unwrap();
        assert_eq!(result.exit_code, 0, "{}", result.stderr);
        assert!(result.stdout.contains("fix: y"), "{}", result.stdout);

        let log = bash.exec("cd /r && git log").await.unwrap();
        assert!(log.stdout.contains("fix: y"), "{}", log.stdout);
        let files = bash.exec("cd /r && git ls-files").await.unwrap();
        assert!(!files.stdout.contains("untracked"), "{}", files.stdout);

        // Split flags work the same way.
        let split = bash
            .exec("cd /r && echo c >> f && git commit -a -m 'second'")
            .await
            .unwrap();
        assert_eq!(split.exit_code, 0, "{}", split.stderr);
    }
}

/// `git clone <local path>`: copy a repo that already lives in the VFS.
mod clone_local {
    use super::*;

    const UPSTREAM: &str = "mkdir -p /srv/git/calc/lib && cd /srv/git/calc && git init -q && \
         echo 'mul() { :; }' > lib/calc.sh && ln -s lib/calc.sh entry && \
         git add . && git commit -q -m 'initial import' && cd /";

    #[tokio::test]
    async fn test_git_clone_local_path_copies_tree_and_history() {
        let mut bash = create_git_bash();
        bash.exec(UPSTREAM).await.unwrap();
        let r = bash
            .exec("mkdir -p /work && cd /work && git clone /srv/git/calc")
            .await
            .unwrap();
        assert_eq!(r.exit_code, 0, "{}", r.stderr);
        assert!(r.stderr.contains("Cloning into 'calc'"), "{}", r.stderr);

        let r = bash
            .exec("cd /work/calc && cat lib/calc.sh && readlink entry && git log && git remote -v")
            .await
            .unwrap();
        assert_eq!(r.exit_code, 0, "{}", r.stderr);
        assert!(r.stdout.contains("mul()"), "{}", r.stdout);
        assert!(r.stdout.contains("lib/calc.sh\n"), "{}", r.stdout);
        assert!(r.stdout.contains("initial import"), "{}", r.stdout);
        assert!(r.stdout.contains("origin\t/srv/git/calc"), "{}", r.stdout);

        // The clone is independent of upstream.
        bash.exec("cd /work/calc && echo changed > lib/calc.sh")
            .await
            .unwrap();
        let up = bash.exec("cat /srv/git/calc/lib/calc.sh").await.unwrap();
        assert!(up.stdout.contains("mul()"), "{}", up.stdout);
    }

    #[tokio::test]
    async fn test_git_clone_local_explicit_dest_and_file_url() {
        let mut bash = create_git_bash();
        bash.exec(UPSTREAM).await.unwrap();
        let r = bash
            .exec("git clone -q file:///srv/git/calc /w/c2 && cat /w/c2/lib/calc.sh")
            .await
            .unwrap();
        assert_eq!(r.exit_code, 0, "{}", r.stderr);
        assert!(r.stdout.contains("mul()"), "{}", r.stdout);
    }

    #[tokio::test]
    async fn test_git_clone_local_errors() {
        let mut bash = create_git_bash();
        bash.exec(UPSTREAM).await.unwrap();
        let r = bash.exec("git clone /srv/git/nope /w/x").await.unwrap();
        assert_eq!(r.exit_code, 128);
        assert!(r.stderr.contains("does not exist"), "{}", r.stderr);

        let r = bash
            .exec("mkdir -p /w/full && touch /w/full/f && git clone /srv/git/calc /w/full")
            .await
            .unwrap();
        assert_eq!(r.exit_code, 128);
        assert!(r.stderr.contains("already exists"), "{}", r.stderr);

        let r = bash
            .exec("git clone /srv/git/calc /srv/git/calc/inner")
            .await
            .unwrap();
        assert_eq!(r.exit_code, 128, "{}", r.stderr);
    }
}