test_prerequisites.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from unittest.mock import Mock, mock_open
  2. import pytest
  3. from reflex import constants
  4. from reflex.config import Config
  5. from reflex.utils.prerequisites import _update_next_config, initialize_requirements_txt
  6. @pytest.mark.parametrize(
  7. "config, export, expected_output",
  8. [
  9. (
  10. Config(
  11. app_name="test",
  12. ),
  13. False,
  14. 'module.exports = {basePath: "", compress: true, reactStrictMode: true, trailingSlash: true};',
  15. ),
  16. (
  17. Config(
  18. app_name="test",
  19. next_compression=False,
  20. ),
  21. False,
  22. 'module.exports = {basePath: "", compress: false, reactStrictMode: true, trailingSlash: true};',
  23. ),
  24. (
  25. Config(
  26. app_name="test",
  27. frontend_path="/test",
  28. ),
  29. False,
  30. 'module.exports = {basePath: "/test", compress: true, reactStrictMode: true, trailingSlash: true};',
  31. ),
  32. (
  33. Config(
  34. app_name="test",
  35. frontend_path="/test",
  36. next_compression=False,
  37. ),
  38. False,
  39. 'module.exports = {basePath: "/test", compress: false, reactStrictMode: true, trailingSlash: true};',
  40. ),
  41. (
  42. Config(
  43. app_name="test",
  44. ),
  45. True,
  46. 'module.exports = {basePath: "", compress: true, reactStrictMode: true, trailingSlash: true, output: "export", distDir: "_static"};',
  47. ),
  48. ],
  49. )
  50. def test_update_next_config(config, export, expected_output):
  51. output = _update_next_config(config, export=export)
  52. assert output == expected_output
  53. def test_initialize_requirements_txt_no_op(mocker):
  54. # File exists, reflex is included, do nothing
  55. mocker.patch("pathlib.Path.exists", return_value=True)
  56. mocker.patch(
  57. "charset_normalizer.from_path",
  58. return_value=Mock(best=lambda: Mock(encoding="utf-8")),
  59. )
  60. mock_fp_touch = mocker.patch("pathlib.Path.touch")
  61. open_mock = mock_open(read_data="reflex==0.2.9")
  62. mocker.patch("builtins.open", open_mock)
  63. initialize_requirements_txt()
  64. assert open_mock.call_count == 1
  65. assert open_mock.call_args.kwargs["encoding"] == "utf-8"
  66. assert open_mock().write.call_count == 0
  67. mock_fp_touch.assert_not_called()
  68. def test_initialize_requirements_txt_missing_reflex(mocker):
  69. # File exists, reflex is not included, add reflex
  70. mocker.patch("pathlib.Path.exists", return_value=True)
  71. mocker.patch(
  72. "charset_normalizer.from_path",
  73. return_value=Mock(best=lambda: Mock(encoding="utf-8")),
  74. )
  75. open_mock = mock_open(read_data="random-package=1.2.3")
  76. mocker.patch("builtins.open", open_mock)
  77. initialize_requirements_txt()
  78. # Currently open for read, then open for append
  79. assert open_mock.call_count == 2
  80. for call_args in open_mock.call_args_list:
  81. assert call_args.kwargs["encoding"] == "utf-8"
  82. assert (
  83. open_mock().write.call_args[0][0]
  84. == f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  85. )
  86. def test_initialize_requirements_txt_not_exist(mocker):
  87. # File does not exist, create file with reflex
  88. mocker.patch("pathlib.Path.exists", return_value=False)
  89. open_mock = mock_open()
  90. mocker.patch("builtins.open", open_mock)
  91. initialize_requirements_txt()
  92. assert open_mock.call_count == 2
  93. # By default, use utf-8 encoding
  94. for call_args in open_mock.call_args_list:
  95. assert call_args.kwargs["encoding"] == "utf-8"
  96. assert open_mock().write.call_count == 1
  97. assert (
  98. open_mock().write.call_args[0][0]
  99. == f"{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  100. )
  101. def test_requirements_txt_cannot_detect_encoding(mocker):
  102. mocker.patch("pathlib.Path.exists", return_value=True)
  103. mock_open = mocker.patch("builtins.open")
  104. mocker.patch(
  105. "charset_normalizer.from_path",
  106. return_value=Mock(best=lambda: None),
  107. )
  108. initialize_requirements_txt()
  109. mock_open.assert_not_called()
  110. def test_requirements_txt_other_encoding(mocker):
  111. mocker.patch("pathlib.Path.exists", return_value=True)
  112. mocker.patch(
  113. "charset_normalizer.from_path",
  114. return_value=Mock(best=lambda: Mock(encoding="utf-16")),
  115. )
  116. initialize_requirements_txt()
  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. for call_args in open_mock.call_args_list:
  123. assert call_args.kwargs["encoding"] == "utf-16"
  124. assert (
  125. open_mock().write.call_args[0][0]
  126. == f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  127. )