test_cli.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2021-2025 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 tempfile
  13. from unittest.mock import patch
  14. import pytest
  15. from taipy.common.config import Config, _inject_section
  16. from taipy.gui import Gui
  17. from taipy.gui._default_config import default_config
  18. from taipy.gui._gui_section import _GuiSection
  19. class NamedTemporaryFile:
  20. def __init__(self, content=None):
  21. with tempfile.NamedTemporaryFile("w", delete=False) as fd:
  22. if content:
  23. fd.write(content)
  24. self.filename = fd.name
  25. def read(self):
  26. with open(self.filename, "r") as fp:
  27. return fp.read()
  28. def __del__(self):
  29. os.unlink(self.filename)
  30. @pytest.fixture
  31. def init_config(reset_configuration_singleton):
  32. def _init_config():
  33. reset_configuration_singleton()
  34. _inject_section(
  35. _GuiSection,
  36. "gui_config",
  37. _GuiSection(property_list=list(default_config)),
  38. [("configure_gui", _GuiSection._configure)],
  39. add_to_unconflicted_sections=True,
  40. )
  41. return _init_config
  42. @pytest.fixture(scope="function", autouse=True)
  43. def cleanup_test(helpers, init_config):
  44. init_config()
  45. helpers.test_cleanup()
  46. yield
  47. init_config()
  48. helpers.test_cleanup()
  49. def test_gui_service_arguments_hierarchy():
  50. # Test default configuration
  51. gui = Gui()
  52. with patch("sys.argv", ["prog"]):
  53. gui.run(run_server=False)
  54. service_config = gui._config.config
  55. assert not service_config["allow_unsafe_werkzeug"]
  56. assert service_config["async_mode"] == "gevent"
  57. assert service_config["change_delay"] is None
  58. assert service_config["chart_dark_template"] is None
  59. assert service_config["dark_mode"]
  60. assert service_config["dark_theme"] is None
  61. assert not service_config["debug"]
  62. assert not service_config["extended_status"]
  63. assert service_config["favicon"] is None
  64. assert not service_config["flask_log"]
  65. assert service_config["host"] == "127.0.0.1"
  66. assert service_config["light_theme"] is None
  67. assert service_config["margin"] is None
  68. assert service_config["ngrok_token"] == ""
  69. assert service_config["notification_duration"] == 3000
  70. assert service_config["port"] == 5000
  71. assert service_config["propagate"]
  72. assert service_config["run_browser"]
  73. assert not service_config["run_in_thread"]
  74. assert not service_config["run_server"]
  75. assert not service_config["single_client"]
  76. assert service_config["state_retention_period"] == 0
  77. assert not service_config["system_notification"]
  78. assert service_config["theme"] is None
  79. assert service_config["time_zone"] is None
  80. assert service_config["title"] is None
  81. assert service_config["upload_folder"] is None
  82. assert not service_config["use_arrow"]
  83. assert not service_config["use_reloader"]
  84. assert service_config["watermark"] == "Taipy inside"
  85. assert service_config["webapp_path"] is None
  86. gui.stop()
  87. # Override default configuration by explicit defined arguments in Gui.run()
  88. gui = Gui()
  89. with patch("sys.argv", ["prog"]):
  90. gui.run(run_server=False, watermark="", host="my_host", port=5001)
  91. service_config = gui._config.config
  92. assert service_config["watermark"] == ""
  93. assert service_config["host"] == "my_host"
  94. assert service_config["port"] == 5001
  95. gui.stop()
  96. # Override Gui.run() arguments by explicit defined arguments in Config.configure_gui()
  97. Config.configure_gui(dark_mode=False, host="my_2nd_host", port=5002)
  98. gui = Gui()
  99. with patch("sys.argv", ["prog"]):
  100. gui.run(run_server=False, watermark="", host="my_host", port=5001)
  101. service_config = gui._config.config
  102. assert not service_config["dark_mode"]
  103. assert service_config["host"] == "my_2nd_host"
  104. assert service_config["watermark"] == ""
  105. assert service_config["port"] == 5002
  106. gui.stop()
  107. # Override Config.configure_gui() arguments by loading a TOML file with [gui] section
  108. toml_config = NamedTemporaryFile(
  109. content="""
  110. [TAIPY]
  111. [gui]
  112. host = "my_3rd_host"
  113. port = 5003
  114. use_reloader = "true:bool"
  115. """
  116. )
  117. Config.load(toml_config.filename)
  118. gui = Gui()
  119. with patch("sys.argv", ["prog"]):
  120. gui.run(run_server=False, host="my_host", port=5001)
  121. service_config = gui._config.config
  122. assert service_config["host"] == "my_3rd_host"
  123. assert service_config["port"] == 5003
  124. assert service_config["use_reloader"]
  125. gui.stop()
  126. # Override TOML configuration file with CLI arguments
  127. with patch("sys.argv", ["prog", "--host", "my_4th_host", "--port", "5004", "--no-reloader", "--debug"]):
  128. gui = Gui()
  129. gui.run(run_server=False, host="my_host", port=5001)
  130. service_config = gui._config.config
  131. assert service_config["host"] == "my_4th_host"
  132. assert service_config["port"] == 5004
  133. assert not service_config["use_reloader"]
  134. assert service_config["debug"]
  135. gui.stop()
  136. def test_clean_config():
  137. gui_config = Config.configure_gui(dark_mode=False)
  138. assert Config.gui_config is gui_config
  139. gui_config._clean()
  140. # Check if the instance before and after _clean() is the same
  141. assert Config.gui_config is gui_config
  142. assert gui_config.dark_mode is None
  143. assert gui_config.properties == {}