test_platform_config.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import sys
  3. from typing import Optional
  4. import pytest
  5. PLUGIN_ROOT = os.path.abspath(os.path.dirname(__file__))
  6. sys.path.insert(0, PLUGIN_ROOT)
  7. sys.path.insert(0, os.environ.get("HERMES_ROOT", "/opt/hermes"))
  8. sys.path.insert(0, "/root/.hermes/plugins/platforms/chatto")
  9. from platform_config import (
  10. _get_env_or_extra_str,
  11. _get_env_or_extra_str_opt,
  12. _get_env_or_extra_truthy,
  13. _split_str_to_list,
  14. _get_env_or_extra_list,
  15. )
  16. class TestPlatformConfigHelpers:
  17. """Test Chatto configuration helper functions."""
  18. def test_get_env_or_extra_str_prefers_env(self, monkeypatch):
  19. monkeypatch.setenv("CHATTO_TEST", " env-value ")
  20. assert _get_env_or_extra_str("CHATTO_TEST", "extra-value") == "env-value"
  21. def test_get_env_or_extra_str_uses_extra_when_no_env(self, monkeypatch):
  22. monkeypatch.delenv("CHATTO_TEST", raising=False)
  23. assert _get_env_or_extra_str("CHATTO_TEST", " extra-value ") == "extra-value"
  24. def test_get_env_or_extra_str_returns_empty_when_missing(self, monkeypatch):
  25. """The non-optional helper coerces a missing value to "" — use
  26. _get_env_or_extra_str_opt() when None has to stay distinguishable."""
  27. monkeypatch.delenv("CHATTO_TEST", raising=False)
  28. assert _get_env_or_extra_str("CHATTO_TEST", None) == ""
  29. def test_get_env_or_extra_str_opt_returns_none_when_missing(self, monkeypatch):
  30. monkeypatch.delenv("CHATTO_TEST", raising=False)
  31. assert _get_env_or_extra_str_opt("CHATTO_TEST", None) is None
  32. def test_get_env_or_extra_str_opt_prefers_env(self, monkeypatch):
  33. monkeypatch.setenv("CHATTO_TEST", " env-value ")
  34. assert _get_env_or_extra_str_opt("CHATTO_TEST", "extra-value") == "env-value"
  35. def test_get_env_or_extra_truthy_parses_truthy_values(self, monkeypatch):
  36. monkeypatch.setenv("CHATTO_TEST", " yes ")
  37. assert _get_env_or_extra_truthy("CHATTO_TEST", None) is True
  38. def test_get_env_or_extra_truthy_uses_extra_when_no_env(self, monkeypatch):
  39. monkeypatch.delenv("CHATTO_TEST", raising=False)
  40. assert _get_env_or_extra_truthy("CHATTO_TEST", " true ") is True
  41. def test_get_env_or_extra_truthy_uses_default_false(self, monkeypatch):
  42. monkeypatch.delenv("CHATTO_TEST", raising=False)
  43. assert _get_env_or_extra_truthy("CHATTO_TEST", "false") is False
  44. def test_get_env_or_extra_truthy_default_true_when_none_and_no_extra(self, monkeypatch):
  45. monkeypatch.delenv("CHATTO_TEST", raising=False)
  46. assert _get_env_or_extra_truthy("CHATTO_TEST", None, default=True) is True
  47. def test_get_env_or_extra_truthy_default_false_when_none_and_no_extra(self, monkeypatch):
  48. monkeypatch.delenv("CHATTO_TEST", raising=False)
  49. assert _get_env_or_extra_truthy("CHATTO_TEST", None, default=False) is False
  50. def test_get_env_or_extra_truthy_explicit_bool_true(self, monkeypatch):
  51. monkeypatch.delenv("CHATTO_TEST", raising=False)
  52. assert _get_env_or_extra_truthy("CHATTO_TEST", True, default=False) is True
  53. def test_get_env_or_extra_truthy_explicit_bool_false(self, monkeypatch):
  54. monkeypatch.delenv("CHATTO_TEST", raising=False)
  55. assert _get_env_or_extra_truthy("CHATTO_TEST", False, default=True) is False
  56. def test_split_str_to_list_handles_comma_separated_values(self):
  57. assert _split_str_to_list("a,b, c ,d") == ["a", "b", "c", "d"]
  58. def test_get_env_or_extra_list_prefers_env(self, monkeypatch):
  59. monkeypatch.setenv("CHATTO_TEST", " one,two , three")
  60. assert _get_env_or_extra_list("CHATTO_TEST", ["ignored", "list"]) == ["one", "two", "three"]
  61. def test_get_env_or_extra_list_uses_extra_list(self, monkeypatch):
  62. monkeypatch.delenv("CHATTO_TEST", raising=False)
  63. assert _get_env_or_extra_list("CHATTO_TEST", ["a", " b "]) == ["a", "b"]
  64. def test_get_env_or_extra_list_uses_extra_string(self, monkeypatch):
  65. monkeypatch.delenv("CHATTO_TEST", raising=False)
  66. assert _get_env_or_extra_list("CHATTO_TEST", "x,y") == ["x", "y"]
  67. def test_get_env_or_extra_list_returns_empty_for_none(self, monkeypatch):
  68. monkeypatch.delenv("CHATTO_TEST", raising=False)
  69. assert _get_env_or_extra_list("CHATTO_TEST", None) == []