test_translations.py 1.7 KB

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