huh
huh copied to clipboard
Adding a test package to simplify testing interactive commands
Is your feature request related to a problem? Please describe.
It is not, but unit-testing huh
required some work when I was building it into our CLI. I wrote an 'answering machine' that supplies the input stream of huh inputs based on the output
Describe the solution you'd like
I'd love to write a test package that provides the user with 2 streams to give to huh
's WithInput
and WithOutput
methods, with settings to respond to certain inputs. Maybe something like:
package main
import "github.com/charmbracelet/huh/huhtest"
func TestHuhForm_DoesWhatItIsSupposedToDo(t *testing.T) {
var formIn io.PipeReader
var formOut io.PipeWriter
var close func()
formIn, formOut, close = huhtest.NewAnsweringMachine().
AddResponse("How Are You Feeling?", "Amazing Thanks!").
AddResponse("Would you like a drink?", huhtest.ConfirmNegative).
Times(2).
AddResponse("Please pick all options that apply", []string{"a", "b", "c"}).
Once().
AddResponses("Are you OK? (gonna ask you twice)", []string{"yes", "yes for sure"}).
Start()
// Alternative available: SetResponses(map[string][]string)
defer formIn.Close()
defer formOut.Close()
defer close()
// Act
err := myForm.WithInput(formIn).WithOutput(formOut).Run()
// Assert
// [...]
}
Describe alternatives you've considered
Turning it into a separate library instead of incorporating it as a contribution here, which I'm fine with as well
Additional context
I'm asking it to see if this is the direction that you might be interested in exploring and what your thoughts are on such a framework