Debian Codegraph @

tests/test_trash.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests as xdp
6
7 import pytest
8 import os
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 with open("/proc/cmdline") as fd:
21 result = trash_intf.TrashFile(fd.fileno())
22
23 assert result == 0
24
25 def test_trash_file(self, portals, dbus_con):
26 trash_intf = xdp.get_portal_iface(dbus_con, "Trash")
27
28 fd, name = tempfile.mkstemp(prefix="trash_portal_mock_", dir=Path.home())
29 result = trash_intf.TrashFile(fd)
30
31 assert result == 1
32 assert not Path(name).exists()
33
34 info_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/info"
35 assert info_dir.exists()
36
37 files_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/files"
38 assert files_dir.exists()
39
40 trashed_files = info_dir.iterdir()
41 trashed_file = next(trashed_files)
42
43 keyfile = GLib.KeyFile.new()
44 content = trashed_file.read_text()
45 assert keyfile.load_from_data(
46 content,
47 len(content),
48 GLib.KeyFileFlags.NONE,
49 )
50 assert keyfile.get_string("Trash Info", "Path") == name
51 assert (files_dir / trashed_file.stem).exists()
52
53 with pytest.raises(StopIteration):
54 next(trashed_files)
55
56 def test_trash_folder(self, portals, dbus_con):
57 trash_intf = xdp.get_portal_iface(dbus_con, "Trash")
58
59 folder = Path(os.environ["HOME"]) / "folder-to-trash"
60 file_in_folder = folder / "foo" / "bar" / "file"
61 file_in_folder.parent.mkdir(parents=True)
62 file_in_folder.write_text("foobar")
63
64 fd = os.open(folder, os.O_RDONLY | os.O_CLOEXEC)
65 try:
66 result = trash_intf.TrashFile(fd)
67 finally:
68 os.close(fd)
69
70 assert result == 1
71 assert not Path(folder).exists()
72
73 info_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/info"
74 assert info_dir.exists()
75
76 files_dir = Path(os.environ["XDG_DATA_HOME"]) / "Trash/files"
77 assert files_dir.exists()
78
79 trashed_files = info_dir.iterdir()
80 trashed_file = next(trashed_files)
81
82 keyfile = GLib.KeyFile.new()
83 content = trashed_file.read_text()
84 assert keyfile.load_from_data(
85 content,
86 len(content),
87 GLib.KeyFileFlags.NONE,
88 )
89 assert keyfile.get_string("Trash Info", "Path") == folder.as_posix()
90
91 trashed_folder = files_dir / trashed_file.stem
92 assert trashed_folder.exists()
93 trashed_file = trashed_folder / "foo" / "bar" / "file"
94 assert trashed_file.exists()
95
96 with pytest.raises(StopIteration):
97 next(trashed_files)