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
//! parallel builtin - GNU parallel-lite (virtual stub)
//!
//! Non-standard builtin. Cannot actually parallelize in VFS,
//! so parses options and reports what commands would be run.

use async_trait::async_trait;

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

/// Parallel builtin - GNU parallel-lite stub.
///
/// Usage: parallel [OPTIONS] COMMAND ::: ARGS...
///
/// Options:
///   -j NUM         Number of parallel jobs (default: number of args)
///   --dry-run      Show commands that would be run
///   --keep-order   Keep output in input order (noted in plan)
///   --bar          Show progress bar (noted in plan)
///   -v             Verbose mode
///
/// The `:::` separator delimits the argument list.
/// `{}` in COMMAND is replaced by each argument.
/// Multiple `:::` groups produce the cartesian product.
///
/// Since this is a virtual environment, execution is always dry-run:
/// the builtin parses the invocation and reports the planned commands.
pub struct Parallel;

struct ParallelConfig {
    jobs: Option<u32>,
    dry_run: bool,
    keep_order: bool,
    bar: bool,
    verbose: bool,
    command_parts: Vec<String>,
    arg_groups: Vec<Vec<String>>,
}

fn parse_parallel_args(args: &[String]) -> std::result::Result<ParallelConfig, String> {
    let mut jobs = None;
    let mut dry_run = false;
    let mut keep_order = false;
    let mut bar = false;
    let mut verbose = false;
    let mut command_parts: Vec<String> = Vec::new();
    let mut arg_groups: Vec<Vec<String>> = Vec::new();

    // Split on ::: to find command template and argument groups
    let mut segments: Vec<Vec<String>> = vec![Vec::new()];
    for arg in args {
        if arg == ":::" {
            segments.push(Vec::new());
        } else if let Some(last) = segments.last_mut() {
            last.push(arg.clone());
        }
    }

    // First segment: options + command template
    let first = &segments[0];
    let mut p = super::arg_parser::ArgParser::new(first);
    while !p.is_done() {
        if let Some(val) = p.flag_value("-j", "parallel")? {
            let n: u32 = val
                .parse()
                .map_err(|_| format!("parallel: invalid job count '{}'", val))?;
            if n == 0 {
                return Err("parallel: -j must be at least 1".to_string());
            }
            jobs = Some(n);
        } else if p.flag("--dry-run") {
            dry_run = true;
        } else if p.flag_any(&["--keep-order", "-k"]) {
            keep_order = true;
        } else if p.flag("--bar") {
            bar = true;
        } else if p.flag("-v") {
            verbose = true;
        } else if p.is_flag() && command_parts.is_empty() {
            if let Some(arg) = p.current() {
                return Err(format!("parallel: unknown option '{}'", arg));
            }
            p.advance();
        } else if let Some(arg) = p.positional() {
            command_parts.push(arg.to_string());
        }
    }

    // Remaining segments are argument groups
    for seg in &segments[1..] {
        if seg.is_empty() {
            return Err("parallel: empty argument group after :::".to_string());
        }
        arg_groups.push(seg.clone());
    }

    Ok(ParallelConfig {
        jobs,
        dry_run,
        keep_order,
        bar,
        verbose,
        command_parts,
        arg_groups,
    })
}

/// Maximum number of cartesian product combinations allowed.
/// Prevents exponential memory blowup with many `:::` groups.
const MAX_CARTESIAN_PRODUCT: usize = 100_000;

/// Generate the cartesian product of multiple argument groups.
///
/// Returns an error if the total number of combinations would exceed
/// `MAX_CARTESIAN_PRODUCT` to prevent exponential memory blowup.
fn cartesian_product(groups: &[Vec<String>]) -> std::result::Result<Vec<Vec<String>>, String> {
    if groups.is_empty() {
        return Ok(vec![vec![]]);
    }

    // Pre-calculate total combinations to reject before allocating.
    groups
        .iter()
        .try_fold(1usize, |acc, g| {
            acc.checked_mul(g.len())
                .filter(|&n| n <= MAX_CARTESIAN_PRODUCT)
        })
        .ok_or_else(|| {
            format!(
                "parallel: cartesian product too large (exceeds {MAX_CARTESIAN_PRODUCT} combinations)"
            )
        })?;

    let mut result = vec![vec![]];
    for group in groups {
        let mut new_result = Vec::with_capacity(result.len() * group.len());
        for existing in &result {
            for item in group {
                let mut combo = existing.clone();
                combo.push(item.clone());
                new_result.push(combo);
            }
        }
        result = new_result;
    }
    Ok(result)
}

