Files
marqdo-cli/crates/cli/tests/package_env.rs
T
cflmy 72f57beee0
ci/woodpecker/push/main Pipeline failed
ci/woodpecker/pr/main Pipeline failed
refactor: accept the ecosystem stdlib Bundle and remove the in-repo sources
Phase B of ADR-0070 (issue #11): the CLI no longer owns standard-library
sources. The pin names the accepted ecosystem Bundle (marqdo-stdlib@0.2.0,
root commit + tree/alias digests); tools/ci/fetch_ecosystem_bundle.py
materializes it from the pinned root commit and stdlib gitlinks and verifies
the digests. bundle.py and the test harness require an explicit verified Bundle
for the accepted identity, and runtime discovery has no compile-time source
fallback in any profile.

Delete stdlib/ (modules, self-tests, aliases manifest). Module and domain tests
stay with their component owners; the CLI product lane now verifies the
external Bundle's public surface, std: resolution and official print/打印 alias
routing (tests/product-stdlib). Composition assertions whose owner is the
ecosystem (Table split, print-format equivalence) moved to the root Bundle
acceptance; the generic TCP round-trip stays a CLI test. Spawned-CLI tests name
the verified Bundle explicitly.

CI (Woodpecker + GitHub) materializes the pinned Bundle before product tests,
packaging and performance lanes; packaging and release take --stdlib-dir from
that artifact. Docs, STATUS, ADR-0070 and a Phase B evidence record updated.

Refs cflmy/marqdo#9, cflmy/marqdo-cli#11
2026-11-27 01:04:01 +08:00

471 lines
15 KiB
Rust

//! Package instance identity, transitive imports, pinned Git and environment
//! materialization (M0.6 R1/R5).
#![cfg(unix)]
#[path = "support/stdlib.rs"]
pub mod stdlib_support;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::atomic::{AtomicU32, Ordering};
static COUNTER: AtomicU32 = AtomicU32::new(0);
struct TempDir(PathBuf);
impl TempDir {
fn new(tag: &str) -> Self {
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"marqdo-package-test-{}-{id}-{tag}",
std::process::id()
));
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 cli(dir: &Path, args: &[&str]) -> Output {
stdlib_support::cli_command()
.args(args)
.current_dir(dir)
.output()
.expect("run marqdo")
}
fn write(path: &Path, text: &str) {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
std::fs::write(path, text).unwrap();
}
fn have_git() -> bool {
Command::new("git")
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
fn git(dir: &Path, args: &[&str]) {
let status = Command::new("git")
.args(args)
.current_dir(dir)
.env("GIT_AUTHOR_NAME", "test")
.env("GIT_AUTHOR_EMAIL", "test@example.invalid")
.env("GIT_COMMITTER_NAME", "test")
.env("GIT_COMMITTER_EMAIL", "test@example.invalid")
.status()
.expect("git");
assert!(status.success(), "git {args:?} failed");
}
#[test]
fn transitive_path_dependency_resolves_from_its_own_manifest() {
let dir = TempDir::new("transitive");
let root = dir.path();
write(
&root.join("deps/a/marqdo.yaml"),
"project:\n name: a\n version: 0.1.0\ndependencies:\n b:\n path: ./b\n",
);
write(
&root.join("deps/a/index.mq.md"),
"---\nimport:\n b: \"@b\"\n---\n\n## @exec outer\n\n`return b.inner()`\n",
);
write(
&root.join("deps/a/b/marqdo.yaml"),
"project:\n name: b\n version: 0.1.0\n",
);
write(
&root.join("deps/a/b/index.mq.md"),
"## @exec inner\n\n`return 42`\n",
);
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n a:\n path: ./deps/a\n",
);
write(
&root.join("index.mq.md"),
"---\nimport:\n a: \"@a\"\n---\n\n## @exec main\n\n`打印(a.outer())`\n",
);
let lock = cli(root, &["lock"]);
assert_eq!(
lock.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&lock.stderr)
);
let output = cli(root, &["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), "42\n");
assert!(
!root.join(".marqdo").exists(),
"run must not materialize env"
);
}
#[test]
fn pinned_git_dependency_locks_syncs_and_runs() {
if !have_git() {
eprintln!("skipping: git is unavailable");
return;
}
let dir = TempDir::new("git");
let root = dir.path().join("project");
let dep = dir.path().join("dep");
std::fs::create_dir_all(&root).unwrap();
std::fs::create_dir_all(&dep).unwrap();
write(
&dep.join("marqdo.yaml"),
"project:\n name: remote\n version: 0.3.0\n",
);
write(&dep.join("index.mq.md"), "## @exec hello\n\n`return 7`\n");
git(&dep, &["init", "-q"]);
git(&dep, &["add", "."]);
git(&dep, &["commit", "-q", "-m", "initial"]);
let rev = String::from_utf8(
Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(&dep)
.output()
.unwrap()
.stdout,
)
.unwrap();
write(
&root.join("marqdo.yaml"),
&format!(
"project:\n name: root\n version: 0.1.0\ndependencies:\n remote:\n git: {}\n rev: {}\n",
dep.display(),
rev.trim()
),
);
write(
&root.join("index.mq.md"),
"---\nimport:\n remote: \"@remote\"\n---\n\n## @exec main\n\n`打印(remote.hello())`\n",
);
let lock = cli(&root, &["lock"]);
assert_eq!(
lock.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&lock.stderr)
);
let lock_text = std::fs::read_to_string(root.join("marqdo.lock")).unwrap();
assert!(lock_text.contains("lock_version: 4"));
assert!(lock_text.contains("root_dependencies:"));
assert!(lock_text.contains("instance_id: dep:remote@0.3.0#"));
assert!(lock_text.contains("kind: git"));
assert!(lock_text.contains("content_digest: "));
let sync = cli(&root, &["sync", "--locked"]);
assert_eq!(
sync.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&sync.stderr)
);
let env_json = std::fs::read_to_string(root.join(".marqdo/env/env.json")).unwrap();
assert!(env_json.contains("dep:remote@0.3.0#"));
let run = cli(&root, &["run", "index.mq.md"]);
assert_eq!(
run.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&run.stderr)
);
assert_eq!(String::from_utf8_lossy(&run.stdout), "7\n");
}
#[test]
fn check_does_not_fetch_a_missing_git_revision() {
if !have_git() {
eprintln!("skipping: git is unavailable");
return;
}
let dir = TempDir::new("git-missing");
let root = dir.path();
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n remote:\n git: /nonexistent/repo\n rev: 0000000000000000000000000000000000000000\n",
);
write(
&root.join("index.mq.md"),
"---\nimport:\n remote: \"@remote\"\n---\n\n## @exec main\n\n`return 1`\n",
);
let output = cli(root, &["check", "index.mq.md"]);
assert_eq!(output.status.code(), Some(1));
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("marqdo.lock is missing"), "{stderr}");
assert!(!root.join(".marqdo/cache/git").exists());
}
#[test]
fn run_and_check_require_a_lock_when_dependencies_exist() {
let dir = TempDir::new("no-lock");
let root = dir.path();
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
);
write(
&root.join("dep/index.mq.md"),
"## @exec value\n\n`return 1`\n",
);
write(
&root.join("index.mq.md"),
"---\nimport:\n dep: \"@dep\"\n---\n\n## @exec main\n\n`return 1`\n",
);
for command in ["check", "run"] {
let output = cli(root, &[command, "index.mq.md"]);
assert_eq!(output.status.code(), Some(1), "{command}");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("marqdo.lock is missing"), "{stderr}");
}
}
#[test]
fn sync_is_strict_and_never_rewrites_the_lock() {
let dir = TempDir::new("strict-sync");
let root = dir.path();
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
);
write(
&root.join("dep/index.mq.md"),
"## @exec value\n\n`return 1`\n",
);
write(&root.join("index.mq.md"), "## @exec main\n\n`return 1`\n");
let lock = cli(root, &["lock"]);
assert_eq!(lock.status.code(), Some(0));
let original = std::fs::read(root.join("marqdo.lock")).unwrap();
write(
&root.join("dep/index.mq.md"),
"## @exec value\n\n`return 2`\n",
);
let stale = cli(root, &["sync"]);
assert_eq!(stale.status.code(), Some(1), "plain sync must be strict");
let stderr = String::from_utf8_lossy(&stale.stderr);
assert!(stderr.contains("lockfile is out of date"), "{stderr}");
assert_eq!(
std::fs::read(root.join("marqdo.lock")).unwrap(),
original,
"sync must never rewrite the lock"
);
let relock = cli(root, &["lock"]);
assert_eq!(relock.status.code(), Some(0));
let after_relock = std::fs::read(root.join("marqdo.lock")).unwrap();
let synced = cli(root, &["sync"]);
assert_eq!(synced.status.code(), Some(0));
assert_eq!(
std::fs::read(root.join("marqdo.lock")).unwrap(),
after_relock
);
}
#[test]
fn lock_records_transitive_closure_and_sync_materializes_it() {
let dir = TempDir::new("closure");
let root = dir.path();
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n a:\n path: ./deps/a\n",
);
write(
&root.join("deps/a/marqdo.yaml"),
"project:\n name: a\n version: 0.1.0\ndependencies:\n b:\n path: ./b\n",
);
write(
&root.join("deps/a/index.mq.md"),
"## @exec a\n\n`return 1`\n",
);
write(
&root.join("deps/a/b/marqdo.yaml"),
"project:\n name: b\n version: 0.1.0\n",
);
write(
&root.join("deps/a/b/index.mq.md"),
"## @exec b\n\n`return 2`\n",
);
write(&root.join("index.mq.md"), "## @exec main\n\n`return 1`\n");
let lock = cli(root, &["lock"]);
assert_eq!(lock.status.code(), Some(0));
let lock_text = std::fs::read_to_string(root.join("marqdo.lock")).unwrap();
assert!(lock_text.contains("name: a"), "{lock_text}");
assert!(lock_text.contains("name: b"), "{lock_text}");
assert!(lock_text.contains("b: dep:b"), "{lock_text}");
let sync = cli(root, &["sync", "--locked"]);
assert_eq!(
sync.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&sync.stderr)
);
let env: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(root.join(".marqdo/env/env.json")).unwrap())
.unwrap();
assert_eq!(env["schema_version"], 2);
assert_eq!(
env["packages"].as_array().map(|packages| packages.len()),
Some(2)
);
assert!(env["lock_digest"].as_str().unwrap().len() == 64);
}
#[test]
fn deleted_lock_edge_fails_check_and_run() {
let dir = TempDir::new("missing-edge");
let root = dir.path();
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
);
write(
&root.join("dep/index.mq.md"),
"## @exec value\n\n`return 1`\n",
);
write(
&root.join("index.mq.md"),
"---\nimport:\n dep: \"@dep\"\n---\n\n## @exec main\n\n`return 1`\n",
);
assert_eq!(cli(root, &["lock"]).status.code(), Some(0));
let lock = std::fs::read_to_string(root.join("marqdo.lock")).unwrap();
let head = &lock[..lock.find("root_dependencies:").unwrap()];
write(
&root.join("marqdo.lock"),
&format!("{head}root_dependencies: {{}}\n"),
);
for command in ["check", "run"] {
let output = cli(root, &[command, "index.mq.md"]);
assert_eq!(output.status.code(), Some(1), "{command}");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("not recorded in marqdo.lock")
|| stderr.contains("not reachable from root_dependencies"),
"{stderr}"
);
}
}
#[test]
fn tampered_path_dependency_content_is_rejected_by_locked_sync() {
let dir = TempDir::new("path-tamper");
let root = dir.path();
write(
&root.join("marqdo.yaml"),
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
);
write(
&root.join("dep/marqdo.yaml"),
"project:\n name: dep\n version: 0.1.0\n",
);
write(
&root.join("dep/index.mq.md"),
"## @exec value\n\n`return 1`\n",
);
write(
&root.join("index.mq.md"),
"---\nimport:\n dep: \"@dep\"\n---\n\n## @exec main\n\n`return 1`\n",
);
assert_eq!(cli(root, &["lock"]).status.code(), Some(0));
let lock_before = std::fs::read(root.join("marqdo.lock")).unwrap();
assert_eq!(cli(root, &["sync", "--locked"]).status.code(), Some(0));
let env_before = std::fs::read(root.join(".marqdo/env/env.json")).unwrap();
// Content change after the lock is a tamper/staleness signal. `run` and
// `check` must never silently accept it, re-lock, or materialize a new
// environment.
write(
&root.join("dep/index.mq.md"),
"## @exec value\n\n`return 2`\n",
);
for args in [
vec!["sync", "--locked"],
vec!["run", "index.mq.md"],
vec!["check", "index.mq.md"],
] {
let output = cli(root, &args);
assert_eq!(
output.status.code(),
Some(1),
"{args:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("out of date")
|| stderr.contains("digest")
|| stderr.contains("integrity"),
"{args:?}: {stderr}"
);
}
assert_eq!(
std::fs::read(root.join("marqdo.lock")).unwrap(),
lock_before,
"a failed locked operation must not rewrite the lock"
);
assert_eq!(
std::fs::read(root.join(".marqdo/env/env.json")).unwrap(),
env_before,
"a failed locked operation must not replace the materialized environment"
);
}
#[test]
fn project_marqdo_range_applies_to_the_running_core() {
let dir = TempDir::new("runtime-range");
let root = dir.path();
let core = env!("CARGO_PKG_VERSION");
write(&root.join("index.mq.md"), "## @exec main\n\n`return 1`\n");
for (range, compatible) in [
(">=3.0.0-beta.1,<4.0.0".to_string(), true),
(format!("={core}"), true),
(">=3.0.0,<4.0.0".to_string(), !core.contains('-')),
(">=0.1,<1".to_string(), false),
] {
write(
&root.join("marqdo.yaml"),
&format!("project:\n name: runtime-range\n version: 0.1.0\n marqdo: \"{range}\"\n"),
);
for command in ["check", "run"] {
let output = cli(root, &[command, "index.mq.md"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(
output.status.success(),
compatible,
"{command}: {range} on {core}: {stderr}"
);
if !compatible {
assert!(stderr.contains("package.marqdo_incompatible"), "{stderr}");
assert!(
stderr.contains(core),
"diagnostic must name the running core: {stderr}"
);
}
}
}
}