|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// Owner: Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Stdlib import resolution — `import Argv` from a program that is NOT a |
| 5 | +// sibling of stdlib/Argv.eph. |
| 6 | +// |
| 7 | +// Resolution order (import_resolver::load_program): |
| 8 | +// 1. literal path / module-declaration index under the program's dir, |
| 9 | +// 2. module-declaration index under the stdlib dir |
| 10 | +// (--stdlib flag > $EPHAPAX_STDLIB > repo stdlib/ for dev builds). |
| 11 | +// Local files shadow stdlib modules of the same name (tier order). |
| 12 | + |
| 13 | +use std::path::{Path, PathBuf}; |
| 14 | +use std::process::Command; |
| 15 | + |
| 16 | +fn ephapax_bin() -> String { |
| 17 | + env!("CARGO_BIN_EXE_ephapax").to_string() |
| 18 | +} |
| 19 | + |
| 20 | +fn repo_root() -> PathBuf { |
| 21 | + Path::new(env!("CARGO_MANIFEST_DIR")) |
| 22 | + .join("../..") |
| 23 | + .canonicalize() |
| 24 | + .expect("repo root") |
| 25 | +} |
| 26 | + |
| 27 | +fn fixture() -> PathBuf { |
| 28 | + repo_root().join("tests/v2-grammar/fixtures/stdlib-import/app.eph") |
| 29 | +} |
| 30 | + |
| 31 | +/// The fixture compiles via the dev-default stdlib path (repo stdlib/). |
| 32 | +#[test] |
| 33 | +fn stdlib_import_compiles_via_default_path() { |
| 34 | + let tmp = tempfile::tempdir().expect("tempdir"); |
| 35 | + let out = tmp.path().join("app.wasm"); |
| 36 | + let output = Command::new(ephapax_bin()) |
| 37 | + .args(["compile"]) |
| 38 | + .arg(fixture()) |
| 39 | + .arg("-o") |
| 40 | + .arg(&out) |
| 41 | + .output() |
| 42 | + .expect("spawn ephapax"); |
| 43 | + assert!( |
| 44 | + output.status.success(), |
| 45 | + "compile failed:\n{}{}", |
| 46 | + String::from_utf8_lossy(&output.stdout), |
| 47 | + String::from_utf8_lossy(&output.stderr) |
| 48 | + ); |
| 49 | + let wasm = std::fs::read(&out).expect("read wasm"); |
| 50 | + wasmparser::validate(&wasm).expect("emitted wasm failed validation"); |
| 51 | +} |
| 52 | + |
| 53 | +/// The program works from a directory with NO sibling Argv.eph — the |
| 54 | +/// point of the search path. Copy it to a tempdir and compile there. |
| 55 | +#[test] |
| 56 | +fn stdlib_import_compiles_from_unrelated_directory() { |
| 57 | + let tmp = tempfile::tempdir().expect("tempdir"); |
| 58 | + let src = tmp.path().join("app.eph"); |
| 59 | + std::fs::copy(fixture(), &src).expect("copy fixture"); |
| 60 | + let out = tmp.path().join("app.wasm"); |
| 61 | + let output = Command::new(ephapax_bin()) |
| 62 | + .args(["compile"]) |
| 63 | + .arg(&src) |
| 64 | + .arg("-o") |
| 65 | + .arg(&out) |
| 66 | + .arg("--stdlib") |
| 67 | + .arg(repo_root().join("stdlib")) |
| 68 | + .output() |
| 69 | + .expect("spawn ephapax"); |
| 70 | + assert!( |
| 71 | + output.status.success(), |
| 72 | + "compile failed:\n{}{}", |
| 73 | + String::from_utf8_lossy(&output.stdout), |
| 74 | + String::from_utf8_lossy(&output.stderr) |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +/// An empty --stdlib dir means the import cannot resolve: the search |
| 79 | +/// path must not silently fall back past an explicit flag. |
| 80 | +#[test] |
| 81 | +fn explicit_stdlib_flag_is_authoritative() { |
| 82 | + let tmp = tempfile::tempdir().expect("tempdir"); |
| 83 | + let empty = tmp.path().join("empty-stdlib"); |
| 84 | + std::fs::create_dir(&empty).expect("mkdir"); |
| 85 | + let src = tmp.path().join("app.eph"); |
| 86 | + std::fs::copy(fixture(), &src).expect("copy fixture"); |
| 87 | + let out = tmp.path().join("app.wasm"); |
| 88 | + let output = Command::new(ephapax_bin()) |
| 89 | + .args(["compile"]) |
| 90 | + .arg(&src) |
| 91 | + .arg("-o") |
| 92 | + .arg(&out) |
| 93 | + .arg("--stdlib") |
| 94 | + .arg(&empty) |
| 95 | + .output() |
| 96 | + .expect("spawn ephapax"); |
| 97 | + assert!( |
| 98 | + !output.status.success(), |
| 99 | + "compile unexpectedly succeeded with an empty --stdlib dir" |
| 100 | + ); |
| 101 | +} |
0 commit comments