| @@ -5,20 +5,30 @@ use std::path::{Path, PathBuf}; |
| 5 | 5 | use std::process::ExitCode; |
| 6 | 6 | use std::sync::Arc; |
| 7 | 7 | |
| 8 | +use louise_core::ir::FnId; |
| 8 | 9 | use louise_package as package; |
| 9 | 10 | use louise_span::timings::Timings; |
| 10 | 11 | |
| 11 | 12 | use crate::compile::{Compiled, compile}; |
| 12 | 13 | use crate::run::build_dir; |
| 13 | 14 | |
| 15 | +/// What `louise test` is asked for. |
| 16 | +pub(crate) struct TestOptions { |
| 17 | + /// Compile the tests natively, each in its own process. |
| 18 | + pub(crate) native: bool, |
| 19 | + /// How many tests run at once (default: one per core). |
| 20 | + pub(crate) jobs: Option<usize>, |
| 21 | + /// Print dependencies' warnings too. |
| 22 | + pub(crate) deps_warnings: bool, |
| 23 | +} |
| 24 | + |
| 14 | 25 | pub(crate) fn test( |
| 15 | 26 | path: Option<&Path>, |
| 16 | | − native: bool, |
| 27 | + options: &TestOptions, |
| 17 | 28 | opts: &package::Options, |
| 18 | | − deps_warnings: bool, |
| 19 | 29 | timings: &mut Timings, |
| 20 | 30 | ) -> ExitCode { |
| 21 | | − let compiled = match compile(path, true, opts, timings, deps_warnings) { |
| 31 | + let compiled = match compile(path, true, opts, timings, options.deps_warnings) { |
| 22 | 32 | Ok(compiled) => compiled, |
| 23 | 33 | Err(code) => return code, |
| 24 | 34 | }; |
| @@ -50,62 +60,46 @@ pub(crate) fn test( |
| 50 | 60 | .map(move |t| (m, t)) |
| 51 | 61 | }) |
| 52 | 62 | .collect(); |
| 53 | | − let native_exe = if native { |
| 63 | + let entries: Vec<Result<FnId, String>> = tests |
| 64 | + .iter() |
| 65 | + .map(|(_, t)| louise_core::runnable(&analysis.hir, &analysis.typeck, program, t.def)) |
| 66 | + .collect(); |
| 67 | + let runner = if options.native { |
| 54 | 68 | match build_tests(&compiled, &tests, timings) { |
| 55 | | − Ok(exe) => Some(exe), |
| 69 | + Ok(exe) => Runner::Native(exe), |
| 56 | 70 | Err(code) => return code, |
| 57 | 71 | } |
| 58 | 72 | } else { |
| 59 | | − None |
| 60 | | − }; |
| 61 | | − // The interpreter runs the same optimised Core the native backend |
| 62 | | − // compiles (ADR 0043), optimised once for every test's entry, and |
| 63 | | − // shared by every test (issue #316). |
| 64 | | − let opt_program = if native { |
| 65 | | − None |
| 66 | | − } else { |
| 67 | | − let live: Vec<louise_core::ir::FnId> = tests |
| 68 | | − .iter() |
| 69 | | − .filter_map(|(_, t)| { |
| 70 | | − louise_core::runnable(&analysis.hir, &analysis.typeck, program, t.def).ok() |
| 71 | | − }) |
| 72 | | − .collect(); |
| 73 | + // The interpreter runs the same optimised Core the native backend |
| 74 | + // compiles (ADR 0043), optimised once for every test's entry, and |
| 75 | + // shared by every test (issue #316). |
| 76 | + let live: Vec<FnId> = entries.iter().filter_map(|e| e.clone().ok()).collect(); |
| 73 | 77 | let mut program = program.clone(); |
| 74 | 78 | timings.time("optimise", || louise_core::opt::run_in(&mut program, &live)); |
| 75 | | − Some(Arc::new(program)) |
| 79 | + Runner::Interpreted(Arc::new(program)) |
| 76 | 80 | }; |
| 77 | | − let (mut passed, mut failed) = (0, 0); |
| 81 | + let jobs = options.jobs.unwrap_or_else(louise_build::cores).max(1); |
| 78 | 82 | let started = std::time::Instant::now(); |
| 79 | | − for (index, (module, t)) in tests.iter().enumerate() { |
| 80 | | − { |
| 81 | | − let name = &t.name; |
| 82 | | − let outcome = louise_core::runnable(&analysis.hir, &analysis.typeck, program, t.def) |
| 83 | | − .and_then(|f| { |
| 84 | | − let ended = match &native_exe { |
| 85 | | − Some(exe) => run_native_test(exe, index), |
| 86 | | − None => Ended::of(louise_interp::run( |
| 87 | | − Arc::clone( |
| 88 | | − opt_program.as_ref().expect("optimised for the interpreter"), |
| 89 | | − ), |
| 90 | | − f, |
| 91 | | − Box::new(io::stdout()), |
| 92 | | − )), |
| 93 | | − }; |
| 94 | | − ended.verdict(t.should_panic.as_deref()) |
| 95 | | − }); |
| 96 | | − match outcome { |
| 97 | | − Ok(()) => { |
| 98 | | − passed += 1; |
| 99 | | − println!("test {module}.{name} ... ok"); |
| 100 | | − } |
| 101 | | − Err(reason) => { |
| 102 | | − failed += 1; |
| 103 | | − println!("test {module}.{name} ... FAILED: {reason}"); |
| 104 | | − } |
| 83 | + let mut printer = Printer::default(); |
| 84 | + louise_build::each_in_parallel( |
| 85 | + (0..tests.len()).collect(), |
| 86 | + jobs, |
| 87 | + |index| { |
| 88 | + let (_, t) = &tests[index]; |
| 89 | + match &entries[index] { |
| 90 | + Ok(entry) => runner.run(index, *entry, jobs, t.should_panic.as_deref()), |
| 91 | + Err(reason) => Report::failed(reason.clone()), |
| 105 | 92 | } |
| 106 | | − } |
| 107 | | − } |
| 93 | + }, |
| 94 | + |index, report| { |
| 95 | + printer.done(index, report, |i| { |
| 96 | + let (module, t) = &tests[i]; |
| 97 | + format!("{module}.{}", t.name) |
| 98 | + }) |
| 99 | + }, |
| 100 | + ); |
| 108 | 101 | timings.add("run", started.elapsed()); |
| 102 | + let Printer { passed, failed, .. } = printer; |
| 109 | 103 | println!("\n{passed} passed, {failed} failed"); |
| 110 | 104 | if failed > 0 { |
| 111 | 105 | ExitCode::FAILURE |
| @@ -114,6 +108,116 @@ pub(crate) fn test( |
| 114 | 108 | } |
| 115 | 109 | } |
| 116 | 110 | |
| 111 | +/// How tests run: in the interpreter, sharing one optimised program, or |
| 112 | +/// natively, each in its own process of one test executable. |
| 113 | +enum Runner { |
| 114 | + Interpreted(Arc<louise_core::ir::Program>), |
| 115 | + Native(PathBuf), |
| 116 | +} |
| 117 | + |
| 118 | +impl Runner { |
| 119 | + /// Run test `index` (`entry`, natively the executable's `index`th), |
| 120 | + /// `jobs` tests running at once. |
| 121 | + fn run(&self, index: usize, entry: FnId, jobs: usize, should_panic: Option<&str>) -> Report { |
| 122 | + let (ended, stdout, stderr) = match self { |
| 123 | + Runner::Interpreted(program) => { |
| 124 | + let (out, err) = (Capture::default(), Capture::default()); |
| 125 | + let ended = Ended::of(louise_interp::run_with( |
| 126 | + Arc::clone(program), |
| 127 | + entry, |
| 128 | + louise_interp::RunOptions { |
| 129 | + out: Box::new(out.clone()), |
| 130 | + err: Box::new(err.clone()), |
| 131 | + exit_ends_run: true, |
| 132 | + ..louise_interp::RunOptions::new(Box::new(io::sink())) |
| 133 | + }, |
| 134 | + )); |
| 135 | + (ended, out.take(), err.take()) |
| 136 | + } |
| 137 | + Runner::Native(exe) => run_native_test(exe, index, jobs), |
| 138 | + }; |
| 139 | + Report { |
| 140 | + stdout, |
| 141 | + stderr, |
| 142 | + verdict: ended.verdict(should_panic), |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/// A test's output and verdict. |
| 148 | +struct Report { |
| 149 | + stdout: Vec<u8>, |
| 150 | + stderr: Vec<u8>, |
| 151 | + verdict: Result<(), String>, |
| 152 | +} |
| 153 | + |
| 154 | +impl Report { |
| 155 | + fn failed(reason: String) -> Report { |
| 156 | + Report { |
| 157 | + stdout: Vec::new(), |
| 158 | + stderr: Vec::new(), |
| 159 | + verdict: Err(reason), |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// Prints reports in the tests' order, as a serial run would, whatever |
| 165 | +/// order they finish in: each test's output, then its result line. |
| 166 | +#[derive(Default)] |
| 167 | +struct Printer { |
| 168 | + waiting: std::collections::BTreeMap<usize, Report>, |
| 169 | + next: usize, |
| 170 | + passed: usize, |
| 171 | + failed: usize, |
| 172 | +} |
| 173 | + |
| 174 | +impl Printer { |
| 175 | + fn done(&mut self, index: usize, report: Report, name: impl Fn(usize) -> String) { |
| 176 | + use std::io::Write; |
| 177 | + self.waiting.insert(index, report); |
| 178 | + while let Some(report) = self.waiting.remove(&self.next) { |
| 179 | + let _ = io::stdout().write_all(&report.stdout); |
| 180 | + let _ = io::stderr().write_all(&report.stderr); |
| 181 | + let name = name(self.next); |
| 182 | + match report.verdict { |
| 183 | + Ok(()) => { |
| 184 | + self.passed += 1; |
| 185 | + println!("test {name} ... ok"); |
| 186 | + } |
| 187 | + Err(reason) => { |
| 188 | + self.failed += 1; |
| 189 | + println!("test {name} ... FAILED: {reason}"); |
| 190 | + } |
| 191 | + } |
| 192 | + self.next += 1; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +/// A test's output, written by the interpreter's thread. |
| 198 | +#[derive(Clone, Default)] |
| 199 | +struct Capture(Arc<std::sync::Mutex<Vec<u8>>>); |
| 200 | + |
| 201 | +impl Capture { |
| 202 | + fn take(&self) -> Vec<u8> { |
| 203 | + std::mem::take(&mut self.0.lock().unwrap_or_else(|e| e.into_inner())) |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +impl io::Write for Capture { |
| 208 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 209 | + self.0 |
| 210 | + .lock() |
| 211 | + .unwrap_or_else(|e| e.into_inner()) |
| 212 | + .extend_from_slice(buf); |
| 213 | + Ok(buf.len()) |
| 214 | + } |
| 215 | + |
| 216 | + fn flush(&mut self) -> io::Result<()> { |
| 217 | + Ok(()) |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 117 | 221 | /// Whether the module `path` belongs to the dependency `name`: its modules |
| 118 | 222 | /// are named `name` (its `src/lib.lse`) and `name/...`. |
| 119 | 223 | fn in_package(path: &str, name: &str) -> bool { |
| @@ -206,30 +310,41 @@ impl Ended { |
| 206 | 310 | } |
| 207 | 311 | |
| 208 | 312 | /// Run test `index` of a test executable, selected by `LOUISE_ENTRY` |
| 209 | | −/// (argv is the program's own, issue #287). The runtime reports a panic as |
| 210 | | −/// `error: panic: <message>` on stderr; a test that fails without one |
| 211 | | −/// crashed, which `@should_panic` does not accept. |
| 212 | | −fn run_native_test(exe: &Path, index: usize) -> Ended { |
| 213 | | − use std::io::Write; |
| 214 | | − let out = match std::process::Command::new(exe) |
| 215 | | − .env("LOUISE_ENTRY", index.to_string()) |
| 216 | | − .output() |
| 217 | | − { |
| 313 | +/// (argv is the program's own, issue #287), `jobs` tests running at once: |
| 314 | +/// unless `LOUISE_THREADS` says otherwise, each process gets its share of |
| 315 | +/// the cores. The runtime reports a panic as `error: panic: <message>` on |
| 316 | +/// stderr; a test that fails without one crashed, which `@should_panic` |
| 317 | +/// does not accept. Returns how it ended and its stdout (its stderr is the |
| 318 | +/// runtime's report). |
| 319 | +fn run_native_test(exe: &Path, index: usize, jobs: usize) -> (Ended, Vec<u8>, Vec<u8>) { |
| 320 | + let mut command = std::process::Command::new(exe); |
| 321 | + command.env("LOUISE_ENTRY", index.to_string()); |
| 322 | + if std::env::var_os("LOUISE_THREADS").is_none() && jobs > 1 { |
| 323 | + let share = (louise_build::cores() / jobs).max(1); |
| 324 | + command.env("LOUISE_THREADS", share.to_string()); |
| 325 | + } |
| 326 | + let out = match command.output() { |
| 218 | 327 | Ok(out) => out, |
| 219 | | − Err(e) => return Ended::Failed(format!("cannot run the tests: {e}")), |
| 328 | + Err(e) => { |
| 329 | + return ( |
| 330 | + Ended::Failed(format!("cannot run the tests: {e}")), |
| 331 | + Vec::new(), |
| 332 | + Vec::new(), |
| 333 | + ); |
| 334 | + } |
| 220 | 335 | }; |
| 221 | | − let _ = io::stdout().write_all(&out.stdout); |
| 222 | 336 | if out.status.success() { |
| 223 | | − return Ended::Returned; |
| 337 | + return (Ended::Returned, out.stdout, Vec::new()); |
| 224 | 338 | } |
| 225 | 339 | let stderr = String::from_utf8_lossy(&out.stderr); |
| 226 | | − match stderr |
| 340 | + let ended = match stderr |
| 227 | 341 | .lines() |
| 228 | 342 | .find_map(|l| l.strip_prefix("error: panic: ")) |
| 229 | 343 | { |
| 230 | 344 | Some(message) => Ended::Panicked(message.to_owned()), |
| 231 | 345 | None => Ended::Failed("the test process failed".to_owned()), |
| 232 | | − } |
| 346 | + }; |
| 347 | + (ended, out.stdout, Vec::new()) |
| 233 | 348 | } |
| 234 | 349 | |
| 235 | 350 | #[cfg(test)] |