Semi-persistent, scoped test directories

Related tags

Command-line testdir
Overview

Semi-persistent, scoped test directories

This crate aims to make it easier to use temporary directories in tests, primarily by calling the testdir!() macro somewhere in a test function. The direcetories are structured per-scope and per-test and will be available for inspection after the test has finished. On subsequent test runs older generations of test directories will be cleaned up.

If you've ever used pytest's tmp_path or tmpdir fixtures this pattern should be similar.

By default test directories are created in cargo's target directory:

target/testdir-$N/module/path/test_name

Here $N is an integer generation number increasing with each cargo test invocation. Generations older than the 8 most recent ones are removed on the next cargo test run.

There is also a symlink pointing to the most recent generation:

target/testdir-current -> testdir-$N`

Example

Even when executing this with cargo test --jobs=1 these tests will pass as each gets their own unique directory:

// E.g. in lib.rs

mod tests {
    use std::path::PathBuf;
    use testdir::testdir;

    #[test]
    fn test_write() {
        let dir: PathBuf = testdir!();
        let path = dir.join("hello.txt");
        std::fs::write(&path, "hi there").ok();
        assert!(path.exists());
    }
    
    #[test]
    fn test_read() {
        let dir: PathBuf = testdir!();
        let path = dir.join("hello.txt");
        assert!(!path.exists());
    }
}

Afterwards you can inspect the directories and they should look something like this:

$ tree target/
target/
+- testdir-0
|    +- cratename
|         +- tests
|              +- test_read
|              +- test_write
|                   +- hello.txt
+- testdir-current -> testdir-0

Feedback and contributing

The code lives in a git repository at https://github.com/flub/testdir from which you can create issues, clone the repository and create pull requests.

You might also like...
Find and clean heavy build or cache directories.
Find and clean heavy build or cache directories.

ProjClean Find and clean heavy build or cache directories. ProjClean finds directories such as node_modules(node), target(rust), build(java) and their

Tool for managing dotfiles directories; Heavily based on rcm.

Paro paro : to prepare, get ready / set, put / furnish, supply. Tool for managing dotfiles directories; Heavily based on rcm. TODO Rust Boilerplate CI

Quickly find all blackhole directories with a huge amount of filesystem entries in a flat structure
Quickly find all blackhole directories with a huge amount of filesystem entries in a flat structure

findlargedir About Findlargedir is a tool specifically written to help quickly identify "black hole" directories on an any filesystem having more than

Wraps cargo to move target directories to a central location [super experimental]

targo Wraps cargo to move target directories to a central location [super experimental] To use, cargo install --git https://github.com/sunshowers/targ

Temporary files and directories with UTF-8 paths.

camino-tempfile A secure, cross-platform, temporary file library for Rust with UTF-8 paths. This crate is a wrapper around tempfile that works with th

Bookmark directories for easy directory-hopping in the terminal
Bookmark directories for easy directory-hopping in the terminal

markd Bookmark directories for easy directory-hopping in the terminal. All it takes is one command markd to bookmark your current directory, or use th

Utility to display a tree view of directories.

TreeCraft v0.2.3 (16 October 2023) TreeCraft is a command-line utility written in pure Rust that helps you visualize directory structures in ASCII for

Remove files or directories.

Wrm - Remove files or directories Installation Run the following Cargo command: cargo install wrm Usage To move files to trash($HOME/.local/share/wrm

Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're working with files or directories, this tool simplifies common file operations with ease.

Rust FileOps Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're w

Owner
Floris Bruynooghe
Floris Bruynooghe
A simple solution for scoped styles in Leptos

Styled: Easy Styling for Leptos Components If you're looking for an easy way to apply scoped styles to your Leptos components, Styled is the Leptos ma

Eran Boodnero 8 Mar 5, 2023
A quick-and-dirty attempt to get scoped tasks in Rust.

scoped_tasks_prototype A quick-and-dirty attempt to get scoped tasks in Rust. This library tries to provide an interface similar to scoped threads, ac

Olivier FAURE 8 Jun 6, 2023
Shellharden is a syntax highlighter and a tool to semi-automate the rewriting of scripts to ShellCheck conformance, mainly focused on quoting

Shellharden is a syntax highlighter and a tool to semi-automate the rewriting of scripts to ShellCheck conformance, mainly focused on quoting

Andreas Nordal 4.3k Dec 28, 2022
A collection of semi-useful tools made for GNU/Linux

DECTOOLS A collection of semi-useful tools made for GNU/Linux. Some may work on macOS, though functionality isn't a priority. Depenencies: python, bas

Decator 3 Jun 8, 2022
Simple calculator REPL, similar to bc(1), with syntax highlighting and persistent history

eva simple calculator REPL, similar to bc(1), with syntax highlighting and persistent history installation Homebrew $ brew install eva crates.io $ car

Akshay 632 Jan 4, 2023
Collection of immutable and persistent data structures written in Rust, inspired by the standard libraries found in Haskell, Closure and OCaml

PRust: (P)ersistent & Immutable Data Structures in (Rust) This library houses a collection of immutable and persistent data structures, inspired by th

Victor Colombo 13 Aug 13, 2023
Coppers is a custom test harnass for Rust that measures the energy usage of your test suite.

Coppers Coppers is a test harness for Rust that can measure the evolution of power consumptions of a Rust program between different versions with the

Thijs Raymakers 175 Dec 4, 2022
A cli tool to download specific GitHub directories or files

cloneit A cli tool to download specific GitHub directories or files. Installation From git git clone https://github.com/alok8bb/cloneit cd cloneit car

Alok 54 Dec 20, 2022
Voila is a domain-specific language launched through CLI tool for operating with files and directories in massive amounts in a fast & reliable way.

Voila is a domain-specific language designed for doing complex operations to folders & files. It is based on a CLI tool, although you can write your V

Guillem Jara 86 Dec 12, 2022
RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions

RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions. Features Batch rename files and direct

Ismael González Valverde 219 Dec 31, 2022