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

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

Open
thoughtpolice opened this issue Sep 1, 2024 · 2 comments

Comments

@thoughtpolice
Copy link

thoughtpolice commented Sep 1, 2024

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

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
Copy link
Sponsor Collaborator

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...

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