mv.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import pytest
  3. import fsspec
  4. def test_move_raises_error_with_tmpdir(tmpdir):
  5. # Create a file in the temporary directory
  6. source = tmpdir.join("source_file.txt")
  7. source.write("content")
  8. # Define a destination that simulates a protected or invalid path
  9. destination = tmpdir.join("non_existent_directory/destination_file.txt")
  10. # Instantiate the filesystem (assuming the local file system interface)
  11. fs = fsspec.filesystem("file")
  12. # Use the actual file paths as string
  13. with pytest.raises(FileNotFoundError):
  14. fs.mv(str(source), str(destination))
  15. @pytest.mark.parametrize("recursive", (True, False))
  16. def test_move_raises_error_with_tmpdir_permission(recursive, tmpdir):
  17. # Create a file in the temporary directory
  18. source = tmpdir.join("source_file.txt")
  19. source.write("content")
  20. # Create a protected directory (non-writable)
  21. protected_dir = tmpdir.mkdir("protected_directory")
  22. protected_path = str(protected_dir)
  23. # Set the directory to read-only
  24. if os.name == "nt":
  25. os.system(f'icacls "{protected_path}" /deny Everyone:(W)')
  26. else:
  27. os.chmod(protected_path, 0o555) # Sets the directory to read-only
  28. # Define a destination inside the protected directory
  29. destination = protected_dir.join("destination_file.txt")
  30. # Instantiate the filesystem (assuming the local file system interface)
  31. fs = fsspec.filesystem("file")
  32. # Try to move the file to the read-only directory, expecting a permission error
  33. with pytest.raises(PermissionError):
  34. fs.mv(str(source), str(destination), recursive=recursive)
  35. # Assert the file was not created in the destination
  36. assert not os.path.exists(destination)
  37. # Cleanup: Restore permissions so the directory can be cleaned up
  38. if os.name == "nt":
  39. os.system(f'icacls "{protected_path}" /remove:d Everyone')
  40. else:
  41. os.chmod(protected_path, 0o755) # Restore write permission for cleanup