zuzu-rust 0.4.0

Rust implementation of ZuzuScript
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
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode, Output};

use zuzu_rust::{module_search_roots, Runtime, RuntimePolicy};

const RUN_ONE_ARG: &str = "--__zuzu-rust-run-one";

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Verbosity {
    Normal,
    Quiet,
    UltraQuiet,
}

#[derive(Clone, Debug)]
struct TestRunnerOptions {
    targets: Vec<PathBuf>,
    include_dirs: Vec<PathBuf>,
    denied_capabilities: Vec<String>,
    denied_modules: Vec<String>,
    debug_level: u32,
    show_help: bool,
    verbosity: Verbosity,
}

impl Default for TestRunnerOptions {
    fn default() -> Self {
        Self {
            targets: Vec::new(),
            include_dirs: Vec::new(),
            denied_capabilities: Vec::new(),
            denied_modules: Vec::new(),
            debug_level: 1,
            show_help: false,
            verbosity: Verbosity::Normal,
        }
    }
}

fn main() -> ExitCode {
    match run() {
        Ok(all_ok) => {
            if all_ok {
                ExitCode::SUCCESS
            } else {
                ExitCode::from(1)
            }
        }
        Err(message) => {
            eprintln!("{message}");
            ExitCode::from(2)
        }
    }
}

fn run() -> Result<bool, String> {
    let args: Vec<String> = env::args().skip(1).collect();
    if args.first().map(String::as_str) == Some(RUN_ONE_ARG) {
        let options = parse_args(&args[1..])?;
        if options.show_help {
            print_help();
            return Ok(true);
        }
        let file = match options.targets.as_slice() {
            [file] => file,
            _ => {
                return Err(format!(
                    "usage: zuzu-rust-run-tests {RUN_ONE_ARG} [options] <test-file>"
                ));
            }
        };
        return run_one(file, &options);
    }

    let options = parse_args(&args)?;
    if options.show_help {
        print_help();
        return Ok(true);
    }
    if options.targets.is_empty() {
        return Err(
            "usage: zuzu-rust-run-tests [options] <test-file-or-directory> [...]".to_owned(),
        );
    }

    let mut files = Vec::new();
    for target in &options.targets {
        collect_targets(target, &mut files)?;
    }
    files.sort();
    files.dedup();

    let mut all_ok = true;
    let mut passed = 0usize;
    let mut failed = 0usize;
    let zuzu = zuzu_binary_path()?;

    for file in files {
        let display = file.display().to_string();
        let output = run_test_child(&file, &zuzu, &options)?;
        if options.verbosity == Verbosity::Normal {
            print!("{}", String::from_utf8_lossy(&output.stdout));
            eprint!("{}", String::from_utf8_lossy(&output.stderr));
        }

        let (file_ok, failure_reason) = test_output_passed(&output);
        if file_ok {
            if options.verbosity != Verbosity::UltraQuiet {
                print_file_result(true, &display, None);
            }
            passed += 1;
        } else {
            if options.verbosity != Verbosity::UltraQuiet {
                print_file_result(false, &display, failure_reason.as_deref());
            }
            failed += 1;
            all_ok = false;
        }
    }

    if options.verbosity == Verbosity::UltraQuiet
        || (options.verbosity == Verbosity::Normal && passed + failed > 1)
    {
        print_total_result(passed, failed);
    }

    Ok(all_ok)
}

fn print_help() {
    println!(
        "\
Usage: zuzu-rust-run-tests [options] <test-file-or-directory> [...]
Options:
  -d[=N]                 set debug level (defaults to -d1)
  -I/path/to/lib         add module include directory
  -q                     quiet: print one result line per test file
  -Q                     ultra-quiet: print one overall result line
  --deny=CAP             deny runtime capability (repeatable or comma-separated)
  --denymodule=MODULE    deny a specific module (repeatable or comma-separated)
  -h, --help             show this help"
    );
}

