la10736 / rstest

Fixture-based test framework for Rust
Apache License 2.0
1.21k stars 43 forks source link

New test files are only recognized after `cargo clean` #256

Closed GordianDziwis closed 5 months ago

GordianDziwis commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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

la10736 commented 6 months ago

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 commented 5 months ago

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.