test_translations.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Tests for translation files.
  3. """
  4. import warnings
  5. from fnmatch import fnmatch
  6. from os import walk
  7. from os.path import dirname, join
  8. from homeassistant.util.json import load_json
  9. import custom_components.tuya_local as root
  10. def get_translations():
  11. translations = join(dirname(root.__file__), "translations")
  12. for path, dirs, files in walk(translations):
  13. for file in files:
  14. if fnmatch(file, "*.json") and file != "en.json":
  15. yield (file, load_json(join(path, file)))
  16. english = None
  17. def get_english():
  18. global english
  19. if english is None:
  20. translations = join(dirname(root.__file__), "translations", "en.json")
  21. english = load_json(translations)
  22. return english
  23. def json_compare_keys(english, json, file, path=""):
  24. matched = True
  25. for key in english:
  26. if key not in json:
  27. # Issue a warning rather than a failure.
  28. # This lets us catch all the missing translations at once.
  29. # Also, contributors shouldn't need to add translations for every language.
  30. warnings.warn(f"{file} Missing translation for {path}{key}")
  31. matched = False
  32. elif isinstance(english[key], dict):
  33. json_compare_keys(english[key], json[key], file, f"{path}{key}.")
  34. for key in json:
  35. if key not in english:
  36. warnings.warn(f"{file} Extra translation for {path}{key}")
  37. matched = False
  38. return matched
  39. def test_missing_translations():
  40. english = get_english()
  41. unmatched = []
  42. for file, json in get_translations():
  43. if not json_compare_keys(english, json, file):
  44. unmatched.append(file)
  45. if unmatched:
  46. raise AssertionError(f"Inconsistent translations in {', '.join(unmatched)}")
  47. # @pytest.mark.parametrize("device", get_devices())
  48. # def test_device_covered(device):
  49. # for entity in device.all_entities():
  50. # if entity.deprecated:
  51. # subtest_entity_covered(entity)