278 lines
8.8 KiB
Rust
278 lines
8.8 KiB
Rust
//! 真实命令与同一构建缓存验证;不凭生成文本判断二进制已经更新。
|
|
|
|
use dever::compiler::{
|
|
build::{self, Profile},
|
|
check::{ownership, program},
|
|
emit,
|
|
project::Project,
|
|
};
|
|
use std::{
|
|
fs,
|
|
path::{Path, PathBuf},
|
|
process::{Command, Output},
|
|
};
|
|
|
|
#[path = "build/api/mod.rs"]
|
|
mod api;
|
|
#[path = "build/bytes.rs"]
|
|
mod bytes;
|
|
#[path = "build/constants.rs"]
|
|
mod constants;
|
|
#[path = "build/database/mod.rs"]
|
|
mod database;
|
|
#[path = "build/job/mod.rs"]
|
|
mod job;
|
|
#[path = "build/json.rs"]
|
|
mod json;
|
|
#[path = "build/package/mod.rs"]
|
|
mod package;
|
|
#[path = "build/port/mod.rs"]
|
|
mod port;
|
|
#[path = "build/project/mod.rs"]
|
|
mod project;
|
|
#[path = "build/settings.rs"]
|
|
mod settings;
|
|
#[path = "build/standard.rs"]
|
|
mod standard;
|
|
#[path = "build/upload/mod.rs"]
|
|
mod upload;
|
|
|
|
fn directory(name: &str) -> PathBuf {
|
|
let directory = Path::new(env!("CARGO_MANIFEST_DIR")).join(format!(
|
|
"../target/next-build-tests/{}-{name}",
|
|
std::process::id()
|
|
));
|
|
fs::create_dir_all(&directory).unwrap();
|
|
directory
|
|
}
|
|
|
|
fn write_source(path: impl AsRef<Path>, source: impl AsRef<[u8]>) -> std::io::Result<()> {
|
|
let path = path.as_ref();
|
|
fs::create_dir_all(path.parent().unwrap())?;
|
|
fs::write(path, source)
|
|
}
|
|
|
|
fn cli(arguments: &[&str]) -> Output {
|
|
Command::new(env!("CARGO_BIN_EXE_dever"))
|
|
.args(arguments)
|
|
.output()
|
|
.unwrap()
|
|
}
|
|
fn success(output: Output) -> String {
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
String::from_utf8(output.stdout).unwrap()
|
|
}
|
|
fn analyze(path: &Path) -> (Project, dever::compiler::model::Program) {
|
|
let project = Project::load(path).unwrap();
|
|
assert!(project.diagnostics.is_empty());
|
|
let program = program::check(&project.declarations, &project.modules).unwrap();
|
|
(project, program)
|
|
}
|
|
fn run(binary: &Path) -> String {
|
|
success(Command::new(binary).output().unwrap())
|
|
.trim()
|
|
.into()
|
|
}
|
|
|
|
#[test]
|
|
fn cli_formats_checks_builds_and_preserves_existing_outputs() {
|
|
let directory = directory("cli");
|
|
let source = directory.join("main.dever");
|
|
fs::write(&source, "main() (value: Int) { value = 42 }").unwrap();
|
|
let path = directory.to_str().unwrap();
|
|
assert!(!cli(&["check", path]).status.success());
|
|
success(cli(&["fmt", path]));
|
|
success(cli(&["fmt", path, "--check"]));
|
|
success(cli(&["check", path]));
|
|
assert_eq!(success(cli(&["run", path])).trim(), "42");
|
|
let output = directory.join("release");
|
|
success(cli(&["build", path, "--output", output.to_str().unwrap()]));
|
|
assert_eq!(run(&output), "42");
|
|
let bytes = fs::read(&output).unwrap();
|
|
assert!(
|
|
!cli(&["build", path, "--output", output.to_str().unwrap()])
|
|
.status
|
|
.success()
|
|
);
|
|
assert_eq!(bytes, fs::read(&output).unwrap());
|
|
assert!(!cli(&["run", path, "--profile", "unknown"]).status.success());
|
|
}
|
|
|
|
#[test]
|
|
fn module_changes_reuse_stable_files_and_remove_only_owned_stale_outputs() {
|
|
let root = directory("modules");
|
|
let domain = root.join("module/demo/math");
|
|
fs::create_dir_all(&domain).unwrap();
|
|
write_source(
|
|
domain.join("app/main.dever"),
|
|
"main() (value: Int) { value = domain.answer() }",
|
|
)
|
|
.unwrap();
|
|
fs::write(
|
|
domain.join("domain.dever"),
|
|
"answer() (value: Int) { value = 7 }",
|
|
)
|
|
.unwrap();
|
|
let (project, program) = analyze(&root);
|
|
let artifact =
|
|
build::build(&project, &program, "demo.math.app.main", Profile::Dev, None).unwrap();
|
|
assert_eq!(run(&artifact.binary), "7");
|
|
let generated = artifact.directory.clone();
|
|
let old_module = generated.join("module/d_4_demo_4_math_6_domain.rs");
|
|
let app = generated.join("module/d_4_demo_4_math_3_app.rs");
|
|
let old_modified = fs::metadata(&app).unwrap().modified().unwrap();
|
|
let old_bytes = fs::read(&app).unwrap();
|
|
fs::write(generated.join("notes.txt"), "not generated").unwrap();
|
|
drop(artifact);
|
|
let artifact =
|
|
build::build(&project, &program, "demo.math.app.main", Profile::Dev, None).unwrap();
|
|
assert_eq!(artifact.directory, generated);
|
|
assert_eq!(
|
|
fs::metadata(&app).unwrap().modified().unwrap(),
|
|
old_modified
|
|
);
|
|
drop(artifact);
|
|
fs::write(
|
|
domain.join("domain.dever"),
|
|
"answer() (value: Int) { value = 8 }",
|
|
)
|
|
.unwrap();
|
|
let (project, program) = analyze(&root);
|
|
let artifact =
|
|
build::build(&project, &program, "demo.math.app.main", Profile::Dev, None).unwrap();
|
|
assert_eq!(run(&artifact.binary), "8");
|
|
assert_eq!(old_bytes, fs::read(&app).unwrap());
|
|
drop(artifact);
|
|
fs::write(
|
|
domain.join("domain.dever"),
|
|
"answer(extra: Int) (value: Int) { value = 8 + extra }",
|
|
)
|
|
.unwrap();
|
|
write_source(
|
|
domain.join("app/main.dever"),
|
|
"main() (value: Int) { value = domain.answer(1) }",
|
|
)
|
|
.unwrap();
|
|
let (project, program) = analyze(&root);
|
|
let artifact =
|
|
build::build(&project, &program, "demo.math.app.main", Profile::Dev, None).unwrap();
|
|
assert_eq!(run(&artifact.binary), "9");
|
|
drop(artifact);
|
|
fs::remove_file(domain.join("domain.dever")).unwrap();
|
|
write_source(
|
|
domain.join("app/main.dever"),
|
|
"main() (value: Int) { value = 10 }",
|
|
)
|
|
.unwrap();
|
|
let (project, program) = analyze(&root);
|
|
let artifact =
|
|
build::build(&project, &program, "demo.math.app.main", Profile::Dev, None).unwrap();
|
|
assert_eq!(run(&artifact.binary), "10");
|
|
assert!(!old_module.exists());
|
|
assert_eq!(
|
|
fs::read_to_string(generated.join("notes.txt")).unwrap(),
|
|
"not generated"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn rust_errors_keep_native_details_and_map_back_to_dever() {
|
|
let root = directory("mapping");
|
|
let path = root.join("app.dever");
|
|
fs::write(
|
|
&path,
|
|
"add(value: Int) (out: Int) { out = value + 1 }\nmain() (out: Int) { out = add(1) }",
|
|
)
|
|
.unwrap();
|
|
let (project, program) = analyze(&path);
|
|
let mut generated = emit::emit(&program, &ownership::analyze(&program), &project.sources);
|
|
generated
|
|
.get_mut("main.rs")
|
|
.unwrap()
|
|
.text
|
|
.push_str(&emit::application::standalone(&program, "main").unwrap());
|
|
let output = generated
|
|
.values_mut()
|
|
.find(|output| output.text.contains("int::add("))
|
|
.expect("generated integer addition");
|
|
output.text = output.text.replace("int::add(", "int::unknown_add(");
|
|
let error = build::compile(
|
|
&project.root,
|
|
&program,
|
|
&generated,
|
|
Profile::Dev,
|
|
&project,
|
|
None,
|
|
)
|
|
.err()
|
|
.expect("injected Rust error")
|
|
.to_string();
|
|
assert!(error.contains("unknown_add"), "{error}");
|
|
assert!(
|
|
error.contains("Dever:") && error.contains("app.dever:1:"),
|
|
"{error}"
|
|
);
|
|
}
|
|
|
|
#[path = "build/inspect.rs"]
|
|
mod inspect;
|
|
|
|
#[test]
|
|
fn deep_calls_keep_borrowed_values_and_original_fault_locations() {
|
|
let root = directory("deep-calls");
|
|
let path = root.join("main.dever");
|
|
let mut source =
|
|
String::from("type Parcel {\n name: Text\n tags: List<Text>\n number: Int\n}\n");
|
|
source.push_str("step0(value: Parcel) (answer: Int) { answer = value.number + length(value.name) + length(value.tags) }\n");
|
|
for index in 1..96 {
|
|
source.push_str(&format!(
|
|
"step{index}(value: Parcel) (answer: Int) {{ answer = step{}(value) + 1 }}\n",
|
|
index - 1
|
|
));
|
|
}
|
|
fs::write(&path, &source).unwrap();
|
|
let (project, program) = analyze(&path);
|
|
let mut generated = emit::emit(&program, &ownership::analyze(&program), &project.sources);
|
|
let parcel = emit::symbol("main.main.app.Parcel");
|
|
let call = emit::symbol("main.main.app.step95");
|
|
let harness = format!(
|
|
r#"
|
|
fn main() {{
|
|
let value = {parcel} {{ f0: "中".into(), f1: vec![], f2: 1 }};
|
|
assert_eq!({call}(&value), 97);
|
|
assert_eq!(value.f0, "中");
|
|
for (number, line) in [(i64::MAX, 6), (i64::MAX - 50, 56)] {{
|
|
let value = {parcel} {{ f0: "中".into(), f1: vec![], f2: number }};
|
|
let error = std::panic::catch_unwind(|| {call}(&value)).unwrap_err();
|
|
let info = error.downcast_ref::<&'static FaultInfo>().unwrap();
|
|
assert_eq!(info.source, {source_path:?});
|
|
assert_eq!(info.line, line);
|
|
assert_eq!(info.message, "arithmetic fault");
|
|
}}
|
|
println!("ok");
|
|
}}
|
|
"#,
|
|
source_path = path.canonicalize().unwrap().to_str().unwrap()
|
|
);
|
|
generated
|
|
.get_mut("main.rs")
|
|
.unwrap()
|
|
.text
|
|
.push_str(&harness);
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let artifact =
|
|
build::compile(&project.root, &program, &generated, profile, &project, None).unwrap();
|
|
assert_eq!(run(&artifact.binary).trim(), "ok");
|
|
}
|
|
}
|
|
|
|
#[path = "build/crypto.rs"]
|
|
mod crypto;
|
|
|
|
#[path = "build/effects/mod.rs"]
|
|
mod effects;
|