| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
|
| 7 |
import os |
| 8 |
import pytest |
| 9 |
import tempfile |
| 10 |
from pathlib import Path |
| 11 |
from gi.repository import GLib |
| 12 |
|
| 13 |
|
| 14 |
class TestTrash: |
| 15 |
def test_version(self, portals, dbus_con): |
| 16 |
xdp.check_version(dbus_con, "Trash", 1) |
| 17 |
|
| 18 |
def test_trash_file_fails(self, portals, dbus_con): |
| 19 |
trash_intf = xdp.get_portal_iface(dbus_con, "Trash") |
| 20 |
try: |
| 21 |
with open("/proc/cmdline") as fd: |
| 22 |
result = trash_intf.TrashFile(fd.fileno()) |
| 23 |
except PermissionError as e: |
| 24 |
pytest.skip(f"Couldn't open file /proc/cmdline: {e}") |
| 25 |
|
| 26 |
assert result == 0 |
| 27 |
|
| 28 |
def test_trash_file(self, portals, dbus_con): |
| 29 |
trash_intf = xdp.get_portal_iface(dbus_con, "Trash") |
| 30 |
|
| 31 |
fd, name = tempfile.mkstemp(prefix="trash_portal_mock_", dir=Path.home()) |
| 32 |
result = trash_intf.TrashFile(fd) |
| 33 |
|
| 34 |
assert result == 1 |
| 35 |
assert not Path(name).exists() |
| 36 |
|
| 37 |
info_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/info" |
| 38 |
assert info_dir.exists() |
| 39 |
|
| 40 |
files_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/files" |
| 41 |
assert files_dir.exists() |
| 42 |
|
| 43 |
trashed_files = info_dir.iterdir() |
| 44 |
trashed_file = next(trashed_files) |
| 45 |
|
| 46 |
keyfile = GLib.KeyFile.new() |
| 47 |
content = trashed_file.read_text() |
| 48 |
assert keyfile.load_from_data( |
| 49 |
content, |
| 50 |
len(content), |
| 51 |
GLib.KeyFileFlags.NONE, |
| 52 |
) |
| 53 |
assert keyfile.get_string("Trash Info", "Path") == name |
| 54 |
assert (files_dir / trashed_file.stem).exists() |
| 55 |
|
| 56 |
with pytest.raises(StopIteration): |
| 57 |
next(trashed_files) |
| 58 |
|
| 59 |
@pytest.mark.skip(reason="Portal requires write perm, so dirs are not supported") |
| 60 |
def test_trash_folder(self, portals, dbus_con): |
| 61 |
trash_intf = xdp.get_portal_iface(dbus_con, "Trash") |
| 62 |
|
| 63 |
folder = Path(os.environ["HOME"]) / "folder-to-trash" |
| 64 |
file_in_folder = folder / "foo" / "bar" / "file" |
| 65 |
file_in_folder.parent.mkdir(parents=True) |
| 66 |
file_in_folder.write_text("foobar") |
| 67 |
|
| 68 |
fd = os.open(folder, os.O_RDONLY | os.O_CLOEXEC) |
| 69 |
try: |
| 70 |
result = trash_intf.TrashFile(fd) |
| 71 |
finally: |
| 72 |
os.close(fd) |
| 73 |
|
| 74 |
assert result == 1 |
| 75 |
assert not Path(folder).exists() |
| 76 |
|
| 77 |
info_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/info" |
| 78 |
assert info_dir.exists() |
| 79 |
|
| 80 |
files_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/files" |
| 81 |
assert files_dir.exists() |
| 82 |
|
| 83 |
trashed_files = info_dir.iterdir() |
| 84 |
trashed_file = next(trashed_files) |
| 85 |
|
| 86 |
keyfile = GLib.KeyFile.new() |
| 87 |
content = trashed_file.read_text() |
| 88 |
assert keyfile.load_from_data( |
| 89 |
content, |
| 90 |
len(content), |
| 91 |
GLib.KeyFileFlags.NONE, |
| 92 |
) |
| 93 |
assert keyfile.get_string("Trash Info", "Path") == folder.as_posix() |
| 94 |
|
| 95 |
trashed_folder = files_dir / trashed_file.stem |
| 96 |
assert trashed_folder.exists() |
| 97 |
trashed_file = trashed_folder / "foo" / "bar" / "file" |
| 98 |
assert trashed_file.exists() |
| 99 |
|
| 100 |
with pytest.raises(StopIteration): |
| 101 |
next(trashed_files) |