If you want to test your project against some sample data it might be too tedious to deal with all the setup - you might need to clone some repo and clean it up afterwards. Rust already has crates, so can we use them for that?

Suppose we want to distribute some images to test our tool against. For some reason we want super huge images and maybe a lot of them, maybe for performance testing. This would not be wise to check into our main repo.

Let's assume we placed the images in images/ next to our src/ dir (at the crate root)

Let's start be creating a test crate (library) - I named it testdata:

cargo init --lib

#![feature(option_flattening)]
use std::path::PathBuf;

pub fn get_project_path() -> Option<PathBuf> {
    PathBuf::from(file!()).parent()
    .map(|p| p.parent())
    .flatten()
    .map(|p| p.join("images"))
}


#[test]
fn test_path() {
    dbg!(get_project_path());
}

Pretty simple, no? The main part is just the file!() macro. This returns the absolute path to lib.rs whether it is local or compiled somewhere on your computer or whatever shit you run this on.

In our actual crate, include the test crate either from crates.io, locally or from git - whatever you like. See here for more info:

https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html

We specify the dependencies as dev-dependencies so they do not get build on a regular build:

[dev-dependencies]
testdata = { path = "../testdata"}
#testdata = { git = "https://github..."}

Then in main.rs:

#[cfg(test)]
use testdata;
#[cfg(test)]
use std::fs;

fn main() {
    println!("Yo");
}

#[test]
fn show_pics() {
    let dir = testdata::get_project_path().unwrap();
    for entry in fs::read_dir(dir).unwrap(){
        println!("{:?}", entry.unwrap());
    }
}

If you then run your test with cargo test -- --nocapture (shows regular output as opposed to cargo test) you should now get access/see all your images supplied in your images/ folder of your testdata crate.