test_env_file_config.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import os
  12. import pytest
  13. from taipy.config.config import Config
  14. from taipy.config.exceptions.exceptions import ConfigurationUpdateBlocked
  15. from tests.config.utils.named_temporary_file import NamedTemporaryFile
  16. config_from_filename = NamedTemporaryFile(
  17. """
  18. [TAIPY]
  19. custom_property_not_overwritten = true
  20. custom_property_overwritten = 10
  21. """
  22. )
  23. config_from_environment = NamedTemporaryFile(
  24. """
  25. [TAIPY]
  26. custom_property_overwritten = 11
  27. """
  28. )
  29. def test_load_from_environment_overwrite_load_from_filename():
  30. os.environ[Config._ENVIRONMENT_VARIABLE_NAME_WITH_CONFIG_PATH] = config_from_environment.filename
  31. Config.load(config_from_filename.filename)
  32. assert Config.global_config.custom_property_not_overwritten is True
  33. assert Config.global_config.custom_property_overwritten == 11
  34. os.environ.pop(Config._ENVIRONMENT_VARIABLE_NAME_WITH_CONFIG_PATH)
  35. def test_block_load_from_environment_overwrite_load_from_filename():
  36. Config.load(config_from_filename.filename)
  37. assert Config.global_config.custom_property_not_overwritten is True
  38. assert Config.global_config.custom_property_overwritten == 10
  39. Config.block_update()
  40. with pytest.raises(ConfigurationUpdateBlocked):
  41. os.environ[Config._ENVIRONMENT_VARIABLE_NAME_WITH_CONFIG_PATH] = config_from_environment.filename
  42. Config.load(config_from_filename.filename)
  43. os.environ.pop(Config._ENVIRONMENT_VARIABLE_NAME_WITH_CONFIG_PATH)
  44. assert Config.global_config.custom_property_not_overwritten is True
  45. assert Config.global_config.custom_property_overwritten == 10 # The Config.load is failed to override