moksha_core/
fixture.rs

1//! This module defines helper functions for loading fixtures in tests.
2//!
3//! The `read_fixture` function reads a fixture file from the `src/fixtures` directory relative to the Cargo manifest directory. The function takes a `name` argument that specifies the name of the fixture file to read. The function returns a `Result` containing the contents of the fixture file as a `String`.
4//!
5//! The `read_fixture_as` function is a generic function that reads a fixture file and deserializes its contents into a value of type `T`. The function takes a `name` argument that specifies the name of the fixture file to read, and a type parameter `T` that specifies the type to deserialize the fixture contents into. The function returns a `Result` containing the deserialized value.
6//!
7//! Both functions return an `anyhow::Result`, which allows for easy error handling using the `?` operator. The functions are intended to be used in tests to load fixture data for testing purposes.
8pub fn read_fixture(name: &str) -> anyhow::Result<String> {
9    let base_dir = std::env::var("CARGO_MANIFEST_DIR")?;
10    let raw_token = std::fs::read_to_string(format!("{base_dir}/src/fixtures/{name}"))?;
11    Ok(raw_token.trim().to_string())
12}
13
14pub fn read_fixture_as<T>(name: &str) -> anyhow::Result<T>
15where
16    T: serde::de::DeserializeOwned,
17{
18    Ok(serde_json::from_str::<T>(&read_fixture(name)?)?)
19}