blob: 9a46ec475f3fe5beb12b09a9dfca9116f176cfa3 [file] [log] [blame]
# Copyright 2016-2021 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import unittest
from itertools import chain
from pathlib import Path
import mesonbuild.mlog
import mesonbuild.depfile
import mesonbuild.dependencies.base
import mesonbuild.dependencies.factory
import mesonbuild.envconfig
import mesonbuild.environment
import mesonbuild.coredata
import mesonbuild.modules.gnome
from mesonbuild.interpreter import Interpreter
from mesonbuild.ast import AstInterpreter
from mesonbuild.mesonlib import (
MachineChoice, OptionKey
from mesonbuild.compilers import (
detect_c_compiler, detect_cpp_compiler
import mesonbuild.modules.pkgconfig
from run_tests import (
FakeBuild, get_fake_env
from .helpers import *
@unittest.skipIf(is_tarball(), 'Skipping because this is a tarball release')
class DataTests(unittest.TestCase):
def test_snippets(self):
hashcounter = re.compile('^ *(#)+')
snippet_dir = Path('docs/markdown/snippets')
for f in snippet_dir.glob('*'):
if f.suffix == '.md':
in_code_block = False
with'utf-8') as snippet:
for line in snippet:
if line.startswith(' '):
if line.startswith('```'):
in_code_block = not in_code_block
if in_code_block:
m = re.match(hashcounter, line)
if m:
self.assertEqual(len(, 2, 'All headings in snippets must have two hash symbols: ' +
self.assertFalse(in_code_block, 'Unclosed code block.')
if != 'add_release_note_snippets_here':
self.assertTrue(False, 'A file without .md suffix in snippets dir: ' +
def test_compiler_options_documented(self):
Test that C and C++ compiler options and base options are documented in Only tests the default compiler for the current
platform on the CI.
md = None
with open('docs/markdown/', encoding='utf-8') as f:
md =
env = get_fake_env()
# FIXME: Support other compilers
cc = detect_c_compiler(env, MachineChoice.HOST)
cpp = detect_cpp_compiler(env, MachineChoice.HOST)
for comp in (cc, cpp):
for opt in comp.get_options():
self.assertIn(str(opt), md)
for opt in comp.base_options:
self.assertIn(str(opt), md)
self.assertNotIn('b_unknown', md)
def _get_section_content(name, sections, md):
for section in sections:
if section and == name:
next_section = next(sections)
end = next_section.start()
except StopIteration:
end = len(md)
# Extract the content for this section
return md[section.end():end]
raise RuntimeError(f'Could not find "{name}" heading')
def test_builtin_options_documented(self):
Test that universal options and base options are documented in
from itertools import tee
md = None
with open('docs/markdown/', encoding='utf-8') as f:
md =
found_entries = set()
sections = re.finditer(r"^## (.+)$", md, re.MULTILINE)
# Extract the content for this section
u_subcontents = []
content = self._get_section_content("Universal options", sections, md)
subsections = tee(re.finditer(r"^### (.+)$", content, re.MULTILINE))
u_subcontents.append(self._get_section_content("Directories", subsections[0], content))
u_subcontents.append(self._get_section_content("Core options", subsections[1], content))
mod_subcontents = []
content = self._get_section_content("Module options", sections, md)
subsections = tee(re.finditer(r"^### (.+)$", content, re.MULTILINE))
for idx, mod in enumerate(['Pkgconfig', 'Python']):
mod_subcontents.append(self._get_section_content(f'{mod} module', subsections[idx], content))
for subcontent in u_subcontents + mod_subcontents:
# Find the option names
options = set()
# Match either a table row or a table heading separator: | ------ |
rows = re.finditer(r"^\|(?: (\w+) .* | *-+ *)\|", subcontent, re.MULTILINE)
# Skip the header of the first table
# Skip the heading separator of the first table
for m in rows:
value =
# End when the `buildtype` table starts
if value is None:
self.assertEqual(len(found_entries & options), 0)
found_entries |= options
self.assertEqual(found_entries, {
*(str(k.evolve(module=None)) for k in mesonbuild.coredata.BUILTIN_OPTIONS),
*(str(k.evolve(module=None)) for k in mesonbuild.coredata.BUILTIN_OPTIONS_PER_MACHINE),
# Check that `buildtype` table inside `Core options` matches how
# setting of builtin options behaves
# Find all tables inside this subsection
tables = re.finditer(r"^\| (\w+) .* \|\n\| *[-|\s]+ *\|$", u_subcontents[1], re.MULTILINE)
# Get the table we want using the header of the first column
table = self._get_section_content('buildtype', tables, u_subcontents[1])
# Get table row data
rows = re.finditer(r"^\|(?: (\w+)\s+\| (\w+)\s+\| (\w+) .* | *-+ *)\|", table, re.MULTILINE)
env = get_fake_env()
for m in rows:
buildtype, debug, opt = m.groups()
if debug == 'true':
debug = True
elif debug == 'false':
debug = False
raise RuntimeError(f'Invalid debug value {debug!r} in row:\n{}')
env.coredata.set_option(OptionKey('buildtype'), buildtype)
self.assertEqual(env.coredata.options[OptionKey('buildtype')].value, buildtype)
self.assertEqual(env.coredata.options[OptionKey('optimization')].value, opt)
self.assertEqual(env.coredata.options[OptionKey('debug')].value, debug)
def test_cpu_families_documented(self):
with open("docs/markdown/", encoding='utf-8') as f:
md =
sections = re.finditer(r"^## (.+)$", md, re.MULTILINE)
content = self._get_section_content("CPU families", sections, md)
# Find the list entries
arches = [ for m in re.finditer(r"^\| (\w+) +\|", content, re.MULTILINE)]
# Drop the header
arches = set(arches[1:])
self.assertEqual(arches, set(mesonbuild.environment.known_cpu_families))
def test_markdown_files_in_sitemap(self):
Test that each markdown files in docs/markdown is referenced in sitemap.txt
with open("docs/sitemap.txt", encoding='utf-8') as f:
md =
toc = list( for m in re.finditer(r"^\s*(\w.*)$", md, re.MULTILINE))
markdownfiles = [ for f in Path("docs/markdown").iterdir() if f.is_file() and f.suffix == '.md']
exceptions = ['']
for f in markdownfiles:
if f not in exceptions and not f.startswith('_include'):
self.assertIn(f, toc)
def test_modules_in_navbar(self):
Test that each module is referenced in navbar_links.html
with open("docs/theme/extra/templates/navbar_links.html", encoding='utf-8') as f:
html =
for f in Path('mesonbuild/modules').glob('*.py'):
if in {'', '', ''}:
name = f'{f.stem}-module.html'
name = name.replace('unstable_', '')
name = name.replace('python3', 'python-3')
name = name.replace('_', '-')
self.assertIn(name, html)
def test_vim_syntax_highlighting(self):
Ensure that vim syntax highlighting files were updated for new
functions in the global namespace in build files.
env = get_fake_env()
interp = Interpreter(FakeBuild(env), mock=True)
with open('data/syntax-highlighting/vim/syntax/meson.vim', encoding='utf-8') as f:
res ='syn keyword mesonBuiltin(\s+\\\s\w+)+',, re.MULTILINE)
defined = set([a.strip() for a in'\\')][1:])
self.assertEqual(defined, set(chain(interp.funcs.keys(), interp.builtin.keys())))
def test_all_functions_defined_in_ast_interpreter(self):
Ensure that the all functions defined in the Interpreter are also defined
in the AstInterpreter (and vice versa).
env = get_fake_env()
interp = Interpreter(FakeBuild(env), mock=True)
astint = AstInterpreter('.', '', '')
self.assertEqual(set(interp.funcs.keys()), set(astint.funcs.keys()))