Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "47ac5f10
# `generation` tip (only `conformance/`, which this crate never touches,
# differs), so matching it here keeps one `comline-codegen` in the tree.
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "1f8c5e290eba72a578860ebb6dc33453a2f0726b" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "a70cafcdd2015686ff1dbfba59b2b56ff70b730d", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "7f2ddd3ed97c412389e0a0bf06420774b1a659d3", optional = true }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "1191e76068eca75ea7c4148d58ab127bb005e81a", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "86fc4eb586529ef92041f8dd3c46ff9340103411", optional = true }

[features]
# Which language generators are compiled in. Drop one to shed its whole
Expand Down
108 changes: 108 additions & 0 deletions tests/cli/end_to_end.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//! The whole pipeline, for real: a `.comline` schema with a `protocol` →
//! `comline generate --mode lib` → the emitted crate is compiled against
//! `comline-runtime` and a hand-written driver runs a client ⇆ provider
//! round-trip over an in-memory transport (a request/response call, a raised
//! typed error, a one-way notify).
//!
//! Slow: it fetches and builds `comline-runtime`. Needs network.

use std::fs;
use std::process::Command;

use crate::util::*;

/// Driven against the generated `chat_e2e` crate (see
/// `tests/fixtures/chat_project/src/chat.ids`).
const DRIVER: &str = r#"
use std::sync::{Arc, Mutex};
use std::thread;

use chat_e2e::chat::{Chat, ChatClient, ChatDispatcher, ChatSendError, Message, Rejected};
use comline_runtime::client::Client;
use comline_runtime::contract::CallError;
use comline_runtime::format::MsgPack;
use comline_runtime::serve::Server;
use comline_runtime::transport::duplex;

struct Svc {
notes: Arc<Mutex<Vec<String>>>,
}

impl Chat for Svc {
fn send(&self, text: &str) -> Result<Message, ChatSendError> {
if text.is_empty() {
return Err(ChatSendError::Rejected(Rejected { reason: "empty".into() }));
}
Ok(Message { body: format!("echo: {text}"), seq: 1 })
}
fn note(&self, text: &str) {
self.notes.lock().unwrap().push(text.to_string());
}
}

#[test]
fn client_and_provider_over_duplex() {
let (client_side, provider_side) = duplex();
let notes = Arc::new(Mutex::new(Vec::new()));
let notes_for_svc = notes.clone();

let provider = thread::spawn(move || {
let mut provider_side = provider_side;
Server::new(ChatDispatcher(Svc { notes: notes_for_svc }), MsgPack)
.serve(&mut provider_side)
.unwrap();
});

let mut client = ChatClient::new(Client::new(client_side, MsgPack));

assert_eq!(client.send("hi").unwrap().body, "echo: hi");

match client.send("").unwrap_err() {
CallError::App(ChatSendError::Rejected(r)) => assert_eq!(r.reason, "empty"),
other => panic!("expected a Rejected, got {other:?}"),
}

client.note("saved").unwrap(); // one-way

drop(client);
provider.join().unwrap();

assert_eq!(&*notes.lock().unwrap(), &["saved".to_string()]);
}
"#;

#[test]
fn a_generated_protocol_crate_runs_a_real_round_trip() {
let temp = tempfile::tempdir().unwrap();
let project = copy_fixture("chat_project", temp.path());

comline_cmd()
.current_dir(&project)
.args([
"generate", "--target", "rust", "--mode", "lib", "--out", "gen",
])
.assert()
.success();

let crate_dir = project.join("gen/rust");
assert!(crate_dir.join("Cargo.toml").exists());
assert!(crate_dir.join("src/chat.rs").exists());

// A lib crate git-deps `comline-runtime`; drop the driver in `tests/`.
fs::create_dir_all(crate_dir.join("tests")).unwrap();
fs::write(crate_dir.join("tests/roundtrip.rs"), DRIVER).unwrap();

let out = Command::new(env!("CARGO"))
.args(["test", "--quiet"])
.current_dir(&crate_dir)
.env("CARGO_TARGET_DIR", crate_dir.join("target"))
.output()
.expect("run cargo test on the generated crate");

assert!(
out.status.success(),
"the generated crate's round-trip test failed\n--- stdout ---\n{}\n--- stderr ---\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
}
1 change: 1 addition & 0 deletions tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod build;
mod check;
mod clean;
mod diff;
mod end_to_end;
mod generate;
mod global;
mod new;
Expand Down
9 changes: 8 additions & 1 deletion tests/cli/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ pub fn comline_cmd() -> Command {
/// files) is skipped, so the tests stay hermetic even if someone has run the
/// CLI against the fixture locally.
pub fn fixture_project(temp: &Path) -> PathBuf {
copy_fixture("simple_project", temp)
}

/// Like [`fixture_project`] for an arbitrary fixture under `tests/fixtures/`.
pub fn copy_fixture(name: &str, temp: &Path) -> PathBuf {
let dest = temp.join("proj");
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/simple_project");
let src = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures")
.join(name);
fs::create_dir_all(&dest).unwrap();
fs::copy(src.join("config.idp"), dest.join("config.idp")).unwrap();
copy_dir(&src.join("src"), &dest.join("src"));
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/chat_project/config.idp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
congregation chat_e2e
specification_version = 1

code_generation = {
languages = {
rust#1.70.0 = {}
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/chat_project/src/chat.ids
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct Message {
body: str
seq: u64
}

error Rejected {
message = "rejected: {self.reason}"
reason: str
}

protocol Chat {
/// Request/response with a struct return and a raised error.
function send(text: str) -> Message ! Rejected;
/// Fire-and-forget.
function note(text: str);
}
Loading