test_fileops.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from pathlib import Path
  2. import pytest
  3. from openhands.runtime.utils import files
  4. SANDBOX_PATH_PREFIX = '/workspace'
  5. WORKSPACE_BASE = 'workspace'
  6. def test_resolve_path():
  7. assert (
  8. files.resolve_path('test.txt', '/workspace')
  9. == Path(WORKSPACE_BASE) / 'test.txt'
  10. )
  11. assert (
  12. files.resolve_path('subdir/test.txt', '/workspace')
  13. == Path(WORKSPACE_BASE) / 'subdir' / 'test.txt'
  14. )
  15. assert (
  16. files.resolve_path(Path(SANDBOX_PATH_PREFIX) / 'test.txt', '/workspace')
  17. == Path(WORKSPACE_BASE) / 'test.txt'
  18. )
  19. assert (
  20. files.resolve_path(
  21. Path(SANDBOX_PATH_PREFIX) / 'subdir' / 'test.txt', '/workspace'
  22. )
  23. == Path(WORKSPACE_BASE) / 'subdir' / 'test.txt'
  24. )
  25. assert (
  26. files.resolve_path(
  27. Path(SANDBOX_PATH_PREFIX) / 'subdir' / '..' / 'test.txt', '/workspace'
  28. )
  29. == Path(WORKSPACE_BASE) / 'test.txt'
  30. )
  31. with pytest.raises(PermissionError):
  32. files.resolve_path(Path(SANDBOX_PATH_PREFIX) / '..' / 'test.txt', '/workspace')
  33. with pytest.raises(PermissionError):
  34. files.resolve_path(Path('..') / 'test.txt', '/workspace')
  35. with pytest.raises(PermissionError):
  36. files.resolve_path(Path('/') / 'test.txt', '/workspace')
  37. assert (
  38. files.resolve_path('test.txt', '/workspace/test')
  39. == Path(WORKSPACE_BASE) / 'test' / 'test.txt'
  40. )