-
Notifications
You must be signed in to change notification settings - Fork 17
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
FoundCrate::Itself does not distinguish between crate and integration tests #10
Comments
Hmm. I always use
In the lib.rs file |
So you can refer to your crate by its name? I guess I could do that and then hardcode e.g. // my-crate/src/lib.rs
pub struct Something {}
extern crate self as my_crate; // my-crate-macros/src/lib.rs
// Some macro code
fn root_crate() -> TokenStream {
match crate_name("my-crate").expect("Find crate") {
FoundCrate::Itself => quote!(::my_crate),
FoundCrate::Name(name) => quote!(::#name),
}
} // my-crate/tests/my-integration-test.rs
use my_crate::Something;
#[test]
fn some_test() {
// Some test code
} |
Yes you can refer a crate by its name. If your proposed solution works flawlessly, I'm open to accept a pr. However, this should be testeted properly. |
I tried it myself and the environment variable doesn't appear to be available, even when I use the compile-time macro; so, not sure if there's another way to determine. |
I'm not aware of any way. |
I've also been using the if let Ok(FoundCrate::Name(name)) = crate_name("original_name") {
let import = format_ident!("{}", name);
quote! { ::#import }
} else {
quote! { ::original_name }
} as a utility function. The only downside is that as you move things between crates in a workspace, you can accidentally have |
Just did some testing, and it looks like So if |
If that works, it sounds like a way to solve this problem. |
Two observations (no time to work on this right now):
|
As suggested, using However, it appears that this environment variable isn't set for doctests. Checking here doesn't say anything about environment variables set specifically for doctests either, so there doesn't appear to be something else we can check for that case. Is not setting |
I have code set up like this:
This works great now to support
crate
when I'm within my crate and using::my_crate
for when I'm outside of my crate. The problem is that there is no way to distinguish being in the crate itself or being in thetests
directory for integration tests. Because of this, the above function yieldscrate
instead of::my_crate
for integration tests, which causes compilation errors because the integration test does not have access to the crate itself viacrate
but rather via::my_crate
.Is there any plan to support distinguishing the integration tests? Really, just identifying that situation and returning
FoundCrate::Name
would be enough.My first thought was to check for
CARGO_BIN_EXE_<name>
, which only exists for integration tests and benchmarks. If it exists and aCargo.toml
is found that matchesFoundCrate::Itself
, we could instead swap to the crate's actual name. Thoughts?The text was updated successfully, but these errors were encountered: