Skip to content

Commit 5b7492b

Browse files
committed
added a unit test for checking if the DefaultValueSet is set or not
1 parent 4a24742 commit 5b7492b

4 files changed

Lines changed: 65 additions & 7 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: v2beta1
2+
name: test
3+
4+
vars:
5+
MYSQL_VERSION:
6+
question: Which mysql version do you want to use?
7+
default: ""
8+
pipelines:
9+
dev: |-
10+
echo ${MYSQL_VERSION}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v2beta1
2+
name: test
3+
4+
vars:
5+
MYSQL_VERSION:
6+
question: Which mysql version do you want to use?
7+
pipelines:
8+
dev: |-
9+
echo ${MYSQL_VERSION}

pkg/devspace/config/loader/variable/undefined_variable.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func convertStringValue(value string) interface{} {
6969
}
7070

7171
func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
72+
params := getParams(variable)
73+
74+
answer, err := log.Question(params)
75+
if err != nil {
76+
return "", err
77+
}
78+
79+
return answer, nil
80+
}
81+
82+
func getParams(variable *latest.Variable) *survey.QuestionOptions {
7283
params := &survey.QuestionOptions{}
7384

7485
if variable == nil {
@@ -107,11 +118,5 @@ func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
107118
}
108119
}
109120
}
110-
111-
answer, err := log.Question(params)
112-
if err != nil {
113-
return "", err
114-
}
115-
116-
return answer, nil
121+
return params
117122
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package variable
2+
3+
import (
4+
"fmt"
5+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
6+
"gopkg.in/yaml.v3"
7+
"os"
8+
"testing"
9+
)
10+
11+
func TestGetParams(t *testing.T) {
12+
testCases := map[string]bool{"testing/with_default_value/devspace.yaml": true, "testing/without_default_value/devspace.yaml": false}
13+
for input, expected := range testCases {
14+
config := getConfig(input)
15+
variable := config.Vars["MYSQL_VERSION"]
16+
actual := getParams(variable)
17+
if expected != actual.DefaultValueSet {
18+
t.Errorf("TestCase %s\nactual:%t\nexpected:%t", input, actual.DefaultValueSet, true)
19+
}
20+
}
21+
}
22+
23+
func getConfig(filename string) *latest.Config {
24+
v := &latest.Config{}
25+
yamlFile, err := os.ReadFile(filename)
26+
if err != nil {
27+
fmt.Printf("yamlFile.Get err #%v ", err)
28+
}
29+
err = yaml.Unmarshal(yamlFile, v)
30+
if err != nil {
31+
fmt.Printf("Unmarshal: %v", err)
32+
}
33+
return v
34+
}

0 commit comments

Comments
 (0)