| 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 |
""" |
| 9 |
Test case for Common |
| 10 |
""" |
| 11 |
|
| 12 |
# standard imports |
| 13 |
import os |
| 14 |
import tempfile |
| 15 |
|
| 16 |
# first party imports |
| 17 |
from tests import common |
| 18 |
|
| 19 |
|
| 20 |
class CommonTestCase(common.BleachbitTestCase): |
| 21 |
"""Test case for Common.""" |
| 22 |
|
| 23 |
_valid_language_codes = [ |
| 24 |
'be@latin', |
| 25 |
'C.UTF-8', |
| 26 |
'C.utf8', |
| 27 |
'C', |
| 28 |
'de_DE.iso88591', |
| 29 |
'de-CH', # seen in Fedora in Docker |
| 30 |
'en_US', |
| 31 |
'en-US', |
| 32 |
'en', |
| 33 |
'es_419', |
| 34 |
'fr_FR.utf8', |
| 35 |
'ja_JP.SJIS', |
| 36 |
'ko_KR.eucKR', |
| 37 |
'nb_NO.ISO-8859-1', |
| 38 |
'POSIX', |
| 39 |
'ru_RU.KOI8-R', |
| 40 |
'zh_Hant', |
| 41 |
'be_BY.utf8@latin', |
| 42 |
|
| 43 |
] |
| 44 |
|
| 45 |
_invalid_language_codes = ['e', 'en_', 'english', 'en_US_', '123', |
| 46 |
'en_us', 'en_US.', |
| 47 |
'en_us.utf8', |
| 48 |
'en_us.UTF-8', |
| 49 |
'utf8', |
| 50 |
'UTF-8', |
| 51 |
'.utf8', |
| 52 |
'.UTF-8', |
| 53 |
'[email protected]', |
| 54 |
'[email protected]', |
| 55 |
'', |
| 56 |
[], |
| 57 |
0, |
| 58 |
None] |
| 59 |
|
| 60 |
def test_assertIsLanguageCode_hardcoded_valid(self): |
| 61 |
"""Test assertIsLanguageCode() accepts hard-coded valid codes""" |
| 62 |
for code in self._valid_language_codes: |
| 63 |
with self.subTest(code=code): |
| 64 |
self.assertIsLanguageCode(code) |
| 65 |
|
| 66 |
def test_assertIsLanguageCode_hardcoded_invalid(self): |
| 67 |
"""Test assertIsLanguageCode() rejects hard-coded invalid codes""" |
| 68 |
invalid_codes = list(self._invalid_language_codes) |
| 69 |
invalid_codes.extend( |
| 70 |
[code + ' ' for code in self._valid_language_codes]) |
| 71 |
invalid_codes.extend( |
| 72 |
[' ' + code for code in self._valid_language_codes]) |
| 73 |
for code in invalid_codes: |
| 74 |
with self.subTest(code=code): |
| 75 |
with self.assertRaises(AssertionError, msg=f'Expected exception for {code}'): |
| 76 |
self.assertIsLanguageCode(code) |
| 77 |
|
| 78 |
def test_assertIsLanguageCode_live(self): |
| 79 |
"""Test assertIsLanguageCode() using live data""" |
| 80 |
from bleachbit import locale_dir |
| 81 |
locale_dirs = list(set([locale_dir, '/usr/share/locale'])) |
| 82 |
lang_codes = [] |
| 83 |
# Skip directories that are not valid language codes |
| 84 |
# 'UTF-8' is a macOS-specific LC_CTYPE directory, not a locale. |
| 85 |
skip_dirs = {'l10n', 'UTF-8'} |
| 86 |
for this_dir in locale_dirs: |
| 87 |
if not os.path.isdir(this_dir): |
| 88 |
continue |
| 89 |
for lang_code in os.listdir(this_dir): |
| 90 |
if not os.path.isdir(os.path.join(this_dir, lang_code)): |
| 91 |
continue |
| 92 |
if lang_code in skip_dirs: |
| 93 |
continue |
| 94 |
lang_codes.append(lang_code) |
| 95 |
if os.path.exists('/etc/locale.alias'): |
| 96 |
with open('/etc/locale.alias', 'r') as f: |
| 97 |
for line in f: |
| 98 |
line = line.strip() |
| 99 |
if not line.startswith('#'): |
| 100 |
parts = line.split() |
| 101 |
if len(parts) > 1: |
| 102 |
lang_codes.append(parts[1]) |
| 103 |
for lang_code in lang_codes: |
| 104 |
if lang_code in common.SKIP_ALIAS_CODES: |
| 105 |
continue |
| 106 |
self.assertIsLanguageCode(lang_code) |
| 107 |
|
| 108 |
def test_environment(self): |
| 109 |
"""Test for important environment variables""" |
| 110 |
# useful for researching |
| 111 |
# grep -Poh "([\\$%]\w+)" cleaners/*xml | cut -b2- | sort | uniq -i |
| 112 |
# bleachbit/init.py sets these variables on posix, if they do not exist. |
| 113 |
# Docker does not set USER |
| 114 |
envs = {'posix': ['HOME', 'XDG_CACHE_HOME', 'XDG_CONFIG_HOME', 'XDG_DATA_HOME'], |
| 115 |
'nt': ['AppData', 'CommonAppData', 'Documents', 'ProgramFiles', 'UserProfile', 'WinDir']} |
| 116 |
for env in envs[os.name]: |
| 117 |
e = os.getenv(env) |
| 118 |
self.assertIsNotNone(e, f"Environment variable {env} is not set") |
| 119 |
self.assertNotEqual( |
| 120 |
e.strip(), "", f"Environment variable {env} is empty") |
| 121 |
|
| 122 |
def test_xdist_tempdir_isolation(self): |
| 123 |
"""Use a private temporary directory in each xdist worker""" |
| 124 |
worker_id = os.environ.get('PYTEST_XDIST_WORKER') |
| 125 |
if not worker_id: |
| 126 |
self.skipTest('requires pytest-xdist worker') |
| 127 |
options_dir = os.environ.get('BLEACHBIT_TEST_OPTIONS_DIR') |
| 128 |
self.assertIsNotNone( |
| 129 |
options_dir, 'conftest must set BLEACHBIT_TEST_OPTIONS_DIR') |
| 130 |
# The per-worker keying is the isolation mechanism: without it every |
| 131 |
# worker would share one directory and the environment checks below |
| 132 |
# would still pass, so also require the worker id in the name. |
| 133 |
self.assertIn(worker_id, os.path.basename(options_dir)) |
| 134 |
for env_var in ('TEMP', 'TMP', 'TMPDIR'): |
| 135 |
env_dir = os.environ.get(env_var) |
| 136 |
self.assertIsNotNone(env_dir, f'conftest must set {env_var}') |
| 137 |
self.assertExists(env_dir) |
| 138 |
self.assertTrue( |
| 139 |
os.path.samefile(options_dir, env_dir), |
| 140 |
f'{env_var} must point to the worker temp directory') |
| 141 |
temp_dir = tempfile.gettempdir() |
| 142 |
self.assertExists(temp_dir) |
| 143 |
self.assertTrue(os.path.samefile(options_dir, temp_dir)) |
| 144 |
# setUpClass creates self.tempdir via tempfile.mkdtemp(); with |
| 145 |
# tempfile.tempdir pointed at the worker dir it must live inside it. |
| 146 |
# samefile (not string equality) is used so path case or symlinks |
| 147 |
# do not cause spurious failures. |
| 148 |
self.assertExists(self.tempdir) |
| 149 |
self.assertTrue(os.path.samefile( |
| 150 |
options_dir, os.path.commonpath((options_dir, self.tempdir)))) |
| 151 |
|
| 152 |
def test_get_put_env(self): |
| 153 |
"""Unit test for get_env() and put_env()""" |
| 154 |
self.assertIsNone(common.get_env('PUTENV_TEST')) |
| 155 |
|
| 156 |
common.put_env('PUTENV_TEST', '1') |
| 157 |
self.assertEqual('1', common.get_env('PUTENV_TEST')) |
| 158 |
|
| 159 |
common.put_env('PUTENV_TEST', None) |
| 160 |
self.assertIsNone(common.get_env('PUTENV_TEST')) |
| 161 |
|
| 162 |
@common.skipUnlessWindows |
| 163 |
def test_get_opened_windows_titles(self): |
| 164 |
"""Unit test for get_opened_windows_titles()""" |
| 165 |
titles = common.get_opened_windows_titles() |
| 166 |
|
| 167 |
self.assertIsInstance(titles, list, "Should return a list") |
| 168 |
self.assertGreater(len(titles), 0, |
| 169 |
f"Should have at least one window title. Got: {titles}") |
| 170 |
|
| 171 |
# Verify all items are strings and none are empty |
| 172 |
for title in titles: |
| 173 |
self.assertIsInstance( |
| 174 |
title, str, f"All titles should be strings, got {type(title)}") |
| 175 |
self.assertGreater(len(title.strip()), 0, |
| 176 |
f"Window title should not be empty or whitespace-only: '{title}'") |
| 177 |
|
| 178 |
def test_mock_missing_package(self): |
| 179 |
"""Test mock_missing_package context manager""" |
| 180 |
# Inside the context, importing logging should raise an errorl. |
| 181 |
with common.mock_missing_package('logging', clear_prefixes=('bleachbit.CLI',)): |
| 182 |
with self.assertRaises(ImportError): |
| 183 |
import logging # pylint: disable=import-outside-toplevel |
| 184 |
with self.assertRaises(ImportError): |
| 185 |
import bleachbit.CLI # pylint: disable=import-outside-toplevel |
| 186 |
# Importing sys should be unaffected. |
| 187 |
import sys # pylint: disable=import-outside-toplevel |
| 188 |
# Outside the context, importing should work. |
| 189 |
import logging # pylint: disable=import-outside-toplevel |
| 190 |
import bleachbit.CLI # pylint: disable=import-outside-toplevel |
| 191 |
|
| 192 |
def test_set_temporary_env(self): |
| 193 |
"""Test set_temporary_env context manager""" |
| 194 |
# Test with an environment variable that was not set before. |
| 195 |
self.assertIsNone(os.environ.get('TEST_VAR_NEW')) |
| 196 |
with common.set_temporary_env('TEST_VAR_NEW', 'test_value'): |
| 197 |
self.assertEqual('test_value', os.environ.get('TEST_VAR_NEW')) |
| 198 |
# Outside the context, the environment variable should be unset. |
| 199 |
self.assertIsNone(os.environ.get('TEST_VAR_NEW')) |
| 200 |
|
| 201 |
# Test with an environment variable that was set before. |
| 202 |
os.environ['TEST_VAR_EXISTING'] = 'original_value' |
| 203 |
with common.set_temporary_env('TEST_VAR_EXISTING', 'override_value'): |
| 204 |
self.assertEqual('override_value', |
| 205 |
os.environ.get('TEST_VAR_EXISTING')) |
| 206 |
# Outside the context, the environment variable should be restored. |
| 207 |
self.assertEqual('original_value', os.environ.get('TEST_VAR_EXISTING')) |
| 208 |
del os.environ['TEST_VAR_EXISTING'] |
| 209 |
|
| 210 |
def test_touch_file(self): |
| 211 |
"""Unit test for touch_file""" |
| 212 |
fn = os.path.join(self.tempdir, 'test_touch_file') |
| 213 |
self.assertNotExists(fn) |
| 214 |
|
| 215 |
# Create empty file. |
| 216 |
common.touch_file(fn) |
| 217 |
from bleachbit.FileUtilities import getsize |
| 218 |
self.assertExists(fn) |
| 219 |
self.assertEqual(0, getsize(fn)) |
| 220 |
|
| 221 |
# Increase size of file. |
| 222 |
fsize = 2**13 |
| 223 |
with open(fn, "w") as f: |
| 224 |
f.write(' ' * fsize) |
| 225 |
self.assertEqual(fsize, getsize(fn)) |
| 226 |
|
| 227 |
# Do not truncate. |
| 228 |
common.touch_file(fn) |
| 229 |
self.assertExists(fn) |
| 230 |
self.assertEqual(fsize, getsize(fn)) |
| 231 |
|
| 232 |
def test_assertExists_relative_path(self): |
| 233 |
"""Unit test for assertExists with relative path""" |
| 234 |
# Create a file in the current directory |
| 235 |
os.chdir(self.tempdir) |
| 236 |
relative_fn = 'test_relative_file.txt' |
| 237 |
common.touch_file(relative_fn) |
| 238 |
self.assertExists(relative_fn) |
| 239 |
os.remove(relative_fn) |
| 240 |
self.assertNotExists(relative_fn) |
| 241 |
self.assertNotExists('this-does-not-exist') |