Skip to content

Commit f9d3fb3

Browse files
Lendemormasenf
authored andcommitted
fix bun path handling and add a test (#4785)
* fix bun path handling and add a test * fix flags * fix tests * fix unit tests and mock object * fix units test again * revert some changes for now * remove unused test
1 parent 9cd5511 commit f9d3fb3

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

reflex/reflex.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ def _run(
145145
exec.output_system_info()
146146

147147
# If no --frontend-only and no --backend-only, then turn on frontend and backend both
148-
if not frontend and not backend:
149-
frontend = True
150-
backend = True
151-
148+
frontend, backend = prerequisites.check_running_mode(frontend, backend)
152149
if not frontend and backend:
153150
_skip_compile()
154151

@@ -306,10 +303,18 @@ def export(
306303
True, "--no-zip", help="Disable zip for backend and frontend exports."
307304
),
308305
frontend: bool = typer.Option(
309-
True, "--backend-only", help="Export only backend.", show_default=False
306+
False,
307+
"--frontend-only",
308+
help="Export only frontend.",
309+
show_default=False,
310+
envvar=environment.REFLEX_FRONTEND_ONLY.name,
310311
),
311312
backend: bool = typer.Option(
312-
True, "--frontend-only", help="Export only frontend.", show_default=False
313+
False,
314+
"--backend-only",
315+
help="Export only backend.",
316+
show_default=False,
317+
envvar=environment.REFLEX_BACKEND_ONLY.name,
313318
),
314319
zip_dest_dir: str = typer.Option(
315320
str(Path.cwd()),
@@ -332,7 +337,9 @@ def export(
332337
from reflex.utils import export as export_utils
333338
from reflex.utils import prerequisites
334339

335-
if prerequisites.needs_reinit(frontend=True):
340+
frontend, backend = prerequisites.check_running_mode(frontend, backend)
341+
342+
if prerequisites.needs_reinit(frontend=frontend or not backend):
336343
_init(name=config.app_name, loglevel=loglevel)
337344

338345
if frontend and not config.show_built_with_reflex:

reflex/utils/path_ops.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ def find_replace(directory: str | Path, find: str, replace: str):
247247
filepath.write_text(text, encoding="utf-8")
248248

249249

250+
def samefile(file1: Path, file2: Path) -> bool:
251+
"""Check if two files are the same.
252+
253+
Args:
254+
file1: The first file.
255+
file2: The second file.
256+
257+
Returns:
258+
Whether the files are the same. If either file does not exist, returns False.
259+
"""
260+
if file1.exists() and file2.exists():
261+
return file1.samefile(file2)
262+
263+
return False
264+
265+
250266
def update_directory_tree(src: Path, dest: Path):
251267
"""Recursively copies a directory tree from src to dest.
252268
Only copies files if the destination file is missing or modified earlier than the source file.

reflex/utils/prerequisites.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,21 @@ def install_frontend_packages(packages: set[str], config: Config):
12251225
)
12261226

12271227

1228+
def check_running_mode(frontend: bool, backend: bool) -> tuple[bool, bool]:
1229+
"""Check if the app is running in frontend or backend mode.
1230+
1231+
Args:
1232+
frontend: Whether to run the frontend of the app.
1233+
backend: Whether to run the backend of the app.
1234+
1235+
Returns:
1236+
The running modes.
1237+
"""
1238+
if not frontend and not backend:
1239+
return True, True
1240+
return frontend, backend
1241+
1242+
12281243
def needs_reinit(frontend: bool = True) -> bool:
12291244
"""Check if an app needs to be reinitialized.
12301245
@@ -1293,10 +1308,13 @@ def validate_bun():
12931308
"""
12941309
bun_path = path_ops.get_bun_path()
12951310

1296-
if bun_path and not bun_path.samefile(constants.Bun.DEFAULT_PATH):
1311+
if bun_path is None:
1312+
return
1313+
1314+
if not path_ops.samefile(bun_path, constants.Bun.DEFAULT_PATH):
12971315
console.info(f"Using custom Bun path: {bun_path}")
12981316
bun_version = get_bun_version()
1299-
if not bun_version:
1317+
if bun_version is None:
13001318
console.error(
13011319
"Failed to obtain bun version. Make sure the specified bun path in your config is correct."
13021320
)

tests/units/utils/test_utils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,33 @@ def test_typehint_issubclass(subclass, superclass, expected):
115115
assert types.typehint_issubclass(subclass, superclass) == expected
116116

117117

118-
def test_validate_invalid_bun_path(mocker):
118+
def test_validate_none_bun_path(mocker):
119+
"""Test that an error is thrown when a bun path is not specified.
120+
121+
Args:
122+
mocker: Pytest mocker object.
123+
"""
124+
mocker.patch("reflex.utils.path_ops.get_bun_path", return_value=None)
125+
# with pytest.raises(typer.Exit):
126+
prerequisites.validate_bun()
127+
128+
129+
def test_validate_invalid_bun_path(
130+
mocker,
131+
):
119132
"""Test that an error is thrown when a custom specified bun path is not valid
120133
or does not exist.
121134
122135
Args:
123136
mocker: Pytest mocker object.
124137
"""
125138
mock_path = mocker.Mock()
126-
mock_path.samefile.return_value = False
127139
mocker.patch("reflex.utils.path_ops.get_bun_path", return_value=mock_path)
140+
mocker.patch("reflex.utils.path_ops.samefile", return_value=False)
128141
mocker.patch("reflex.utils.prerequisites.get_bun_version", return_value=None)
129142

130143
with pytest.raises(typer.Exit):
131144
prerequisites.validate_bun()
132-
mock_path.samefile.assert_called_once()
133145

134146

135147
def test_validate_bun_path_incompatible_version(mocker):
@@ -141,6 +153,7 @@ def test_validate_bun_path_incompatible_version(mocker):
141153
mock_path = mocker.Mock()
142154
mock_path.samefile.return_value = False
143155
mocker.patch("reflex.utils.path_ops.get_bun_path", return_value=mock_path)
156+
mocker.patch("reflex.utils.path_ops.samefile", return_value=False)
144157
mocker.patch(
145158
"reflex.utils.prerequisites.get_bun_version",
146159
return_value=version.parse("0.6.5"),

0 commit comments

Comments
 (0)