Skip to content

Commit

Permalink
Correctly process the concretize attribute within cfg_attr
Browse files Browse the repository at this point in the history
The concretize attribute isn't processed as a normal Rust attribute.
Instead, Mockall processes it as text.  So we need to process any
cfg_attr directive ourselves.  Rather than attempt to evaluate the
conditional, just assume that it's true.  By far the most common use
case will be `cfg_attr(test, concretize)`.

Fixes #427
  • Loading branch information
asomers committed Nov 14, 2022
1 parent b51cb30 commit ce8f1e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
26 changes: 26 additions & 0 deletions mockall/tests/cfg_attr_concretize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// vim: tw=80
//! #[concretize] can be used inside of #[cfg_attr()]`
#![deny(warnings)]

use std::path::{Path, PathBuf};

use mockall::{automock, concretize};

#[automock]
trait Foo {
#[cfg_attr(not(target_os = "ia64-unknown-multics"), concretize)]
fn foo<P: AsRef<Path>>(&self, p: P);
}


#[test]
fn withf() {
let mut foo = MockFoo::new();
foo.expect_foo()
.withf(|p| p.as_ref() == Path::new("/tmp"))
.times(3)
.return_const(());
foo.foo(Path::new("/tmp"));
foo.foo(PathBuf::from(Path::new("/tmp")));
foo.foo("/tmp");
}
5 changes: 5 additions & 0 deletions mockall_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@ impl<'a> AttrFormatter<'a> {
} else if *i.as_ref().unwrap() == "concretize" {
// Internally used attribute. Never emit.
false
} else if *i.as_ref().unwrap() == "cfg_attr" &&
attr.tokens.to_string().contains("concretize")
{
// Internally used attribute. Never emit.
false
} else {
true
}
Expand Down
8 changes: 7 additions & 1 deletion mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ impl<'a> Builder<'a> {
.any(|attr|
attr.path.segments.last()
.map(|ps| ps.ident == "concretize")
.unwrap_or(false)
.unwrap_or(false) ||
(
attr.path.segments.last()
.map(|ps| ps.ident == "cfg_attr")
.unwrap_or(false) &&
attr.tokens.to_string().contains("concretize")
)
) {
self.concretize = true;
}
Expand Down

0 comments on commit ce8f1e4

Please sign in to comment.