Debian Codegraph @

debian/go/src/github.com/gohugoio/go-i18n/v2/goi18n/merge_command_test.go

raw
1 package main
2
3 import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "golang.org/x/text/language"
10 )
11
12 type testCase struct {
13 name string
14 inFiles map[string][]byte
15 sourceLanguage language.Tag
16 outFiles map[string][]byte
17 deleteFiles []string
18 }
19
20 func expectFile(s string) []byte {
21 // Trimming leading newlines gives nicer formatting for file literals in test cases.
22 return bytes.TrimLeft([]byte(s), "\n")
23 }
24
25 func TestMerge(t *testing.T) {
26 testCases := []*testCase{
27 {
28 name: "single identity",
29 sourceLanguage: language.AmericanEnglish,
30 inFiles: map[string][]byte{
31 "one.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"),
32 },
33 outFiles: map[string][]byte{
34 "active.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"),
35 },
36 },
37 {
38 name: "single identity, one localization text missing",
39 sourceLanguage: language.AmericanEnglish,
40 inFiles: map[string][]byte{
41 "one.en-US.toml": []byte(`
42 1HelloMessage = ""
43 Body = "Finally some text!"
44 `),
45 },
46 outFiles: map[string][]byte{
47 "active.en-US.toml": []byte(`Body = "Finally some text!"
48 `),
49 },
50 },
51 {
52 name: "plural identity",
53 sourceLanguage: language.AmericanEnglish,
54 inFiles: map[string][]byte{
55 "active.en-US.toml": []byte(`
56 [UnreadEmails]
57 description = "Message that tells the user how many unread emails they have"
58 one = "{{.Count}} unread email"
59 other = "{{.Count}} unread emails"
60 `),
61 },
62 outFiles: map[string][]byte{
63 "active.en-US.toml": expectFile(`
64 [UnreadEmails]
65 description = "Message that tells the user how many unread emails they have"
66 one = "{{.Count}} unread email"
67 other = "{{.Count}} unread emails"
68 `),
69 },
70 },
71 {
72 name: "plural identity, missing localization text",
73 sourceLanguage: language.AmericanEnglish,
74 inFiles: map[string][]byte{
75 "active.en-US.toml": []byte(`
76 Body = "Some text!"
77
78 [MissingTranslation]
79 description = "I wonder what it was?!"
80 one = ""
81 other = ""
82 `),
83 },
84 outFiles: map[string][]byte{
85 "active.en-US.toml": expectFile(`Body = "Some text!"
86 `),
87 },
88 },
89 {
90 name: "migrate source lang from v1 format",
91 sourceLanguage: language.AmericanEnglish,
92 inFiles: map[string][]byte{
93 "one.en-US.json": []byte(`[
94 {
95 "id": "simple",
96 "translation": "simple translation"
97 },
98 {
99 "id": "everything",
100 "translation": {
101 "zero": "zero translation",
102 "one": "one translation",
103 "two": "two translation",
104 "few": "few translation",
105 "many": "many translation",
106 "other": "other translation"
107 }
108 }
109 ]`),
110 },
111 outFiles: map[string][]byte{
112 "active.en-US.toml": expectFile(`
113 simple = "simple translation"
114
115 [everything]
116 few = "few translation"
117 many = "many translation"
118 one = "one translation"
119 other = "other translation"
120 two = "two translation"
121 zero = "zero translation"
122 `),
123 },
124 },
125 {
126 name: "migrate source lang from v1 flat format",
127 sourceLanguage: language.AmericanEnglish,
128 inFiles: map[string][]byte{
129 "one.en-US.json": []byte(`{
130 "simple": {
131 "other": "simple translation"
132 },
133 "everything": {
134 "zero": "zero translation",
135 "one": "one translation",
136 "two": "two translation",
137 "few": "few translation",
138 "many": "many translation",
139 "other": "other translation"
140 }
141 }`),
142 },
143 outFiles: map[string][]byte{
144 "active.en-US.toml": expectFile(`
145 simple = "simple translation"
146
147 [everything]
148 few = "few translation"
149 many = "many translation"
150 one = "one translation"
151 other = "other translation"
152 two = "two translation"
153 zero = "zero translation"
154 `),
155 },
156 },
157 {
158 name: "merge source files",
159 sourceLanguage: language.AmericanEnglish,
160 inFiles: map[string][]byte{
161 "one.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"),
162 "two.en-US.toml": []byte("2GoodbyeMessage = \"Goodbye\"\n"),
163 },
164 outFiles: map[string][]byte{
165 "active.en-US.toml": []byte("1HelloMessage = \"Hello\"\n2GoodbyeMessage = \"Goodbye\"\n"),
166 },
167 },
168 {
169 name: "missing hash",
170 sourceLanguage: language.AmericanEnglish,
171 inFiles: map[string][]byte{
172 "en-US.toml": []byte(`
173 1HelloMessage = "Hello"
174 `),
175 "es-ES.toml": []byte(`
176 [1HelloMessage]
177 other = "Hola"
178 `),
179 },
180 outFiles: map[string][]byte{
181 "active.en-US.toml": expectFile(`
182 1HelloMessage = "Hello"
183 `),
184 "active.es-ES.toml": expectFile(`
185 [1HelloMessage]
186 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
187 other = "Hola"
188 `),
189 },
190 },
191 {
192 name: "add single translation",
193 sourceLanguage: language.AmericanEnglish,
194 inFiles: map[string][]byte{
195 "en-US.toml": []byte(`
196 1HelloMessage = "Hello"
197 2GoodbyeMessage = "Goodbye"
198 `),
199 "es-ES.toml": []byte(`
200 [1HelloMessage]
201 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
202 other = "Hola"
203 `),
204 },
205 outFiles: map[string][]byte{
206 "active.en-US.toml": expectFile(`
207 1HelloMessage = "Hello"
208 2GoodbyeMessage = "Goodbye"
209 `),
210 "active.es-ES.toml": expectFile(`
211 [1HelloMessage]
212 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
213 other = "Hola"
214 `),
215 "translate.es-ES.toml": expectFile(`
216 [2GoodbyeMessage]
217 hash = "sha1-b5b29c53e3c71cb9c6581ab053d7758fab8ca24d"
218 other = "Goodbye"
219 `),
220 },
221 },
222 {
223 name: "remove single translation",
224 sourceLanguage: language.AmericanEnglish,
225 inFiles: map[string][]byte{
226 "en-US.toml": []byte(`
227 1HelloMessage = "Hello"
228 `),
229 "es-ES.toml": []byte(`
230 [1HelloMessage]
231 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
232 other = "Hola"
233
234 [2GoodbyeMessage]
235 hash = "sha1-b5b29c53e3c71cb9c6581ab053d7758fab8ca24d"
236 other = "Goodbye"
237 `),
238 },
239 outFiles: map[string][]byte{
240 "active.en-US.toml": expectFile(`
241 1HelloMessage = "Hello"
242 `),
243 "active.es-ES.toml": expectFile(`
244 [1HelloMessage]
245 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
246 other = "Hola"
247 `),
248 },
249 },
250 {
251 name: "edit single translation",
252 sourceLanguage: language.AmericanEnglish,
253 inFiles: map[string][]byte{
254 "en-US.toml": []byte(`
255 1HelloMessage = "Hi"
256 `),
257 "es-ES.toml": []byte(`
258 [1HelloMessage]
259 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
260 other = "Hola"
261 `),
262 },
263 outFiles: map[string][]byte{
264 "active.en-US.toml": expectFile(`
265 1HelloMessage = "Hi"
266 `),
267 "translate.es-ES.toml": expectFile(`
268 [1HelloMessage]
269 hash = "sha1-94dd9e08c129c785f7f256e82fbe0a30e6d1ae40"
270 other = "Hi"
271 `),
272 },
273 },
274 {
275 name: "add plural translation",
276 sourceLanguage: language.AmericanEnglish,
277 inFiles: map[string][]byte{
278 "en-US.toml": []byte(`
279 [UnreadEmails]
280 description = "Message that tells the user how many unread emails they have"
281 one = "{{.Count}} unread email"
282 other = "{{.Count}} unread emails"
283 `),
284 "es-ES.toml": nil,
285 "ar-AR.toml": nil,
286 "zh-CN.toml": nil,
287 },
288 outFiles: map[string][]byte{
289 "active.en-US.toml": expectFile(`
290 [UnreadEmails]
291 description = "Message that tells the user how many unread emails they have"
292 one = "{{.Count}} unread email"
293 other = "{{.Count}} unread emails"
294 `),
295 "translate.es-ES.toml": expectFile(`
296 [UnreadEmails]
297 description = "Message that tells the user how many unread emails they have"
298 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
299 many = "{{.Count}} unread emails"
300 one = "{{.Count}} unread email"
301 other = "{{.Count}} unread emails"
302 `),
303 "translate.ar-AR.toml": expectFile(`
304 [UnreadEmails]
305 description = "Message that tells the user how many unread emails they have"
306 few = "{{.Count}} unread emails"
307 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
308 many = "{{.Count}} unread emails"
309 one = "{{.Count}} unread email"
310 other = "{{.Count}} unread emails"
311 two = "{{.Count}} unread emails"
312 zero = "{{.Count}} unread emails"
313 `),
314 "translate.zh-CN.toml": expectFile(`
315 [UnreadEmails]
316 description = "Message that tells the user how many unread emails they have"
317 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
318 other = "{{.Count}} unread emails"
319 `),
320 },
321 },
322 {
323 name: "remove plural translation",
324 sourceLanguage: language.AmericanEnglish,
325 inFiles: map[string][]byte{
326 "en-US.toml": []byte(`
327 1HelloMessage = "Hello"
328 `),
329 "es-ES.toml": []byte(`
330 [1HelloMessage]
331 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
332 other = "Hola"
333
334 [UnreadEmails]
335 description = "Message that tells the user how many unread emails they have"
336 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
337 one = "{{.Count}} unread emails"
338 other = "{{.Count}} unread emails"
339 `),
340 "ar-AR.toml": []byte(`
341 [1HelloMessage]
342 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
343 other = "Hello"
344
345 [UnreadEmails]
346 description = "Message that tells the user how many unread emails they have"
347 few = "{{.Count}} unread emails"
348 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
349 many = "{{.Count}} unread emails"
350 one = "{{.Count}} unread emails"
351 other = "{{.Count}} unread emails"
352 two = "{{.Count}} unread emails"
353 zero = "{{.Count}} unread emails"
354 `),
355 "zh-CN.toml": []byte(`
356 [1HelloMessage]
357 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
358 other = "Hello"
359
360 [UnreadEmails]
361 description = "Message that tells the user how many unread emails they have"
362 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
363 other = "{{.Count}} unread emails"
364 `),
365 },
366 outFiles: map[string][]byte{
367 "active.en-US.toml": expectFile(`
368 1HelloMessage = "Hello"
369 `),
370 "active.es-ES.toml": expectFile(`
371 [1HelloMessage]
372 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
373 other = "Hola"
374 `),
375 "active.ar-AR.toml": expectFile(`
376 [1HelloMessage]
377 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
378 other = "Hello"
379 `),
380 "active.zh-CN.toml": expectFile(`
381 [1HelloMessage]
382 hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"
383 other = "Hello"
384 `),
385 },
386 },
387 {
388 name: "edit plural translation",
389 sourceLanguage: language.AmericanEnglish,
390 inFiles: map[string][]byte{
391 "en-US.toml": []byte(`
392 [UnreadEmails]
393 description = "Message that tells the user how many unread emails they have"
394 one = "{{.Count}} unread emails!"
395 other = "{{.Count}} unread emails!"
396 `),
397 "es-ES.toml": []byte(`
398 [UnreadEmails]
399 description = "Message that tells the user how many unread emails they have"
400 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
401 one = "{{.Count}} unread emails"
402 other = "{{.Count}} unread emails"
403 `),
404 "ar-AR.toml": []byte(`
405 [UnreadEmails]
406 description = "Message that tells the user how many unread emails they have"
407 few = "{{.Count}} unread emails"
408 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
409 many = "{{.Count}} unread emails"
410 one = "{{.Count}} unread emails"
411 other = "{{.Count}} unread emails"
412 two = "{{.Count}} unread emails"
413 zero = "{{.Count}} unread emails"
414 `),
415 "zh-CN.toml": []byte(`
416 [UnreadEmails]
417 description = "Message that tells the user how many unread emails they have"
418 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
419 other = "{{.Count}} unread emails"
420 `),
421 },
422 deleteFiles: []string{
423 "active.es-ES.toml",
424 "active.ar-AR.toml",
425 "active.zh-CN.toml",
426 },
427 outFiles: map[string][]byte{
428 "active.en-US.toml": expectFile(`
429 [UnreadEmails]
430 description = "Message that tells the user how many unread emails they have"
431 one = "{{.Count}} unread emails!"
432 other = "{{.Count}} unread emails!"
433 `),
434 "translate.es-ES.toml": expectFile(`
435 [UnreadEmails]
436 description = "Message that tells the user how many unread emails they have"
437 hash = "sha1-92a24983c5bbc0c42462cdc252dca68ebdb46501"
438 many = "{{.Count}} unread emails!"
439 one = "{{.Count}} unread emails!"
440 other = "{{.Count}} unread emails!"
441 `),
442 "translate.ar-AR.toml": expectFile(`
443 [UnreadEmails]
444 description = "Message that tells the user how many unread emails they have"
445 few = "{{.Count}} unread emails!"
446 hash = "sha1-92a24983c5bbc0c42462cdc252dca68ebdb46501"
447 many = "{{.Count}} unread emails!"
448 one = "{{.Count}} unread emails!"
449 other = "{{.Count}} unread emails!"
450 two = "{{.Count}} unread emails!"
451 zero = "{{.Count}} unread emails!"
452 `),
453 "translate.zh-CN.toml": expectFile(`
454 [UnreadEmails]
455 description = "Message that tells the user how many unread emails they have"
456 hash = "sha1-92a24983c5bbc0c42462cdc252dca68ebdb46501"
457 other = "{{.Count}} unread emails!"
458 `),
459 },
460 },
461 {
462 name: "merge plural translation",
463 sourceLanguage: language.AmericanEnglish,
464 inFiles: map[string][]byte{
465 "en-US.toml": []byte(`
466 [UnreadEmails]
467 description = "Message that tells the user how many unread emails they have"
468 one = "{{.Count}} unread emails"
469 other = "{{.Count}} unread emails"
470 `),
471 "zero.ar-AR.toml": []byte(`
472 [UnreadEmails]
473 description = "Message that tells the user how many unread emails they have"
474 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
475 zero = "{{.Count}} unread emails"
476 `),
477 "one.ar-AR.toml": []byte(`
478 [UnreadEmails]
479 description = "Message that tells the user how many unread emails they have"
480 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
481 one = "{{.Count}} unread emails"
482 `),
483 "two.ar-AR.toml": []byte(`
484 [UnreadEmails]
485 description = "Message that tells the user how many unread emails they have"
486 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
487 two = "{{.Count}} unread emails"
488 `),
489 "few.ar-AR.toml": []byte(`
490 [UnreadEmails]
491 description = "Message that tells the user how many unread emails they have"
492 few = "{{.Count}} unread emails"
493 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
494 `),
495 "many.ar-AR.toml": []byte(`
496 [UnreadEmails]
497 description = "Message that tells the user how many unread emails they have"
498 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
499 many = "{{.Count}} unread emails"
500 `),
501 "other.ar-AR.toml": []byte(`
502 [UnreadEmails]
503 description = "Message that tells the user how many unread emails they have"
504 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
505 other = "{{.Count}} unread emails"
506 `),
507 },
508 outFiles: map[string][]byte{
509 "active.en-US.toml": expectFile(`
510 [UnreadEmails]
511 description = "Message that tells the user how many unread emails they have"
512 one = "{{.Count}} unread emails"
513 other = "{{.Count}} unread emails"
514 `),
515 "active.ar-AR.toml": expectFile(`
516 [UnreadEmails]
517 description = "Message that tells the user how many unread emails they have"
518 few = "{{.Count}} unread emails"
519 hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099"
520 many = "{{.Count}} unread emails"
521 one = "{{.Count}} unread emails"
522 other = "{{.Count}} unread emails"
523 two = "{{.Count}} unread emails"
524 zero = "{{.Count}} unread emails"
525 `),
526 },
527 },
528 }
529
530 for _, testCase := range testCases {
531 t.Run(testCase.name, func(t *testing.T) {
532 indir := mustTempDir("TestMergeCommandIn")
533 defer mustRemoveAll(t, indir)
534 outdir := mustTempDir("TestMergeCommandOut")
535 defer mustRemoveAll(t, outdir)
536
537 infiles := make([]string, 0, len(testCase.inFiles))
538 for name, content := range testCase.inFiles {
539 path := filepath.Join(indir, name)
540 infiles = append(infiles, path)
541 if err := os.WriteFile(path, content, 0666); err != nil {
542 t.Fatal(err)
543 }
544 }
545
546 for _, name := range testCase.deleteFiles {
547 path := filepath.Join(outdir, name)
548 if err := os.WriteFile(path, []byte(`this file should get deleted`), 0666); err != nil {
549 t.Fatal(err)
550 }
551 }
552
553 args := append([]string{"merge", "-sourceLanguage", testCase.sourceLanguage.String(), "-outdir", outdir}, infiles...)
554 if code := testableMain(args); code != 0 {
555 t.Fatalf("expected exit code 0; got %d\n", code)
556 }
557
558 files, err := os.ReadDir(outdir)
559 if err != nil {
560 t.Fatal(err)
561 }
562
563 // Verify that all actual files have expected contents.
564 actualFiles := make(map[string]struct{}, len(files))
565 for _, f := range files {
566 actualFiles[f.Name()] = struct{}{}
567 if f.IsDir() {
568 t.Errorf("found unexpected dir %s", f.Name())
569 continue
570 }
571 path := filepath.Join(outdir, f.Name())
572 actual, err := os.ReadFile(path)
573 if err != nil {
574 t.Error(err)
575 continue
576 }
577 expected, ok := testCase.outFiles[f.Name()]
578 if !ok {
579 t.Errorf("found unexpected file %s with contents:\n%s\n", f.Name(), actual)
580 continue
581 }
582 if !bytes.Equal(actual, expected) {
583 t.Errorf("unexpected contents %s\ngot\n%s\nexpected\n%s", f.Name(), actual, expected)
584 continue
585 }
586 }
587
588 // Verify that all expected files are accounted for.
589 for name := range testCase.outFiles {
590 if _, ok := actualFiles[name]; !ok {
591 t.Errorf("did not find expected file %s", name)
592 }
593 }
594 })
595 }
596 }