fn parse_args(args: &[String]) -> Result<TestRunnerOptions, String> {
    let mut options = TestRunnerOptions::default();
    let mut index = 0;
    while index < args.len() {
        let arg = &args[index];
        match arg.as_str() {
            "-h" | "--help" => {
                options.show_help = true;
            }
            "-q" => {
                options.verbosity = Verbosity::Quiet;
            }
            "-Q" => {
                options.verbosity = Verbosity::UltraQuiet;
            }
            "--" => {
                options
                    .targets
                    .extend(args[index + 1..].iter().map(PathBuf::from));
                break;
            }
            "-I" => {
                index += 1;
                options
                    .include_dirs
                    .push(PathBuf::from(expect_arg(args, index, "-I")?));
            }
            "--deny" => {
                index += 1;
                options
                    .denied_capabilities
                    .extend(split_csv(expect_arg(args, index, "--deny")?));
            }
            "--denymodule" => {
                index += 1;
                options
                    .denied_modules
                    .extend(split_csv(expect_arg(args, index, "--denymodule")?));
            }
            "-d" => {
                options.debug_level = 1;
            }
            _ if arg.starts_with("-I") && arg.len() > 2 => {
                options.include_dirs.push(PathBuf::from(&arg[2..]));
            }
            _ if arg.starts_with("-d=") => {
                options.debug_level = parse_debug_level(&arg[3..])?;
            }
            _ if is_debug_short_option(arg) => {
                options.debug_level = parse_debug_level(&arg[2..])?;
            }
            _ if arg.starts_with("--deny=") => {
                options
                    .denied_capabilities
                    .extend(split_csv(&arg["--deny=".len()..]));
            }
            _ if arg.starts_with("--denymodule=") => {
                options
                    .denied_modules
                    .extend(split_csv(&arg["--denymodule=".len()..]));
            }
            _ if arg.starts_with('-') => {
                return Err(format!("unsupported option: {arg}"));
            }
            _ => options.targets.push(PathBuf::from(arg)),
        }
        index += 1;
    }
    Ok(options)
}

fn expect_arg<'a>(args: &'a [String], index: usize, option: &str) -> Result<&'a str, String> {
    args.get(index)
        .map(String::as_str)
        .ok_or_else(|| format!("expected value after {option}"))
}

fn split_csv(value: &str) -> Vec<String> {
    value
        .split(',')
        .map(str::trim)
        .filter(|item| !item.is_empty())
        .map(str::to_owned)
        .collect()
}

fn is_debug_short_option(arg: &str) -> bool {
    let Some(value) = arg.strip_prefix("-d") else {
        return false;
    };
    !value.is_empty() && value.chars().all(|ch| ch.is_ascii_digit())
}

fn parse_debug_level(value: &str) -> Result<u32, String> {
    if value.is_empty() {
        return Ok(1);
    }
    value
        .parse::<u32>()
        .map_err(|_| "debug level must be a non-negative integer".to_owned())
}

fn run_one(file: &Path, options: &TestRunnerOptions) -> Result<bool, String> {
    let runtime = Runtime::with_policy(
        module_search_roots(options.include_dirs.clone()),
        runtime_policy(
            options.denied_capabilities.clone(),
            options.denied_modules.clone(),
        )
        .debug_level(options.debug_level),
    );
    match runtime.run_script_file(file) {
        Ok(output) => Ok(tap_passed(&output.stdout)),
        Err(err) => {
            eprintln!("{err}");
            Ok(false)
        }
    }
}

fn run_test_child(file: &Path, zuzu: &Path, options: &TestRunnerOptions) -> Result<Output, String> {
    let current_exe = env::current_exe().map_err(|err| err.to_string())?;
    let mut command = Command::new(current_exe);
    command.arg(RUN_ONE_ARG);
    for arg in child_runtime_args(options) {
        command.arg(arg);
    }
    command
        .arg(file)
        .env("ZUZU", zuzu)
        .output()
        .map_err(|err| format!("failed to run {}: {err}", file.display()))
}

fn child_runtime_args(options: &TestRunnerOptions) -> Vec<String> {
    let mut args = Vec::new();
    for path in &options.include_dirs {
        args.push("-I".to_owned());
        args.push(path.display().to_string());
    }
    for capability in &options.denied_capabilities {
        args.push("--deny".to_owned());
        args.push(capability.clone());
    }
    for module in &options.denied_modules {
        args.push("--denymodule".to_owned());
        args.push(module.clone());
    }
    args.push(format!("-d{}", options.debug_level));
    args
}

fn zuzu_binary_path() -> Result<PathBuf, String> {
    let current_exe = env::current_exe().map_err(|err| err.to_string())?;
    let mut path = current_exe.to_path_buf();
    path.set_file_name(format!("zuzu-rust{}", env::consts::EXE_SUFFIX));
    if path.is_file() {
        Ok(path)
    } else {
        Err(format!(
            "could not locate zuzu-rust binary next to {}",
            current_exe.display()
        ))
    }
}

