pacak / bpaf

Command line parser with applicative interface
337 stars 17 forks source link

Feature request: a way to programmatically test the completions #312

Closed VorpalBlade closed 8 months ago

VorpalBlade commented 10 months ago

https://docs.rs/bpaf/latest/bpaf/#testing-your-parsers-and-running-them lists a few ways to test the parser. But what about testing completions?

I could probably inspect what flags the generated completion uses, and then use that directly when testing the parser, but I presume that is not a public API to be relied upon.

What is the recommended way (if any) to write tests for the completion?

I don't want to make a integration test that executes the actual binary: that doesn't work at all when cross compiling with cross (it runs the tests under qemu user mode emulation).

pacak commented 10 months ago

First you can take a look at completion tests - they are using public API only. set_comp with revision 0 generates some textual shell agnostic representation for completion bpaf would generate. 0th revision is something I consider to be public API and not planning to remove or change. If you want to test your part of the completion without running the binary - that's where I would start.

Then there's comtester crate. You'll probably have to clone it and add to your repo in some way, but it can configure the completion environment, run each command line in a separate shell and collect the results. You'll have to make sure shells you want to test on are available. I'm planning to make changes to it to make it more end user friendly, big documentation update I'm working on is somewhat related to this change.

And finally https://crates.io/crates/completest crate - a fork of comptester made by Ed Page. It should be more user friendly than comptester but I haven't look much in the details.

Later two methods require running a binary and test all the things end to end, they take longer to run though.