| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2016-2021 The Meson development team |
| 3 |
|
| 4 |
import re |
| 5 |
import unittest |
| 6 |
from itertools import chain |
| 7 |
from pathlib import Path |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
import mesonbuild.mlog |
| 11 |
import mesonbuild.depfile |
| 12 |
import mesonbuild.dependencies.base |
| 13 |
import mesonbuild.dependencies.factory |
| 14 |
import mesonbuild.envconfig |
| 15 |
import mesonbuild.environment |
| 16 |
import mesonbuild.coredata |
| 17 |
import mesonbuild.options |
| 18 |
import mesonbuild.modules.gnome |
| 19 |
from mesonbuild.interpreter import Interpreter |
| 20 |
from mesonbuild.ast import AstInterpreter |
| 21 |
from mesonbuild.mesonlib import MachineChoice |
| 22 |
from mesonbuild.options import OptionKey |
| 23 |
from mesonbuild.compilers import ( |
| 24 |
detect_c_compiler, detect_cpp_compiler |
| 25 |
) |
| 26 |
import mesonbuild.modules.pkgconfig |
| 27 |
|
| 28 |
|
| 29 |
from run_tests import ( |
| 30 |
FakeBuild, get_fake_env |
| 31 |
) |
| 32 |
|
| 33 |
from .helpers import * |
| 34 |
|
| 35 |
@unittest.skipIf(is_tarball(), 'Skipping because this is a tarball release') |
| 36 |
class DataTests(unittest.TestCase): |
| 37 |
|
| 38 |
def test_snippets(self): |
| 39 |
hashcounter = re.compile('^ *(#)+') |
| 40 |
snippet_dir = Path('docs/markdown/snippets') |
| 41 |
self.assertTrue(snippet_dir.is_dir()) |
| 42 |
for f in snippet_dir.glob('*'): |
| 43 |
self.assertTrue(f.is_file()) |
| 44 |
if f.parts[-1].endswith('~'): |
| 45 |
continue |
| 46 |
if f.suffix == '.md': |
| 47 |
in_code_block = False |
| 48 |
with f.open(encoding='utf-8') as snippet: |
| 49 |
for line in snippet: |
| 50 |
if line.startswith(' '): |
| 51 |
continue |
| 52 |
if line.startswith('```'): |
| 53 |
in_code_block = not in_code_block |
| 54 |
if in_code_block: |
| 55 |
continue |
| 56 |
m = re.match(hashcounter, line) |
| 57 |
if m: |
| 58 |
self.assertEqual(len(m.group(0)), 2, 'All headings in snippets must have two hash symbols: ' + f.name) |
| 59 |
self.assertFalse(in_code_block, 'Unclosed code block.') |
| 60 |
else: |
| 61 |
if f.name != 'add_release_note_snippets_here': |
| 62 |
self.assertTrue(False, 'A file without .md suffix in snippets dir: ' + f.name) |
| 63 |
|
| 64 |
def test_compiler_options_documented(self): |
| 65 |
''' |
| 66 |
Test that C and C++ compiler options and base options are documented in |
| 67 |
Builtin-Options.md. Only tests the default compiler for the current |
| 68 |
platform on the CI. |
| 69 |
''' |
| 70 |
md = None |
| 71 |
with open('docs/markdown/Builtin-options.md', encoding='utf-8') as f: |
| 72 |
md = f.read() |
| 73 |
self.assertIsNotNone(md) |
| 74 |
env = get_fake_env() |
| 75 |
# FIXME: Support other compilers |
| 76 |
cc = detect_c_compiler(env, MachineChoice.HOST) |
| 77 |
cpp = detect_cpp_compiler(env, MachineChoice.HOST) |
| 78 |
for comp in (cc, cpp): |
| 79 |
for opt in comp.get_options(): |
| 80 |
self.assertIn(str(opt), md) |
| 81 |
for opt in comp.base_options: |
| 82 |
self.assertIn(str(opt), md) |
| 83 |
self.assertNotIn('b_unknown', md) |
| 84 |
|
| 85 |
@staticmethod |
| 86 |
def _get_section_content(name, sections, md): |
| 87 |
for section in sections: |
| 88 |
if section and section.group(1) == name: |
| 89 |
try: |
| 90 |
next_section = next(sections) |
| 91 |
end = next_section.start() |
| 92 |
except StopIteration: |
| 93 |
end = len(md) |
| 94 |
# Extract the content for this section |
| 95 |
return md[section.end():end] |
| 96 |
raise RuntimeError(f'Could not find "{name}" heading') |
| 97 |
|
| 98 |
def test_builtin_options_documented(self): |
| 99 |
''' |
| 100 |
Test that universal options and base options are documented in |
| 101 |
Builtin-Options.md. |
| 102 |
''' |
| 103 |
from itertools import tee |
| 104 |
md = None |
| 105 |
with open('docs/markdown/Builtin-options.md', encoding='utf-8') as f: |
| 106 |
md = f.read() |
| 107 |
self.assertIsNotNone(md) |
| 108 |
|
| 109 |
found_entries = set() |
| 110 |
sections = re.finditer(r"^## (.+)$", md, re.MULTILINE) |
| 111 |
# Extract the content for this section |
| 112 |
u_subcontents = [] |
| 113 |
content = self._get_section_content("Universal options", sections, md) |
| 114 |
subsections = tee(re.finditer(r"^### (.+)$", content, re.MULTILINE)) |
| 115 |
u_subcontents.append(self._get_section_content("Directories", subsections[0], content)) |
| 116 |
u_subcontents.append(self._get_section_content("Core options", subsections[1], content)) |
| 117 |
|
| 118 |
mod_subcontents = [] |
| 119 |
content = self._get_section_content("Module options", sections, md) |
| 120 |
subsections = tee(re.finditer(r"^### (.+)$", content, re.MULTILINE)) |
| 121 |
for idx, mod in enumerate(['Pkgconfig', 'Python']): |
| 122 |
mod_subcontents.append(self._get_section_content(f'{mod} module', subsections[idx], content)) |
| 123 |
for subcontent in u_subcontents + mod_subcontents: |
| 124 |
# Find the option names |
| 125 |
options = set() |
| 126 |
# Match either a table row or a table heading separator: | ------ | |
| 127 |
rows = re.finditer(r"^\|(?: (\w+) .* | *-+ *)\|", subcontent, re.MULTILINE) |
| 128 |
# Skip the header of the first table |
| 129 |
next(rows) |
| 130 |
# Skip the heading separator of the first table |
| 131 |
next(rows) |
| 132 |
for m in rows: |
| 133 |
value = m.group(1) |
| 134 |
# End when the `buildtype` table starts |
| 135 |
if value is None: |
| 136 |
break |
| 137 |
options.add(value) |
| 138 |
self.assertEqual(len(found_entries & options), 0) |
| 139 |
found_entries |= options |
| 140 |
|
| 141 |
# TODO: put the module name back in the OptionKey |
| 142 |
def remove_module_name(key: OptionKey) -> OptionKey: |
| 143 |
if '.' in key.name: |
| 144 |
return key.evolve(name=key.name.split('.', 1)[1]) |
| 145 |
return key |
| 146 |
|
| 147 |
self.assertEqual(found_entries, { |
| 148 |
*(str(remove_module_name(k)) for k in mesonbuild.options.BUILTIN_OPTIONS), |
| 149 |
*(str(remove_module_name(k)) for k in mesonbuild.options.BUILTIN_OPTIONS_PER_MACHINE), |
| 150 |
}) |
| 151 |
|
| 152 |
# Check that `buildtype` table inside `Core options` matches how |
| 153 |
# setting of builtin options behaves |
| 154 |
# |
| 155 |
# Find all tables inside this subsection |
| 156 |
tables = re.finditer(r"^\| (\w+) .* \|\n\| *[-|\s]+ *\|$", u_subcontents[1], re.MULTILINE) |
| 157 |
# Get the table we want using the header of the first column |
| 158 |
table = self._get_section_content('buildtype', tables, u_subcontents[1]) |
| 159 |
# Get table row data |
| 160 |
rows = re.finditer(r"^\|(?: (\w+)\s+\| (\w+)\s+\| (\w+) .* | *-+ *)\|", table, re.MULTILINE) |
| 161 |
env = get_fake_env() |
| 162 |
for m in rows: |
| 163 |
buildtype, debug, opt = m.groups() |
| 164 |
if debug == 'true': |
| 165 |
debug = True |
| 166 |
elif debug == 'false': |
| 167 |
debug = False |
| 168 |
else: |
| 169 |
raise RuntimeError(f'Invalid debug value {debug!r} in row:\n{m.group()}') |
| 170 |
env.coredata.optstore.set_option(OptionKey('buildtype'), buildtype) |
| 171 |
self.assertEqual(env.coredata.optstore.get_value_for('buildtype'), buildtype) |
| 172 |
self.assertEqual(env.coredata.optstore.get_value_for('optimization'), opt) |
| 173 |
self.assertEqual(env.coredata.optstore.get_value_for('debug'), debug) |
| 174 |
|
| 175 |
def test_cpu_families_documented(self): |
| 176 |
with open("docs/markdown/Reference-tables.md", encoding='utf-8') as f: |
| 177 |
md = f.read() |
| 178 |
self.assertIsNotNone(md) |
| 179 |
|
| 180 |
sections = re.finditer(r"^## (.+)$", md, re.MULTILINE) |
| 181 |
content = self._get_section_content("CPU families", sections, md) |
| 182 |
# Find the list entries |
| 183 |
arches = [m.group(1) for m in re.finditer(r"^\| (\w+) +\|", content, re.MULTILINE)] |
| 184 |
# Drop the header |
| 185 |
arches = set(arches[1:]) |
| 186 |
self.assertEqual(arches, set(mesonbuild.envconfig.known_cpu_families)) |
| 187 |
|
| 188 |
def test_markdown_files_in_sitemap(self): |
| 189 |
''' |
| 190 |
Test that each markdown files in docs/markdown is referenced in sitemap.txt |
| 191 |
''' |
| 192 |
with open("docs/sitemap.txt", encoding='utf-8') as f: |
| 193 |
md = f.read() |
| 194 |
self.assertIsNotNone(md) |
| 195 |
toc = list(m.group(1) for m in re.finditer(r"^\s*(\w.*)$", md, re.MULTILINE)) |
| 196 |
markdownfiles = [f.name for f in Path("docs/markdown").iterdir() if f.is_file() and f.suffix == '.md'] |
| 197 |
exceptions = ['_Sidebar.md'] |
| 198 |
for f in markdownfiles: |
| 199 |
if f not in exceptions and not f.startswith('_include'): |
| 200 |
self.assertIn(f, toc) |
| 201 |
|
| 202 |
def test_modules_in_navbar(self): |
| 203 |
''' |
| 204 |
Test that each module is referenced in navbar_links.html |
| 205 |
''' |
| 206 |
with open("docs/theme/extra/templates/navbar_links.html", encoding='utf-8') as f: |
| 207 |
html = f.read().lower() |
| 208 |
self.assertIsNotNone(html) |
| 209 |
for f in Path('mesonbuild/modules').glob('*.py'): |
| 210 |
if f.name.startswith('_') or f.name == 'modtest.py': |
| 211 |
continue |
| 212 |
name = f'{f.stem}-module.html' |
| 213 |
name = name.replace('unstable_', '') |
| 214 |
name = name.replace('python3', 'python-3') |
| 215 |
name = name.replace('_', '-') |
| 216 |
self.assertIn(name, html) |
| 217 |
|
| 218 |
@mock.patch.dict(os.environ) |
| 219 |
@mock.patch.object(Interpreter, 'load_root_meson_file', mock.Mock(return_value=None)) |
| 220 |
@mock.patch.object(Interpreter, 'sanity_check_ast', mock.Mock(return_value=None)) |
| 221 |
@mock.patch.object(Interpreter, 'parse_project', mock.Mock(return_value=None)) |
| 222 |
def test_vim_syntax_highlighting(self): |
| 223 |
''' |
| 224 |
Ensure that vim syntax highlighting files were updated for new |
| 225 |
functions in the global namespace in build files. |
| 226 |
''' |
| 227 |
# Disable unit test specific syntax |
| 228 |
del os.environ['MESON_RUNNING_IN_PROJECT_TESTS'] |
| 229 |
env = get_fake_env() |
| 230 |
interp = Interpreter(FakeBuild(env)) |
| 231 |
with open('data/syntax-highlighting/vim/syntax/meson.vim', encoding='utf-8') as f: |
| 232 |
res = re.search(r'syn keyword mesonBuiltin(\s+\\\s\w+)+', f.read(), re.MULTILINE) |
| 233 |
defined = set([a.strip() for a in res.group().split('\\')][1:]) |
| 234 |
self.assertEqual(defined, set(chain(interp.funcs.keys(), interp.builtin.keys()))) |
| 235 |
|
| 236 |
@mock.patch.dict(os.environ) |
| 237 |
@mock.patch.object(Interpreter, 'load_root_meson_file', mock.Mock(return_value=None)) |
| 238 |
@mock.patch.object(Interpreter, 'sanity_check_ast', mock.Mock(return_value=None)) |
| 239 |
@mock.patch.object(Interpreter, 'parse_project', mock.Mock(return_value=None)) |
| 240 |
def test_all_functions_defined_in_ast_interpreter(self): |
| 241 |
''' |
| 242 |
Ensure that the all functions defined in the Interpreter are also defined |
| 243 |
in the AstInterpreter (and vice versa). |
| 244 |
''' |
| 245 |
# Disable unit test specific syntax |
| 246 |
del os.environ['MESON_RUNNING_IN_PROJECT_TESTS'] |
| 247 |
env = get_fake_env() |
| 248 |
interp = Interpreter(FakeBuild(env)) |
| 249 |
astint = AstInterpreter('.', '', '', '', env) |
| 250 |
self.assertEqual(set(interp.funcs.keys()), set(astint.funcs.keys())) |