assert-rs / snapbox

Snapshot testing for a herd of CLI tests
docs.rs/trycmd
Apache License 2.0
137 stars 18 forks source link

trycmd: allow access to environment variables within a trycmd command-line #365

Open sebhoss opened 2 months ago

sebhoss commented 2 months ago

I'm trying to define a variable with contains a random value and want to inject it somehow into tests executed by trycmd. I can mask the value with insert_var(..) and tried to use env(..) to make the variable accessible in my tests but something like $ echo $TEST_VALUE never substitutes the variable with the value given to env(..). Is there some way to access the value behind a variable?

epage commented 2 months ago

If I'm understanding correctly, you are wanting to access environment variables on the "command-line" as these are inputs that are determined at runtime?

sebhoss commented 2 months ago

yeah exactly! A simplified code example looks like this:

let key = "some-randomly-generated-value";

trycmd::TestCases::new()
            .case("tests/cmd/secrets/*.md")
            .env("KEY", key)
            .insert_var("[KEY]", key)
            .unwrap();

and I want to run a test like this:

$ bin-command secrets add $KEY
key '[KEY]' added

my current workaround is to write the generated value into a file, add that file to gitignore (so that it won't get flagged by security scans..) and allow my add subcommand to read from files

epage commented 1 month ago

As we have another concept of substitutions (also called redactions) in terms of insert_var, I've clarified the issue name.

This is another part in the overall story thats building up in issues of "it would be nice if we had an embedded shell to work with". We have requests for pipes (#172) and some common unix built-ins (e.g. #168, #43)