Skip to content

Commit

Permalink
feat: show empty variable value when variable was optional
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Apr 4, 2024
1 parent dc265a4 commit de97ad9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pkg/ui/editable/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (m Model) View() string {
func (m *Model) AddRow() {
row := make(Row, len(m.cols))
for i := range row {
row[i].input = m.newTextInput(m.cols[i])
row[i].input = m.newTextInput()
}

m.rows = append(m.rows, row)
Expand Down Expand Up @@ -452,7 +452,7 @@ func (m *Model) validateCell(y, x int) {
}

// newTextInput initializes a text input which is used inside a cell.
func (m Model) newTextInput(c Column) textinput.Model {
func (m Model) newTextInput() textinput.Model {
ti := textinput.New()
ti.Prompt = ""

Expand Down
8 changes: 7 additions & 1 deletion pkg/ui/survey/prompt/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ func (m StringModel) View() string {

if m.submitted {
s.WriteString(": ")
s.WriteString(m.textInput.Value())

if m.textInput.Value() == "" {
s.WriteString(m.styles.HelpText.Render("empty"))
} else {
s.WriteString(m.textInput.Value())
}

return s.String()
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/ui/survey/prompt/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m TableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

if !m.variable.Optional && len(m.table.Values()) == 0 {
if !m.variable.Optional && m.IsEmpty() {
m.err = errors.New("table can not be empty since the variable is not optional")
return m, nil
}
Expand Down Expand Up @@ -108,6 +108,13 @@ func (m TableModel) View() string {

if m.submitted {
s.WriteString(": ")

if m.IsEmpty() {
s.WriteString(m.styles.HelpText.Render("empty"))
} else {
s.WriteString(m.tableAsCSV)
}

s.WriteString(m.tableAsCSV)
return s.String()
}
Expand Down Expand Up @@ -177,3 +184,7 @@ func (m TableModel) ValueAsCSV() string {

return s
}

func (m TableModel) IsEmpty() bool {
return len(m.table.Values()) == 0
}
57 changes: 43 additions & 14 deletions pkg/ui/survey/survey_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package survey_test

import (
"io"
"reflect"
"regexp"
"strings"
"testing"
"time"

Expand All @@ -13,18 +16,19 @@ import (

func TestPromptUserForValues(t *testing.T) {
testCases := []struct {
name string
variables []recipe.Variable
existingValues recipe.VariableValues
expected recipe.VariableValues
input string
name string
variables []recipe.Variable
existingValues recipe.VariableValues
expectedValues recipe.VariableValues
input string
expectedOutputRegexp string
}{
{
name: "string_variable",
variables: []recipe.Variable{
{Name: "VAR_1"},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": "foo",
},
input: "foo\r",
Expand All @@ -34,7 +38,7 @@ func TestPromptUserForValues(t *testing.T) {
variables: []recipe.Variable{
{Name: "VAR_1", Confirm: true},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": true,
},
input: "→\r",
Expand All @@ -44,7 +48,7 @@ func TestPromptUserForValues(t *testing.T) {
variables: []recipe.Variable{
{Name: "VAR_1", Confirm: true},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": true,
},
input: "y\r",
Expand All @@ -54,7 +58,7 @@ func TestPromptUserForValues(t *testing.T) {
variables: []recipe.Variable{
{Name: "VAR_1", Options: []string{"a", "b", "c"}},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": "c",
},
input: "↓↓\r",
Expand All @@ -64,7 +68,7 @@ func TestPromptUserForValues(t *testing.T) {
variables: []recipe.Variable{
{Name: "VAR_1", Columns: []string{"column_1", "column_2"}},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": recipe.TableValue{
Columns: []string{"column_1", "column_2"},
Rows: [][]string{{"foo", "bar"}, {"", "quz"}},
Expand All @@ -77,7 +81,7 @@ func TestPromptUserForValues(t *testing.T) {
variables: []recipe.Variable{
{Name: "VAR_1", Columns: []string{"column_1", "column_2"}},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": recipe.TableValue{
Columns: []string{"column_1", "column_2"},
Rows: [][]string{{"foo", "bar"}, {"baz", "quz"}},
Expand All @@ -91,12 +95,23 @@ func TestPromptUserForValues(t *testing.T) {
{Name: "VAR_1"},
{Name: "VAR_2", Confirm: true},
},
expected: recipe.VariableValues{
expectedValues: recipe.VariableValues{
"VAR_1": "foo",
"VAR_2": true,
},
input: "foo\ry\r",
},
{
name: "optional_variable",
variables: []recipe.Variable{
{Name: "VAR_1", Optional: true},
},
expectedValues: recipe.VariableValues{
"VAR_1": "",
},
input: "\r",
expectedOutputRegexp: "VAR_1: empty",
},
}

for _, tc := range testCases {
Expand All @@ -115,8 +130,22 @@ func TestPromptUserForValues(t *testing.T) {

// Assert that the result is correct
result := m.Values()
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Unexpected result. Got %v, expected %v", result, tc.expected)
if !reflect.DeepEqual(result, tc.expectedValues) {
t.Errorf("Unexpected result. Got %v, expected %v", result, tc.expectedValues)
}

// Assert that the output is correct
if tc.expectedOutputRegexp != "" {
buf := new(strings.Builder)
_, err := io.Copy(buf, tm.FinalOutput(tt))
if err != nil {
t.Fatalf("Could not read output of the survey: %v", err)
}

reg := regexp.MustCompile(tc.expectedOutputRegexp)
if !reg.MatchString(buf.String()) {
t.Errorf("The output did not match the regular expression \"%s\". Output is: %v", tc.expectedOutputRegexp, buf.String())
}
}
})
}
Expand Down

0 comments on commit de97ad9

Please sign in to comment.