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