-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinjafier_test.go
More file actions
271 lines (221 loc) · 8.19 KB
/
jinjafier_test.go
File metadata and controls
271 lines (221 loc) · 8.19 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestMain(m *testing.M) {
// Build the binary once before all tests
cmd := exec.Command("go", "build", "-o", "jinjafier_test_bin")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic("failed to build: " + err.Error())
}
code := m.Run()
os.Remove("jinjafier_test_bin")
os.Exit(code)
}
func runJinjafier(t *testing.T, dir string, args ...string) {
t.Helper()
bin, _ := filepath.Abs("jinjafier_test_bin")
cmd := exec.Command(bin, args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("jinjafier %v failed: %v\n%s", args, err, out)
}
}
func readFile(t *testing.T, path string) string {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read %s: %v", path, err)
}
return string(data)
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("failed to write %s: %v", path, err)
}
}
func setupTempDir(t *testing.T) string {
t.Helper()
dir := t.TempDir()
return dir
}
func assertContains(t *testing.T, file, content, substr string) {
t.Helper()
if !strings.Contains(content, substr) {
t.Errorf("%s: expected to contain %q", file, substr)
}
}
func assertNotContains(t *testing.T, file, content, substr string) {
t.Helper()
if strings.Contains(content, substr) {
t.Errorf("%s: expected NOT to contain %q", file, substr)
}
}
const testProperties = `# a comment
org.wibble.testCamelCaps=20
org.wibble.test.long.property=hello world
# testing = in value
org.wibble.brokerURL=tcp://localhost:61616?jms.useAsyncSend=true
# testing - in key
org.wibble.message-listener.retryCount=3
# testing weird cron
org.wibble.cronFormat=*/5 * * * * *
`
const testYaml = `org:
wibble:
testCamelCaps: 20
test:
long:
property: hello world
brokerURL: "tcp://localhost:61616?jms.useAsyncSend=true"
message-listener:
retryCount: 3
cronFormat: "*/5 * * * * *"
`
func TestPropertiesDefaultMode(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.properties"), testProperties)
runJinjafier(t, dir, "app.properties")
// Check .j2 template
j2 := readFile(t, filepath.Join(dir, "app.properties.j2"))
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_TESTCAMELCAPS }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_BROKERURL }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_MESSAGE_LISTENER_RETRYCOUNT }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_CRONFORMAT }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_TEST_LONG_PROPERTY }}")
// camelCase should NOT be split
assertNotContains(t, "j2", j2, "TEST_CAMEL_CAPS")
assertNotContains(t, "j2", j2, "BROKER_URL")
assertNotContains(t, "j2", j2, "CRON_FORMAT")
assertNotContains(t, "j2", j2, "RETRY_COUNT")
// Check .env.j2 template
env := readFile(t, filepath.Join(dir, "app.properties.env.j2"))
assertContains(t, "env.j2", env, "ORG_WIBBLE_TESTCAMELCAPS={{ ORG_WIBBLE_TESTCAMELCAPS }}")
assertContains(t, "env.j2", env, "ORG_WIBBLE_BROKERURL={{ ORG_WIBBLE_BROKERURL }}")
// Check .yml output
yml := readFile(t, filepath.Join(dir, "app.properties.yml"))
assertContains(t, "yml", yml, "ORG_WIBBLE_TESTCAMELCAPS:")
assertContains(t, "yml", yml, "ORG_WIBBLE_BROKERURL:")
assertNotContains(t, "yml", yml, "TEST_CAMEL_CAPS:")
}
func TestPropertiesCamelSplitMode(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.properties"), testProperties)
runJinjafier(t, dir, "-camel-split", "app.properties")
j2 := readFile(t, filepath.Join(dir, "app.properties.j2"))
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_TEST_CAMEL_CAPS }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_BROKER_URL }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_MESSAGE_LISTENER_RETRY_COUNT }}")
assertContains(t, "j2", j2, "{{ ORG_WIBBLE_CRON_FORMAT }}")
// Should NOT contain the unsplit versions
assertNotContains(t, "j2", j2, "TESTCAMELCAPS")
assertNotContains(t, "j2", j2, "BROKERURL")
assertNotContains(t, "j2", j2, "CRONFORMAT")
}
func TestYamlDefaultMode(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.yml"), testYaml)
runJinjafier(t, dir, "app.yml")
env := readFile(t, filepath.Join(dir, "app.yml.env"))
assertContains(t, "yml.env", env, "ORG_WIBBLE_TESTCAMELCAPS=20")
assertContains(t, "yml.env", env, "ORG_WIBBLE_BROKERURL=")
assertContains(t, "yml.env", env, "ORG_WIBBLE_MESSAGE_LISTENER_RETRYCOUNT=3")
assertContains(t, "yml.env", env, "ORG_WIBBLE_CRONFORMAT=")
assertContains(t, "yml.env", env, "ORG_WIBBLE_TEST_LONG_PROPERTY=hello world")
assertNotContains(t, "yml.env", env, "TEST_CAMEL_CAPS")
assertNotContains(t, "yml.env", env, "BROKER_URL")
}
func TestYamlCamelSplitMode(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.yml"), testYaml)
runJinjafier(t, dir, "-camel-split", "app.yml")
env := readFile(t, filepath.Join(dir, "app.yml.env"))
assertContains(t, "yml.env", env, "ORG_WIBBLE_TEST_CAMEL_CAPS=20")
assertContains(t, "yml.env", env, "ORG_WIBBLE_BROKER_URL=")
assertContains(t, "yml.env", env, "ORG_WIBBLE_MESSAGE_LISTENER_RETRY_COUNT=3")
assertContains(t, "yml.env", env, "ORG_WIBBLE_CRON_FORMAT=")
assertNotContains(t, "yml.env", env, "TESTCAMELCAPS")
assertNotContains(t, "yml.env", env, "BROKERURL")
}
func TestPropertiesPreservesComments(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.properties"), testProperties)
runJinjafier(t, dir, "app.properties")
j2 := readFile(t, filepath.Join(dir, "app.properties.j2"))
assertContains(t, "j2", j2, "# a comment")
assertContains(t, "j2", j2, "# testing = in value")
assertContains(t, "j2", j2, "# testing - in key")
assertContains(t, "j2", j2, "# testing weird cron")
env := readFile(t, filepath.Join(dir, "app.properties.env.j2"))
assertContains(t, "env.j2", env, "# a comment")
}
func TestPropertiesPreservesBlankLines(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.properties"), testProperties)
runJinjafier(t, dir, "app.properties")
j2 := readFile(t, filepath.Join(dir, "app.properties.j2"))
assertContains(t, "j2", j2, "\n\n")
}
func TestPropertiesOriginalKeysPreservedInJ2(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.properties"), testProperties)
runJinjafier(t, dir, "app.properties")
j2 := readFile(t, filepath.Join(dir, "app.properties.j2"))
assertContains(t, "j2", j2, "org.wibble.testCamelCaps=")
assertContains(t, "j2", j2, "org.wibble.brokerURL=")
assertContains(t, "j2", j2, "org.wibble.message-listener.retryCount=")
}
func TestPropertiesEqualsInValue(t *testing.T) {
dir := setupTempDir(t)
writeFile(t, filepath.Join(dir, "app.properties"), testProperties)
runJinjafier(t, dir, "app.properties")
yml := readFile(t, filepath.Join(dir, "app.properties.yml"))
assertContains(t, "yml", yml, "tcp://localhost:61616?jms.useAsyncSend=true")
}
func TestVersionFlag(t *testing.T) {
bin, _ := filepath.Abs("jinjafier_test_bin")
out, err := exec.Command(bin, "-v").CombinedOutput()
if err != nil {
t.Fatalf("-v failed: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Jinjafier version:") {
t.Errorf("expected version output, got: %s", out)
}
}
func TestUnsupportedFileFormat(t *testing.T) {
bin, _ := filepath.Abs("jinjafier_test_bin")
cmd := exec.Command(bin, "file.txt")
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("expected error for unsupported file format")
}
if !strings.Contains(string(out), "Unsupported file format") {
t.Errorf("expected unsupported format message, got: %s", out)
}
}
func TestNoArgs(t *testing.T) {
bin, _ := filepath.Abs("jinjafier_test_bin")
cmd := exec.Command(bin)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("expected error with no args")
}
if !strings.Contains(string(out), "Jinjafier - Convert Java properties/YAML") {
t.Errorf("expected usage message, got: %s", out)
}
}
func TestHyphenInKeyConvertedToUnderscore(t *testing.T) {
dir := setupTempDir(t)
props := "my.app.some-key=value\n"
writeFile(t, filepath.Join(dir, "app.properties"), props)
runJinjafier(t, dir, "app.properties")
j2 := readFile(t, filepath.Join(dir, "app.properties.j2"))
assertContains(t, "j2", j2, "{{ MY_APP_SOME_KEY }}")
}