/// Build a command string by substituting `{}` with the argument.
/// If no `{}` is present, append the argument to the command.
fn build_command(template: &[String], args: &[String]) -> String {
    let template_str = template.join(" ");
    if args.len() == 1 {
        if template_str.contains("{}") {
            template_str.replace("{}", &args[0])
        } else {
            format!("{} {}", template_str, args[0])
        }
    } else {
        // Multiple arg groups: replace {1}, {2}, etc. or append all
        let mut cmd = template_str.clone();
        let mut had_placeholder = false;
        for (idx, arg) in args.iter().enumerate() {
            let placeholder = format!("{{{}}}", idx + 1);
            if cmd.contains(&placeholder) {
                cmd = cmd.replace(&placeholder, arg);
                had_placeholder = true;
            }
        }
        if !had_placeholder {
            if cmd.contains("{}") {
                cmd = cmd.replace("{}", &args.join(" "));
            } else {
                cmd = format!("{} {}", cmd, args.join(" "));
            }
        }
        cmd
    }
}

#[async_trait]
impl Builtin for Parallel {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: parallel [OPTIONS] COMMAND ::: ARGS...\nRun commands in parallel.\n\n  -j NUM\tnumber of parallel jobs\n  --dry-run\tshow commands that would be run\n  --keep-order, -k\tkeep output in input order\n  --bar\tshow progress bar\n  -v\tverbose mode\n  :::\tdelimits the argument list\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("parallel (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        if ctx.args.is_empty() {
            return Ok(ExecResult::err(
                "parallel: usage: parallel [OPTIONS] COMMAND ::: ARGS...\n".to_string(),
                1,
            ));
        }

        let config = match parse_parallel_args(ctx.args) {
            Ok(c) => c,
            Err(e) => return Ok(ExecResult::err(format!("{e}\n"), 1)),
        };

        if config.command_parts.is_empty() {
            return Ok(ExecResult::err(
                "parallel: no command specified\n".to_string(),
                1,
            ));
        }

        if config.arg_groups.is_empty() {
            return Ok(ExecResult::err(
                "parallel: no arguments provided (missing :::)\n".to_string(),
                1,
            ));
        }

        let combinations = match cartesian_product(&config.arg_groups) {
            Ok(c) => c,
            Err(e) => return Ok(ExecResult::err(format!("{e}\n"), 1)),
        };
        let num_commands = combinations.len();
        let effective_jobs = config.jobs.unwrap_or(num_commands as u32);

        let mut output = String::new();

        // Header
        if config.verbose || config.dry_run {
            output.push_str(&format!(
                "parallel: {} command(s), {} job(s)",
                num_commands, effective_jobs,
            ));
            if config.keep_order {
                output.push_str(", ordered output");
            }
            if config.bar {
                output.push_str(", progress bar");
            }
            output.push('\n');
        }

        // List commands
        for combo in &combinations {
            let cmd = build_command(&config.command_parts, combo);
            output.push_str(&cmd);
            output.push('\n');
        }

        if !config.dry_run {
            output.push_str("parallel: not supported in virtual environment\n");
        }

        Ok(ExecResult::ok(output))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::sync::Arc;

    use crate::fs::InMemoryFs;

