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

Support using output of one command to match output of next #169

Open
cloudhead opened this issue Nov 17, 2022 · 6 comments
Open

Support using output of one command to match output of next #169

cloudhead opened this issue Nov 17, 2022 · 6 comments
Labels
A-trycmd Area: trycmd package enhancement Improve the expected

Comments

@cloudhead
Copy link

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.

@epage
Copy link
Contributor

epage commented Nov 17, 2022

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.

@cloudhead
Copy link
Author

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?

@cloudhead
Copy link
Author

Otherwise I think I'd have either of two options:

  1. Go straight to assert_cmd and assert against rust strings (since I don't think I'd get much out of snapbox), or
  2. Use a non-rust tool, eg. cram or 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.

@epage
Copy link
Contributor

epage commented Nov 18, 2022

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.

@epage epage added A-trycmd enhancement Improve the expected labels Nov 18, 2022
@cloudhead
Copy link
Author

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?

@epage
Copy link
Contributor

epage commented Nov 22, 2022

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

@settings settings bot removed the A-trycmd label Aug 14, 2023
@epage epage added the A-trycmd Area: trycmd package label Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trycmd Area: trycmd package enhancement Improve the expected
Projects
None yet
Development

No branches or pull requests

2 participants