test_translations.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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"):
  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. json = load_json(translations)
  22. return json
  23. def json_compare_keys(english, json, file, path=""):
  24. for key in english:
  25. if key not in json:
  26. # Issue a warning rather than a failure.
  27. # This lets us catch all the missing translations at once.
  28. # Also, contributors shouldn't need to add translations for every language.
  29. warnings.warn(f"{file} Missing translation for {path}{key}")
  30. elif isinstance(english[key], dict):
  31. json_compare_keys(english[key], json[key], file, f"{path}{key}.")
  32. def test_missing_translations():
  33. english = get_english()
  34. for file, json in get_translations():
  35. json_compare_keys(english, json, file)
  36. # @pytest.mark.parametrize("device", get_devices())
  37. # def test_device_covered(device):
  38. # for entity in device.all_entities():
  39. # if entity.deprecated:
  40. # subtest_entity_covered(entity)