228 lines
7.8 KiB
Rust
228 lines
7.8 KiB
Rust
use super::*;
|
|
mod command;
|
|
mod native;
|
|
mod native_package;
|
|
mod selection;
|
|
pub(crate) mod worker;
|
|
|
|
fn project(name: &str, port: &str, adapter: &str, app: &str) -> PathBuf {
|
|
let root = directory(name);
|
|
let domain = root.join("module/demo/service");
|
|
for (file, source) in [
|
|
("port.dever", port),
|
|
("adapter.dever", adapter),
|
|
("app/main.dever", app),
|
|
] {
|
|
write_source(domain.join(file), source).unwrap();
|
|
}
|
|
root
|
|
}
|
|
|
|
#[test]
|
|
fn port_binds_directly_and_keeps_table_kind_in_inspect() {
|
|
let root = project(
|
|
"port-native",
|
|
"add(value: Int) (out: Int)",
|
|
"port.add(value: Int) (out: Int) { out = value + 1 }",
|
|
"flow start() (out: Int)\n| step | kind | do |\n| out | port | port.add(41) |",
|
|
);
|
|
let (project, program) = analyze(&root);
|
|
assert_eq!(program.functions.len(), 2, "no forwarding implementation");
|
|
let files = emit::emit(&program, &ownership::analyze(&program), &project.sources);
|
|
let app = &files["module/d_4_demo_7_service_3_app.rs"].text;
|
|
assert!(
|
|
app.contains("d_4_demo_7_service_7_adapter_4_port_3_add(41i64)"),
|
|
"{app}"
|
|
);
|
|
let artifact = build::build(
|
|
&project,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(run(&artifact.binary), "42");
|
|
let path = root.to_str().unwrap();
|
|
success(cli(&["fmt", path]));
|
|
success(cli(&["fmt", path, "--check"]));
|
|
let index: serde_json::Value =
|
|
serde_json::from_str(&success(cli(&["inspect", path, "--json"]))).unwrap();
|
|
let contract = index["symbols"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|symbol| symbol["kind"] == "port")
|
|
.unwrap();
|
|
assert_eq!(contract["name"], "demo.service.port.add");
|
|
assert!(
|
|
index["calls"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.any(|edge| edge["caller"] == contract["id"] && edge["kind"] == "implementation")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn effectful_adapter_is_awaited_directly_and_cannot_be_called_around_its_port() {
|
|
let root = project(
|
|
"port-async",
|
|
"pause() () fails dever.time.SleepResult",
|
|
"flow port.pause() ()\n| step | kind | do |\n| - | flow | dever.time.sleep(0) |",
|
|
"flow start() (out: Int)\n| step | kind | do |\n| - | port | port.pause() |\n| out | calc | 42 |",
|
|
);
|
|
let (loaded, program) = analyze(&root);
|
|
let target = program
|
|
.functions
|
|
.iter()
|
|
.find(|function| function.signature.name == "demo.service.app.start")
|
|
.unwrap();
|
|
assert!(target.effects.suspends && target.effects.clock);
|
|
let artifact = build::build(
|
|
&loaded,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(run(&artifact.binary), "42");
|
|
write_source(
|
|
root.join("module/demo/service/app/main.dever"),
|
|
"flow start() ()\n| step | kind | do |\n| - | - | demo.service.adapter.port.pause() |",
|
|
)
|
|
.unwrap();
|
|
let loaded = Project::load(&root).unwrap();
|
|
assert!(program::check(&loaded.declarations, &loaded.modules).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn adapter_contract_rejects_mismatch_hidden_effects_and_undeclared_failures() {
|
|
let app = "flow start() (out: Int)\n| step | kind | do |\n| out | port | port.read(1) |";
|
|
for (name, contract, implementation, message) in [
|
|
(
|
|
"signature",
|
|
"read(value: Int) (out: Int)",
|
|
"port.read(value: Bool) (out: Int) { out = 1 }",
|
|
"signature",
|
|
),
|
|
(
|
|
"failure",
|
|
"read(value: Int) (out: Int)",
|
|
"port.read(value: Int) (out: Int) { check value > 0 else Invalid\nout = value }",
|
|
"failures",
|
|
),
|
|
(
|
|
"effect",
|
|
"read(value: Int) (out: Int)",
|
|
"port.read(value: Int) (out: Int) { time = dever.time.now()\nout = value }",
|
|
"effectful",
|
|
),
|
|
(
|
|
"missing",
|
|
"read(value: Int) (out: Int)",
|
|
"helper() () {}",
|
|
"requires an Adapter",
|
|
),
|
|
] {
|
|
let root = project(&format!("port-{name}"), contract, implementation, app);
|
|
let project = Project::load(&root).unwrap();
|
|
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
|
let error = program::check(&project.declarations, &project.modules)
|
|
.err()
|
|
.unwrap();
|
|
assert!(error.message.contains(message), "{name}: {error}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn adapter_propagates_declared_error_through_a_rule_without_wrapping() {
|
|
let root = project(
|
|
"port-fails",
|
|
"type Error { error Rejected }\nread(value: Int) (out: Int) fails Error",
|
|
"rule port.read(value: Int) (out: Int)\n| value | out |\n| < 0 | fail(port.Error.Rejected) |\n| >= 0 | value |",
|
|
"flow start() (out: Int)\n| step | kind | do |\n| out | port | port.read(-1) |",
|
|
);
|
|
let (project, program) = analyze(&root);
|
|
let artifact = build::build(
|
|
&project,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
let output = Command::new(&artifact.binary).output().unwrap();
|
|
assert!(!output.status.success());
|
|
assert!(
|
|
String::from_utf8_lossy(&output.stderr).contains("Rejected"),
|
|
"{:?}",
|
|
output
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn adapter_private_settings_load_once_from_their_own_namespace() {
|
|
let root = project(
|
|
"port-private-setting",
|
|
"read() (out: Text)",
|
|
"setting { prefix: Text\nlimit: Int >= 1 and <= 100\nkey: Secret }\nport.read() (out: Text) { out = setting.prefix }",
|
|
"setting prefix: Text\nflow start() (out: Text)\n| step | kind | do |\n| out | port | port.read() |",
|
|
);
|
|
fs::create_dir_all(root.join("config")).unwrap();
|
|
let configuration = root.join("config/setting.json");
|
|
fs::write(&configuration,r#"{"module":{"demo":{"service":{"prefix":"domain"}}},"adapter":{"demo.service":{"setting":{"prefix":"adapter","limit":10,"key":"private-value"}}}}"#).unwrap();
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(
|
|
&loaded,
|
|
&program,
|
|
"demo.service.app.start",
|
|
Profile::Dev,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
let run = || {
|
|
Command::new(&artifact.binary)
|
|
.arg("--config")
|
|
.arg(&configuration)
|
|
.output()
|
|
.unwrap()
|
|
};
|
|
let result = run();
|
|
assert!(
|
|
result.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&result.stderr)
|
|
);
|
|
assert_eq!(
|
|
String::from_utf8_lossy(&result.stdout).trim(),
|
|
"\"adapter\""
|
|
);
|
|
success(cli(&["fmt", root.to_str().unwrap()]));
|
|
success(cli(&["fmt", root.to_str().unwrap(), "--check"]));
|
|
assert!(
|
|
fs::read_to_string(root.join("module/demo/service/adapter.dever"))
|
|
.unwrap()
|
|
.starts_with("setting {")
|
|
);
|
|
success(cli(&["inspect", root.to_str().unwrap(), "--json"]));
|
|
for bad in [
|
|
r#"{"module":{"demo":{"service":{"prefix":"domain"}}},"adapter":{"demo.service":{"setting":{"prefix":"adapter","limit":0,"key":"private-value"}}}}"#,
|
|
r#"{"module":{"demo":{"service":{"prefix":"domain"}}},"adapter":{"demo.service":{"setting":{"prefix":"adapter","limit":10}}}}"#,
|
|
r#"{"module":{"demo":{"service":{"prefix":"domain"}}}}"#,
|
|
] {
|
|
fs::write(&configuration, bad).unwrap();
|
|
let result = run();
|
|
assert!(!result.status.success());
|
|
assert!(!String::from_utf8_lossy(&result.stderr).contains("private-value"));
|
|
}
|
|
write_source(
|
|
root.join("module/demo/service/app/main.dever"),
|
|
"start() (out: Text) { out = adapter.setting.prefix }",
|
|
)
|
|
.unwrap();
|
|
let loaded = Project::load(&root).unwrap();
|
|
assert!(program::check(&loaded.declarations, &loaded.modules).is_err());
|
|
}
|