    async fn run_parallel(args: &[&str]) -> ExecResult {
        let fs = Arc::new(InMemoryFs::new());
        let mut variables = HashMap::new();
        let env = HashMap::new();
        let mut cwd = PathBuf::from("/");
        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs, None);
        Parallel.execute(ctx).await.unwrap()
    }

    #[tokio::test]
    async fn test_no_args() {
        let result = run_parallel(&[]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("usage"));
    }

    #[tokio::test]
    async fn test_basic_command_generation() {
        let result = run_parallel(&["echo", ":::", "a", "b", "c"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("echo a"));
        assert!(result.stdout.contains("echo b"));
        assert!(result.stdout.contains("echo c"));
    }

    #[tokio::test]
    async fn test_placeholder_substitution() {
        let result = run_parallel(&["echo", "hello", "{}", ":::", "world", "test"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("echo hello world"));
        assert!(result.stdout.contains("echo hello test"));
    }

    #[tokio::test]
    async fn test_dry_run_header() {
        let result = run_parallel(&["--dry-run", "echo", ":::", "x", "y"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("2 command(s)"));
        // dry-run should NOT print the "not supported" message
        assert!(!result.stdout.contains("not supported"));
    }

    #[tokio::test]
    async fn test_jobs_option() {
        let result = run_parallel(&["-j", "4", "--dry-run", "echo", ":::", "a", "b"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("4 job(s)"));
    }

    #[tokio::test]
    async fn test_keep_order_flag() {
        let result = run_parallel(&["--keep-order", "--dry-run", "echo", ":::", "x"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("ordered output"));
    }

    #[tokio::test]
    async fn test_bar_flag() {
        let result = run_parallel(&["--bar", "--dry-run", "echo", ":::", "x"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("progress bar"));
    }

    #[tokio::test]
    async fn test_no_command() {
        let result = run_parallel(&[":::", "a", "b"]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("no command"));
    }

    #[tokio::test]
    async fn test_no_separator() {
        let result = run_parallel(&["echo", "hello"]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("no arguments"));
    }

    #[tokio::test]
    async fn test_invalid_jobs() {
        let result = run_parallel(&["-j", "abc", "echo", ":::", "x"]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("invalid job count"));
    }

    #[tokio::test]
    async fn test_zero_jobs() {
        let result = run_parallel(&["-j", "0", "echo", ":::", "x"]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("must be at least 1"));
    }

    #[tokio::test]
    async fn test_missing_j_arg() {
        let result = run_parallel(&["-j"]).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("-j requires an argument"));
    }

    #[tokio::test]
    async fn test_cartesian_product_two_groups() {
        let result = run_parallel(&["echo", "{1}", "{2}", ":::", "a", "b", ":::", "1", "2"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("echo a 1"));
        assert!(result.stdout.contains("echo a 2"));
        assert!(result.stdout.contains("echo b 1"));
        assert!(result.stdout.contains("echo b 2"));
    }

    #[tokio::test]
    async fn test_virtual_env_message() {
        let result = run_parallel(&["echo", ":::", "x"]).await;
        assert_eq!(result.exit_code, 0);
        assert!(
            result
                .stdout
                .contains("not supported in virtual environment")
        );
    }

    #[test]
    fn test_cartesian_product_small() {
        let groups = vec![
            vec!["a".to_string(), "b".to_string()],
            vec!["1".to_string(), "2".to_string()],
        ];
        let result = cartesian_product(&groups).unwrap();
        assert_eq!(result.len(), 4);
        assert!(result.contains(&vec!["a".to_string(), "1".to_string()]));
        assert!(result.contains(&vec!["b".to_string(), "2".to_string()]));
    }

    #[test]
    fn test_cartesian_product_exceeds_limit() {
        // 20 groups of 4 elements each = 4^20 = ~1 trillion combinations
        let groups: Vec<Vec<String>> = (0..20)
            .map(|_| vec!["a".into(), "b".into(), "c".into(), "d".into()])
            .collect();
        let result = cartesian_product(&groups);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("cartesian product too large"));
    }

    #[tokio::test]
    async fn test_cartesian_product_limit_via_builtin() {
        // Build args: echo ::: a b c d ::: a b c d ... (20 groups)
        let mut args: Vec<&str> = vec!["echo"];
        for _ in 0..20 {
            args.push(":::");
            args.extend(["a", "b", "c", "d"]);
        }
        let result = run_parallel(&args).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("cartesian product too large"));
    }
}