66
77import reflex as rx
88import reflex .constants as constants
9+ from reflex .assets import remove_stale_external_asset_symlinks
910
1011
11- def test_shared_asset () -> None :
12+ @pytest .fixture
13+ def mock_asset_path (tmp_path : Path , monkeypatch : pytest .MonkeyPatch ) -> Path :
14+ """Create a mock asset file and patch the current working directory.
15+
16+ Args:
17+ tmp_path: A temporary directory provided by pytest.
18+ monkeypatch: A pytest fixture for patching.
19+
20+ Returns:
21+ The path to a tmp cwd that will be used for assets.
22+ """
23+ # Create a temporary directory to act as the current working directory.
24+ mock_cwd = tmp_path / "mock_asset_path"
25+ mock_cwd .mkdir ()
26+ monkeypatch .chdir (mock_cwd )
27+
28+ return mock_cwd
29+
30+
31+ def test_shared_asset (mock_asset_path : Path ) -> None :
1232 """Test shared assets."""
1333 # The asset function copies a file to the app's external assets directory.
1434 asset = rx .asset (path = "custom_script.js" , shared = True , subfolder = "subfolder" )
1535 assert asset == "/external/test_assets/subfolder/custom_script.js"
1636 result_file = Path (
17- Path .cwd (), "assets/external/test_assets/subfolder/custom_script.js"
37+ mock_asset_path ,
38+ "assets" ,
39+ "external" ,
40+ "test_assets" ,
41+ "subfolder" ,
42+ "custom_script.js" ,
1843 )
1944 assert result_file .exists ()
2045
@@ -24,17 +49,19 @@ def test_shared_asset() -> None:
2449 # Test the asset function without a subfolder.
2550 asset = rx .asset (path = "custom_script.js" , shared = True )
2651 assert asset == "/external/test_assets/custom_script.js"
27- result_file = Path (Path .cwd (), "assets/external/test_assets/custom_script.js" )
52+ result_file = Path (
53+ mock_asset_path , "assets" , "external" , "test_assets" , "custom_script.js"
54+ )
2855 assert result_file .exists ()
2956
3057 # clean up
31- shutil .rmtree (Path . cwd ( ) / "assets/ external" )
58+ shutil .rmtree (Path ( mock_asset_path ) / "assets" / " external" )
3259
3360 with pytest .raises (FileNotFoundError ):
3461 asset = rx .asset ("non_existent_file.js" )
3562
3663 # Nothing is done to assets when file does not exist.
37- assert not Path (Path . cwd () / "assets/ external" ).exists ()
64+ assert not Path (mock_asset_path / "assets" / " external" ).exists ()
3865
3966
4067@pytest .mark .parametrize (
@@ -56,13 +83,13 @@ def test_invalid_assets(path: str, shared: bool) -> None:
5683
5784
5885@pytest .fixture
59- def custom_script_in_asset_dir () -> Generator [Path , None , None ]:
86+ def custom_script_in_asset_dir (mock_asset_path : Path ) -> Generator [Path , None , None ]:
6087 """Create a custom_script.js file in the app's assets directory.
6188
6289 Yields:
6390 The path to the custom_script.js file.
6491 """
65- asset_dir = Path . cwd () / constants .Dirs .APP_ASSETS
92+ asset_dir = mock_asset_path / constants .Dirs .APP_ASSETS
6693 asset_dir .mkdir (exist_ok = True )
6794 path = asset_dir / "custom_script.js"
6895 path .touch ()
@@ -81,12 +108,10 @@ def test_local_asset(custom_script_in_asset_dir: Path) -> None:
81108 assert asset == "/custom_script.js"
82109
83110
84- def test_remove_stale_external_asset_symlinks () -> None :
111+ def test_remove_stale_external_asset_symlinks (mock_asset_path : Path ) -> None :
85112 """Test that stale symlinks and empty dirs in assets/external/ are cleaned up."""
86- from reflex .assets import remove_stale_external_asset_symlinks
87-
88113 external_dir = (
89- Path . cwd () / constants .Dirs .APP_ASSETS / constants .Dirs .EXTERNAL_APP_ASSETS
114+ mock_asset_path / constants .Dirs .APP_ASSETS / constants .Dirs .EXTERNAL_APP_ASSETS
90115 )
91116
92117 # Set up: create a subdirectory with a broken symlink.
@@ -106,30 +131,23 @@ def test_remove_stale_external_asset_symlinks() -> None:
106131 assert valid_symlink .is_symlink ()
107132 assert valid_symlink .resolve ().exists ()
108133
109- try :
110- remove_stale_external_asset_symlinks ()
134+ remove_stale_external_asset_symlinks ()
111135
112- # Broken symlink and its empty parent dirs should be removed.
113- assert not stale_symlink .exists ()
114- assert not stale_symlink .is_symlink ()
115- assert not stale_dir .exists ()
116- assert not (external_dir / "old_module" ).exists ()
136+ # Broken symlink and its empty parent dirs should be removed.
137+ assert not stale_symlink .exists ()
138+ assert not stale_symlink .is_symlink ()
139+ assert not stale_dir .exists ()
140+ assert not (external_dir / "old_module" ).exists ()
117141
118- # Valid symlink should be preserved.
119- assert valid_symlink .is_symlink ()
120- assert valid_symlink .resolve ().exists ()
121- finally :
122- # Clean up.
123- shutil .rmtree (external_dir )
142+ # Valid symlink should be preserved.
143+ assert valid_symlink .is_symlink ()
144+ assert valid_symlink .resolve ().exists ()
124145
125146
126- def test_remove_stale_symlinks_no_external_dir (tmp_path : Path , monkeypatch ) -> None :
147+ def test_remove_stale_symlinks_no_external_dir (mock_asset_path : Path ) -> None :
127148 """Test that cleanup is a no-op when assets/external/ doesn't exist."""
128- from reflex .assets import remove_stale_external_asset_symlinks
129-
130- monkeypatch .chdir (tmp_path )
131149 external_dir = (
132- tmp_path / constants .Dirs .APP_ASSETS / constants .Dirs .EXTERNAL_APP_ASSETS
150+ mock_asset_path / constants .Dirs .APP_ASSETS / constants .Dirs .EXTERNAL_APP_ASSETS
133151 )
134152 assert not external_dir .exists ()
135153 # Should not raise.
0 commit comments