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

Create a fixture from files attribute #233

Open
Kranzes opened this issue Mar 26, 2024 · 3 comments
Open

Create a fixture from files attribute #233

Kranzes opened this issue Mar 26, 2024 · 3 comments

Comments

@Kranzes
Copy link

Kranzes commented Mar 26, 2024

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

@la10736
Copy link
Owner

la10736 commented Mar 27, 2024

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

@Kranzes
Copy link
Author

Kranzes commented Mar 27, 2024

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
Copy link
Owner

la10736 commented Apr 9, 2024

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.

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