main_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func writeLanguageFile(t *testing.T, content string) string {
  8. t.Helper()
  9. path := filepath.Join(t.TempDir(), "xx-XX.yaml")
  10. if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
  11. t.Fatal(err)
  12. }
  13. return path
  14. }
  15. func TestLoadLanguageFileRejectsEmptyFile(t *testing.T) {
  16. if _, err := loadLanguageFile(writeLanguageFile(t, "")); err == nil {
  17. t.Error("expected an error for an empty language file")
  18. }
  19. }
  20. func TestLoadLanguageFileRejectsWrongSchemaVersion(t *testing.T) {
  21. if _, err := loadLanguageFile(writeLanguageFile(t, "schemaVersion: 2\ntranslations:\n welcome: Hi\n")); err == nil {
  22. t.Error("expected an error for an unsupported schemaVersion")
  23. }
  24. }
  25. func TestLoadLanguageFileRejectsNonIntegerSchemaVersion(t *testing.T) {
  26. for _, schemaVersion := range []string{"1.5", "1.0", "\"1\"", "0x1", "[1]"} {
  27. content := "schemaVersion: " + schemaVersion + "\ntranslations:\n welcome: Hi\n"
  28. if _, err := loadLanguageFile(writeLanguageFile(t, content)); err == nil {
  29. t.Errorf("expected an error for schemaVersion %s", schemaVersion)
  30. }
  31. }
  32. }
  33. func TestLoadLanguageFileRejectsMissingSchemaVersion(t *testing.T) {
  34. if _, err := loadLanguageFile(writeLanguageFile(t, "translations:\n welcome: Hi\n")); err == nil {
  35. t.Error("expected an error for a missing schemaVersion")
  36. }
  37. }
  38. func TestLoadLanguageFileRejectsInvalidYaml(t *testing.T) {
  39. if _, err := loadLanguageFile(writeLanguageFile(t, "schemaVersion: 1\ntranslations: [\n")); err == nil {
  40. t.Error("expected an error for invalid YAML")
  41. }
  42. }
  43. func TestLoadLanguageFileAcceptsValidFile(t *testing.T) {
  44. translations, err := loadLanguageFile(writeLanguageFile(t, "schemaVersion: 1\ntranslations:\n welcome: Hi\n"))
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if translations["welcome"] != "Hi" {
  49. t.Errorf("unexpected translations: %v", translations)
  50. }
  51. }
  52. func TestValidateTranslationsAllowsMissingKeys(t *testing.T) {
  53. output := &CombinedTranslationsOutput{Messages: map[string]map[string]string{
  54. "en": {"welcome": "Hi", "docs": "Docs"},
  55. "xx-XX": {"welcome": "Salut"},
  56. }}
  57. if err := validateTranslations(output); err != nil {
  58. t.Errorf("missing keys should only warn, got %v", err)
  59. }
  60. }
  61. func TestValidateTranslationsRejectsExtraKeys(t *testing.T) {
  62. output := &CombinedTranslationsOutput{Messages: map[string]map[string]string{
  63. "en": {"welcome": "Hi"},
  64. "xx-XX": {"welcome": "Salut", "welcom": "Typo"},
  65. }}
  66. if err := validateTranslations(output); err == nil {
  67. t.Error("expected an error for keys not in 'en'")
  68. }
  69. }
  70. func TestValidateTranslationsRequiresEnglish(t *testing.T) {
  71. output := &CombinedTranslationsOutput{Messages: map[string]map[string]string{
  72. "xx-XX": {"welcome": "Salut"},
  73. }}
  74. if err := validateTranslations(output); err == nil {
  75. t.Error("expected an error when 'en' is missing")
  76. }
  77. }