Debian Codegraph @

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

raw
1 package main
2
3 import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "testing"
8 )
9
10 func TestExtract(t *testing.T) {
11 tests := []struct {
12 name string
13 fileName string
14 file string
15 activeFile []byte
16 expectedExitCode int
17 expectedErr error
18 }{
19 {
20 name: "no translations",
21 fileName: "file.go",
22 file: `package main`,
23 },
24 {
25 name: "global declaration",
26 fileName: "file.go",
27 file: `package main
28
29 import "github.com/gohugoio/go-i18n/v2/i18n"
30
31 var m = &i18n.Message{
32 ID: "Plural ID",
33 }
34 `,
35 },
36 {
37 name: "duplicate ID with same message is not an error",
38 fileName: "file.go",
39 file: `package main
40
41 import "github.com/gohugoio/go-i18n/v2/i18n"
42
43 var m1 = &i18n.Message{
44 ID: "m",
45 Other: "m",
46 }
47 var m2 = &i18n.Message{
48 ID: "m",
49 Other: "m",
50 }
51 `,
52 activeFile: []byte(`m = "m"
53 `),
54 },
55 {
56 name: "duplicate ID with different message is an error",
57 fileName: "file.go",
58 file: `package main
59
60 import "github.com/gohugoio/go-i18n/v2/i18n"
61
62 var m1 = &i18n.Message{
63 ID: "m",
64 Other: "m1",
65 }
66 var m2 = &i18n.Message{
67 ID: "m",
68 Other: "m2",
69 }
70 `,
71 expectedExitCode: 1,
72 },
73 {
74 name: "escape newline",
75 fileName: "file.go",
76 file: `package main
77
78 import "github.com/gohugoio/go-i18n/v2/i18n"
79
80 var hasnewline = &i18n.Message{
81 ID: "hasnewline",
82 Other: "\nfoo\nbar\\",
83 }
84 `,
85 activeFile: []byte(`hasnewline = "\nfoo\nbar\\"
86 `),
87 },
88 {
89 name: "escape",
90 fileName: "file.go",
91 file: `package main
92
93 import "github.com/gohugoio/go-i18n/v2/i18n"
94
95 var a = &i18n.Message{
96 ID: "a",
97 Other: "a \" b",
98 }
99 var b = &i18n.Message{
100 ID: "b",
101 Other: ` + "`" + `a " b` + "`" + `,
102 }
103 `,
104 activeFile: []byte(`a = "a \" b"
105 b = "a \" b"
106 `),
107 },
108 {
109 name: "array",
110 fileName: "file.go",
111 file: `package main
112
113 import "github.com/gohugoio/go-i18n/v2/i18n"
114
115 var a = []*i18n.Message{
116 {
117 ID: "a",
118 Other: "a",
119 },
120 {
121 ID: "b",
122 Other: "b",
123 },
124 }
125 `,
126 activeFile: []byte(`a = "a"
127 b = "b"
128 `),
129 },
130 {
131 name: "map",
132 fileName: "file.go",
133 file: `package main
134
135 import "github.com/gohugoio/go-i18n/v2/i18n"
136
137 var a = map[string]*i18n.Message{
138 "a": {
139 ID: "a",
140 Other: "a",
141 },
142 "b": {
143 ID: "b",
144 Other: "b",
145 },
146 }
147 `,
148 activeFile: []byte(`a = "a"
149 b = "b"
150 `),
151 },
152 {
153 name: "no extract from test",
154 fileName: "file_test.go",
155 file: `package main
156
157 import "github.com/gohugoio/go-i18n/v2/i18n"
158
159 func main() {
160 bundle := i18n.NewBundle(language.English)
161 l := i18n.NewLocalizer(bundle, "en")
162 l.Localize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
163 }
164 `,
165 },
166 {
167 name: "must short form id only",
168 fileName: "file.go",
169 file: `package main
170
171 import "github.com/gohugoio/go-i18n/v2/i18n"
172
173 func main() {
174 bundle := i18n.NewBundle(language.English)
175 l := i18n.NewLocalizer(bundle, "en")
176 l.MustLocalize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
177 }
178 `,
179 },
180 {
181 name: "custom package name",
182 fileName: "file.go",
183 file: `package main
184
185 import bar "github.com/gohugoio/go-i18n/v2/i18n"
186
187 func main() {
188 _ := &bar.Message{
189 ID: "Plural ID",
190 }
191 }
192 `,
193 },
194 {
195 name: "exhaustive plural translation",
196 fileName: "file.go",
197 file: `package main
198
199 import "github.com/gohugoio/go-i18n/v2/i18n"
200
201 func main() {
202 _ := &i18n.Message{
203 ID: "Plural ID",
204 Description: "Plural description",
205 Zero: "Zero translation",
206 One: "One translation",
207 Two: "Two translation",
208 Few: "Few translation",
209 Many: "Many translation",
210 Other: "Other translation",
211 }
212 }
213 `,
214 activeFile: []byte(`["Plural ID"]
215 description = "Plural description"
216 few = "Few translation"
217 many = "Many translation"
218 one = "One translation"
219 other = "Other translation"
220 two = "Two translation"
221 zero = "Zero translation"
222 `),
223 },
224 {
225 name: "concat id",
226 fileName: "file.go",
227 file: `package main
228
229 import "github.com/gohugoio/go-i18n/v2/i18n"
230
231 func main() {
232 _ := &i18n.Message{
233 ID: "Plural" +
234 " " +
235 "ID",
236 }
237 }
238 `,
239 },
240 {
241 name: "global declaration",
242 fileName: "file.go",
243 file: `package main
244
245 import "github.com/gohugoio/go-i18n/v2/i18n"
246
247 const constID = "ConstantID"
248
249 var m = &i18n.Message{
250 ID: constID,
251 Other: "ID is a constant",
252 }
253 `,
254 activeFile: []byte(`ConstantID = "ID is a constant"
255 `),
256 },
257 {
258 name: "undefined identifier in composite lit",
259 fileName: "file.go",
260 file: `package main
261
262 import "github.com/gohugoio/go-i18n/v2/i18n"
263
264 var m = &i18n.LocalizeConfig{
265 Funcs: Funcs,
266 }
267 `,
268 },
269 {
270 name: "casted const",
271 fileName: "file.go",
272 file: `package main
273
274 import "github.com/gohugoio/go-i18n/v2/i18n"
275
276 type ConstType string
277
278 const Const ConstType = "my const"
279
280 var m = &i18n.LocalizeConfig{
281 ID: "id",
282 Other: string(Const),
283 }
284 `,
285 activeFile: []byte(`id = "my const"
286 `),
287 },
288 }
289
290 for _, test := range tests {
291 t.Run(test.name, func(t *testing.T) {
292 indir := mustTempDir("TestExtractCommandIn")
293 defer mustRemoveAll(t, indir)
294 outdir := mustTempDir("TestExtractCommandOut")
295 defer mustRemoveAll(t, outdir)
296
297 inpath := filepath.Join(indir, test.fileName)
298 if err := os.WriteFile(inpath, []byte(test.file), 0o666); err != nil {
299 t.Fatal(err)
300 }
301
302 code := testableMain([]string{"extract", "-outdir", outdir, indir})
303 if code != test.expectedExitCode {
304 t.Fatalf("expected exit code %d; got %d\n", test.expectedExitCode, code)
305 }
306
307 files, err := os.ReadDir(outdir)
308 if err != nil {
309 t.Fatal(err)
310 }
311
312 if code != 0 {
313 if len(files) != 0 {
314 t.Fatalf("expected 0 files; got %#v", files)
315 }
316 return
317 }
318
319 if len(files) != 1 {
320 t.Fatalf("expected 1 file; got %#v", files)
321 }
322 actualFile := files[0]
323 expectedName := "active.en.toml"
324 if actualFile.Name() != expectedName {
325 t.Fatalf("expected %s; got %s", expectedName, actualFile.Name())
326 }
327
328 outpath := filepath.Join(outdir, actualFile.Name())
329 actual, err := os.ReadFile(outpath)
330 if err != nil {
331 t.Fatal(err)
332 }
333 if !bytes.Equal(actual, test.activeFile) {
334 t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", test.activeFile, actual)
335 }
336 })
337 }
338 }
339
340 func TestExtractCommand(t *testing.T) {
341 outdir, err := os.MkdirTemp("", "TestExtractCommand")
342 if err != nil {
343 t.Fatal(err)
344 }
345 defer mustRemoveAll(t, outdir)
346 if code := testableMain([]string{"extract", "-outdir", outdir, "../example/"}); code != 0 {
347 t.Fatalf("expected exit code 0; got %d", code)
348 }
349 actual, err := os.ReadFile(filepath.Join(outdir, "active.en.toml"))
350 if err != nil {
351 t.Fatal(err)
352 }
353 expected := []byte(`HelloPerson = "Hello {{.Name}}"
354
355 [MyUnreadEmails]
356 description = "The number of unread emails I have"
357 one = "I have {{.PluralCount}} unread email."
358 other = "I have {{.PluralCount}} unread emails."
359
360 [PersonUnreadEmails]
361 description = "The number of unread emails a person has"
362 one = "{{.Name}} has {{.UnreadEmailCount}} unread email."
363 other = "{{.Name}} has {{.UnreadEmailCount}} unread emails."
364 `)
365 if !bytes.Equal(actual, expected) {
366 t.Fatalf("files not equal\nactual:\n%s\nexpected:\n%s", actual, expected)
367 }
368 }