feat(stdlib): ship a verified bundled stdlib with std: imports
- Move the Compat-A print prototype into stdlib/ as the official offline bundle with stdlib.json (version 0.1.0 + sha256 content digest). - Add std:<relative> import resolution in the loader with bundle path confinement, extension inference and fail-closed digest verification. - Record the verified version/digest/instance_id in Analysis v2 project.stdlib; stdlib modules get a std:name@version#digest instance id. - Add CLI tests: std:io execution, digest-tamper rejection, missing manifest, Analysis identity, no foreign/plugin calls, and frozen binary hash equality. - Update ADR-0028, specs, status, changelog and evidence. Gate: fmt, clippy -D warnings, 415 tests passed.
This commit is contained in:
1 parent
1a978c7a42
commit
1377af1601
28 files changed
+879
-23
No files matched your search
@@ -33,3 +33,6 @@ semver = { workspace = true }
|
||||
[[bin]]
|
||||
name = "marqdo"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
@@ -61,6 +61,11 @@ pub fn build(project: &LoadedProject) -> Value {
|
||||
"root_id": project.identity.root_id(),
|
||||
"identity_kind": project.identity.kind(),
|
||||
"lock_digest": lock_digest(project),
|
||||
"stdlib": project.stdlib.as_ref().map(|stdlib| json!({
|
||||
"version": stdlib.version,
|
||||
"digest": stdlib.digest,
|
||||
"instance_id": stdlib.instance_id,
|
||||
})),
|
||||
},
|
||||
"modules": modules,
|
||||
"diagnostics": diagnostics_json(project),
|
||||
|
||||
@@ -20,6 +20,8 @@ use marqdo_package::{
|
||||
};
|
||||
use marqdo_value::{codes, Diagnostic};
|
||||
|
||||
use crate::stdlib::StdlibBundle;
|
||||
|
||||
/// One analysed source file.
|
||||
pub struct LoadedFile {
|
||||
/// Canonical module identity (package instance + package-relative path).
|
||||
@@ -125,6 +127,17 @@ pub struct LocatedSource<'a> {
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
/// Identity of the verified bundled stdlib used by a load, if any.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StdlibInfo {
|
||||
/// Declared version.
|
||||
pub version: String,
|
||||
/// Verified content digest.
|
||||
pub digest: String,
|
||||
/// Package instance id.
|
||||
pub instance_id: String,
|
||||
}
|
||||
|
||||
/// A fully loaded project ready for checking or running.
|
||||
pub struct LoadedProject {
|
||||
/// Project root.
|
||||
@@ -141,6 +154,8 @@ pub struct LoadedProject {
|
||||
pub packages: BTreeMap<String, PathBuf>,
|
||||
/// All diagnostics in deterministic order.
|
||||
pub diagnostics: Vec<FileDiagnostic>,
|
||||
/// Verified bundled stdlib identity, when one was available.
|
||||
pub stdlib: Option<StdlibInfo>,
|
||||
}
|
||||
|
||||
impl LoadedProject {
|
||||
@@ -205,6 +220,7 @@ struct Loader {
|
||||
program: Program,
|
||||
diagnostics: Vec<FileDiagnostic>,
|
||||
stack: Vec<PathBuf>,
|
||||
stdlib: Option<StdlibBundle>,
|
||||
}
|
||||
|
||||
/// Load the entry file and its explicitly imported modules.
|
||||
@@ -250,6 +266,7 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
};
|
||||
let resolver = PackageResolver::new(root.clone(), manifest.clone(), lock);
|
||||
let entry_module = resolver.resolve_entry(&entry)?;
|
||||
let stdlib = StdlibBundle::discover().map_err(|error| format!("stdlib: {error}"))?;
|
||||
let mut loader = Loader {
|
||||
resolver,
|
||||
packages: BTreeMap::new(),
|
||||
@@ -257,6 +274,7 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
program: Program::default(),
|
||||
diagnostics: Vec::new(),
|
||||
stack: Vec::new(),
|
||||
stdlib,
|
||||
};
|
||||
let entry_id = loader.load_module(&entry_module.package, &entry_module.relative_path)?;
|
||||
loader.program.main_module = Some(entry_id.clone());
|
||||
@@ -270,6 +288,11 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
});
|
||||
}
|
||||
}
|
||||
let stdlib_info = loader.stdlib.as_ref().map(|bundle| StdlibInfo {
|
||||
version: bundle.version().to_string(),
|
||||
digest: bundle.digest().to_string(),
|
||||
instance_id: bundle.instance_id(),
|
||||
});
|
||||
let identity = match &manifest {
|
||||
Some(_) => ProjectIdentity::Manifest {
|
||||
instance: entry_id.instance.clone(),
|
||||
@@ -286,6 +309,7 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
files: loader.files,
|
||||
packages: loader.packages,
|
||||
diagnostics: loader.diagnostics,
|
||||
stdlib: stdlib_info,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -360,7 +384,16 @@ impl Loader {
|
||||
.unwrap_or_default();
|
||||
|
||||
for (alias, target, span) in imports {
|
||||
let resolved = if let Some(alias_name) = target.strip_prefix('@') {
|
||||
let resolved = if let Some(std_target) = target.strip_prefix("std:") {
|
||||
match &self.stdlib {
|
||||
Some(stdlib) => stdlib.resolve(std_target),
|
||||
None => Err(format!(
|
||||
"no bundled stdlib is available for `{target}`; install the official \
|
||||
CLI bundle or set {STDLIB_DIR_ENV}",
|
||||
STDLIB_DIR_ENV = crate::stdlib::STDLIB_DIR_ENV
|
||||
)),
|
||||
}
|
||||
} else if let Some(alias_name) = target.strip_prefix('@') {
|
||||
self.resolver.resolve_alias_from(package, alias_name)
|
||||
} else {
|
||||
self.resolver.resolve_relative(package, &path, &target)
|
||||
|
||||
@@ -9,6 +9,7 @@ mod args;
|
||||
mod commands;
|
||||
mod loader;
|
||||
mod output;
|
||||
mod stdlib;
|
||||
mod tool;
|
||||
mod trace;
|
||||
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
//! Bundled standard library discovery, digest verification and `std:` imports.
|
||||
//!
|
||||
//! The official CLI ships a fixed `marqdo-stdlib` tree (ADR-0026/CM-A10). This
|
||||
//! module finds that tree, verifies its declared content digest before any
|
||||
//! module is loaded, and resolves explicit `std:<relative>` imports to it.
|
||||
//! Nothing is fetched implicitly and there is no second module search path.
|
||||
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
use marqdo_package::{PackageOrigin, ResolvedModule, ResolvedPackage};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
/// Environment override used by tests and relocatable installations.
|
||||
pub const STDLIB_DIR_ENV: &str = "MARQDO_STDLIB_DIR";
|
||||
|
||||
/// Verified bundled standard library.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StdlibBundle {
|
||||
root: PathBuf,
|
||||
name: String,
|
||||
version: String,
|
||||
digest: String,
|
||||
}
|
||||
|
||||
impl StdlibBundle {
|
||||
/// Discover the bundle.
|
||||
///
|
||||
/// Order: `MARQDO_STDLIB_DIR` (explicit, fail if invalid), a `stdlib`
|
||||
/// directory next to the executable, then the development tree.
|
||||
pub fn discover() -> Result<Option<Self>, String> {
|
||||
if let Some(dir) = std::env::var_os(STDLIB_DIR_ENV) {
|
||||
return Self::load(PathBuf::from(dir)).map(Some);
|
||||
}
|
||||
if let Ok(exe) = std::env::current_exe() {
|
||||
if let Some(parent) = exe.parent() {
|
||||
let candidate = parent.join("stdlib");
|
||||
if candidate.is_dir() {
|
||||
return Self::load(candidate).map(Some);
|
||||
}
|
||||
}
|
||||
}
|
||||
let dev = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("..")
|
||||
.join("stdlib");
|
||||
if dev.is_dir() {
|
||||
return Self::load(dev).map(Some);
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Load and verify one bundle directory.
|
||||
pub fn load(root: impl Into<PathBuf>) -> Result<Self, String> {
|
||||
let root = root.into();
|
||||
let root = root.canonicalize().map_err(|error| {
|
||||
format!("cannot open stdlib directory `{}`: {error}", root.display())
|
||||
})?;
|
||||
let manifest_path = root.join("stdlib.json");
|
||||
let text = std::fs::read_to_string(&manifest_path)
|
||||
.map_err(|error| format!("cannot read `{}`: {error}", manifest_path.display()))?;
|
||||
let manifest: serde_json::Value = serde_json::from_str(&text)
|
||||
.map_err(|error| format!("invalid `{}`: {error}", manifest_path.display()))?;
|
||||
let name = manifest
|
||||
.get("name")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| format!("`{}` has no name", manifest_path.display()))?
|
||||
.to_string();
|
||||
let version = manifest
|
||||
.get("version")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| format!("`{}` has no version", manifest_path.display()))?
|
||||
.to_string();
|
||||
let declared = manifest
|
||||
.get("digest")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| format!("`{}` has no digest", manifest_path.display()))?
|
||||
.to_string();
|
||||
let actual = tree_digest(&root)?;
|
||||
if declared != actual {
|
||||
return Err(format!(
|
||||
"stdlib digest mismatch: declared {declared}, actual {actual}"
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
root,
|
||||
name,
|
||||
version,
|
||||
digest: actual,
|
||||
})
|
||||
}
|
||||
|
||||
/// Declared stdlib version.
|
||||
pub fn version(&self) -> &str {
|
||||
&self.version
|
||||
}
|
||||
|
||||
/// Verified content digest (`sha256:<hex>`).
|
||||
pub fn digest(&self) -> &str {
|
||||
&self.digest
|
||||
}
|
||||
|
||||
/// Logical package identity used for module ids.
|
||||
pub fn instance_id(&self) -> String {
|
||||
format!(
|
||||
"std:{}@{}#{}",
|
||||
self.name,
|
||||
self.version,
|
||||
self.digest.trim_start_matches("sha256:")
|
||||
)
|
||||
}
|
||||
|
||||
/// Package view consumed by the loader.
|
||||
pub fn package(&self) -> ResolvedPackage {
|
||||
ResolvedPackage {
|
||||
instance_id: self.instance_id(),
|
||||
name: self.name.clone(),
|
||||
version: self.version.clone(),
|
||||
origin: PackageOrigin::Path {
|
||||
declared: PathBuf::from("stdlib"),
|
||||
},
|
||||
revision: None,
|
||||
content_digest: Some(self.digest.clone()),
|
||||
module_root: self.root.clone(),
|
||||
entry_module: "index.mq.md".to_string(),
|
||||
manifest: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a `std:<relative>` target inside the bundle.
|
||||
pub fn resolve(&self, target: &str) -> Result<ResolvedModule, String> {
|
||||
let mut normalised = PathBuf::new();
|
||||
for component in Path::new(target).components() {
|
||||
match component {
|
||||
Component::Normal(part) => normalised.push(part),
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
|
||||
return Err(format!(
|
||||
"stdlib import `{target}` must be a relative path inside the stdlib"
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
if normalised.as_os_str().is_empty() {
|
||||
return Err("stdlib import must name a module".to_string());
|
||||
}
|
||||
if normalised.extension().is_none() {
|
||||
normalised.set_extension("mq.md");
|
||||
}
|
||||
let candidate = self.root.join(&normalised);
|
||||
let canonical = candidate.canonicalize().map_err(|error| {
|
||||
format!(
|
||||
"cannot open stdlib module `{target}` at `{}`: {error}",
|
||||
candidate.display()
|
||||
)
|
||||
})?;
|
||||
let relative = canonical
|
||||
.strip_prefix(&self.root)
|
||||
.map_err(|_| format!("stdlib import `{target}` escapes the stdlib root"))?;
|
||||
let relative_path = relative
|
||||
.components()
|
||||
.map(|component| component.as_os_str().to_string_lossy().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("/");
|
||||
Ok(ResolvedModule {
|
||||
package: self.package(),
|
||||
relative_path,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Content digest over all `.mq.md` files: sorted path, NUL, bytes, NUL.
|
||||
pub fn tree_digest(root: &Path) -> Result<String, String> {
|
||||
let mut files = Vec::new();
|
||||
collect_markdown(root, root, &mut files)?;
|
||||
files.sort();
|
||||
let mut hasher = Sha256::new();
|
||||
for (relative, path) in files {
|
||||
let bytes = std::fs::read(&path)
|
||||
.map_err(|error| format!("cannot read `{}`: {error}", path.display()))?;
|
||||
hasher.update(relative.as_bytes());
|
||||
hasher.update([0]);
|
||||
hasher.update(&bytes);
|
||||
hasher.update([0]);
|
||||
}
|
||||
Ok(format!("sha256:{}", hex(&hasher.finalize())))
|
||||
}
|
||||
|
||||
fn collect_markdown(
|
||||
root: &Path,
|
||||
dir: &Path,
|
||||
out: &mut Vec<(String, PathBuf)>,
|
||||
) -> Result<(), String> {
|
||||
let entries = std::fs::read_dir(dir)
|
||||
.map_err(|error| format!("cannot read `{}`: {error}", dir.display()))?;
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|error| format!("cannot read `{}`: {error}", dir.display()))?;
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
collect_markdown(root, &path, out)?;
|
||||
} else if path
|
||||
.file_name()
|
||||
.is_some_and(|name| name.to_string_lossy().ends_with(".mq.md"))
|
||||
{
|
||||
let relative = path
|
||||
.strip_prefix(root)
|
||||
.map_err(|_| "stdlib file escaped its root".to_string())?
|
||||
.components()
|
||||
.map(|component| component.as_os_str().to_string_lossy().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("/");
|
||||
out.push((relative, path));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn hex(bytes: &[u8]) -> String {
|
||||
let mut out = String::with_capacity(bytes.len() * 2);
|
||||
for byte in bytes {
|
||||
out.push_str(&format!("{byte:02x}"));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn dev_bundle() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("..")
|
||||
.join("stdlib")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dev_bundle_verifies_and_resolves() {
|
||||
let bundle = StdlibBundle::load(dev_bundle()).unwrap();
|
||||
assert!(bundle.digest.starts_with("sha256:"));
|
||||
let io = bundle.resolve("io").unwrap();
|
||||
assert_eq!(io.relative_path, "io.mq.md");
|
||||
assert!(io.package.instance_id.starts_with("std:marqdo-stdlib@"));
|
||||
assert!(bundle.resolve("../escape").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn digest_tamper_is_rejected() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let source = dev_bundle();
|
||||
for entry in std::fs::read_dir(&source).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
if entry.path().is_file() {
|
||||
std::fs::copy(entry.path(), dir.path().join(entry.file_name())).unwrap();
|
||||
}
|
||||
}
|
||||
std::fs::write(dir.path().join("io.mq.md"), "## tampered\n").unwrap();
|
||||
let error = StdlibBundle::load(dir.path()).unwrap_err();
|
||||
assert!(error.contains("digest mismatch"), "{error}");
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ fn stderr(output: &Output) -> String {
|
||||
|
||||
#[test]
|
||||
fn mqmd_stdlib_print_output_matches_builtin_print() {
|
||||
let output = cli(&["run", "examples/stdlib/demo.mq.md"]);
|
||||
let output = cli(&["run", "stdlib/demo.mq.md"]);
|
||||
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let lines: Vec<&str> = stdout.lines().collect();
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
//! Bundled stdlib (`std:`) and frozen-CLI closure tests (CM-A10).
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
fn repo_root() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.unwrap()
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn binary() -> &'static str {
|
||||
env!("CARGO_BIN_EXE_marqdo")
|
||||
}
|
||||
|
||||
fn stdlib_dir() -> PathBuf {
|
||||
repo_root().join("stdlib")
|
||||
}
|
||||
|
||||
fn run(dir: &Path, stdlib: &Path, args: &[&str]) -> std::process::Output {
|
||||
Command::new(binary())
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.env("MARQDO_STDLIB_DIR", stdlib)
|
||||
.output()
|
||||
.expect("run marqdo")
|
||||
}
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
fn new(tag: &str) -> Self {
|
||||
let path = std::env::temp_dir().join(format!("marqdo-stdlib-{}-{tag}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&path);
|
||||
std::fs::create_dir_all(&path).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_stdlib(source: &Path, target: &Path) {
|
||||
std::fs::create_dir_all(target).unwrap();
|
||||
for entry in std::fs::read_dir(source).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
copy_stdlib(&path, &target.join(entry.file_name()));
|
||||
} else {
|
||||
std::fs::copy(&path, target.join(entry.file_name())).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_project(dir: &Path, program: &str) {
|
||||
std::fs::write(dir.join("index.mq.md"), program).unwrap();
|
||||
}
|
||||
|
||||
const IMPORT_IO: &str = r#"---
|
||||
import:
|
||||
io: "std:io"
|
||||
---
|
||||
|
||||
## main
|
||||
|
||||
> io.写值(["bundle", 1, 2.5])
|
||||
|
||||
*None*
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn std_import_resolves_the_verified_bundle() {
|
||||
let dir = TempDir::new("resolve");
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &stdlib_dir(), &["run", "index.mq.md"]);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
"[\"bundle\", 1, 2.5]\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analysis_records_the_verified_stdlib_digest() {
|
||||
let dir = TempDir::new("analysis");
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let target = dir.path().join("analysis.json");
|
||||
let output = run(
|
||||
dir.path(),
|
||||
&stdlib_dir(),
|
||||
&["check", "index.mq.md", "--emit", target.to_str().unwrap()],
|
||||
);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let analysis: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(&target).unwrap()).unwrap();
|
||||
let manifest: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(stdlib_dir().join("stdlib.json")).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
analysis["project"]["stdlib"]["version"],
|
||||
manifest["version"]
|
||||
);
|
||||
assert_eq!(analysis["project"]["stdlib"]["digest"], manifest["digest"]);
|
||||
assert!(analysis["project"]["stdlib"]["instance_id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("std:marqdo-stdlib@"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_binary_hash_is_unchanged_after_stdlib_execution() {
|
||||
let before = std::fs::read(binary()).unwrap();
|
||||
let before_hash = marqdo_value_sha(&before);
|
||||
let dir = TempDir::new("frozen");
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &stdlib_dir(), &["run", "index.mq.md"]);
|
||||
assert_eq!(output.status.code(), Some(0));
|
||||
let after = std::fs::read(binary()).unwrap();
|
||||
assert_eq!(before_hash, marqdo_value_sha(&after));
|
||||
}
|
||||
|
||||
fn marqdo_value_sha(bytes: &[u8]) -> String {
|
||||
use sha2::{Digest, Sha256};
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
hasher
|
||||
.finalize()
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tampered_bundle_is_rejected_before_module_loading() {
|
||||
let dir = TempDir::new("tamper");
|
||||
let bundle = dir.path().join("stdlib");
|
||||
copy_stdlib(&stdlib_dir(), &bundle);
|
||||
std::fs::write(bundle.join("io.mq.md"), "## tampered\n").unwrap();
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &bundle, &["run", "index.mq.md"]);
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(stderr.contains("stdlib digest mismatch"), "{stderr}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bundle_uses_no_foreign_or_plugin_calls() {
|
||||
fn visit(dir: &Path, offenders: &mut Vec<String>) {
|
||||
for entry in std::fs::read_dir(dir).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
visit(&path, offenders);
|
||||
} else if path.to_string_lossy().ends_with(".mq.md") {
|
||||
let source = std::fs::read_to_string(&path).unwrap();
|
||||
if source.contains("foreign.") || source.contains("plugin.") {
|
||||
offenders.push(path.display().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut offenders = Vec::new();
|
||||
visit(&stdlib_dir(), &mut offenders);
|
||||
assert!(
|
||||
offenders.is_empty(),
|
||||
"stdlib bundle must not depend on foreign/plugin calls: {offenders:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_bundle_manifest_is_a_hard_error() {
|
||||
let dir = TempDir::new("missing");
|
||||
let empty = dir.path().join("empty-stdlib");
|
||||
std::fs::create_dir_all(&empty).unwrap();
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &empty, &["run", "index.mq.md"]);
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(stderr.contains("stdlib"), "{stderr}");
|
||||
}
|
||||
Reference in new issue
Block a user