Skip to content

Commit

Permalink
Add proc-macro-server test
Browse files Browse the repository at this point in the history
  • Loading branch information
DelevoXDG committed Dec 11, 2024
1 parent 64f362e commit e1873fd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 30 additions & 1 deletion scarb/tests/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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::<ExpandInline>(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()));
}
25 changes: 25 additions & 0 deletions utils/scarb-test-support/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ impl ProcMacroClient {
responses: Default::default(),
}
}
pub fn new_without_cargo<P: AsRef<Path>>(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<M: Method>(&mut self, params: M::Params) -> PendingRequest<M> {
let id = self.id_counter;
Expand Down

0 comments on commit e1873fd

Please sign in to comment.