1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
# The MIT License (MIT)
#
# Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import pytest
import os
import os.path
import barectf
import shutil
import subprocess
import tempfile
def pytest_collect_file(parent, path):
yaml_ext = '.yaml'
if path.ext != yaml_ext:
# not a YAML file: cancel
return
# If `path` is
# `/home/jo/barectf/tests/tracing/configs/basic/static-array/of-str.yaml`,
# for example, then:
#
# `cat`:
# `basic`
#
# `subcat`:
# `static-array`
#
# `file_name`:
# `of-str.yaml`
path_str = str(path)
file_name = os.path.basename(path_str)
subcat_dir = os.path.dirname(path_str)
subcat = os.path.basename(subcat_dir)
cat_dir = os.path.dirname(subcat_dir)
cat = os.path.basename(cat_dir)
configs_dir = os.path.dirname(cat_dir)
valid_cats = {
'basic',
'counter-clock',
'basic-extra-pc-ft-members',
}
if cat not in valid_cats or os.path.basename(configs_dir) != 'configs':
# not a YAML configuration test
return
# create C source, expectation file, and support directory paths
base_dir = os.path.dirname(configs_dir)
base_name = file_name.replace(yaml_ext, '')
subcat_rel_dir = os.path.join(cat, subcat)
src_path = os.path.join(base_dir, 'src', subcat_rel_dir, f'{base_name}.c')
data_expect_path = os.path.join(base_dir, 'expect', subcat_rel_dir, f'{base_name}.data.expect')
metadata_expect_path = os.path.join(base_dir, 'expect', subcat_rel_dir,
f'{base_name}.metadata.expect')
support_dir_path = os.path.join(base_dir, 'support', cat)
# create the file node
return _YamlFile.from_parent(parent, fspath=path, src_path=src_path,
data_expect_path=data_expect_path,
metadata_expect_path=metadata_expect_path,
support_dir_path=support_dir_path,
name=f'test-{cat}-{subcat}-{base_name}')
class _YamlFile(pytest.File):
def __init__(self, parent, fspath, src_path, data_expect_path, metadata_expect_path,
support_dir_path, name):
super().__init__(parent=parent, fspath=fspath)
self._name = name
self._src_path = src_path
self._data_expect_path = data_expect_path
self._metadata_expect_path = metadata_expect_path
self._support_dir_path = support_dir_path
def collect(self):
# yield a single item
yield _YamlItem.from_parent(self, name=self._name, src_path=self._src_path,
data_expect_path=self._data_expect_path,
metadata_expect_path=self._metadata_expect_path,
support_dir_path=self._support_dir_path)
class _YamlItem(pytest.Item):
def __init__(self, parent, name, src_path, data_expect_path, metadata_expect_path,
support_dir_path):
super().__init__(parent=parent, name=name)
self._src_path = src_path
self._data_expect_path = data_expect_path
self._metadata_expect_path = metadata_expect_path
self._support_dir_path = support_dir_path
def runtest(self):
# create a temporary directory
tmpdir = tempfile.TemporaryDirectory(prefix='pytest-barectf')
# create barectf configuration
with open(self.fspath) as f:
cfg = barectf.configuration_from_file(f, inclusion_directories=[self._support_dir_path])
# generate and write C code files
cg = barectf.CodeGenerator(cfg)
files = cg.generate_c_headers()
files += cg.generate_c_sources()
for file in files:
with open(os.path.join(tmpdir.name, file.name), 'w') as f:
f.write(file.contents)
# generate metadata stream, stripping the version and date
file = cg.generate_metadata_stream()
lines = file.contents.split('\n')
new_lines = []
discard_patterns = [
'Copyright (c)',
'The following code was generated',
'* on ',
'barectf_gen_date =',
'tracer_major =',
'tracer_minor =',
'tracer_patch =',
'tracer_pre =',
]
for line in lines:
skip = False
for pattern in discard_patterns:
if pattern in line:
skip = True
if skip:
continue
new_lines.append(line)
actual_metadata = '\n'.join(new_lines)
# copy Makefile to build directory
shutil.copy(os.path.join(self._support_dir_path, 'Makefile'), tmpdir.name)
# copy platform files to build directory
shutil.copy(os.path.join(self._support_dir_path, 'test-platform.c'), tmpdir.name)
shutil.copy(os.path.join(self._support_dir_path, 'test-platform.h'), tmpdir.name)
# copy specific source code file to build directory
shutil.copy(self._src_path, os.path.join(tmpdir.name, 'test.c'))
# build the test
subprocess.check_output(['make'], cwd=tmpdir.name)
# run the test (produce the data stream)
subprocess.check_output(['./test'], cwd=tmpdir.name)
# read actual stream
with open(os.path.join(tmpdir.name, 'stream'), 'rb') as f:
actual_stream = f.read()
# read data stream expectation file
with open(self._data_expect_path, 'rb') as f:
expected_stream = f.read()
# read metadata stream expectation file
with open(self._metadata_expect_path, 'r') as f:
expected_metadata = f.read()
# validate streams
assert actual_metadata == expected_metadata
assert actual_stream == expected_stream
# delete temporary directory
tmpdir.cleanup()
def repr_failure(self, excinfo, style=None):
return f'`{self.fspath}` failed: {excinfo}.'
def reportinfo(self):
return self.fspath, None, self.name
|