66 lines
2.2 KiB
Rust
Raw Normal View History

2020-04-10 01:43:47 +08:00
//! utils used in proc-macro tests
use crate::dylib;
2020-04-24 10:23:01 +08:00
use crate::ProcMacroSrv;
2020-08-13 12:07:28 +02:00
use proc_macro_api::ListMacrosTask;
2020-04-10 01:43:47 +08:00
use std::str::FromStr;
use test_utils::assert_eq_text;
pub mod fixtures {
2020-05-10 06:22:26 +08:00
use cargo_metadata::Message;
2021-03-02 14:27:29 +02:00
use std::path::PathBuf;
2020-04-10 01:43:47 +08:00
use std::process::Command;
// Use current project metadata to get the proc-macro dylib path
pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf {
2020-08-12 16:52:28 +02:00
let command = Command::new(toolchain::cargo())
.args(&["check", "--tests", "--message-format", "json"])
2020-04-10 01:43:47 +08:00
.output()
.unwrap()
.stdout;
2020-05-10 06:22:26 +08:00
for message in Message::parse_stream(command.as_slice()) {
2020-04-10 01:43:47 +08:00
match message.unwrap() {
Message::CompilerArtifact(artifact) => {
if artifact.target.kind.contains(&"proc-macro".to_string()) {
let repr = format!("{} {}", crate_name, version);
2020-05-10 06:39:36 +08:00
if artifact.package_id.repr.starts_with(&repr) {
2021-03-02 14:27:29 +02:00
return PathBuf::from(&artifact.filenames[0]);
2020-04-10 01:43:47 +08:00
}
}
}
_ => (), // Unknown message
}
}
panic!("No proc-macro dylib for {} found!", crate_name);
}
}
fn parse_string(code: &str) -> Option<crate::rustc_server::TokenStream> {
Some(crate::rustc_server::TokenStream::from_str(code).unwrap())
}
pub fn assert_expand(
crate_name: &str,
macro_name: &str,
version: &str,
2020-07-01 18:08:45 +03:00
ra_fixture: &str,
2020-04-10 01:43:47 +08:00
expect: &str,
) {
let path = fixtures::dylib_path(crate_name, version);
let expander = dylib::Expander::new(&path).unwrap();
2020-07-01 18:08:45 +03:00
let fixture = parse_string(ra_fixture).unwrap();
2020-04-10 01:43:47 +08:00
let res = expander.expand(macro_name, &fixture.into_subtree(), None).unwrap();
assert_eq_text!(&expect.trim(), &format!("{:?}", res));
2020-04-10 01:43:47 +08:00
}
pub fn list(crate_name: &str, version: &str) -> Vec<String> {
let path = fixtures::dylib_path(crate_name, version);
let task = ListMacrosTask { lib: path };
2020-04-24 10:23:01 +08:00
let mut srv = ProcMacroSrv::default();
let res = srv.list_macros(&task).unwrap();
2020-04-10 01:43:47 +08:00
res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect()
}