|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Exercise configured staging recipes and real Automake/libtool installation.""" |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import shlex |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +import unittest |
| 10 | + |
| 11 | + |
| 12 | +SOURCE = Path(__file__).resolve().parent.parent |
| 13 | +MAKE = shutil.which('gmake') or shutil.which('make') |
| 14 | + |
| 15 | + |
| 16 | +class StagedInstallRecipes(unittest.TestCase): |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + if not MAKE: |
| 20 | + raise RuntimeError('GNU make is required') |
| 21 | + cls.temporary = tempfile.TemporaryDirectory(prefix='v3-stage-install-') |
| 22 | + cls.addClassCleanup(cls.temporary.cleanup) |
| 23 | + cls.build = Path(cls.temporary.name).resolve() |
| 24 | + cls.environment = {key: value for key, value in os.environ.items() |
| 25 | + if key not in ('MAKEFLAGS', 'MFLAGS', 'MAKELEVEL')} |
| 26 | + for tool in ('install', 'ranlib'): |
| 27 | + boundary = cls.build / f'native-{tool}-boundary.sh' |
| 28 | + boundary.write_text( |
| 29 | + '#!/bin/sh\n' |
| 30 | + 'for destination do :; done\n' |
| 31 | + '# MSYS cannot pass an embedded drive specification to native tools.\n' |
| 32 | + 'case "$destination" in\n' |
| 33 | + f' /*[a-zA-Z]:/*) echo "native {tool} rejects embedded drive: $destination" >&2; exit 1 ;;\n' |
| 34 | + 'esac\n' |
| 35 | + f'printf "%s\\n" "$destination" >> {tool}-destinations.log\n' |
| 36 | + f'exec {shlex.quote(Path(shutil.which(tool)).as_posix())} "$@"\n') |
| 37 | + boundary.chmod(0o755) |
| 38 | + result = subprocess.run( |
| 39 | + ['sh', (SOURCE / 'configure').as_posix(), '--disable-v3-tls', |
| 40 | + '--disable-examples', '--prefix=D:/ci/prefix', |
| 41 | + 'RANLIB=' + (cls.build / 'native-ranlib-boundary.sh').as_posix()], cwd=cls.build, |
| 42 | + env=cls.environment, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 43 | + if result.returncode: |
| 44 | + raise RuntimeError(result.stdout) |
| 45 | + cls.installer = cls.build / 'native-install-boundary.sh' |
| 46 | + # A tiny real library reaches Automake/libtool's archive and DLL install |
| 47 | + # paths without compiling the unrelated protocol engines in each fixture. |
| 48 | + (cls.build / 'src/stagefixture.cpp').write_text( |
| 49 | + '#ifdef _WIN32\n__declspec(dllexport)\n#endif\n' |
| 50 | + 'int stagefixture() { return 1; }\n') |
| 51 | + compiler = shlex.split(os.environ.get('CXX', 'c++')) |
| 52 | + for arguments in [ |
| 53 | + ['--mode=compile'] + compiler + ['-c', 'stagefixture.cpp', '-o', 'stagefixture.lo'], |
| 54 | + ['--mode=link'] + compiler + ['-o', 'libstagefixture.la', 'stagefixture.lo', |
| 55 | + '-rpath', '/stagefixture', '-no-undefined']]: |
| 56 | + result = subprocess.run(['sh', '../libtool', '--tag=CXX'] + arguments, |
| 57 | + cwd=cls.build / 'src', env=cls.environment, stdout=subprocess.PIPE, |
| 58 | + stderr=subprocess.STDOUT, text=True) |
| 59 | + if result.returncode: |
| 60 | + raise RuntimeError(result.stdout) |
| 61 | + |
| 62 | + def setUp(self): |
| 63 | + for name in ('.hygiene-stage', '.install-stage', '.shared-check-stage', 'missing-stage'): |
| 64 | + shutil.rmtree(self.build / name, ignore_errors=True) |
| 65 | + for path in self.build.rglob('install-destinations.log'): |
| 66 | + path.unlink() |
| 67 | + |
| 68 | + def make(self, target, *variables, shared=False): |
| 69 | + # Exercise the production Makefile, recursive install rules and alias hook. |
| 70 | + # Only the fixture archive replaces the unrelated protocol engines. |
| 71 | + command = [MAKE, '--no-print-directory', target, |
| 72 | + 'lib_LTLIBRARIES=libstagefixture.la', 'noinst_LTLIBRARIES=', 'noinst_PROGRAMS=', |
| 73 | + 'INSTALL=' + self.installer.as_posix()] |
| 74 | + if shared: |
| 75 | + # Only check-local's staged-install body is under test here. |
| 76 | + text = (SOURCE / 'Makefile.am').read_text() |
| 77 | + prerequisites = text.split('\ncheck-local:', 1)[1].split('\n', 1)[0].split() |
| 78 | + for name in prerequisites: |
| 79 | + command.extend(['-o', name]) |
| 80 | + command.append('AM_MAKEFLAGS=-o check-readme -o check-release-notes -o check-doxygen') |
| 81 | + return subprocess.run(command + list(variables), cwd=self.build, |
| 82 | + env=self.environment, stdout=subprocess.PIPE, |
| 83 | + stderr=subprocess.STDOUT, text=True) |
| 84 | + |
| 85 | + def assert_passed(self, result): |
| 86 | + self.assertEqual(0, result.returncode, result.stdout) |
| 87 | + |
| 88 | + def installed(self, path): |
| 89 | + return self.build / '.hygiene-stage' / path |
| 90 | + |
| 91 | + def install_count(self): |
| 92 | + return sum(line.endswith('/lib/pkgconfig') |
| 93 | + for path in self.build.rglob('install-destinations.log') |
| 94 | + for line in path.read_text().splitlines()) |
| 95 | + |
| 96 | + def test_dos_prefix_standalone_checks_use_real_installed_headers(self): |
| 97 | + self.assert_passed(self.make('check-hygiene')) |
| 98 | + include = self.installed('D/ci/prefix/include') |
| 99 | + self.assertTrue((include / 'httpserverpp').is_file()) |
| 100 | + self.assertTrue((include / 'httpserver/server/readiness.hpp').is_file()) |
| 101 | + self.assertTrue(self.installed('D/ci/prefix/lib/libstagefixture.a').is_file()) |
| 102 | + self.assertIn('prefix=D:/ci/prefix', |
| 103 | + self.installed('D/ci/prefix/lib/pkgconfig/libhttpserver.pc').read_text()) |
| 104 | + self.assertTrue(self.installed('D/ci/prefix/share/man/man3/libhttpserver.3').is_file()) |
| 105 | + self.assertTrue(self.installed('D/ci/prefix/share/cmake/Modules/FindLibHttpServer.cmake').is_file()) |
| 106 | + self.assert_passed(self.make('check-install-layout')) |
| 107 | + self.assertFalse((self.build / '.install-stage').exists()) |
| 108 | + |
| 109 | + def test_dos_prefix_shared_check_local_installs_once_and_cleans_up(self): |
| 110 | + result = self.make('check-local', shared=True) |
| 111 | + self.assert_passed(result) |
| 112 | + self.assertIn('PASS: staged install layout', result.stdout) |
| 113 | + self.assertIn('PASS: no forbidden headers', result.stdout) |
| 114 | + self.assertEqual(1, self.install_count()) |
| 115 | + self.assertFalse((self.build / '.shared-check-stage').exists()) |
| 116 | + |
| 117 | + def test_standalone_hygiene_reuses_its_stamp(self): |
| 118 | + self.assert_passed(self.make('check-hygiene')) |
| 119 | + self.assert_passed(self.make('check-hygiene')) |
| 120 | + self.assertEqual(1, self.install_count()) |
| 121 | + |
| 122 | + def test_unix_and_custom_dos_install_directories(self): |
| 123 | + for prefix, include, library, data, man in [ |
| 124 | + ('/opt/package', '/custom/headers', '/custom/lib64', '/custom/data', '/custom/manual'), |
| 125 | + ('D:/ci/prefix', 'E:/custom/headers', 'F:/custom/lib64', 'G:/custom/data', 'H:/custom/manual')]: |
| 126 | + with self.subTest(prefix=prefix): |
| 127 | + variables = [f'prefix={prefix}', f'includedir={include}', f'libdir={library}', |
| 128 | + f'datadir={data}', f'man3dir={man}'] |
| 129 | + self.assert_passed(self.make('check-hygiene', *variables)) |
| 130 | + relative = include.replace(':', '').lstrip('/') |
| 131 | + self.assertTrue(self.installed(relative + '/httpserver.hpp').is_file()) |
| 132 | + self.assertTrue(self.installed(library.replace(':', '').lstrip('/') + |
| 133 | + '/pkgconfig/libhttpserver.pc').is_file()) |
| 134 | + self.assertTrue(self.installed(data.replace(':', '').lstrip('/') + |
| 135 | + '/cmake/Modules/FindLibHttpServer.cmake').is_file()) |
| 136 | + self.assertTrue(self.installed(man.replace(':', '').lstrip('/') + |
| 137 | + '/libhttpserver.3').is_file()) |
| 138 | + self.assert_passed(self.make('check-install-layout', *variables)) |
| 139 | + shutil.rmtree(self.build / '.hygiene-stage') |
| 140 | + |
| 141 | + def test_shared_hygiene_rejects_missing_and_incomplete_stage(self): |
| 142 | + stage = self.build / 'missing-stage' |
| 143 | + for exists in (False, True): |
| 144 | + if exists: |
| 145 | + stage.mkdir() |
| 146 | + result = self.make('check-hygiene', 'CHECK_HYGIENE_SHARED=yes', |
| 147 | + 'CHECK_HYGIENE_STAGE=' + stage.as_posix()) |
| 148 | + self.assertNotEqual(0, result.returncode, result.stdout) |
| 149 | + self.assertIn('does not contain' if exists else 'does not exist', result.stdout) |
| 150 | + |
| 151 | + def test_install_failure_cleans_standalone_stages(self): |
| 152 | + for target, directory in [('check-hygiene', '.hygiene-stage'), |
| 153 | + ('check-install-layout', '.install-stage')]: |
| 154 | + with self.subTest(target=target): |
| 155 | + result = self.make(target, 'INSTALL=false') |
| 156 | + self.assertNotEqual(0, result.returncode, result.stdout) |
| 157 | + self.assertIn('FAIL: staged install failed', result.stdout) |
| 158 | + self.assertFalse((self.build / directory).exists()) |
| 159 | + |
| 160 | + def test_installed_backend_header_leak_is_rejected(self): |
| 161 | + self.assert_passed(self.make('check-hygiene')) |
| 162 | + include = self.installed('D/ci/prefix/include') |
| 163 | + (include / 'microhttpd.h').write_text('/* forbidden provider fixture */\n') |
| 164 | + with (include / 'httpserver.hpp').open('a') as output: |
| 165 | + output.write('\n#include <microhttpd.h>\n') |
| 166 | + result = self.make('check-hygiene', 'CHECK_HYGIENE_SHARED=yes') |
| 167 | + self.assertNotEqual(0, result.returncode, result.stdout) |
| 168 | + self.assertIn('FAIL: forbidden headers leaked', result.stdout) |
| 169 | + |
| 170 | + def test_layout_rejects_private_headers_missing_alias_and_broken_readiness(self): |
| 171 | + self.assert_passed(self.make('check-hygiene')) |
| 172 | + stage = self.build / '.hygiene-stage' |
| 173 | + include = self.installed('D/ci/prefix/include') |
| 174 | + variables = ['CHECK_INSTALL_SHARED=yes', 'CHECK_INSTALL_STAGE=' + stage.as_posix()] |
| 175 | + for name, message in [('detail', 'detail/ directory leaked'), |
| 176 | + ('bad_impl.hpp', '*_impl.hpp file leaked')]: |
| 177 | + path = include / name |
| 178 | + if name == 'detail': |
| 179 | + path.mkdir() |
| 180 | + else: |
| 181 | + path.touch() |
| 182 | + result = self.make('check-install-layout', *variables) |
| 183 | + self.assertNotEqual(0, result.returncode, result.stdout) |
| 184 | + self.assertIn(message, result.stdout) |
| 185 | + if path.is_dir(): |
| 186 | + path.rmdir() |
| 187 | + else: |
| 188 | + path.unlink() |
| 189 | + alias = include / 'httpserverpp' |
| 190 | + content = alias.read_bytes() |
| 191 | + alias.unlink() |
| 192 | + result = self.make('check-install-layout', *variables) |
| 193 | + self.assertNotEqual(0, result.returncode, result.stdout) |
| 194 | + self.assertIn('alias not installed', result.stdout) |
| 195 | + alias.write_bytes(content) |
| 196 | + with (include / 'httpserver/server/readiness.hpp').open('a') as output: |
| 197 | + output.write('\n#error readiness fixture must fail\n') |
| 198 | + result = self.make('check-install-layout', *variables) |
| 199 | + self.assertNotEqual(0, result.returncode, result.stdout) |
| 200 | + self.assertIn('readiness fixture must fail', result.stdout) |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == '__main__': |
| 204 | + unittest.main() |
0 commit comments