plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


3562ffd — Peter John 2026-11-09T12:39:44+05:30
Tests: run the plum test sweep and the Rust test executables in parallel
compiler/cli/src/main.rs CHANGED
@@ -430,7 +430,11 @@ fn cmdTest(
430
430
  // matching `scripts/test-plum.sh`'s old find-and-loop shell logic.
431
431
  if let [only] = paths.as_slice() {
432
432
  if only.is_file() {
433
- let (_, failed) = runTestFile(only, &lib_path, fusion)?;
433
+ let (passed, failed) = runTestFile(only, &lib_path, fusion)?;
434
+ // A sweep's worker (see `runSweepParallel`) reads the counts from this line.
435
+ if std::env::var_os(TEST_CHILD_ENV).is_some() {
436
+ println!("{COUNTS_MARK}{passed} {failed}");
437
+ }
434
438
  if failed > 0 {
435
439
  process::exit(1);
436
440
  }
@@ -445,24 +449,29 @@ fn cmdTest(
445
449
  let mut total_passed = 0usize;
446
450
  let mut total_failed = 0usize;
447
451
  let mut files_failed = 0usize;
452
+ let jobs = testJobs();
453
+ if jobs > 1 && files.len() > 1 {
454
+ (total_passed, total_failed, files_failed) = runSweepParallel(&files, &lib_path, fusion, jobs)?;
455
+ } else {
448
- for file in &files {
456
+ for file in &files {
449
- println!("=== {} ===", file.display());
457
+ println!("=== {} ===", file.display());
450
- match runTestFile(file, &lib_path, fusion) {
458
+ match runTestFile(file, &lib_path, fusion) {
451
- Ok((passed, failed)) => {
459
+ Ok((passed, failed)) => {
452
- total_passed += passed;
460
+ total_passed += passed;
453
- total_failed += failed;
461
+ total_failed += failed;
454
- if failed > 0 {
462
+ if failed > 0 {
463
+ files_failed += 1;
464
+ }
465
+ }
466
+ Err(e) => {
467
+ eprintln!("error: {e:#}");
455
468
  files_failed += 1;
456
469
  }
457
470
  }
458
- Err(e) => {
471
+ println!();
459
- eprintln!("error: {e:#}");
460
- files_failed += 1;
461
- }
462
472
  }
463
- println!();
464
473
  }
465
- let color = io::stdout().is_terminal();
474
+ let color = useColor();
466
475
  let green = |s: &str| {
467
476
  if color {
468
477
  format!("\x1b[32m{s}\x1b[0m")
@@ -497,6 +506,118 @@ fn cmdTest(
497
506
  Ok(())
498
507
  }
499
508
 
509
+ /// Set on a sweep's child `plum test <file>`: it then prints its counts on a `COUNTS_MARK` line.
510
+ const TEST_CHILD_ENV: &str = "PLUM_TEST_CHILD";
511
+ /// Set on a child when the sweep's own stdout is a terminal, so the child colours its output.
512
+ const TEST_COLOR_ENV: &str = "PLUM_TEST_COLOR";
513
+ const COUNTS_MARK: &str = "\u{1}plum-counts ";
514
+
515
+ fn useColor() -> bool {
516
+ std::env::var_os(TEST_COLOR_ENV).is_some() || io::stdout().is_terminal()
517
+ }
518
+
519
+ /// How many files a sweep tests at once: `PLUM_TEST_JOBS`, else one per core.
520
+ fn testJobs() -> usize {
521
+ std::env::var("PLUM_TEST_JOBS")
522
+ .ok()
523
+ .and_then(|v| v.parse().ok())
524
+ .unwrap_or_else(|| std::thread::available_parallelism().map_or(1, |n| n.get()))
525
+ }
526
+
527
+ /// Tests every file in a child `plum test <file>` process, `jobs` at a time, and prints each
528
+ /// file's output in file order as soon as it and every earlier file are done. A child process
529
+ /// (not a thread) per file keeps a test's own output and any host state apart from the others.
530
+ /// Returns (passed, failed, files failed).
531
+ fn runSweepParallel(
532
+ files: &[std::path::PathBuf],
533
+ lib_path: &std::path::Path,
534
+ fusion: plum_driver::PassOptions,
535
+ jobs: usize,
536
+ ) -> Result<(usize, usize, usize)> {
537
+ use std::io::Write;
538
+ use std::sync::atomic::{AtomicUsize, Ordering};
539
+ let exe = std::env::current_exe().context("cannot find the plum executable")?;
540
+ let color = useColor();
541
+ let next = AtomicUsize::new(0);
542
+ let (tx, rx) = std::sync::mpsc::channel::<(usize, std::io::Result<process::Output>)>();
543
+ let (mut passed, mut failed, mut filesFailed) = (0usize, 0usize, 0usize);
544
+ std::thread::scope(|scope| {
545
+ for _ in 0..jobs.min(files.len()) {
546
+ let tx = tx.clone();
547
+ let (next, exe) = (&next, &exe);
548
+ scope.spawn(move || loop {
549
+ let i = next.fetch_add(1, Ordering::Relaxed);
550
+ let Some(file) = files.get(i) else { break };
551
+ let mut cmd = process::Command::new(exe);
552
+ cmd.arg("test").arg("--lib-path").arg(lib_path);
553
+ if !fusion.fusion {
554
+ cmd.arg("--no-fusion");
555
+ }
556
+ if !fusion.fbip {
557
+ cmd.arg("--no-fbip");
558
+ }
559
+ if fusion.explain {
560
+ cmd.arg("--explain-fusion");
561
+ }
562
+ cmd.arg(file).env(TEST_CHILD_ENV, "1").stdin(process::Stdio::null());
563
+ if color {
564
+ cmd.env(TEST_COLOR_ENV, "1");
565
+ }
566
+ if tx.send((i, cmd.output())).is_err() {
567
+ break;
568
+ }
569
+ });
570
+ }
571
+ drop(tx);
572
+ let mut done: Vec<Option<std::io::Result<process::Output>>> = (0..files.len()).map(|_| None).collect();
573
+ let mut shown = 0;
574
+ let (out, err) = (io::stdout(), io::stderr());
575
+ for (i, r) in rx {
576
+ done[i] = Some(r);
577
+ while let Some(r) = done.get_mut(shown).and_then(Option::take) {
578
+ let (mut out, mut err) = (out.lock(), err.lock());
579
+ let _ = writeln!(out, "=== {} ===", files[shown].display());
580
+ match r {
581
+ Ok(o) => {
582
+ let text = String::from_utf8_lossy(&o.stdout);
583
+ let mut counts = None;
584
+ for line in text.lines() {
585
+ match line.strip_prefix(COUNTS_MARK) {
586
+ Some(c) => {
587
+ let mut it = c.split(' ').filter_map(|n| n.parse::<usize>().ok());
588
+ counts = it.next().zip(it.next());
589
+ }
590
+ None => {
591
+ let _ = writeln!(out, "{line}");
592
+ }
593
+ }
594
+ }
595
+ let _ = out.flush();
596
+ let _ = err.write_all(&o.stderr);
597
+ let _ = err.flush();
598
+ match counts {
599
+ Some((p, f)) => {
600
+ passed += p;
601
+ failed += f;
602
+ filesFailed += (f > 0) as usize;
603
+ }
604
+ // No counts: the file failed to compile or load, or the child crashed.
605
+ None => filesFailed += 1,
606
+ }
607
+ }
608
+ Err(e) => {
609
+ let _ = writeln!(err, "error: cannot run {}: {e}", exe.display());
610
+ filesFailed += 1;
611
+ }
612
+ }
613
+ let _ = writeln!(out);
614
+ shown += 1;
615
+ }
616
+ }
617
+ });
618
+ Ok((passed, failed, filesFailed))
619
+ }
620
+
500
621
  /// Runs every `test` block in one file, printing the Spock-flavored per-test
501
622
  /// tree as it goes, and returns `(passed, failed)` counts — shared by both
502
623
  /// the single-file and directory-recursive paths of `cmdTest`.
@@ -531,7 +652,7 @@ fn runTestFile(
531
652
  // Colored green/red like every other test runner's PASS/FAIL — but only
532
653
  // when stdout is a real terminal, so piping `plum test` into a file/CI log
533
654
  // doesn't fill it with raw escape codes.
534
- let color = io::stdout().is_terminal();
655
+ let color = useColor();
535
656
  let green = |s: &str| {
536
657
  if color {
537
658
  format!("\x1b[32m{s}\x1b[0m")
compiler/scripts/test-all.sh CHANGED
@@ -1,8 +1,13 @@
1
1
  #!/usr/bin/env bash
2
2
  # Runs every test: all Rust test executables, then the Plum sweep in three modes.
3
+ # The Rust executables run side by side (TEST_ALL_JOBS at a time, default 4: each one also
4
+ # runs its own tests on several threads; one that fails is rerun alone), and `plum test` tests its files in parallel
5
+ # (PLUM_TEST_JOBS, default one per core). Output is printed in a fixed order.
3
6
  cd "$(dirname "$0")/../.." || exit 1
4
7
  fail=0
5
8
  failed_names=""
9
+ tmp=$(mktemp -d "${TMPDIR:-/tmp}/test-all.XXXXXX") || exit 1
10
+ trap 'rm -rf "$tmp"' EXIT
6
11
 
7
12
  if ! cargo build --workspace --tests; then
8
13
  echo "FAIL build"
@@ -17,17 +22,33 @@ if [ -z "$exes" ]; then
17
22
  exit 1
18
23
  fi
19
24
 
25
+ # One output file and one status file per executable, named by its position in the list.
26
+ i=0
20
27
  for exe in $exes; do
28
+ i=$((i + 1))
29
+ printf '%s\t%s\n' "$i" "$exe"
30
+ done > "$tmp/list"
31
+ cut -f1 "$tmp/list" | xargs -P "${TEST_ALL_JOBS:-4}" -I{} sh -c '
32
+ exe=$(awk -F "\t" -v n="$1" "\$1 == n { print \$2 }" "$2/list")
21
- out=$("$exe" 2>&1)
33
+ "$exe" > "$2/$1.out" 2>&1
34
+ echo $? > "$2/$1.status"
22
- if [ $? -eq 0 ]; then
35
+ ' _ {} "$tmp"
36
+
37
+ # A test that times itself (a speed ratio) can fail only because the others share the CPU:
38
+ # a failed executable is run once more, alone, and counts only if it fails again.
39
+ while IFS=" " read -r n exe; do
40
+ if [ "$(cat "$tmp/$n.status" 2>/dev/null)" = "0" ]; then
23
41
  echo "ok $exe"
42
+ elif "$exe" > "$tmp/$n.alone" 2>&1; then
43
+ echo "ok $exe (failed beside the others, passed alone:$(grep -E '^test .* FAILED$' "$tmp/$n.out" | sed -E 's/^test (.*) \.\.\. FAILED$/ \1/' | tr -d '\n'))"
24
44
  else
45
+ cp "$tmp/$n.alone" "$tmp/$n.out"
25
46
  echo "FAIL $exe"
26
47
  fail=$((fail + 1))
27
48
  failed_names="$failed_names
28
- $(echo "$out" | grep -E '^test .* FAILED$')"
49
+ $(grep -E '^test .* FAILED$' "$tmp/$n.out")"
29
50
  fi
30
- done
51
+ done < "$tmp/list"
31
52
 
32
53
  for flag in "" --no-fbip --no-fusion; do
33
54
  # shellcheck disable=SC2086