Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
mv.py58 linesDownload Raw Back to abstract
1import os2 3import pytest4 5import fsspec6 7 8def test_move_raises_error_with_tmpdir(tmpdir):9    # Create a file in the temporary directory10    source = tmpdir.join("source_file.txt")11    source.write("content")12 13    # Define a destination that simulates a protected or invalid path14    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 string20    with pytest.raises(FileNotFoundError):21        fs.mv(str(source), str(destination))22 23 24@pytest.mark.parametrize("recursive", (True, False))25def test_move_raises_error_with_tmpdir_permission(recursive, tmpdir):26    # Create a file in the temporary directory27    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-only35    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-only39 40    # Define a destination inside the protected directory41    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 error47    with pytest.raises(PermissionError):48        fs.mv(str(source), str(destination), recursive=recursive)49 50    # Assert the file was not created in the destination51    assert not os.path.exists(destination)52 53    # Cleanup: Restore permissions so the directory can be cleaned up54    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 cleanup58 
codekingpro/portable-devtools · Team Ai