-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
169 lines (141 loc) · 4.41 KB
/
run_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"os"
"path/filepath"
"testing"
)
func TestRunInvalidTemplate(t *testing.T) {
tempDir := t.TempDir()
templatePath := filepath.Join(tempDir, "invalid_template.txt")
templateContent := "Hello {{.NAME!}"
err := os.WriteFile(templatePath, []byte(templateContent), 0644)
if err != nil {
t.Fatalf("Failed to create template file: %v", err)
}
args := []string{"zep", templatePath}
env := []string{"NAME=World"}
output, err := Run(args, env)
if err == nil {
t.Errorf("Expected error for invalid template but got none")
}
expectedErrorMsg := "error rendering template"
if err != nil && !contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain %q but got %q", expectedErrorMsg, err.Error())
}
if output != "" {
t.Errorf("Expected empty output for invalid template but got %q", output)
}
}
func TestRun(t *testing.T) {
tempDir := t.TempDir()
tests := []struct {
name string
args []string
env []string
templateFile string
templateContent string
expectedOutput string
expectError bool
}{
{
name: "Basic template rendering",
args: []string{"zep", "template.txt"},
env: []string{"NAME=World", "COUNT=3"},
templateFile: "template.txt",
templateContent: "Hello {{.NAME}}! Count: {{.COUNT}}",
expectedOutput: "Hello World! Count: 3",
expectError: false,
},
{
name: "Template with function",
args: []string{"zep", "template.txt"},
env: []string{"NAME=World", "ITEMS=a,b,c"},
templateFile: "template.txt",
templateContent: "{{range asStringSlice \"ITEMS\" \",\"}}{{.}} {{end}}",
expectedOutput: "a b c ",
expectError: false,
},
{
name: "Missing template file",
args: []string{"zep", "nonexistent.txt"},
env: []string{"NAME=World"},
expectError: true,
},
{
name: "Invalid arguments",
args: []string{"zep"},
env: []string{},
expectError: true,
},
{
name: "Template with error",
args: []string{"zep", "template.txt"},
env: []string{"NAME=World"},
templateFile: "template.txt",
templateContent: "{{asInt \"COUNT\"}}",
expectError: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.templateFile != "" && tc.templateContent != "" {
templatePath := filepath.Join(tempDir, tc.templateFile)
err := os.WriteFile(templatePath, []byte(tc.templateContent), 0644)
if err != nil {
t.Fatalf("Failed to create template file: %v", err)
}
if len(tc.args) > 1 {
tc.args[1] = templatePath
}
}
output, err := Run(tc.args, tc.env)
if tc.expectError && err == nil {
t.Errorf("Expected error but got none")
}
if !tc.expectError && err != nil {
t.Errorf("Got unexpected error: %v", err)
}
if !tc.expectError && output != tc.expectedOutput {
t.Errorf("Expected output %q but got %q", tc.expectedOutput, output)
}
})
}
}
func TestRunWithEmptyEnvironment(t *testing.T) {
tempDir := t.TempDir()
templatePath := filepath.Join(tempDir, "template.txt")
templateContent := "Hello {{asStringOr \"NAME\" \"default\"}}!"
err := os.WriteFile(templatePath, []byte(templateContent), 0644)
if err != nil {
t.Fatalf("Failed to create template file: %v", err)
}
args := []string{"zep", templatePath}
env := []string{}
output, err := Run(args, env)
if err != nil {
t.Errorf("Unexpected error with empty environment: %v", err)
}
expectedOutput := "Hello default!"
if output != expectedOutput {
t.Errorf("Expected output %q with empty environment but got %q", expectedOutput, output)
}
}
func TestRunWithMalformedEnvironment(t *testing.T) {
tempDir := t.TempDir()
templatePath := filepath.Join(tempDir, "template.txt")
templateContent := "Hello {{asStringOr \"NAME\" \"default\"}}!"
err := os.WriteFile(templatePath, []byte(templateContent), 0644)
if err != nil {
t.Fatalf("Failed to create template file: %v", err)
}
args := []string{"zep", templatePath}
env := []string{"NAME=World", "INVALID_ENTRY", "=VALUE_WITHOUT_KEY"}
output, err := Run(args, env)
if err != nil {
t.Errorf("Unexpected error with malformed environment: %v", err)
}
expectedOutput := "Hello World!"
if output != expectedOutput {
t.Errorf("Expected output %q with malformed environment but got %q", expectedOutput, output)
}
}