Debian Codegraph @

tests/TestCommand.py

raw
1 # vim: ts=4:sw=4:expandtab
2
3 # BleachBit
4 # Copyright (C) 2008-2025 Andrew Ziem
5 # https://www.bleachbit.org
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 """
22 Test case for Command
23 """
24
25 import os
26 import sqlite3
27 from unittest import mock
28
29 from tests import common
30 from bleachbit import FileUtilities
31 from bleachbit.Command import Delete, Function, Shred, Truncate
32
33
34 class CommandTestCase(common.BleachbitTestCase):
35 """Test case for Command"""
36
37 def test_Delete(self, cls=Delete):
38 """Unit test for Delete"""
39 path = self.write_file('test_Delete', b'foo')
40 cmd = cls(path)
41 self.assertExists(path)
42
43 # preview
44 ret = next(cmd.execute(really_delete=False))
45 self.assertGreater(ret['size'], 0)
46 self.assertEqual(ret['path'], path)
47 self.assertExists(path)
48
49 # delete
50 ret = next(cmd.execute(really_delete=True))
51 self.assertGreater(ret['size'], 0)
52 self.assertEqual(ret['path'], path)
53 self.assertNotExists(path)
54
55 def test_delete_permission(self):
56 """Test delete with permission denied for getsize()"""
57 path = self.write_file('test_delete_permission', b'foo')
58 cmd = Delete(path)
59 self.assertExists(path)
60
61 with mock.patch('bleachbit.FileUtilities.getsize') as mock_getsize:
62 mock_getsize.side_effect = PermissionError('Permission denied')
63
64 # preview
65 ret = next(cmd.execute(really_delete=False))
66 self.assertIsNone(ret['size'])
67 self.assertEqual(ret['path'], path)
68 self.assertExists(path)
69
70 def test_Function(self):
71 """Unit test for Function"""
72 path = self.write_file('test_Function', b'foo')
73 cmd = Function(path, FileUtilities.delete, 'bar')
74 self.assertExists(path)
75 self.assertGreater(os.path.getsize(path), 0)
76
77 # preview
78 next(cmd.execute(False))
79 self.assertExists(path)
80 self.assertGreater(os.path.getsize(path), 0)
81
82 # delete
83 ret = next(cmd.execute(True))
84 self.assertGreater(ret['size'], 0)
85 self.assertEqual(ret['path'], path)
86 self.assertNotExists(path)
87
88 def test_Function_no_collation(self):
89 """Unit test for Function with no collation
90
91 See https://github.com/bleachbit/bleachbit/issues/1866
92 """
93 path = self.write_file('test_Function_no_collation', b'')
94 cmd = Function(path,
95 lambda p: FileUtilities.execute_sqlite3(
96 p, 'CREATE TABLE test (name TEXT COLLATE foo);'),
97 'test_no_collation')
98
99 with mock.patch('bleachbit.Command.logger.debug') as mock_debug:
100 with self.assertRaises(StopIteration):
101 next(cmd.execute(True))
102 mock_debug.assert_called_with(mock.ANY)
103
104 def test_Function_sqlite_error_propagates(self):
105 """Unit test for Function with sqlite error that is not collation
106
107 Non-collation sqlite errors must propagate to the Worker instead of
108 being silently swallowed by Command.execute().
109 """
110 path = self.write_file('test_Function_sqlite_error', b'')
111 cmd = Function(path,
112 lambda p: FileUtilities.execute_sqlite3(
113 p, 'SELECT * FROM nonexistent_table;'),
114 'test_sqlite_error')
115
116 with self.assertRaises(sqlite3.OperationalError):
117 next(cmd.execute(True))
118
119 def test_Shred(self):
120 """Unit test for Shred"""
121 self.test_Delete(Shred)
122
123 def test_Truncate(self):
124 """Unit test for Truncate"""
125 path = self.write_file('test_Truncate', b'foo')
126 cmd = Truncate(path)
127
128 # preview leaves the file intact
129 ret = next(cmd.execute(really_delete=False))
130 self.assertEqual(ret['path'], path)
131 self.assertGreater(os.path.getsize(path), 0)
132
133 # truncate empties the file but keeps it
134 ret = next(cmd.execute(really_delete=True))
135 self.assertEqual(ret['path'], path)
136 self.assertExists(path)
137 self.assertEqual(os.path.getsize(path), 0)