trycmd
trycmd copied to clipboard
Support using output of one command to match output of next
I was wondering if there was any functionality that would allow using command output to bind a variable, such that it could be used to test a subsequent command? For example, given a command to generate some key and list generated keys:
Generate two keys:
$ gen-key
yD0zydG611bl6RzwLutdnGd7zfgj6fdNDDreI786
$ gen-key
0zydG611bl6RzwLutdnGd7zfgj6fdNDDreI786UH
Then list all your keys:
$ list-keys
yD0zydG611bl6RzwLutdnGd7zfgj6fdNDDreI786
0zydG611bl6RzwLutdnGd7zfgj6fdNDDreI786UH
One way I imagine this could work is if we could bind variables in the output, for example:
$ gen-key
[KEY1]
$ gen-key
[KEY2]
$ list-keys
[KEY1]
[KEY2]
The first [KEY1] and [KEY2] would bind new variables. The second [KEY1] and [KEY2] (in list-keys) would expect the value to equal the bound variables.
Without any way to use command output in the test, it requires the tests to be implemented partly in rust and partly in .md files, which is cumbersome, whenever there is output that can't be known in advance.
Thoughts? I'm happy to help implement something like this if it makes sense for this project.
This isn't supported. If we do go this route, id recommend it be [..KEY..] as .. is used for wild cards, this would allow named wildcards where future instances must match the first.
I do wonder if doing this in rust with snapbox would be a better fit. Trying to do arbitrary cases isn't a forte of trycmd.
I was looking at the snapbox docs, but the main value I'm getting out of trycmd is the ability to format tests in markdown files, and I couldn't find that in snapbox. Is there a way I can parse these .md files via rust and use snapbox to run the tests with some customized behavior?
Otherwise I think I'd have either of two options:
- Go straight to
assert_cmdand assert against rust strings (since I don't think I'd get much out of snapbox), or - Use a non-rust tool, eg.
cramor something like that
The holy grail would obviously be a rust tool (ie. something that integrates well with cargo) that doesn't force me to use string syntax inside .rs files for stdout tests; which trycmd is pretty close to, modulo some limitations.
Curious to hear your thoughts.
Go straight to assert_cmd and assert against rust strings (since I don't think I'd get much out of snapbox), or
For me, I've found that I don't need the extra flexibility of assert_cmd and the output for snapbox is easier to read. I've switched all of my tests over to either snapbox or trycmd
Use a non-rust tool, eg. cram or something like that
The advantage of cram is that you can run do nearly everything because it runs in a shell. The downside is it isn't cross platform. The main challenge with trycmd is remaining cross platform.
I will say being able to support these kinds of cases would be nice but my time is limited atm and my focus is elsewhere but I will review things people work on towards it.
For me, I've found that I don't need the extra flexibility of assert_cmd and the output for snapbox is easier to read. I've switched all of my tests over to either snapbox or trycmd
Do you have examples of using snapbox directly that I can look at?
For typos, I use snapbox for codegen
Cargo also has a lot of tests with snapbox, like for cargo add
cargo edit similarly uses snapbox
See also snapbox's dependents