Ver Fonte

Added Environmental variable REFLEX_DIR (#2457)

wassaf shahzad há 1 ano atrás
pai
commit
84a2bb9671
2 ficheiros alterados com 24 adições e 1 exclusões
  1. 5 1
      reflex/constants/base.py
  2. 19 0
      tests/test_config.py

+ 5 - 1
reflex/constants/base.py

@@ -61,10 +61,14 @@ class Reflex(SimpleNamespace):
 
     # Files and directories used to init a new project.
     # The directory to store reflex dependencies.
-    DIR = (
+    # Get directory value from enviroment variables if it exists.
+    _dir = os.environ.get("REFLEX_DIR", "")
+
+    DIR = _dir or (
         # on windows, we use C:/Users/<username>/AppData/Local/reflex.
         # on macOS, we use ~/Library/Application Support/reflex.
         # on linux, we use ~/.local/share/reflex.
+        # If user sets REFLEX_DIR envroment variable use that instead.
         PlatformDirs(MODULE_NAME, False).user_data_dir
     )
     # The root directory of the reflex library.

+ 19 - 0
tests/test_config.py

@@ -1,3 +1,4 @@
+import multiprocessing
 import os
 from typing import Any, Dict
 
@@ -200,3 +201,21 @@ def test_replace_defaults(
     c._set_persistent(**set_persistent_vars)
     for key, value in exp_config_values.items():
         assert getattr(c, key) == value
+
+
+def reflex_dir_constant():
+    return rx.constants.Reflex.DIR
+
+
+def test_reflex_dir_env_var(monkeypatch, tmp_path):
+    """Test that the REFLEX_DIR environment variable is used to set the Reflex.DIR constant.
+
+    Args:
+        monkeypatch: The pytest monkeypatch object.
+        tmp_path: The pytest tmp_path object.
+    """
+    monkeypatch.setenv("REFLEX_DIR", str(tmp_path))
+
+    mp_ctx = multiprocessing.get_context(method="spawn")
+    with mp_ctx.Pool(processes=1) as pool:
+        assert pool.apply(reflex_dir_constant) == str(tmp_path)