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

Concretize doesn't work with cfg_attr #427

Closed
asomers opened this issue Nov 14, 2022 · 5 comments · Fixed by #428
Closed

Concretize doesn't work with cfg_attr #427

asomers opened this issue Nov 14, 2022 · 5 comments · Fixed by #428
Labels
bug Something isn't working

Comments

@asomers
Copy link
Owner

asomers commented Nov 14, 2022

The #[concretize] attribute isn't processed as a real Rust attribute. It's processed as text by Mockall, and used as a directive to tell it how to mock something that's already being mocked. That's why it must be used with its canonical name; you can't do use mockall::concretize as something_else.
But as of this writing, Mockall doesn't process the text correctly if it appears within a cfg_attr directive. Instead, it passes it through in the emitted code, causing various compile failures. This example will trigger the problem.

use std::path::Path;

#[cfg(test)]
use mockall::{automock, concretize};

#[cfg_attr(test, automock)]
trait Foo {
    #[cfg_attr(test, concretize)]
    fn foo<P: AsRef<Path>>(&self, p: P);
}

First reported as #408 (comment) .

@asomers asomers added the bug Something isn't working label Nov 14, 2022
asomers added a commit that referenced this issue Nov 14, 2022
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
asomers added a commit that referenced this issue Nov 20, 2022
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
@asomers
Copy link
Owner Author

asomers commented Nov 20, 2022

@cjriches this should be fixed now.

@cjriches
Copy link

Thanks! Yup, seems to work now.

@horacimacias
Copy link

I'm trying to use this but if I understand correctly this concretize is on a non-released version of mockall.
Any idea when a new release containing concretize will be available?

@asomers
Copy link
Owner Author

asomers commented Oct 22, 2023

I've held off from releasing it because it's a big change, not just to the implementation but to the API. And it's a weird change, too. I haven't heard much feedback about it. Why don't you try it out, on the master branch, and let me know how it works for you?

@horacimacias
Copy link

sure, let me give it a try. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants