| 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 |
"""Unit tests for bleachbit.PathUtils.""" |
| 8 |
|
| 9 |
import os |
| 10 |
import unittest |
| 11 |
from unittest import mock |
| 12 |
|
| 13 |
from bleachbit import FS_CASE_SENSITIVE, IS_MAC, IS_WINDOWS |
| 14 |
from bleachbit.FileUtilities import whitelisted_windows |
| 15 |
from bleachbit.PathUtils import ( |
| 16 |
expand_path, |
| 17 |
expand_path_entries, |
| 18 |
normalize_path, |
| 19 |
path_equal, |
| 20 |
path_has_relative_suffix, |
| 21 |
path_startswith, |
| 22 |
) |
| 23 |
from bleachbit.ProtectedPath import check_protected_path |
| 24 |
from tests import common |
| 25 |
|
| 26 |
|
| 27 |
class PathUtilsTestCase(unittest.TestCase): |
| 28 |
"""Tests for the PathUtils helper functions.""" |
| 29 |
|
| 30 |
def test_path_equal_case_sensitivity(self): |
| 31 |
"""Verify path_equal honors the case_sensitive flag.""" |
| 32 |
self.assertTrue(path_equal( |
| 33 |
'/Home/Foo', '/Home/Foo', case_sensitive=True)) |
| 34 |
self.assertFalse(path_equal( |
| 35 |
'/Home/Foo', '/home/foo', case_sensitive=True)) |
| 36 |
self.assertTrue(path_equal( |
| 37 |
'/Home/Foo', '/home/foo', case_sensitive=False)) |
| 38 |
|
| 39 |
def test_path_equal_does_not_normalize(self): |
| 40 |
"""Verify path_equal compares paths without normalization.""" |
| 41 |
self.assertFalse(path_equal('/home/foo', '/home/foo/')) |
| 42 |
self.assertFalse(path_equal('/home/foo', '/home/./foo')) |
| 43 |
|
| 44 |
def test_path_startswith_component_boundaries(self): |
| 45 |
"""Verify path_startswith respects component boundaries.""" |
| 46 |
self.assertTrue(path_startswith('/home/folder/file', '/home/folder')) |
| 47 |
self.assertFalse(path_startswith('/home/folder', '/home/folder')) |
| 48 |
self.assertFalse(path_startswith('/home/folder2', '/home/folder')) |
| 49 |
self.assertFalse(path_startswith('/home/folder/file', '/home/folder/')) |
| 50 |
self.assertTrue(path_startswith('/home/folder/', '/home/folder')) |
| 51 |
self.assertFalse(path_startswith('/home/folder', '/')) |
| 52 |
self.assertFalse(path_startswith( |
| 53 |
r'D:\folder', 'D:\\', case_sensitive=False)) |
| 54 |
|
| 55 |
def test_normalize_path(self): |
| 56 |
"""Verify normalize_path collapses segments and applies case rules.""" |
| 57 |
self.assertEqual(normalize_path('/home/foo/../bar', case_sensitive=True), |
| 58 |
os.path.normpath('/home/bar')) |
| 59 |
self.assertEqual(normalize_path('/Home/Foo', case_sensitive=False), |
| 60 |
os.path.normpath('/home/foo')) |
| 61 |
|
| 62 |
def test_expand_path(self): |
| 63 |
"""Verify expand_path resolves environment placeholders and normalizes.""" |
| 64 |
variable = 'TEST_PATH_UTILS_PATH' |
| 65 |
placeholder = f'%{variable}%' if IS_WINDOWS else f'${variable}' |
| 66 |
value = os.path.join(os.sep, 'pathutils', 'expanded') |
| 67 |
with common.set_temporary_env(variable, value): |
| 68 |
self.assertEqual( |
| 69 |
expand_path(os.path.join(placeholder, '.', 'child')), |
| 70 |
os.path.join(value, 'child')) |
| 71 |
self.assertEqual(expand_path(''), '') |
| 72 |
|
| 73 |
def test_expand_path_entries(self): |
| 74 |
"""Verify expand_path_entries splits list-style environment values.""" |
| 75 |
variable = 'TEST_PATH_UTILS_ENTRIES' |
| 76 |
placeholder = f'%{variable}%' if IS_WINDOWS else f'${variable}' |
| 77 |
first = os.path.join(os.sep, 'pathutils-first') |
| 78 |
second = os.path.join(os.sep, 'pathutils-second') |
| 79 |
with common.set_temporary_env(variable, os.pathsep.join((first, '', second))): |
| 80 |
self.assertEqual(expand_path_entries(placeholder), (first, second)) |
| 81 |
self.assertEqual( |
| 82 |
expand_path_entries(os.path.join(placeholder, 'child')), |
| 83 |
(os.path.normpath( |
| 84 |
os.pathsep.join((first, '', second)) + os.sep + 'child'),)) |
| 85 |
|
| 86 |
def test_path_has_relative_suffix(self): |
| 87 |
"""Verify path_has_relative_suffix matches suffix components only.""" |
| 88 |
self.assertTrue(path_has_relative_suffix('/home/user/.git', '.git')) |
| 89 |
self.assertTrue(path_has_relative_suffix('.git', '.git')) |
| 90 |
self.assertFalse(path_has_relative_suffix( |
| 91 |
'/home/user/not-git', '.git')) |
| 92 |
self.assertFalse(path_has_relative_suffix( |
| 93 |
'/home/user/.gitignore', '.git')) |
| 94 |
self.assertTrue(path_has_relative_suffix( |
| 95 |
'/home/user/.GIT', '.git', case_sensitive=False)) |
| 96 |
|
| 97 |
@unittest.skipUnless(IS_WINDOWS, 'Windows treats backslash as separator') |
| 98 |
def test_path_startswith_windows_separators(self): |
| 99 |
"""Verify path_startswith handles backslash and mixed separators.""" |
| 100 |
self.assertTrue(path_startswith(r'D:\folder\file', r'D:\folder')) |
| 101 |
self.assertTrue(path_startswith(r'D:\folder/file', r'D:\folder')) |
| 102 |
self.assertTrue(path_startswith('D:/folder/file', 'D:/folder')) |
| 103 |
self.assertFalse(path_startswith(r'D:\folder2', r'D:\folder')) |
| 104 |
self.assertFalse(path_startswith(r'D:\folder', r'D:\folder')) |
| 105 |
|
| 106 |
@unittest.skipUnless(IS_WINDOWS, 'Windows treats backslash as separator') |
| 107 |
def test_path_has_relative_suffix_windows_separators(self): |
| 108 |
"""Verify path_has_relative_suffix handles backslash separators.""" |
| 109 |
self.assertTrue(path_has_relative_suffix(r'D:\folder\.git', '.git')) |
| 110 |
self.assertTrue(path_has_relative_suffix(r'D:\folder\.git', r'.git')) |
| 111 |
self.assertFalse(path_has_relative_suffix( |
| 112 |
r'D:\folder\not-git', '.git')) |
| 113 |
self.assertFalse(path_has_relative_suffix( |
| 114 |
r'D:\folder\.gitignore', '.git')) |
| 115 |
|
| 116 |
def test_whitelisted_windows_drive_root_matches_descendants(self): |
| 117 |
"""Verify a whitelisted drive root protects its descendants.""" |
| 118 |
with mock.patch( |
| 119 |
'bleachbit.Options.options.get_whitelist_paths', |
| 120 |
return_value=[('folder', 'D:\\')]): |
| 121 |
self.assertTrue(whitelisted_windows('d:\\')) |
| 122 |
self.assertTrue(whitelisted_windows(r'D:\folder\file')) |
| 123 |
self.assertFalse(whitelisted_windows(r'D:folder\file')) |
| 124 |
self.assertFalse(whitelisted_windows(r'E:\folder\file')) |
| 125 |
|
| 126 |
@unittest.skipUnless(IS_MAC, 'macOS-specific protected-path behavior') |
| 127 |
def test_protected_path_matching_is_case_insensitive_on_macos(self): |
| 128 |
"""Verify protected-path matching is case-insensitive on macOS.""" |
| 129 |
protected = '/PathUtilsProtected/CaseSensitive' |
| 130 |
definition = { |
| 131 |
'path': protected, |
| 132 |
'depth': 0, |
| 133 |
'case_sensitive': FS_CASE_SENSITIVE, |
| 134 |
} |
| 135 |
with mock.patch( |
| 136 |
'bleachbit.ProtectedPath.load_protected_paths', |
| 137 |
return_value=[definition]): |
| 138 |
self.assertEqual(check_protected_path(protected), definition) |
| 139 |
self.assertEqual( |
| 140 |
check_protected_path(protected.swapcase()), definition) |