Bladeren bron

fix tests

Khaleel Al-Adhami 1 maand geleden
bovenliggende
commit
e9d08dd861

+ 11 - 6
reflex/app.py

@@ -602,10 +602,12 @@ class App(MiddlewareMixin, LifespanMixin):
         compile_future = concurrent.futures.ThreadPoolExecutor(max_workers=1).submit(
             self._compile
         )
-        compile_future.add_done_callback(
+
+        def callback(f: concurrent.futures.Future):
             # Force background compile errors to print eagerly
-            lambda f: f.result()
-        )
+            return f.result()
+
+        compile_future.add_done_callback(callback)
         # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted.
         if is_prod_mode():
             compile_future.result()
@@ -1329,10 +1331,12 @@ class App(MiddlewareMixin, LifespanMixin):
         ExecutorSafeFunctions.STATE = self._state
 
         with console.timing("Compile to Javascript"), executor as executor:
-            result_futures: list[concurrent.futures.Future[tuple[str, str]]] = []
+            result_futures: list[concurrent.futures.Future[tuple[str, str] | None]] = []
 
             def _submit_work(
-                fn: Callable[P, tuple[str, str]], *args: P.args, **kwargs: P.kwargs
+                fn: Callable[P, tuple[str, str] | None],
+                *args: P.args,
+                **kwargs: P.kwargs,
             ):
                 f = executor.submit(fn, *args, **kwargs)
                 f.add_done_callback(lambda _: progress.advance(task))
@@ -1356,8 +1360,9 @@ class App(MiddlewareMixin, LifespanMixin):
 
             # Wait for all compilation tasks to complete.
             compile_results.extend(
-                future.result()
+                result
                 for future in concurrent.futures.as_completed(result_futures)
+                if (result := future.result()) is not None
             )
 
         app_root = self._app_root(app_wrappers=app_wrappers)

+ 5 - 1
reflex/plugins/base.py

@@ -18,7 +18,11 @@ class AddTaskProtcol(Protocol):
     """Protocol for adding a task to the pre-compile context."""
 
     def __call__(
-        self, task: Callable[P, tuple[str, str]], /, *args: P.args, **kwargs: P.kwargs
+        self,
+        task: Callable[P, tuple[str, str] | None],
+        /,
+        *args: P.args,
+        **kwargs: P.kwargs,
     ) -> None:
         """Add a task to the pre-compile context.
 

+ 7 - 2
reflex/plugins/tailwind_v3.py

@@ -153,10 +153,11 @@ def add_tailwind_to_postcss_config():
 
     postcss_file = get_web_dir() / Dirs.POSTCSS_JS
     if not postcss_file.exists():
-        raise ValueError(
+        print(  # noqa: T201
             f"Could not find {Dirs.POSTCSS_JS}. "
             "Please make sure the file exists and is valid."
         )
+        return
 
     postcss_file_lines = postcss_file.read_text().splitlines()
 
@@ -169,10 +170,11 @@ def add_tailwind_to_postcss_config():
         None,
     )
     if not line_with_postcss_plugins:
-        raise ValueError(
+        print(  # noqa: T201
             f"Could not find line with 'plugins' in {Dirs.POSTCSS_JS}. "
             "Please make sure the file exists and is valid."
         )
+        return
 
     postcss_file_lines.insert(line_with_postcss_plugins + 1, "tailwindcss: {},")
 
@@ -185,6 +187,9 @@ class TailwindV3Plugin(Plugin):
     def get_frontend_development_dependancies(self, **context) -> list[str]:
         """Get the packages required by the plugin.
 
