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

Unable to use trybuild with macros that read files #202

Closed
msrd0 opened this issue Oct 19, 2022 · 4 comments
Closed

Unable to use trybuild with macros that read files #202

msrd0 opened this issue Oct 19, 2022 · 4 comments

Comments

@msrd0
Copy link

msrd0 commented Oct 19, 2022

First of all, thanks for this amazing tool!

I'm trying to test a macro which reads files from the source directory of the crate. It reads the CARGO_MANIFEST_DIR environment variable (please let me know if there's a better way to do this) and resolves files relative to that directory. Unfortunately, with trybuild it points to $DIR/target/tests/<crate-name>, which doesn't contain the files I need.

Is it possible to have CARGO_MANIFEST_DIR point to the manifest that defines the test, or to the directory where the test file is located in?

@dtolnay
Copy link
Owner

dtolnay commented Oct 19, 2022

You should use:

std::env::set_var(
    "CARGO_MANIFEST_DIR_OVERRIDE",
    std::env::var_os("CARGO_MANIFEST_DIR").unwrap(),
);

at the top of your #[test] that runs trybuild and then this in the macro:

let cargo_manifest_dir = std::env::var_os("CARGO_MANIFEST_DIR_OVERRIDE")
    .or(std::env::var_os("CARGO_MANIFEST_DIR"))
    .unwrap();

@msrd0
Copy link
Author

msrd0 commented Oct 19, 2022

This works, thanks. Just one problem that I noticed: My macro outputs "Help: Searching in directory $CARGO_MANIFEST_DIR" when the file it was looking for doesn't exist, which by its nature, will not always be the same path. However, trybuild for some reason doesn't replace that directory with $DIR but leaves the path untouched. Previously, it would output $DIR/target/tests/<crate-name> so maybe there's a problem where $DIR isn't used when there's no additional path segments behind it?

@msrd0
Copy link
Author

msrd0 commented Oct 20, 2022

I think the replacement isn't happening because of this: https://github.com/dtolnay/trybuild/blob/master/src/directory.rs#L18

Is there a particular reason why you only replace the source directory with trailing slashes?

@dtolnay
Copy link
Owner

dtolnay commented Oct 26, 2022

Filed separately as #204.

@dtolnay dtolnay closed this as completed Oct 26, 2022
Repository owner locked and limited conversation to collaborators Mar 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants