Debian Codegraph @

tests/TestMakefile.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 """
9 Test case for Makefile
10 """
11
12 import os
13
14 from tests import common
15 from bleachbit import APP_VERSION, IS_LINUX, IS_MAC, IS_WINDOWS
16 from bleachbit.FileUtilities import exe_exists
17 from bleachbit.General import get_executable, run_external
18
19
20 class MakefileTestCase(common.BleachbitTestCase):
21 """Test case for Makefile"""
22
23 def setUp(self):
24 """Locate xgettext, make, and tar before running the test"""
25 super().setUp()
26 src_base_dir = os.path.abspath(
27 os.path.join(os.path.dirname(__file__), '..'))
28 self.src_base_dir = src_base_dir
29
30 # the CI workflow adds the MSYS2 bin directory to PATH before tests,
31 # so the tools are found via exe_exists() (which searches PATH).
32 # On Windows, exists_in_path() does not append .exe, so do it here.
33 exe_suffix = '.exe' if IS_WINDOWS else ''
34
35 tools = {
36 'xgettext_exe': ('xgettext', 'xgettext (run `apt install gettext` or equivalent)'),
37 'tar_exe': ('tar', 'tar (not found in PATH)'),
38 }
39 # make is only used on non-Windows; the Windows path returns early
40 # before invoking it, so do not require it there.
41 if not IS_WINDOWS:
42 tools['make_exe'] = ('make', 'make (not found in PATH)')
43 else:
44 self.make_exe = None
45
46 missing = []
47 for attr, (tool_name, error_msg) in tools.items():
48 exe_name = tool_name + exe_suffix
49 if exe_exists(exe_name):
50 setattr(self, attr, exe_name)
51 else:
52 setattr(self, attr, None)
53 missing.append(error_msg)
54
55 if missing:
56 # Linux always requires the build tools. On macOS and Windows,
57 # they are optional locally but required in CI.
58 if IS_LINUX or ('CI' in os.environ and (IS_MAC or IS_WINDOWS)):
59 raise RuntimeError(
60 'Missing required build tools: ' + '; '.join(missing))
61 self.skipTest(
62 'Missing required build tools: ' + '; '.join(missing))
63
64 def test_make_source(self):
65 """Test Makefile for source distribution"""
66 src_base_dir = self.src_base_dir
67 os.chdir(src_base_dir)
68 make_exe = self.make_exe
69 tar_exe = self.tar_exe
70
71 ver_name = 'bleachbit-' + APP_VERSION
72
73 # clean up from any previous test
74 pkg_fn = os.path.join('dist', ver_name + '.tar.gz')
75 if os.path.exists(pkg_fn):
76 os.remove(pkg_fn)
77 self.assertNotExists(pkg_fn)
78
79 # build source distribution
80 # Override PYTHONWARNINGS because the CI environment sets it to 'error',
81 # which can cause sdist to fail silently on Windows (rc=0 but no archive).
82 sdist_env = dict(os.environ)
83 if IS_WINDOWS:
84 sdist_env['PYTHONWARNINGS'] = 'default'
85 sdist_cmd = [get_executable(), 'setup.py', 'sdist', '--formats=gztar']
86 (rc, stdout, stderr) = run_external(
87 sdist_cmd, env=sdist_env, clean_env=False)
88 self.assertEqual(rc, 0, stderr + stdout)
89 pkg_fn = os.path.join('dist', ver_name + '.tar.gz')
90 self.assertExists(pkg_fn)
91
92 # extract source distribution
93 extract_dir = os.path.join(self.tempdir, 'extract')
94 os.mkdir(extract_dir)
95 tar_cmd = [tar_exe, 'xzf', pkg_fn, '-C', extract_dir]
96 (rc, _, stderr) = run_external(tar_cmd)
97 self.assertEqual(rc, 0, stderr)
98 extract_subdir = os.path.join(extract_dir, ver_name)
99 self.assertExists(os.path.join(extract_subdir, 'Makefile'))
100
101 # Verify a cleaner is present in the source distribution
102 self.assertExists(os.path.join(
103 extract_subdir, 'cleaners', 'chromium.xml'))
104
105 if IS_WINDOWS:
106 # The make targets `delete_windows_files` and `install` do not
107 # work under MSYS2 because bash strips backslashes from the
108 # Windows make path, and Windows does not use make for installation.
109 return
110
111 # delete Windows files using make
112 canary_fn = os.path.join(
113 extract_subdir, 'cleaners', 'windows_explorer.xml')
114 self.assertExists(canary_fn)
115 del_win_command = [make_exe, 'delete_windows_files']
116 os.chdir(extract_subdir)
117 (rc, _, stderr) = run_external(del_win_command)
118 self.assertEqual(rc, 0, stderr)
119 self.assertNotExists(canary_fn)
120
121 # install using make
122 install_tgt_dir = os.path.join(self.tempdir, 'install')
123 install_command = [make_exe,
124 'install', 'DESTDIR=' + install_tgt_dir]
125 os.chdir(extract_subdir)
126 (rc, _, stderr) = run_external(install_command)
127 self.assertEqual(
128 rc, 0, f"make install failed with rc={rc}, stderr={stderr}")
129 self.assertTrue(os.path.isdir(install_tgt_dir))
130 self.assertExists(os.path.join(
131 install_tgt_dir, 'usr/local/share/bleachbit/cleaners/chromium.xml'))