Debian Codegraph @

tests/TestDesktopMenuOptions.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (c) 2008-2026 Andrew Ziem.
3 #
4 # This work is licensed under the terms of the GNU GPL, version 3 or
5 # later. See the COPYING file in the top-level directory.
6
7 """
8 Test case for module DesktopMenuOptions
9 """
10
11 import os
12 import stat
13 from unittest import mock
14
15 from tests import common
16 from bleachbit.DesktopMenuOptions import install_kde_service_menu_file
17 from bleachbit.Options import options
18
19
20 class DesktopMenuOptionsTestCase(common.BleachbitTestCase):
21 """Test case for install_kde_service_menu_file()"""
22
23 def _service_file_path(self, home):
24 return os.path.join(
25 home, '.local', 'share', 'kio', 'servicemenus',
26 'shred_with_bleachbit.desktop')
27
28 @common.skipIfWindows
29 def test_install_kde_service_menu_file_refuses_symlink(self):
30 """A planted symlink at the service file path must be refused, not followed"""
31 home = self.mkdtemp(prefix='bb-kde-home')
32 service_file = self._service_file_path(home)
33 os.makedirs(os.path.dirname(service_file))
34 with mock.patch.dict(os.environ, {'HOME': home}, clear=False), \
35 mock.patch.object(options, 'get', return_value=True), \
36 mock.patch('bleachbit.FileUtilities.os.path.islink',
37 side_effect=lambda p: p == service_file):
38 os.environ.pop('XDG_DATA_HOME', None)
39 install_kde_service_menu_file()
40 self.assertNotExists(service_file)
41
42 @common.skipIfWindows
43 def test_install_kde_service_menu_file_creates_file(self):
44 """The service file is created with mode 0o755 when the option is enabled"""
45 home = self.mkdtemp(prefix='bb-kde-home')
46 service_file = self._service_file_path(home)
47 with mock.patch.dict(os.environ, {'HOME': home}, clear=False), \
48 mock.patch.object(options, 'get', return_value=True):
49 os.environ.pop('XDG_DATA_HOME', None)
50 install_kde_service_menu_file()
51 self.assertExists(service_file)
52 with open(service_file, encoding='utf-8') as f:
53 self.assertIn('Shred With Bleachbit', f.read())
54 mode = stat.S_IMODE(os.stat(service_file).st_mode)
55 self.assertEqual(mode, 0o755)
56
57 @common.skipIfWindows
58 def test_install_kde_service_menu_file_removes_when_disabled(self):
59 """The service file is removed when the option is disabled"""
60 home = self.mkdtemp(prefix='bb-kde-home')
61 service_file = self._service_file_path(home)
62 os.makedirs(os.path.dirname(service_file))
63 with open(service_file, 'w', encoding='utf-8') as f:
64 f.write('placeholder')
65 with mock.patch.dict(os.environ, {'HOME': home}, clear=False), \
66 mock.patch.object(options, 'get', return_value=False):
67 os.environ.pop('XDG_DATA_HOME', None)
68 install_kde_service_menu_file()
69 self.assertNotExists(service_file)