test_prerequisites.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from unittest.mock import mock_open
  2. import pytest
  3. from reflex import constants
  4. from reflex.config import Config
  5. from reflex.utils.prerequisites import (
  6. initialize_requirements_txt,
  7. install_node,
  8. update_next_config,
  9. )
  10. @pytest.mark.parametrize(
  11. "template_next_config, reflex_config, expected_next_config",
  12. [
  13. (
  14. """
  15. module.exports = {
  16. basePath: "",
  17. compress: true,
  18. reactStrictMode: true,
  19. trailingSlash: true,
  20. };
  21. """,
  22. Config(
  23. app_name="test",
  24. ),
  25. """
  26. module.exports = {
  27. basePath: "",
  28. compress: true,
  29. reactStrictMode: true,
  30. trailingSlash: true,
  31. };
  32. """,
  33. ),
  34. (
  35. """
  36. module.exports = {
  37. basePath: "",
  38. compress: true,
  39. reactStrictMode: true,
  40. trailingSlash: true,
  41. };
  42. """,
  43. Config(
  44. app_name="test",
  45. next_compression=False,
  46. ),
  47. """
  48. module.exports = {
  49. basePath: "",
  50. compress: false,
  51. reactStrictMode: true,
  52. trailingSlash: true,
  53. };
  54. """,
  55. ),
  56. (
  57. """
  58. module.exports = {
  59. basePath: "",
  60. compress: true,
  61. reactStrictMode: true,
  62. trailingSlash: true,
  63. };
  64. """,
  65. Config(
  66. app_name="test",
  67. frontend_path="/test",
  68. ),
  69. """
  70. module.exports = {
  71. basePath: "/test",
  72. compress: true,
  73. reactStrictMode: true,
  74. trailingSlash: true,
  75. };
  76. """,
  77. ),
  78. (
  79. """
  80. module.exports = {
  81. basePath: "",
  82. compress: true,
  83. reactStrictMode: true,
  84. trailingSlash: true,
  85. };
  86. """,
  87. Config(
  88. app_name="test",
  89. frontend_path="/test",
  90. next_compression=False,
  91. ),
  92. """
  93. module.exports = {
  94. basePath: "/test",
  95. compress: false,
  96. reactStrictMode: true,
  97. trailingSlash: true,
  98. };
  99. """,
  100. ),
  101. ],
  102. )
  103. def test_update_next_config(template_next_config, reflex_config, expected_next_config):
  104. assert (
  105. update_next_config(template_next_config, reflex_config) == expected_next_config
  106. )
  107. def test_initialize_requirements_txt(mocker):
  108. # File exists, reflex is included, do nothing
  109. mocker.patch("os.path.exists", return_value=True)
  110. open_mock = mock_open(read_data="reflex==0.2.9")
  111. mocker.patch("builtins.open", open_mock)
  112. initialize_requirements_txt()
  113. assert open_mock.call_count == 1
  114. assert open_mock().write.call_count == 0
  115. def test_initialize_requirements_txt_missing_reflex(mocker):
  116. # File exists, reflex is not included, add reflex
  117. open_mock = mock_open(read_data="random-package=1.2.3")
  118. mocker.patch("builtins.open", open_mock)
  119. initialize_requirements_txt()
  120. # Currently open for read, then open for append
  121. assert open_mock.call_count == 2
  122. assert open_mock().write.call_count == 1
  123. assert (
  124. open_mock().write.call_args[0][0]
  125. == f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  126. )
  127. def test_initialize_requirements_txt_not_exist(mocker):
  128. # File does not exist, create file with reflex
  129. mocker.patch("os.path.exists", return_value=False)
  130. open_mock = mock_open()
  131. mocker.patch("builtins.open", open_mock)
  132. initialize_requirements_txt()
  133. assert open_mock.call_count == 2
  134. assert open_mock().write.call_count == 1
  135. assert (
  136. open_mock().write.call_args[0][0]
  137. == f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  138. )
  139. @pytest.mark.parametrize(
  140. "is_windows, is_linux, release, expected",
  141. [
  142. (True, False, "10.0.20348", True),
  143. (False, True, "6.2.0-1015-azure", False),
  144. (False, True, "4.4.0-17763-Microsoft", True),
  145. (False, False, "21.6.0", False),
  146. ],
  147. )
  148. def test_install_node(is_windows, is_linux, release, expected, mocker):
  149. mocker.patch("reflex.utils.prerequisites.constants.IS_WINDOWS", is_windows)
  150. mocker.patch("reflex.utils.prerequisites.constants.IS_LINUX", is_linux)
  151. mocker.patch("reflex.utils.prerequisites.platform.release", return_value=release)
  152. mocker.patch("reflex.utils.prerequisites.download_and_extract_fnm_zip")
  153. mocker.patch("reflex.utils.prerequisites.processes.new_process")
  154. mocker.patch("reflex.utils.prerequisites.processes.show_status")
  155. mocker.patch("reflex.utils.prerequisites.os.chmod")
  156. path_ops = mocker.patch("reflex.utils.prerequisites.path_ops.mkdir")
  157. install_node()
  158. if expected:
  159. path_ops.assert_called_once()
  160. else:
  161. path_ops.assert_not_called()