Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
(fix): editor blocking testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jun 29, 2021
1 parent 2bbbbea commit 959b32a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 61 deletions.
4 changes: 2 additions & 2 deletions _tasks.hcl
@@ -1,13 +1,13 @@
task "install-deps" {
description = "Install all of package dependencies"
pipeline = [
"go get {{.files}}",
"go get -v {{.files}}",
]
}

task "tests" {
description = "Run the test suite"
command = "go test {{.files}}"
command = "go test -v {{.files}}"
environment = {
GOFLAGS = "-mod=vendor"
}
Expand Down
12 changes: 4 additions & 8 deletions editor_test.go
Expand Up @@ -102,10 +102,6 @@ func TestEditorRender(t *testing.T) {
}

func TestEditorPrompt(t *testing.T) {
if os.Getenv("SKIP_EDITOR_PROMPT_TESTS") != "" {
t.Skip("editor prompt tests skipped by dev")
}

if _, err := exec.LookPath("vi"); err != nil {
t.Skip("vi not found in PATH")
}
Expand All @@ -122,7 +118,7 @@ func TestEditorPrompt(t *testing.T) {
c.SendLine("")
go c.ExpectEOF()
time.Sleep(time.Millisecond)
c.Send("iAdd editor prompt tests\x1b")
c.Send("ccAdd editor prompt tests\x1b")
c.SendLine(":wq!")
},
"Add editor prompt tests\n",
Expand Down Expand Up @@ -155,7 +151,7 @@ func TestEditorPrompt(t *testing.T) {
c.SendLine("")
go c.ExpectEOF()
time.Sleep(time.Millisecond)
c.Send("iAdd editor prompt tests\x1b")
c.Send("ccAdd editor prompt tests\x1b")
c.SendLine(":wq!")
},
"Add editor prompt tests\n",
Expand Down Expand Up @@ -196,7 +192,7 @@ func TestEditorPrompt(t *testing.T) {
c.SendLine("")
go c.ExpectEOF()
time.Sleep(time.Millisecond)
c.Send("iAdd editor prompt tests\x1b")
c.Send("ccAdd editor prompt tests\x1b")
c.SendLine(":wq!")
},
"Add editor prompt tests\n",
Expand Down Expand Up @@ -230,7 +226,7 @@ func TestEditorPrompt(t *testing.T) {
c.SendLine("")
go c.ExpectEOF()
time.Sleep(time.Millisecond)
c.Send("iAdd editor prompt tests\x1b")
c.Send("ccAdd editor prompt tests\x1b")
c.SendLine(":wq!")
},
"Add editor prompt tests\n",
Expand Down
78 changes: 27 additions & 51 deletions survey_test.go
Expand Up @@ -2,8 +2,6 @@ package survey

import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -134,23 +132,6 @@ func TestPagination_lastHalf(t *testing.T) {
}

func TestAsk(t *testing.T) {

_, err := exec.LookPath("vi")
noEditor := err != nil || os.Getenv("SKIP_EDITOR_PROMPT_TESTS") != ""

filter := func(qs []*Question) []*Question { return qs }
if noEditor {
filter = func(qs []*Question) []*Question {
r := make([]*Question, 0)
for _, q := range qs {
if _, ok := q.Prompt.(*Editor); !ok {
r = append(r, q)
}
}
return r
}
}

tests := []struct {
name string
questions []*Question
Expand All @@ -159,7 +140,7 @@ func TestAsk(t *testing.T) {
}{
{
"Test Ask for all prompts",
filter([]*Question{
[]*Question{
{
Name: "pizza",
Prompt: &Confirm{
Expand Down Expand Up @@ -215,35 +196,35 @@ func TestAsk(t *testing.T) {
Options: []string{"red", "blue", "green", "yellow"},
},
},
}),
},
func(c *expect.Console) {
// Confirm
c.ExpectString("Is pizza your favorite food? (y/N)")
c.SendLine("Y")

if !noEditor {
// Editor
c.ExpectString("Edit git commit message [Enter to launch editor]")
c.SendLine("")
time.Sleep(time.Millisecond)
c.Send("iAdd editor prompt tests\x1b")
c.SendLine(":wq!")

// Editor validated
c.ExpectString("Edit git commit message [Enter to launch editor]")
c.SendLine("")
time.Sleep(time.Millisecond)
c.Send("i invalid input first try\x1b")
c.SendLine(":wq!")
time.Sleep(time.Millisecond)
c.ExpectString("invalid error message")
c.ExpectString("Edit git commit message [Enter to launch editor]")
c.SendLine("")
time.Sleep(time.Millisecond)
c.ExpectString("first try")
c.Send("iAdd editor prompt tests\x1b")
c.SendLine(":wq!")
}
// Editor
c.ExpectString("Edit git commit message [Enter to launch editor]")
c.SendLine("")
time.Sleep(time.Millisecond)
c.SendLine(":d")
c.Send(string(terminal.SpecialKeyEnd))
c.Send("iAdd editor prompt tests\x1b")
c.SendLine(":wq!")

// Editor validated
c.ExpectString("Edit git commit message [Enter to launch editor]")
c.SendLine("")
time.Sleep(time.Millisecond)
c.Send("i invalid input first try\x1b")
c.SendLine(":wq!")
time.Sleep(time.Millisecond)
c.ExpectString("invalid error message")
c.ExpectString("Edit git commit message [Enter to launch editor]")
c.SendLine("")
time.Sleep(time.Millisecond)
c.ExpectString("first try")
c.Send("ccAdd editor prompt tests, but validated\x1b")
c.SendLine(":wq!")

// Input
c.ExpectString("What is your name?")
Expand Down Expand Up @@ -274,7 +255,7 @@ func TestAsk(t *testing.T) {
map[string]interface{}{
"pizza": true,
"commit-message": "Add editor prompt tests\n",
"commit-message-validated": "Add editor prompt tests\n",
"commit-message-validated": "Add editor prompt tests, but validated\n",
"name": "Johnny Appleseed",
/* TODO
"day": []string{"Monday", "Wednesday"},
Expand Down Expand Up @@ -332,16 +313,11 @@ func TestAsk(t *testing.T) {
// Capture range variable.
test := test
t.Run(test.name, func(t *testing.T) {
expectedAnswers := make(map[string]interface{}, 0)
for _, q := range test.questions {
expectedAnswers[q.Name] = test.expected[q.Name]
}

answers := make(map[string]interface{})
RunTest(t, test.procedure, func(stdio terminal.Stdio) error {
return Ask(test.questions, &answers, WithStdio(stdio.In, stdio.Out, stdio.Err))
})
require.Equal(t, expectedAnswers, answers)
require.Equal(t, test.expected, answers)
})
}
}
Expand Down

0 comments on commit 959b32a

Please sign in to comment.