use super::*;
const VARIABLE: &str = "LOCAL_LIBS";
const CARGO_TEMPLATE: &str = r#"[package]
name = "floom-engine"
[dependencies]
anyhow = "1.0"
[dependencies.capdag]
{%- if env.LOCAL_LIBS %}
# LOCAL_LIBS was set when this was rendered: the working copy in this
# workspace rather than a published tag. Re-render without it before
# committing or releasing — a path resolves on this machine only.
path = "{{ projects.capdag_rs.path_from_here }}"
{%- else %}
git = "https://github.com/machinefabric/capdag-rs"
tag = "v{{ projects.capdag_rs.latest_remote_version }}"
{%- endif %}
# Capture-capable host/cartridge: builds the device backends (avdevice).
features = ["capture"]
[dependencies.modelcartridge]
path = "{{ projects.modelcartridge.path_from_here }}"
"#;
const CARGO_COMMITTED: &str = r#"[package]
name = "floom-engine"
[dependencies]
anyhow = "1.0"
[dependencies.capdag]
git = "https://github.com/machinefabric/capdag-rs"
tag = "v1.710.0"
# Capture-capable host/cartridge: builds the device backends (avdevice).
features = ["capture"]
[dependencies.modelcartridge]
path = "../../cartridges/modelcartridge"
"#;
const CARGO_LOCAL: &str = r#"[package]
name = "floom-engine"
[dependencies]
anyhow = "1.0"
[dependencies.capdag]
# LOCAL_LIBS was set when this was rendered: the working copy in this
# workspace rather than a published tag. Re-render without it before
# committing or releasing — a path resolves on this machine only.
path = "../../capdag/capdag-rs"
# Capture-capable host/cartridge: builds the device backends (avdevice).
features = ["capture"]
[dependencies.modelcartridge]
path = "../../cartridges/modelcartridge"
"#;
const NPM_TEMPLATE: &str = r#"{
"name": "unifloom",
"dependencies": {
"capdag": {% if env.LOCAL_LIBS %}"file:{{ projects.capdag_js.path_from_here }}"{% else %}"^{{ projects.capdag_js.latest_remote_version }}"{% endif %},
"monaco-editor": "^0.55.1"
}
}
"#;
const NPM_COMMITTED: &str = r#"{
"name": "unifloom",
"dependencies": {
"capdag": "^1.520.4",
"monaco-editor": "^0.55.1"
}
}
"#;
const NPM_LOCAL: &str = r#"{
"name": "unifloom",
"dependencies": {
"capdag": "file:../../capdag/capdag-js",
"monaco-editor": "^0.55.1"
}
}
"#;
const GO_TEMPLATE: &str = r#"module github.com/machinefabric/capdag-interop-relay-host-go
go 1.21
require github.com/machinefabric/capdag-go v{% if env.LOCAL_LIBS %}0.0.0{% else %}{{ projects.capdag_go.latest_remote_version }}{% endif %}
require (
github.com/google/uuid v1.6.0 // indirect
)
{%- if env.LOCAL_LIBS %}
// LOCAL_LIBS: the working copy in this workspace rather than a published
// tag. The require above carries the v0.0.0 placeholder a replaced module
// takes. Re-render without it before committing or releasing.
replace github.com/machinefabric/capdag-go => {{ projects.capdag_go.path_from_here }}
{%- endif %}
"#;
const GO_COMMITTED: &str = r#"module github.com/machinefabric/capdag-interop-relay-host-go
go 1.21
require github.com/machinefabric/capdag-go v1.386.0
require (
github.com/google/uuid v1.6.0 // indirect
)
"#;
const GO_LOCAL: &str = r#"module github.com/machinefabric/capdag-interop-relay-host-go
go 1.21
require github.com/machinefabric/capdag-go v0.0.0
require (
github.com/google/uuid v1.6.0 // indirect
)
// LOCAL_LIBS: the working copy in this workspace rather than a published
// tag. The require above carries the v0.0.0 placeholder a replaced module
// takes. Re-render without it before committing or releasing.
replace github.com/machinefabric/capdag-go => ../../../../../../capdag/capdag-go
"#;
const SWIFT_TEMPLATE: &str = r#"// swift-tools-version:5.9
import PackageDescription
let package = Package(
name: "mlxcartridge",
dependencies: [
{%- if env.LOCAL_LIBS %}
// LOCAL_LIBS: the working copy in this workspace rather than a
// published tag. Re-render without it before committing or releasing.
.package(path: "{{ projects.capdag_objc.path_from_here }}"),
{%- else %}
.package(url: "https://github.com/machinefabric/capdag-objc", exact: "{{ projects.capdag_objc.latest_remote_version }}"),
{%- endif %}
.package(path: "../shared-swift"),
]
)
"#;
const SWIFT_COMMITTED: &str = r#"// swift-tools-version:5.9
import PackageDescription
let package = Package(
name: "mlxcartridge",
dependencies: [
.package(url: "https://github.com/machinefabric/capdag-objc", exact: "1.710.0"),
.package(path: "../shared-swift"),
]
)
"#;
const SWIFT_LOCAL: &str = r#"// swift-tools-version:5.9
import PackageDescription
let package = Package(
name: "mlxcartridge",
dependencies: [
// LOCAL_LIBS: the working copy in this workspace rather than a
// published tag. Re-render without it before committing or releasing.
.package(path: "../../capdag/capdag-objc"),
.package(path: "../shared-swift"),
]
)
"#;
#[test]
fn test11243_a_local_render_is_recognised_in_every_shape_the_fleet_uses() {
for (name, template, local, committed) in [
("Cargo.toml", CARGO_TEMPLATE, CARGO_LOCAL, CARGO_COMMITTED),
("package.json", NPM_TEMPLATE, NPM_LOCAL, NPM_COMMITTED),
("go.mod", GO_TEMPLATE, GO_LOCAL, GO_COMMITTED),
("Package.swift", SWIFT_TEMPLATE, SWIFT_LOCAL, SWIFT_COMMITTED),
] {
let hits = carried_by(template, local, VARIABLE);
assert!(
!hits.is_empty(),
"{name}: the local render was not recognised — a release would ship it"
);
for Carried { line, text, block } in &hits {
let actual = local
.lines()
.nth(line - 1)
.unwrap_or_else(|| panic!("{name}: reported line {line}, which is past the end"));
assert_eq!(actual.trim(), text, "{name}: line {line} is not what was reported");
for one in block {
assert!(
local.lines().any(|l| l.trim() == one),
"{name}: the reported branch contains a line the file does not: {one}"
);
}
assert!(
!committed.contains(text.as_str()),
"{name}: line {line} appears in the ordinary render too, so it shows nothing: {text}"
);
}
}
}
#[test]
fn test11244_a_committed_render_is_not_mistaken_for_a_local_one() {
for (name, template, committed) in [
("Cargo.toml", CARGO_TEMPLATE, CARGO_COMMITTED),
("package.json", NPM_TEMPLATE, NPM_COMMITTED),
("go.mod", GO_TEMPLATE, GO_COMMITTED),
("Package.swift", SWIFT_TEMPLATE, SWIFT_COMMITTED),
] {
assert_eq!(
carried_by(template, committed, VARIABLE),
Vec::<Carried>::new(),
"{name}: an ordinary committed manifest was refused as a local render"
);
}
assert!(
CARGO_COMMITTED.contains("path = \"../../cartridges/modelcartridge\""),
"the fixture no longer contains the committed path dependency it exists to protect"
);
}
#[test]
fn test11245_a_manifest_no_template_generates_is_not_judged() {
let dir = tempfile::tempdir().expect("a scratch directory");
std::fs::write(
dir.path().join("Cargo.toml"),
"[dependencies]\nwsb = { path = \"..\" }\ncapdag = { path = \"../../capdag/capdag-rs\" }\n",
)
.expect("the manifest writes");
let found = scan(dir.path(), VARIABLE).expect("the scan runs");
assert!(
found.is_empty(),
"a manifest with no template was judged: {found:?}"
);
}
#[test]
fn test11246_the_scan_names_the_file_it_found_and_the_line() {
let dir = tempfile::tempdir().expect("a scratch directory");
let nested = dir.path().join("fabricloom").join("floom-engine");
std::fs::create_dir_all(&nested).expect("the tree is created");
std::fs::write(nested.join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(nested.join("Cargo.toml"), CARGO_LOCAL).expect("writes");
let found = scan(dir.path(), VARIABLE).expect("the scan runs");
assert_eq!(found.len(), 1, "expected exactly one finding, got {found:?}");
assert_eq!(found[0].file, "fabricloom/floom-engine/Cargo.toml");
assert_eq!(
CARGO_LOCAL.lines().nth(found[0].line - 1).map(str::trim),
Some(found[0].text.as_str())
);
let located = found[0].message();
assert!(
located.contains("fabricloom/floom-engine/Cargo.toml"),
"a finding must name the file: {located}"
);
assert!(
!CARGO_COMMITTED.contains(&found[0].text),
"the line shown appears in the ordinary render too, so it shows nothing: {located}"
);
let refusal = refusal(&found);
assert!(
refusal.contains("wsb local off"),
"the refusal must name the command that undoes it: {refusal}"
);
assert!(
refusal.contains(&located),
"and carry the evidence: {refusal}"
);
std::fs::write(nested.join("Cargo.toml"), CARGO_COMMITTED).expect("writes");
assert!(
scan(dir.path(), VARIABLE).expect("the scan runs").is_empty(),
"the committed render was refused"
);
}
#[test]
fn test11247_a_template_with_no_render_is_not_a_finding() {
let dir = tempfile::tempdir().expect("a scratch directory");
std::fs::write(dir.path().join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
assert!(
scan(dir.path(), VARIABLE).expect("the scan runs").is_empty(),
"a template with nothing rendered from it was reported"
);
}
#[test]
fn test11248_dependency_and_build_trees_are_not_walked() {
let dir = tempfile::tempdir().expect("a scratch directory");
for skipped in ["node_modules", "target", ".git", "build"] {
let nested = dir.path().join(skipped).join("someone-elses");
std::fs::create_dir_all(&nested).expect("the tree is created");
std::fs::write(nested.join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(nested.join("Cargo.toml"), CARGO_LOCAL).expect("writes");
}
assert!(
scan(dir.path(), VARIABLE).expect("the scan runs").is_empty(),
"a vendored tree was walked"
);
std::fs::write(dir.path().join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(dir.path().join("Cargo.toml"), CARGO_LOCAL).expect("writes");
assert_eq!(
scan(dir.path(), VARIABLE).expect("the scan runs").len(),
1,
"the file this scan is for was not found"
);
}
#[test]
fn test11262_a_committed_local_render_survives_a_restore_from_the_commit() {
let dir = tempfile::tempdir().expect("a scratch directory");
let root = dir.path();
let run = |args: &[&str]| {
let status = crate::git_backend::git_command()
.current_dir(root)
.args(args)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.expect("git runs");
assert!(status.success(), "git {args:?} failed");
};
run(&["init", "-q", "-b", "main"]);
run(&["config", "user.email", "t@example.com"]);
run(&["config", "user.name", "Test"]);
std::fs::write(root.join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(root.join("Cargo.toml"), CARGO_LOCAL).expect("writes");
run(&["add", "-A"]);
run(&["commit", "-q", "-m", "the local render, committed"]);
std::fs::write(root.join("Cargo.toml"), CARGO_COMMITTED).expect("writes");
assert!(
scan(root, VARIABLE).expect("the scan runs").is_empty(),
"the published render must not be flagged"
);
run(&["checkout", "--", "Cargo.toml"]);
let found = scan(root, VARIABLE).expect("the scan runs");
assert_eq!(
found.len(),
1,
"restoring the commit must leave the tree local again, or this test protects nothing"
);
assert_eq!(found[0].file, "Cargo.toml");
std::fs::write(root.join("Cargo.toml"), CARGO_COMMITTED).expect("writes");
assert!(
scan(root, VARIABLE).expect("the scan runs").is_empty(),
"a fresh render against published tags is what escapes it"
);
}
#[test]
fn test11263_the_fleets_answer_does_not_depend_on_where_it_was_asked() {
let outer = tempfile::tempdir().expect("a scratch directory");
let root = outer.path();
let git = |dir: &std::path::Path, args: &[&str]| {
let status = crate::git_backend::git_command()
.current_dir(dir)
.args(args)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.expect("git runs");
assert!(status.success(), "git {args:?} failed in {}", dir.display());
};
git(root, &["init", "-q", "-b", "main"]);
let engine = root.join("fabricloom").join("floom-engine");
std::fs::create_dir_all(&engine).expect("the tree");
std::fs::write(engine.join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(engine.join("Cargo.toml"), CARGO_LOCAL).expect("writes");
let inner = root.join("wsb");
std::fs::create_dir_all(&inner).expect("the tree");
git(&inner, &["init", "-q", "-b", "main"]);
std::fs::write(inner.join("Cargo.toml"), "[package]\nname = \"wsb\"\n").expect("writes");
assert!(
scan(&inner, VARIABLE).expect("the scan runs").is_empty(),
"the nested repository has no templates, so a scan of it finds nothing"
);
let found = scan_workspace(&inner, VARIABLE).expect("the scan runs");
assert_eq!(
found.len(),
1,
"asked from a submodule, the fleet's answer was that submodule's: {found:?}"
);
assert_eq!(found[0].file, "fabricloom/floom-engine/Cargo.toml");
assert_eq!(scan_workspace(root, VARIABLE).expect("the scan runs"), found);
}
#[test]
fn test11275_a_survivor_names_why_it_survived() {
let dir = tempfile::tempdir().expect("a scratch directory");
let root = dir.path();
let run = |args: &[&str]| {
let status = crate::git_backend::git_command()
.current_dir(root)
.args(args)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.expect("git runs");
assert!(status.success(), "git {args:?} failed");
};
run(&["init", "-q", "-b", "main"]);
run(&["config", "user.email", "t@example.com"]);
run(&["config", "user.name", "Test"]);
std::fs::write(root.join("clean.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(root.join("frozen.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(root.join("clean.toml"), CARGO_COMMITTED).expect("writes");
std::fs::write(root.join("frozen.toml"), CARGO_LOCAL).expect("writes");
run(&["add", "-A"]);
run(&["commit", "-q", "-m", "one clean, one frozen"]);
std::fs::write(root.join("clean.toml"), CARGO_LOCAL).expect("writes");
let found = scan(root, VARIABLE).expect("the scan runs");
assert_eq!(found.len(), 2, "both files are local on disk");
let of = |name: &str| {
let one = found
.iter()
.find(|f| f.file == name)
.unwrap_or_else(|| panic!("{name} is among the findings"));
persistence(root, one, VARIABLE)
};
assert_eq!(
of("clean.toml"),
Persistence::NotRestored,
"its committed form holds a published tag, so nothing about its history is wrong"
);
assert_eq!(
of("frozen.toml"),
Persistence::Committed,
"its committed form IS the local render, so restoring the commit restores it"
);
assert_ne!(of("clean.toml"), of("frozen.toml"));
}
#[test]
fn test11276_an_untracked_render_makes_no_claim_about_history() {
let dir = tempfile::tempdir().expect("a scratch directory");
let root = dir.path();
let run = |args: &[&str]| {
let status = crate::git_backend::git_command()
.current_dir(root)
.args(args)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.expect("git runs");
assert!(status.success(), "git {args:?} failed");
};
run(&["init", "-q", "-b", "main"]);
run(&["config", "user.email", "t@example.com"]);
run(&["config", "user.name", "Test"]);
std::fs::write(root.join("seed"), "seed").expect("writes");
run(&["add", "-A"]);
run(&["commit", "-q", "-m", "seed"]);
std::fs::write(root.join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
std::fs::write(root.join("Cargo.toml"), CARGO_LOCAL).expect("writes");
let found = scan(root, VARIABLE).expect("the scan runs");
assert_eq!(found.len(), 1);
assert_eq!(persistence(root, &found[0], VARIABLE), Persistence::Untracked);
}
const PYPROJECT_TEMPLATE: &str = r#"[project]
name = "capdag-interop-tests"
requires-python = ">=3.10"
dependencies = [
"pytest>=7.0",
{%- if env.LOCAL_LIBS %}
"capdag @ file://{{ projects.capdag_py.path }}",
{%- else %}
"capdag>={{ projects.capdag_py.latest_remote_version }}",
{%- endif %}
"tagged-urn>=1.27.122",
]
"#;
const PYPROJECT_LOCAL: &str = r#"[project]
name = "capdag-interop-tests"
requires-python = ">=3.10"
dependencies = [
"pytest>=7.0",
"capdag @ file:///home/bahram/ws/prj/machinefabric/main-worktree/capdag/capdag-py",
"tagged-urn>=1.27.122",
]
"#;
const PLACED: &[(&str, &str, &str, &str)] = &[
(
"Cargo.toml",
"fabricloom/floom-engine",
CARGO_TEMPLATE,
CARGO_LOCAL,
),
(
"package.json",
"fabricloom/unifloom",
NPM_TEMPLATE,
NPM_LOCAL,
),
(
"go.mod",
"mfab-tests/capdag-interop/src/capdag_interop/hosts/go-relay",
GO_TEMPLATE,
GO_LOCAL,
),
(
"Package.swift",
"cartridges/mlxcartridge",
SWIFT_TEMPLATE,
SWIFT_LOCAL,
),
];
fn place(root: &std::path::Path, dir: &str, name: &str, template: &str, rendered: &str) {
let at = root.join(dir);
std::fs::create_dir_all(&at).expect("the tree is created");
std::fs::write(at.join(format!("{name}.wstemplate")), template).expect("writes");
std::fs::write(at.join(name), rendered).expect("writes");
}
#[test]
fn test11466_a_local_render_inside_the_workspace_travels_with_it() {
let dir = tempfile::tempdir().expect("a scratch directory");
for (name, at, template, rendered) in PLACED {
place(dir.path(), at, name, template, rendered);
}
let found = scan(dir.path(), VARIABLE).expect("the scan runs");
assert_eq!(
found.len(),
PLACED.len(),
"every fixture is a local render and a release door refuses it: {found:?}"
);
let escaping = escaping(&found);
assert!(
escaping.is_empty(),
"these renders are copied into the guest intact and must not be refused: {:?}",
escaping
.iter()
.map(|one| (one.file.clone(), one.escapes.clone()))
.collect::<Vec<_>>()
);
}
#[test]
fn test11467_an_absolute_reference_does_not_travel() {
let dir = tempfile::tempdir().expect("a scratch directory");
for (name, at, template, rendered) in PLACED {
place(dir.path(), at, name, template, rendered);
}
place(
dir.path(),
"mfab-tests/capdag-interop",
"pyproject.toml",
PYPROJECT_TEMPLATE,
PYPROJECT_LOCAL,
);
let found = scan(dir.path(), VARIABLE).expect("the scan runs");
let escaping = escaping(&found);
assert_eq!(
escaping.len(),
1,
"exactly one of these references leaves the workspace: {:?}",
escaping
.iter()
.map(|one| one.file.clone())
.collect::<Vec<_>>()
);
assert_eq!(escaping[0].file, "mfab-tests/capdag-interop/pyproject.toml");
assert_eq!(
escaping[0].escapes,
vec!["/home/bahram/ws/prj/machinefabric/main-worktree/capdag/capdag-py".to_string()],
"the reference is recorded as the place it points at"
);
let refusal = staging_refusal(&escaping);
assert!(
refusal.contains("mfab-tests/capdag-interop/pyproject.toml"),
"the refusal must name the file: {refusal}"
);
assert!(
refusal.contains("/home/bahram/ws/prj/machinefabric/main-worktree/capdag/capdag-py"),
"and the reference that cannot come with it: {refusal}"
);
for (name, at, _, _) in PLACED {
assert!(
!refusal.contains(&format!("{at}/{name}")),
"{at}/{name} travels with the workspace and must not appear: {refusal}"
);
}
}
#[test]
fn test11468_a_relative_reference_that_leaves_the_workspace_does_not_travel() {
let dir = tempfile::tempdir().expect("a scratch directory");
let escaped = CARGO_LOCAL.replace(
"path = \"../../capdag/capdag-rs\"",
"path = \"../../../elsewhere/capdag-rs\"",
);
place(
dir.path(),
"fabricloom/floom-engine",
"Cargo.toml",
CARGO_TEMPLATE,
&escaped,
);
let found = scan(dir.path(), VARIABLE).expect("the scan runs");
let escaping = escaping(&found);
assert_eq!(escaping.len(), 1, "the reference leaves the workspace");
assert_eq!(
escaping[0].escapes,
vec!["../../../elsewhere/capdag-rs".to_string()]
);
assert!(
escaped.contains("path = \"../../cartridges/modelcartridge\""),
"the fixture no longer carries the deliberate cross-repository path"
);
assert_eq!(
escaping[0].escapes.len(),
1,
"only the reference that leaves the workspace is reported: {:?}",
escaping[0].escapes
);
}
#[test]
fn test11469_a_windows_path_is_absolute_wherever_it_is_read() {
let dir = tempfile::tempdir().expect("a scratch directory");
for (reference, why) in [
("C:/Users/bahram/capdag/capdag-rs", "a drive letter"),
(r"C:\Users\bahram\capdag\capdag-rs", "a drive letter with backslashes"),
(r"\\build\share\capdag-rs", "a UNC share"),
("~/ws/prj/machinefabric/main-worktree/capdag/capdag-rs", "a home directory"),
] {
let at = dir.path().join(reference.len().to_string());
let rendered = CARGO_LOCAL.replace("../../capdag/capdag-rs", reference);
place(&at, "fabricloom/floom-engine", "Cargo.toml", CARGO_TEMPLATE, &rendered);
let found = scan(&at, VARIABLE).expect("the scan runs");
assert_eq!(
escaping(&found).len(),
1,
"{why} names a place outside the workspace: {reference}"
);
}
}
#[test]
fn test12440_only_a_local_branch_that_reads_the_build_root_needs_a_build() {
let dir = tempfile::tempdir().expect("a scratch directory");
std::fs::write(dir.path().join("Cargo.toml.wstemplate"), CARGO_TEMPLATE).expect("writes");
assert!(!local_render_reads_build_root(dir.path(), VARIABLE).unwrap(), "a path to a working copy reads no build root");
let nested = dir.path().join("capdag").join("capdag-go");
std::fs::create_dir_all(&nested).expect("the tree is created");
std::fs::write(
nested.join("go.mod.wstemplate"),
"module example.com/x\n{% if env.LOCAL_LIBS %}\nreplace example.com/y => {{ build.root_from_here }}/lungo/distribution/go\n{% endif %}\n",
)
.expect("writes");
assert!(local_render_reads_build_root(dir.path(), VARIABLE).unwrap());
let other = tempfile::tempdir().expect("a scratch directory");
std::fs::write(other.path().join("x.toml.wstemplate"), "root = \"{{ build.root_from_here }}\"\n").expect("writes");
assert!(!local_render_reads_build_root(other.path(), VARIABLE).unwrap());
}