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

New test files are only recognized after cargo clean #256

Closed
GordianDziwis opened this issue May 23, 2024 · 4 comments
Closed

New test files are only recognized after cargo clean #256

GordianDziwis opened this issue May 23, 2024 · 4 comments

Comments

@GordianDziwis
Copy link

I have a test with the files macro:

  #[rstest]
    fn from(
        #[files("tests/rdf-diagram/*.drawio")] path: PathBuf,
    ) -> anyhow::Result<()> {}

When add/remove a test file, cargo test only shows the updated tests after a cargo clean.

Is this by design, a bug, or a user error?

@la10736
Copy link
Owner

la10736 commented May 24, 2024

Unfortunately is not possible to automatize it without writing a build.rs script. I would like to avoid the use of this kind of stuff and ask to the user to introduce a build.rs script in his project just for testing.

But, I'll investigate how to write it and document how to add this support to the project: the idea here is to navigate the code find the #[rstest]'s marked functions with #[files] arguments, take the results and hash them in a key. Use these keys to change the source hash of the build process.

@GordianDziwis
Copy link
Author

Do you have an example for an build.rs script achieving this?

@la10736
Copy link
Owner

la10736 commented May 26, 2024

No, it's just an idea sketch ... I'll try to write it next weeks (I've not too much time to work on it).

I've putted it here just as my notes.

@la10736
Copy link
Owner

la10736 commented Jun 2, 2024

Ok, I've studied it a little bit and I found that there's a simple solution that should work in the 99% of the cases with just a little effort.

Suppose that you have a project where your test files live in my/resource/folder relative to Cargo.toml manifest project file. Should be enough to add the following line to your build.rs file:

    println!("cargo::rerun-if-changed=my/resource/folder")

You can add a line like this for every folder you need to watch.

Follow a concrete example where your tests files live in resource/tests and in integration_tests/examples folders. If you don't have any build.rs script create one in your crate folder with the following content:

pub fn main() {
    println!("cargo::rerun-if-changed=resource/tests")
    println!("cargo::rerun-if-changed=integration_tests/examples")
}

Maybe in the future I'll write a create to une in build.rs scripts that scan the source to search the #[rstest]'s #[files(...)] attributes and emit these lines in standard output. This crate will automatize this process but my guess that is not a big deal parse all source in build.rs and the handwritten way maybe is the best solution almost of the times.

tvlbot pushed a commit to tvlfyi/tvix that referenced this issue Jun 5, 2024
Context: la10736/rstest#256

Cargo will now cause a rebuild whenever a new test case file is added.
Previsouly running `cargo test` after adding a new test case resulted in
that case not being picked up and ignored.

Change-Id: Ibfc420b5bfe3f3ee41d3ebd3fb9d248819fa6ed9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11751
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Autosubmit: Ilan Joselevich <personal@ilanjoselevich.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants