test_assets.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import shutil
  2. from pathlib import Path
  3. import pytest
  4. import reflex as rx
  5. def test_asset():
  6. # Test the asset function.
  7. # The asset function copies a file to the app's external assets directory.
  8. asset = rx._x.asset("custom_script.js", "subfolder")
  9. assert asset == "/external/test_assets/subfolder/custom_script.js"
  10. result_file = Path(
  11. Path.cwd(), "assets/external/test_assets/subfolder/custom_script.js"
  12. )
  13. assert result_file.exists()
  14. # Running a second time should not raise an error.
  15. asset = rx._x.asset("custom_script.js", "subfolder")
  16. # Test the asset function without a subfolder.
  17. asset = rx._x.asset("custom_script.js")
  18. assert asset == "/external/test_assets/custom_script.js"
  19. result_file = Path(Path.cwd(), "assets/external/test_assets/custom_script.js")
  20. assert result_file.exists()
  21. # clean up
  22. shutil.rmtree(Path.cwd() / "assets/external")
  23. with pytest.raises(FileNotFoundError):
  24. asset = rx._x.asset("non_existent_file.js")
  25. # Nothing is done to assets when file does not exist.
  26. assert not Path(Path.cwd() / "assets/external").exists()