test_VFSImporter.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from panda3d.core import VirtualFileSystem, VirtualFileMountRamdisk, Filename
  2. import sys
  3. def test_VFSImporter():
  4. from direct.showbase import VFSImporter
  5. VFSImporter.register()
  6. vfs = VirtualFileSystem.get_global_ptr()
  7. mount = VirtualFileMountRamdisk()
  8. success = vfs.mount(mount, "/ram", 0)
  9. assert success
  10. try:
  11. sys.path.insert(0, "/ram")
  12. vfs.write_file("/ram/testmod.py", b"var = 1\n", False)
  13. vfs.make_directory("/ram/testpkg")
  14. vfs.write_file("/ram/testpkg/__init__.py", b"var = 2\n", False)
  15. vfs.write_file("/ram/testpkg/test.py", b"var = 3\n", False)
  16. vfs.make_directory("/ram/testnspkg")
  17. vfs.write_file("/ram/testnspkg/test.py", b"var = 4\n", False)
  18. import testmod
  19. assert testmod.var == 1
  20. assert testmod.__spec__.name == 'testmod'
  21. filename = Filename('/ram/testmod.py').to_os_specific()
  22. assert testmod.__spec__.origin == filename
  23. assert testmod.__file__ == filename
  24. import testpkg
  25. assert testpkg.var == 2
  26. assert testpkg.__package__ == 'testpkg'
  27. assert testpkg.__path__ == [Filename('/ram/testpkg').to_os_specific()]
  28. assert testpkg.__spec__.name == 'testpkg'
  29. filename = Filename('/ram/testpkg/__init__.py').to_os_specific()
  30. assert testpkg.__spec__.origin == filename
  31. assert testpkg.__file__ == filename
  32. from testpkg import test
  33. assert test.var == 3
  34. assert test.__spec__.name == 'testpkg.test'
  35. filename = Filename('/ram/testpkg/test.py').to_os_specific()
  36. assert test.__spec__.origin == filename
  37. assert test.__file__ == filename
  38. from testnspkg import test
  39. assert test.var == 4
  40. assert test.__spec__.name == 'testnspkg.test'
  41. filename = Filename('/ram/testnspkg/test.py').to_os_specific()
  42. assert test.__spec__.origin == filename
  43. assert test.__file__ == filename
  44. finally:
  45. vfs.unmount(mount)
  46. try:
  47. del sys.path[sys.path.index("/ram")]
  48. except ValueError:
  49. pass