fn runtime_policy(denied_capabilities: Vec<String>, denied_modules: Vec<String>) -> RuntimePolicy {
    let mut policy = RuntimePolicy::new();
    for capability in denied_capabilities {
        policy = policy.deny_capability(capability);
    }
    for module in denied_modules {
        policy = policy.deny_module(module);
    }
    policy
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct TapAnalysis {
    passed: bool,
    failure_reason: Option<String>,
}

fn test_output_passed(output: &Output) -> (bool, Option<String>) {
    let analysis = analyse_tap(&String::from_utf8_lossy(&output.stdout));
    (
        output.status.success() && analysis.passed,
        analysis.failure_reason,
    )
}

fn print_file_result(passed: bool, display: &str, failure_reason: Option<&str>) {
    if passed {
        println!("{display}");
    } else if let Some(reason) = failure_reason {
        println!("{display} ({reason})");
    } else {
        println!("{display}");
    }
}

fn print_total_result(passed: usize, failed: usize) {
    if failed == 0 {
        println!("✅  total: {passed} passed, {failed} failed");
    } else {
        println!("❌  total: {passed} passed, {failed} failed");
    }
}

fn tap_passed(stdout: &str) -> bool {
    analyse_tap(stdout).passed
}

fn analyse_tap(stdout: &str) -> TapAnalysis {
    let mut plan = None;
    let mut top_level_tests = 0usize;
    let mut has_top_level_not_ok = false;
    let mut plan_error = None;

    for line in stdout.lines().filter(|line| is_top_level_tap_line(line)) {
        if line.starts_with("1..") {
            match parse_plan_count(line) {
                Some(count) if plan.is_none() => {
                    plan = Some(count);
                }
                Some(_) => {
                    plan_error = Some("bad TAP plan: multiple top-level plans".to_owned());
                }
                None => {
                    plan_error = Some("bad TAP plan: malformed top-level plan".to_owned());
                }
            }
        }

        if is_top_level_ok_line(line) {
            top_level_tests += 1;
        } else if is_top_level_not_ok_line(line) {
            top_level_tests += 1;
            has_top_level_not_ok = true;
        }
    }

    let failure_reason = if let Some(reason) = plan_error {
        Some(reason)
    } else {
        match plan {
            Some(planned) if planned != top_level_tests => Some(format!(
                "bad TAP plan: planned {planned} top-level tests, saw {top_level_tests}"
            )),
            None => Some("bad TAP plan: missing top-level plan".to_owned()),
            _ if has_top_level_not_ok => None,
            _ => None,
        }
    };

    TapAnalysis {
        passed: failure_reason.is_none() && !has_top_level_not_ok,
        failure_reason,
    }
}

fn is_top_level_tap_line(line: &str) -> bool {
    !line.starts_with(|ch: char| ch.is_ascii_whitespace())
}

fn parse_plan_count(line: &str) -> Option<usize> {
    let rest = line.strip_prefix("1..")?;
    let digits_len = rest
        .bytes()
        .take_while(|byte| byte.is_ascii_digit())
        .count();
    if digits_len == 0 {
        return None;
    }
    let (digits, trailing) = rest.split_at(digits_len);
    if !trailing.is_empty()
        && !trailing
            .chars()
            .next()
            .is_some_and(|ch| ch.is_ascii_whitespace() || ch == '#')
    {
        return None;
    }
    digits.parse::<usize>().ok()
}

fn is_top_level_ok_line(line: &str) -> bool {
    has_tap_keyword(line, "ok")
}

fn is_top_level_not_ok_line(line: &str) -> bool {
    has_tap_keyword(line, "not ok")
}

fn has_tap_keyword(line: &str, keyword: &str) -> bool {
    let Some(rest) = line.strip_prefix(keyword) else {
        return false;
    };
    rest.is_empty()
        || rest
            .chars()
            .next()
            .is_some_and(|ch| ch.is_ascii_whitespace())
}

fn collect_targets(path: &Path, out: &mut Vec<PathBuf>) -> Result<(), String> {
    let metadata =
        fs::metadata(path).map_err(|err| format!("failed to stat {}: {err}", path.display()))?;

    if metadata.is_file() {
        if is_test_script(path) {
            out.push(path.to_path_buf());
        }
        return Ok(());
    }

    if metadata.is_dir() {
        collect_dir(path, out)?;
        return Ok(());
    }

    Ok(())
}

fn collect_dir(path: &Path, out: &mut Vec<PathBuf>) -> Result<(), String> {
    let entries = fs::read_dir(path)
        .map_err(|err| format!("failed to read directory {}: {err}", path.display()))?;

    for entry in entries {
        let entry = entry.map_err(|err| {
            format!(
                "failed to read directory entry in {}: {err}",
                path.display()
            )
        })?;
        let child = entry.path();
        let metadata = entry
            .metadata()
            .map_err(|err| format!("failed to stat {}: {err}", child.display()))?;

        if metadata.is_dir() {
            collect_dir(&child, out)?;
        } else if metadata.is_file() && is_test_script(&child) {
            out.push(child);
        }
    }

    Ok(())
}

fn is_test_script(path: &Path) -> bool {
    matches!(path.extension().and_then(|ext| ext.to_str()), Some("zzs"))
}