la10736 / rstest

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

Create a fixture from `files` attribute #233

Open Kranzes opened 8 months ago

Kranzes commented 8 months ago

I want to be able to create a fixture by reusing the files attribute logic from the rstest attribute.

la10736 commented 8 months ago

Sorry, but I cannot understand what you want to do... can you write an example that show what you want to do?

Kranzes commented 8 months ago

I want to use the files feature to generate a bunch of fixtures that will construct a struct with serde and some other methods from the PathBuf from files.

Here's an example: I want to basically generate lots of fixtures (for each file) that inside their body do everything until this part line https://github.com/Kranzes/tvix-narinfo-thingamabob/blob/8c991a6ed4df882cbd328a35c19d78f66f57d379/src/lib.rs#L114 to create the HashMap, the fixture will return HashMap with the struct data generated for each file.

Right now I will have to do all this part every time I want to use a HashMap of the struct. I guess I could create a a singular fixture that returns a HashMap<PathBuf,HashMap<StorePathRef,NarInfo> or whatever, but I think having multiple fixtures make more sense, sort of like a matrix.

la10736 commented 7 months ago

Ok, maybe a good syntax could be something like:

    fn nar_info(path: PathBuf) -> HashMap<String, Closure> {
        let attrs_file = File::open(path).unwrap();
        let reader = BufReader::new(attrs_file);
        let attrs: Attrs = serde_json::from_reader(reader).unwrap();

        attrs
            .closure
            .iter()
            .map(|c| {
                let c = c.to_narinfo().unwrap();
                (c.store_path, c)
            })
            .collect()
    }

    #[rstest]
    fn all_references_uploaded(#[files("src/fixtures/*.json")] #[map(nar_infos)] nar_infos: HashMap<String, Closure>) {
        let all_references = nar_infos
            .values()
            .flat_map(|x| x.references.clone())
            .collect::<HashSet<_>>();
        let uploaded = do_work(make_work(nar_infos));

        assert_eq!(all_references, uploaded);
    }

where #[map(...)] act like map(...) for iterators.