Skip to content

Conversation

@Stavrospanakakis
Copy link
Contributor

This commit refactors the confirmation prompt to a separate pkg.

Signed-off-by: Stavros Panakakis stavrospanakakis@gmail.com


func TestConfirm(t *testing.T) {
writeToPrompt := func(answer []byte) error {
tmpfile, err := ioutil.TempFile("", "tmp")
Copy link
Owner

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?

Copy link
Contributor Author

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)
			}
		}
	})
}

Copy link
Owner

@marcosnils marcosnils Apr 11, 2022

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loved it, thx ❤️

Copy link
Owner

@marcosnils marcosnils left a 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 👀 ?

@sirlatrom
Copy link
Collaborator

sirlatrom commented Apr 13, 2022

How about providing the io.Reader as a parameter to Confirm instead?

@marcosnils
Copy link
Owner

How about providing the io.Reader as a parameter to Confirm instead?

Check #132 (comment). I though it was easier to handle it separately 🙏

@Stavrospanakakis Stavrospanakakis force-pushed the refactor-prompt-new-pkg branch from 9bdf104 to 5f7c17d Compare July 9, 2022 13:51
@Stavrospanakakis
Copy link
Contributor Author

The branch had conflicts so I rebased :)

@sirlatrom sirlatrom merged commit 4d8bde3 into marcosnils:master Jul 9, 2022
@Stavrospanakakis Stavrospanakakis deleted the refactor-prompt-new-pkg branch July 9, 2022 15:54
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

Successfully merging this pull request may close these issues.

3 participants