mitsuhiko / insta

A snapshot testing library for rust
https://insta.rs
Apache License 2.0
2.13k stars 95 forks source link

`INSTA_WORKSPACE_ROOT` should also work with `assert_snapshot!` at runtime, for manual test binaries #575

Open thoughtpolice opened 2 weeks ago

thoughtpolice commented 2 weeks ago

I am evaluating insta with a project that uses Buck2 (and also a very happy user using it with Cargo as part of Jujutsu). When using a project with Buck2, Cargo is not used, and so tests for example are run "manually" by the build system itself after compilation with --cfg=test

Currently, I'm using inline snapshots, with a test like this:

#[cfg(test)]
mod tests {
    fn split_words(s: &str) -> Vec<&str> {
        s.split_whitespace().collect()
    }

    #[test]
    fn test_split_words() {
        let words = split_words("goodbye from the other side");
        insta::assert_snapshot!(format!("{:?}", words), @"");
    }
}

By default, when I compile this with buck2 build, I get an error like so:

error: environment variable `CARGO_MANIFEST_DIR` not defined at compile time
   --> src/qq-cli/commands/script.rs:148:9
    |
148 |         insta::assert_snapshot!(format!("{:?}", words), @"");
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Buck2 executes all tests from the root of the project, so luckily I can set CARGO_MANIFEST_DIR=. during the compilation of these tests which solves the problem. But it's a bit ugly I think.

While using Code Search to browse the source code to figure out how the macro worked, it looks like INSTA_WORKSPACE_ROOT also has a similar function. It would at least be more explanatory when I write INSTA_WORKSPACE_ROOT=. in the source code why it exists. However, it seems like it only works when you use cargo insta.

Alternatively, some other more general form or utility for assert_snapshot!() to work in the presence of non-Cargo systems would be nice.

thoughtpolice commented 2 weeks ago

Oh, apparently I need both CARGO_MANIFEST_DIR and INSTA_WORKSPACE_RULE for my tests to work. So my Buck2 rule for building insta tests looks like this (roughly):

rust_test(
    name = 'qq-cli-tests',
    srcs = glob(['**/*.rs']),
    deps = COMMON_DEPS,
    env = {
        'CARGO_MANIFEST_DIR': '.',
        'INSTA_WORKSPACE_ROOT': '.',
    }
)
max-sixty commented 2 weeks ago

Thanks for the issue.

If we checked INSTA_WORKSPACE_ROOT before CARGO_MANIFEST_DIR, would that solve everything here? I haven't worked with rust without cargo so I don't have much context on the standard approach for figuring things out such as the root of the workspace...