github.com/CharukaK/i18n4go@v0.6.0/integration/verify_strings/f_option_test.go (about) 1 // Copyright © 2015-2023 The Knative Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package verify_strings_test 16 17 import ( 18 "os" 19 "path/filepath" 20 21 . "github.com/CharukaK/i18n4go/integration/test_helpers" 22 . "github.com/onsi/ginkgo" 23 . "github.com/onsi/gomega" 24 ) 25 26 var _ = Describe("verify-strings -f fileName --languages \"[lang,?]+\"", func() { 27 var ( 28 inputFilesPath string 29 expectedFilesPath string 30 ) 31 32 BeforeEach(func() { 33 fixturesPath := filepath.Join("..", "..", "test_fixtures", "verify_strings") 34 inputFilesPath = filepath.Join(fixturesPath, "f_option", "input_files") 35 expectedFilesPath = filepath.Join(fixturesPath, "f_option", "expected_output") 36 }) 37 38 Context("Using legacy commands", func() { 39 Context("valid input file provided", func() { 40 Context("using --source-language", func() { 41 Context("passes verifications", func() { 42 BeforeEach(func() { 43 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr,zh_CN\"", "-o", expectedFilesPath, "--source-language", "en") 44 Ω(session.ExitCode()).Should(Equal(0)) 45 }) 46 47 It("with language file with valid keys", func() { 48 _, err := os.Stat(GetFilePath(inputFilesPath, "quota.go.en.json.missing.diff")) 49 Ω(os.IsNotExist(err)).Should(Equal(true)) 50 51 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.en.json.extra.diff")) 52 Ω(os.IsNotExist(err)).Should(Equal(true)) 53 }) 54 }) 55 }) 56 57 Context("not using --source-language", func() { 58 Context("passes verifications", func() { 59 BeforeEach(func() { 60 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr,zh_CN\"", "-o", expectedFilesPath) 61 Ω(session.ExitCode()).Should(Equal(0)) 62 }) 63 64 It("with language file with valid keys", func() { 65 _, err := os.Stat(GetFilePath(inputFilesPath, "quota.go.fr.json.missing.diff.json")) 66 Ω(os.IsNotExist(err)).Should(Equal(true)) 67 68 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.fr.json.missing.diff.json")) 69 Ω(os.IsNotExist(err)).Should(Equal(true)) 70 71 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.zh_CN.json.extra.diff.json")) 72 Ω(os.IsNotExist(err)).Should(Equal(true)) 73 74 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.zh_CN.json.extra.diff.json")) 75 Ω(os.IsNotExist(err)).Should(Equal(true)) 76 }) 77 }) 78 }) 79 80 Context("fails verification", func() { 81 Context("with one language file", func() { 82 Context("with missing keys", func() { 83 BeforeEach(func() { 84 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"de\"", "-o", expectedFilesPath, "--source-language", "en") 85 Ω(session.ExitCode()).Should(Equal(1)) 86 }) 87 88 AfterEach(func() { 89 RemoveAllFiles( 90 GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json"), 91 ) 92 }) 93 94 It("generates missing diff file", func() { 95 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json")) 96 Ω(err).Should(BeNil()) 97 Ω(fileInfo.Name()).Should(Equal("quota.go.de.json.missing.diff.json")) 98 }) 99 }) 100 101 Context("with missing and extra keys", func() { 102 BeforeEach(func() { 103 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"af\"", "-o", expectedFilesPath, "--source-language", "en") 104 Ω(session.ExitCode()).Should(Equal(1)) 105 }) 106 107 AfterEach(func() { 108 RemoveAllFiles( 109 GetFilePath(expectedFilesPath, "quota.go.af.json.missing.diff.json"), 110 GetFilePath(expectedFilesPath, "quota.go.af.json.extra.diff.json"), 111 ) 112 }) 113 114 It("generates missing and extra diff file", func() { 115 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.af.json.missing.diff.json")) 116 Ω(err).Should(BeNil()) 117 Ω(fileInfo.Name()).Should(Equal("quota.go.af.json.missing.diff.json")) 118 119 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.af.json.extra.diff.json")) 120 Ω(err).Should(BeNil()) 121 Ω(fileInfo.Name()).Should(Equal("quota.go.af.json.extra.diff.json")) 122 }) 123 }) 124 125 Context("with templated keys whose translation does not contain same arguments", func() { 126 BeforeEach(func() { 127 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"es\"", "-o", expectedFilesPath, "--source-language", "en") 128 Ω(session.ExitCode()).Should(Equal(1)) 129 }) 130 131 AfterEach(func() { 132 RemoveAllFiles( 133 GetFilePath(expectedFilesPath, "quota.go.es.json.invalid.diff.json"), 134 ) 135 }) 136 137 It("generates invalid diff file", func() { 138 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.es.json.invalid.diff.json")) 139 Ω(err).Should(BeNil()) 140 Ω(fileInfo.Name()).Should(Equal("quota.go.es.json.invalid.diff.json")) 141 }) 142 }) 143 }) 144 145 Context("with multiple language files", func() { 146 BeforeEach(func() { 147 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"de,it\"", "-o", expectedFilesPath, "--source-language", "en") 148 Ω(session.ExitCode()).Should(Equal(1)) 149 }) 150 151 AfterEach(func() { 152 RemoveAllFiles( 153 GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json"), 154 GetFilePath(expectedFilesPath, "quota.go.it.json.missing.diff.json"), 155 ) 156 }) 157 158 It("with invalid keys", func() { 159 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json")) 160 Ω(err).Should(BeNil()) 161 Ω(fileInfo.Name()).Should(Equal("quota.go.de.json.missing.diff.json")) 162 163 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.it.json.missing.diff.json")) 164 Ω(err).Should(BeNil()) 165 Ω(fileInfo.Name()).Should(Equal("quota.go.it.json.missing.diff.json")) 166 }) 167 }) 168 }) 169 170 Context("with language file", func() { 171 BeforeEach(func() { 172 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"ja\"", "-o", expectedFilesPath) 173 Ω(session.ExitCode()).Should(Equal(1)) 174 }) 175 176 AfterEach(func() { 177 RemoveAllFiles( 178 GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json"), 179 ) 180 }) 181 182 It("with additional keys", func() { 183 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json")) 184 Ω(err).Should(BeNil()) 185 Ω(fileInfo.Name()).Should(Equal("quota.go.ja.json.extra.diff.json")) 186 }) 187 }) 188 189 Context("with multiple language file", func() { 190 BeforeEach(func() { 191 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"ja,cs\"", "-o", expectedFilesPath) 192 Ω(session.ExitCode()).Should(Equal(1)) 193 }) 194 195 AfterEach(func() { 196 RemoveAllFiles( 197 GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json"), 198 GetFilePath(expectedFilesPath, "quota.go.cs.json.extra.diff.json"), 199 ) 200 }) 201 202 It("with additional keys", func() { 203 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json")) 204 Ω(err).Should(BeNil()) 205 Ω(fileInfo.Name()).Should(Equal("quota.go.ja.json.extra.diff.json")) 206 207 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.cs.json.extra.diff.json")) 208 Ω(err).Should(BeNil()) 209 Ω(fileInfo.Name()).Should(Equal("quota.go.cs.json.extra.diff.json")) 210 }) 211 }) 212 213 Context("when missing a language file", func() { 214 BeforeEach(func() { 215 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"ja,ht\"", "-o", expectedFilesPath) 216 Ω(session.ExitCode()).Should(Equal(1)) 217 }) 218 219 AfterEach(func() { 220 RemoveAllFiles( 221 GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json"), 222 ) 223 }) 224 225 It("with additional keys", func() { 226 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json")) 227 Ω(err).Should(BeNil()) 228 Ω(fileInfo.Name()).Should(Equal("quota.go.ja.json.extra.diff.json")) 229 }) 230 }) 231 }) 232 233 Context("invalid input file provided", func() { 234 Context("does not exist", func() { 235 BeforeEach(func() { 236 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.ht.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "en") 237 Ω(session.ExitCode()).Should(Equal(1)) 238 }) 239 240 It("fails verification", func() { 241 _, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ht.json")) 242 Ω(os.IsNotExist(err)).Should(Equal(true)) 243 }) 244 }) 245 246 Context("does not have any keys", func() { 247 BeforeEach(func() { 248 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.vi.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "en") 249 Ω(session.ExitCode()).Should(Equal(1)) 250 }) 251 252 It("fails verification", func() { 253 fileInfo, err := os.Stat(GetFilePath(inputFilesPath, "quota.go.vi.json")) 254 Ω(err).Should(BeNil()) 255 Ω(fileInfo.Name()).Should(Equal("quota.go.vi.json")) 256 }) 257 }) 258 }) 259 260 }) 261 262 Context("Using cobra commands", func() { 263 Context("valid input file provided", func() { 264 Context("using --source-language", func() { 265 Context("passes verifications", func() { 266 BeforeEach(func() { 267 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr,zh_CN\"", "-o", expectedFilesPath, "--source-language", "en") 268 Ω(session.ExitCode()).Should(Equal(0)) 269 }) 270 271 It("with language file with valid keys", func() { 272 _, err := os.Stat(GetFilePath(inputFilesPath, "quota.go.en.json.missing.diff")) 273 Ω(os.IsNotExist(err)).Should(Equal(true)) 274 275 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.en.json.extra.diff")) 276 Ω(os.IsNotExist(err)).Should(Equal(true)) 277 }) 278 }) 279 }) 280 281 Context("not using --source-language", func() { 282 Context("passes verifications", func() { 283 BeforeEach(func() { 284 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"fr,zh_CN\"", "-o", expectedFilesPath) 285 Ω(session.ExitCode()).Should(Equal(0)) 286 }) 287 288 It("with language file with valid keys", func() { 289 _, err := os.Stat(GetFilePath(inputFilesPath, "quota.go.fr.json.missing.diff.json")) 290 Ω(os.IsNotExist(err)).Should(Equal(true)) 291 292 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.fr.json.missing.diff.json")) 293 Ω(os.IsNotExist(err)).Should(Equal(true)) 294 295 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.zh_CN.json.extra.diff.json")) 296 Ω(os.IsNotExist(err)).Should(Equal(true)) 297 298 _, err = os.Stat(GetFilePath(inputFilesPath, "quota.go.zh_CN.json.extra.diff.json")) 299 Ω(os.IsNotExist(err)).Should(Equal(true)) 300 }) 301 }) 302 }) 303 304 Context("fails verification", func() { 305 Context("with one language file", func() { 306 Context("with missing keys", func() { 307 BeforeEach(func() { 308 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"de\"", "-o", expectedFilesPath, "--source-language", "en") 309 Ω(session.ExitCode()).Should(Equal(1)) 310 }) 311 312 AfterEach(func() { 313 RemoveAllFiles( 314 GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json"), 315 ) 316 }) 317 318 It("generates missing diff file", func() { 319 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json")) 320 Ω(err).Should(BeNil()) 321 Ω(fileInfo.Name()).Should(Equal("quota.go.de.json.missing.diff.json")) 322 }) 323 }) 324 325 Context("with missing and extra keys", func() { 326 BeforeEach(func() { 327 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"af\"", "-o", expectedFilesPath, "--source-language", "en") 328 Ω(session.ExitCode()).Should(Equal(1)) 329 }) 330 331 AfterEach(func() { 332 RemoveAllFiles( 333 GetFilePath(expectedFilesPath, "quota.go.af.json.missing.diff.json"), 334 GetFilePath(expectedFilesPath, "quota.go.af.json.extra.diff.json"), 335 ) 336 }) 337 338 It("generates missing and extra diff file", func() { 339 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.af.json.missing.diff.json")) 340 Ω(err).Should(BeNil()) 341 Ω(fileInfo.Name()).Should(Equal("quota.go.af.json.missing.diff.json")) 342 343 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.af.json.extra.diff.json")) 344 Ω(err).Should(BeNil()) 345 Ω(fileInfo.Name()).Should(Equal("quota.go.af.json.extra.diff.json")) 346 }) 347 }) 348 349 Context("with templated keys whose translation does not contain same arguments", func() { 350 BeforeEach(func() { 351 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"es\"", "-o", expectedFilesPath, "--source-language", "en") 352 Ω(session.ExitCode()).Should(Equal(1)) 353 }) 354 355 AfterEach(func() { 356 RemoveAllFiles( 357 GetFilePath(expectedFilesPath, "quota.go.es.json.invalid.diff.json"), 358 ) 359 }) 360 361 It("generates invalid diff file", func() { 362 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.es.json.invalid.diff.json")) 363 Ω(err).Should(BeNil()) 364 Ω(fileInfo.Name()).Should(Equal("quota.go.es.json.invalid.diff.json")) 365 }) 366 }) 367 }) 368 369 Context("with multiple language files", func() { 370 BeforeEach(func() { 371 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"de,it\"", "-o", expectedFilesPath, "--source-language", "en") 372 Ω(session.ExitCode()).Should(Equal(1)) 373 }) 374 375 AfterEach(func() { 376 RemoveAllFiles( 377 GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json"), 378 GetFilePath(expectedFilesPath, "quota.go.it.json.missing.diff.json"), 379 ) 380 }) 381 382 It("with invalid keys", func() { 383 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.de.json.missing.diff.json")) 384 Ω(err).Should(BeNil()) 385 Ω(fileInfo.Name()).Should(Equal("quota.go.de.json.missing.diff.json")) 386 387 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.it.json.missing.diff.json")) 388 Ω(err).Should(BeNil()) 389 Ω(fileInfo.Name()).Should(Equal("quota.go.it.json.missing.diff.json")) 390 }) 391 }) 392 }) 393 394 Context("with language file", func() { 395 BeforeEach(func() { 396 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"ja\"", "-o", expectedFilesPath) 397 Ω(session.ExitCode()).Should(Equal(1)) 398 }) 399 400 AfterEach(func() { 401 RemoveAllFiles( 402 GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json"), 403 ) 404 }) 405 406 It("with additional keys", func() { 407 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json")) 408 Ω(err).Should(BeNil()) 409 Ω(fileInfo.Name()).Should(Equal("quota.go.ja.json.extra.diff.json")) 410 }) 411 }) 412 413 Context("with multiple language file", func() { 414 BeforeEach(func() { 415 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"ja,cs\"", "-o", expectedFilesPath) 416 Ω(session.ExitCode()).Should(Equal(1)) 417 }) 418 419 AfterEach(func() { 420 RemoveAllFiles( 421 GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json"), 422 GetFilePath(expectedFilesPath, "quota.go.cs.json.extra.diff.json"), 423 ) 424 }) 425 426 It("with additional keys", func() { 427 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json")) 428 Ω(err).Should(BeNil()) 429 Ω(fileInfo.Name()).Should(Equal("quota.go.ja.json.extra.diff.json")) 430 431 fileInfo, err = os.Stat(GetFilePath(expectedFilesPath, "quota.go.cs.json.extra.diff.json")) 432 Ω(err).Should(BeNil()) 433 Ω(fileInfo.Name()).Should(Equal("quota.go.cs.json.extra.diff.json")) 434 }) 435 }) 436 437 Context("when missing a language file", func() { 438 BeforeEach(func() { 439 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.en.json"), "--languages", "\"ja,ht\"", "-o", expectedFilesPath) 440 Ω(session.ExitCode()).Should(Equal(1)) 441 }) 442 443 AfterEach(func() { 444 RemoveAllFiles( 445 GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json"), 446 ) 447 }) 448 449 It("with additional keys", func() { 450 fileInfo, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ja.json.extra.diff.json")) 451 Ω(err).Should(BeNil()) 452 Ω(fileInfo.Name()).Should(Equal("quota.go.ja.json.extra.diff.json")) 453 }) 454 }) 455 }) 456 457 Context("invalid input file provided", func() { 458 Context("does not exist", func() { 459 BeforeEach(func() { 460 session := Runi18n("verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.ht.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "en") 461 Ω(session.ExitCode()).Should(Equal(1)) 462 }) 463 464 It("fails verification", func() { 465 _, err := os.Stat(GetFilePath(expectedFilesPath, "quota.go.ht.json")) 466 Ω(os.IsNotExist(err)).Should(Equal(true)) 467 }) 468 }) 469 470 Context("does not have any keys", func() { 471 BeforeEach(func() { 472 session := Runi18n("-c", "verify-strings", "-v", "-f", filepath.Join(inputFilesPath, "quota.go.vi.json"), "--languages", "\"fr\"", "-o", expectedFilesPath, "--source-language", "en") 473 Ω(session.ExitCode()).Should(Equal(1)) 474 }) 475 476 It("fails verification", func() { 477 fileInfo, err := os.Stat(GetFilePath(inputFilesPath, "quota.go.vi.json")) 478 Ω(err).Should(BeNil()) 479 Ω(fileInfo.Name()).Should(Equal("quota.go.vi.json")) 480 }) 481 }) 482 }) 483 484 }) 485 486 })