From e1873fd45bb25df2cfae9602384710ce1bd21d76 Mon Sep 17 00:00:00 2001 From: Maksim Zdobnikau Date: Tue, 10 Dec 2024 14:12:47 +0100 Subject: [PATCH] Add `proc-macro-server` test --- .github/workflows/ci.yml | 1 + scarb/tests/proc_macro_server.rs | 31 ++++++++++++++++++- .../src/proc_macro_server.rs | 25 +++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b52e7e78f..dd1781180 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -132,3 +132,4 @@ jobs: run: | cargo test -p scarb --test build_cairo_plugin compile_with_prebuilt_plugins -- --exact cargo test -p scarb --test build_cairo_plugin compile_with_invalid_prebuilt_plugins -- --exact + cargo test -p scarb --test proc_macro_server load_prebuilt_proc_macros -- --exact diff --git a/scarb/tests/proc_macro_server.rs b/scarb/tests/proc_macro_server.rs index b01f865dc..b17790be8 100644 --- a/scarb/tests/proc_macro_server.rs +++ b/scarb/tests/proc_macro_server.rs @@ -12,7 +12,7 @@ use scarb_proc_macro_server_types::methods::expand::ExpandInlineMacroParams; use scarb_test_support::cairo_plugin_project_builder::CairoPluginProjectBuilder; use scarb_test_support::proc_macro_server::ProcMacroClient; use scarb_test_support::proc_macro_server::SIMPLE_MACROS; -use scarb_test_support::project_builder::ProjectBuilder; +use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder}; #[test] fn defined_macros() { @@ -171,3 +171,32 @@ fn expand_inline() { TokenStream::new("struct A { field: 25 , other_field: macro_call!(12)}".to_string()) ); } + +#[test] +fn load_prebuilt_proc_macros() { + let t = TempDir::new().unwrap(); + + let project = t.child("test_package"); + + ProjectBuilder::start() + .name("test_package") + .version("1.0.0") + .lib_cairo("") + .dep( + "proc_macro_example", + Dep.version("0.1.2").registry("https://scarbs.dev/"), + ) + .build(&project); + + let mut proc_macro_server = ProcMacroClient::new_without_cargo(&project); + + let response = proc_macro_server + .request_and_wait::(ExpandInlineMacroParams { + name: "some".to_string(), + args: TokenStream::new("42".to_string()), + }) + .unwrap(); + + assert_eq!(response.diagnostics, vec![]); + assert_eq!(response.token_stream, TokenStream::new("42".to_string())); +} diff --git a/utils/scarb-test-support/src/proc_macro_server.rs b/utils/scarb-test-support/src/proc_macro_server.rs index 12e93176f..cf1fefab0 100644 --- a/utils/scarb-test-support/src/proc_macro_server.rs +++ b/utils/scarb-test-support/src/proc_macro_server.rs @@ -90,6 +90,31 @@ impl ProcMacroClient { responses: Default::default(), } } + pub fn new_without_cargo>(path: P) -> Self { + let mut server_process = Scarb::new() + .std() + .arg("--quiet") + .arg("proc-macro-server") + .env("CARGO", "/bin/false") + .env("RUSTC", "/bin/false") + .stdout(Stdio::piped()) + .stdin(Stdio::piped()) + .stderr(Stdio::inherit()) + .current_dir(path) + .spawn() + .unwrap(); + + let requester = server_process.stdin.take().unwrap(); + let responder = BufReader::new(server_process.stdout.take().unwrap()).lines(); + + Self { + requester, + responder, + server_process, + id_counter: Default::default(), + responses: Default::default(), + } + } pub fn request(&mut self, params: M::Params) -> PendingRequest { let id = self.id_counter;