-
Notifications
You must be signed in to change notification settings - Fork 59
Refactor confirmation prompt to separate pkg #132
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
Refactor confirmation prompt to separate pkg #132
Conversation
pkg/prompt/prompt_test.go
Outdated
|
|
||
| func TestConfirm(t *testing.T) { | ||
| writeToPrompt := func(answer []byte) error { | ||
| tmpfile, err := ioutil.TempFile("", "tmp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking about the reason to have a tmpfile here. Why couldn't we just convert the answer to an io.Reader and since we're extracting this to a package, we can make the Confirm function use the reader as well instead of os.Stdin.
thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean something like this?
// prompt.go
package prompt
.
.
.
func Confirm(rd io.Reader, message string) error {
fmt.Printf("\n%s [Y/n] ", message)
reader := bufio.NewReader(rd)
.
.
}// prune.go
package cmd
.
.
.
func newPruneCmd() *pruneCmd {
.
.
.
.
if !root.opts.force {
err := prompt.Confirm(os.Stdin, "The following paths will be removed. Continue?")
if err != nil {
return err
}
}
.
. // prompt_test.go
func TestConfirm(t *testing.T) {
t.Run("User confirms that wants to continue", func(t *testing.T) {
answers := [][]byte{
[]byte("Y\n"),
[]byte("y\n"),
[]byte("\n"),
}
for _, answer := range answers {
rd := bytes.NewReader(answer)
err := Confirm(rd, "Do you want to continue?")
if err != nil {
t.Fatal(err)
}
}
})
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sending os.Stdin to Confirm might be unnecessary. Why not just declaring the stdin at the package level and then making the test override it to a custom io.Reader?
something like:
--- main.go ---
var stdin io.Reader = os.Stdin
func Confirm(prompt string) error {
// use stdin here
}--- main_test.go ---
func TestConfirm (t *testing.T) {
stdin = bytes.NewReader([]byte("whatever"))
// test confirm here
}thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loved it, thx ❤️
marcosnils
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM!
cc @sirlatrom @korpa @breml 👀 ?
|
How about providing the io.Reader as a parameter to Confirm instead? |
Check #132 (comment). I though it was easier to handle it separately 🙏 |
9bdf104 to
5f7c17d
Compare
|
The branch had conflicts so I rebased :) |
This commit refactors the confirmation prompt to a separate pkg.
Signed-off-by: Stavros Panakakis stavrospanakakis@gmail.com