Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #[proc_macros::...] being detected as a proc macro crate #252

Merged
merged 1 commit into from
Jul 29, 2024
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
37 changes: 23 additions & 14 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//! Basic operations useful for building a testsuite

use crate::test_result::Errored;
use bstr::ByteSlice as _;
use color_eyre::eyre::Result;
use crossbeam_channel::unbounded;
use crossbeam_channel::Receiver;
use crossbeam_channel::Sender;
use regex::bytes::RegexSet;
use std::num::NonZeroUsize;
use std::path::Component;
use std::path::Path;
use std::path::Prefix;
use std::process::Command;
use std::process::Output;
use std::sync::OnceLock;
use std::thread;

pub(crate) fn run_command(cmd: &mut Command) -> Result<Output, Errored> {
Expand Down Expand Up @@ -53,20 +54,28 @@ pub(crate) fn strip_path_prefix<'a>(

impl CrateType {
/// Heuristic:
/// * if the file contains `#[test]`, automatically pass `--cfg test`.
/// * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
/// This avoids having to spam `fn main() {}` in almost every test.
/// * [`CrateType::ProcMacro`] if the file contains a [proc macro attribute]
/// * [`CrateType::Test`] if the file contains `#[test]`
/// * [`CrateType::Bin`] if the file contains `fn main()` or `#[start]`
/// * otherwise [`CrateType::Lib`]
///
/// [proc macro attribute]: https://doc.rust-lang.org/reference/procedural-macros.html
pub fn from_file_contents(file_contents: &[u8]) -> CrateType {
if file_contents.find(b"#[proc_macro").is_some() {
CrateType::ProcMacro
} else if file_contents.find(b"#[test]").is_some() {
CrateType::Test
} else if file_contents.find(b"fn main()").is_none()
&& file_contents.find(b"#[start]").is_none()
{
CrateType::Lib
} else {
CrateType::Bin
static RE: OnceLock<RegexSet> = OnceLock::new();
let re = RE.get_or_init(|| {
RegexSet::new([
r"#\[proc_macro(_derive|_attribute)?[\](]",
r"#\[test\]",
r"fn main()|#\[start\]",
])
.unwrap()
});

match re.matches(file_contents).iter().next() {
Some(0) => CrateType::ProcMacro,
Some(1) => CrateType::Test,
Some(2) => CrateType::Bin,
_ => CrateType::Lib,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/integrations/basic/Cargo.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ running 3 tests
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished

Building dependencies ... ok
Building aux file tests/actual_tests/auxiliary/proc_macro_attr.rs ... ok
tests/actual_tests/aux_attr_proc_macro.rs ... ok
Building aux file tests/actual_tests/auxiliary/derive_proc_macro.rs ... ok
tests/actual_tests/aux_derive.fixed ... ok
tests/actual_tests/aux_derive.rs ... ok
Expand Down Expand Up @@ -42,7 +44,7 @@ tests/actual_tests/unicode.rs ... ok
tests/actual_tests/windows_paths.rs ... ok
tests/actual_tests/subdir/aux_proc_macro.rs ... ok

test result: ok. 30 passed;
test result: ok. 31 passed;


running 0 tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@aux-build:proc_macro_attr.rs
//@check-pass

extern crate proc_macro_attr;

#[proc_macro_attr::passthrough]
pub fn f() {}

pub fn g() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn passthrough(_: TokenStream, item: TokenStream) -> TokenStream {
item
}