+        Args:
+            **context: The context for the plugin.
+
         Returns:
             A list of packages required by the plugin.
         """

+ 1 - 1
reflex/utils/prerequisites.py

@@ -1334,7 +1334,7 @@ def install_frontend_packages(packages: set[str], config: Config):
         )
 
     # Install custom packages defined in frontend_packages
-    if len(packages) > 0:
+    if packages:
         run_package_manager(
             [primary_package_manager, "add", "--legacy-peer-deps", *packages],
             show_status_message="Installing frontend packages from config and components",

+ 5 - 1
tests/units/compiler/test_compiler.py

@@ -155,6 +155,7 @@ def test_compile_stylesheets(tmp_path: Path, mocker):
             / (PageNames.STYLESHEET_ROOT + ".css")
         ),
         "@import url('./tailwind.css'); \n"
+        "@import url('@radix-ui/themes/styles.css'); \n"
         "@import url('https://fonts.googleapis.com/css?family=Sofia&effect=neon|outline|emboss|shadow-multiple'); \n"
         "@import url('https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css'); \n"
         "@import url('https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css'); \n"
@@ -215,6 +216,7 @@ def test_compile_stylesheets_scss_sass(tmp_path: Path, mocker):
             / (PageNames.STYLESHEET_ROOT + ".css")
         ),
         "@import url('./tailwind.css'); \n"
+        "@import url('@radix-ui/themes/styles.css'); \n"
         "@import url('./style.css'); \n"
         f"@import url('./{Path('preprocess') / Path('styles_a.css')!s}'); \n"
         f"@import url('./{Path('preprocess') / Path('styles_b.css')!s}'); \n",
@@ -233,6 +235,7 @@ def test_compile_stylesheets_scss_sass(tmp_path: Path, mocker):
             / (PageNames.STYLESHEET_ROOT + ".css")
         ),
         "@import url('./tailwind.css'); \n"
+        "@import url('@radix-ui/themes/styles.css'); \n"
         "@import url('./style.css'); \n"
         f"@import url('./{Path('preprocess') / Path('styles_a.css')!s}'); \n"
         f"@import url('./{Path('preprocess') / Path('styles_b.css')!s}'); \n",
@@ -266,6 +269,7 @@ def test_compile_stylesheets_exclude_tailwind(tmp_path, mocker):
     mock = mocker.Mock()
 
     mocker.patch.object(mock, "tailwind", None)
+    mocker.patch.object(mock, "plugins", [])
     mocker.patch("reflex.compiler.compiler.get_config", return_value=mock)
 
     (assets_dir / "style.css").touch()
@@ -277,7 +281,7 @@ def test_compile_stylesheets_exclude_tailwind(tmp_path, mocker):
 
     assert compiler.compile_root_stylesheet(stylesheets) == (
         str(Path(".web") / "styles" / (PageNames.STYLESHEET_ROOT + ".css")),
-        "@import url('./style.css'); \n",
+        "@import url('@radix-ui/themes/styles.css'); \n@import url('./style.css'); \n",
     )
 
 

+ 13 - 1
tests/units/test_app.py

@@ -1290,6 +1290,17 @@ def compilable_app(tmp_path) -> Generator[tuple[App, Path], None, None]:
     web_dir = app_path / ".web"
     web_dir.mkdir(parents=True)
     (web_dir / constants.PackageJson.PATH).touch()
+    (web_dir / constants.Dirs.POSTCSS_JS).touch()
+    (web_dir / constants.Dirs.POSTCSS_JS).write_text(
+        """
+module.exports = {
+  plugins: {
+    "postcss-import": {},
+    autoprefixer: {},
+  },
+};
+""",
+    )
     app = App(theme=None)
     app._get_frontend_packages = unittest.mock.Mock()
     with chdir(app_path):
@@ -1312,8 +1323,8 @@ def test_app_wrap_compile_theme(
     """
     conf = rx.Config(app_name="testing", react_strict_mode=react_strict_mode)
     mocker.patch("reflex.config._get_config", return_value=conf)
-
     app, web_dir = compilable_app
+    mocker.patch("reflex.utils.prerequisites.get_web_dir", return_value=web_dir)
     app.theme = rx.theme(accent_color="plum")
     app._compile()
     app_js_contents = (web_dir / "pages" / "_app.js").read_text()
@@ -1461,6 +1472,7 @@ def test_raise_on_state():
 def test_call_app():
     """Test that the app can be called."""
     app = App()
+    app._get_frontend_packages = unittest.mock.Mock()
     api = app()
     assert isinstance(api, FastAPI)