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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering output support #25

Open
prologic opened this issue Sep 23, 2022 · 9 comments
Open

Filtering output support #25

prologic opened this issue Sep 23, 2022 · 9 comments

Comments

@prologic
Copy link

Hey 馃憢

Been using your package here to try to write some cli integration tests for my project box and am running into a few issues that I think could be solved by implementing support for filtering output with either regular expressions or glob patterns.

See for example Mercurial's test runner and the section on Filtering output

Would you consider adding this support? 馃

@prologic
Copy link
Author

Here is an implementation of something similar: https://github.com/echlebek/grill/blob/b16dbd1a097be186cad7e5cf840ab802e4cfaa3a/internal/grill/diff.go which we could borrow from?

@jba
Copy link
Collaborator

jba commented Sep 23, 2022

I agree that filtering support would be nice. I don't think the (glob) and (re) suffixes are the right approach, however. The biggest problem is, what happens when you do -update? The nice thing about updating is that once you're sure your changes are correct, you can update your tests in one command with new verbatim output. I can't imagine that being smart enough to preserve globs and regexps.

I think it's better to filter in code, because you can be more precise and more general. For example, https://go.googlesource.com/vuln/+/refs/heads/master/cmd/govulncheck/main_command_118_test.go#56.
Maybe there is an API that would make writing those filters simpler.

Not sure I understand the relevance of the diff code.

@prologic
Copy link
Author

Ahh I see what you mean and what you're doing. I'm writing all my tests purely in .ct files. Maybe some some specific tests with output that changes I might be better writing those ones in Go but using the cmdtest facilities to run the binary?

@prologic
Copy link
Author

The issue I have I guess is that if I have to write normal Go code to test some parts of my CLI (becuase they produce outputs that are not predictable) I may as well write all the tests in Go and this devalues your cmdtest package and its utility hmmm 馃

@jba
Copy link
Collaborator

jba commented Sep 23, 2022

I haven't found that it devalues the package. There is still plenty for it to do. Notice that the tests are still encoded in .ct files, as commands and responses. I just post-process the command output in one place to generalize it.

@prologic
Copy link
Author

For this ti work well, I would have to split out my test execution into per-directory tests containing *.ct files right? And have one func TestXXX per group of tests, where some may just run with no post-processing, whilsts others may perform some post-processing?

@jba
Copy link
Collaborator

jba commented Sep 26, 2022

I don't think so, but I may not understand your situation.

Here's an example of what I'm saying: imagine you're testing a binary called cli that sometimes prints absolute paths. Your test file looks like

$ cli somefile
wrote /home/prologic/output/somefile.out

But when I run it I get /home/jba/output/somefile.out, so the test fails.

I'm suggesting that you define cli in your test setup to run the real cli, then filter the output, replacing lines like

wrote /home/prologic/output/somefile.out

with

wrote .../somefile.out

I don't think that requires that you split your tests into different directories, or that your run some tests with post-processing and some without. But if that were necessary, it might be easier to define a separate command cli-pp that does post-processing.

Again, maybe I don't understand your use case.

@prologic
Copy link
Author

Hmm I see....

@marco-m
Copy link

marco-m commented Aug 10, 2023

@prologic in case you are not aware, https://github.com/rogpeppe/go-internal#testscript might do what you are looking for, since it supports regexp for matching the output.

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

3 participants