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

Adding a test package to simplify testing interactive commands #300

Closed
survivorbat opened this issue Jul 9, 2024 · 3 comments
Closed

Adding a test package to simplify testing interactive commands #300

survivorbat opened this issue Jul 9, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@survivorbat
Copy link

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

@survivorbat
Copy link
Author

Made a package that does this: https://github.com/survivorbat/huhtest

Seems to work fine for my use-cases :)

@caarlos0 caarlos0 added the enhancement New feature or request label Nov 26, 2024
@caarlos0
Copy link
Member

That's a cool idea, and you did it!

I think we can close this, then?

Amazing work btw, and sorry for the delay responding 🙏🏻

@survivorbat
Copy link
Author

Hey @caarlos0, tnx! Sure, I'll close it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants