From 84a2bb9671cf1e8fb73d6a471ad6b1cc2b852062 Mon Sep 17 00:00:00 2001 From: wassaf shahzad Date: Wed, 31 Jan 2024 03:18:29 +0100 Subject: [PATCH] Added Environmental variable REFLEX_DIR (#2457) --- reflex/constants/base.py | 6 +++++- tests/test_config.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/reflex/constants/base.py b/reflex/constants/base.py index d0d9a124c..eeef595eb 100644 --- a/reflex/constants/base.py +++ b/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//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. diff --git a/tests/test_config.py b/tests/test_config.py index bc3531412..1ba2f548d 100644 --- a/tests/test_config.py +++ b/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)