3555844094037db6ba646023b04788c514f7e9df
1 # The MIT License (MIT)
3 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 def pytest_collect_file(parent
, path
):
36 if path
.ext
!= yaml_ext
:
37 # not a YAML file: cancel
41 # `/home/jo/barectf/tests/tracing/configs/basic/static-array/of-str.yaml`,
53 file_name
= os
.path
.basename(path_str
)
54 subcat_dir
= os
.path
.dirname(path_str
)
55 subcat
= os
.path
.basename(subcat_dir
)
56 cat_dir
= os
.path
.dirname(subcat_dir
)
57 cat
= os
.path
.basename(cat_dir
)
58 configs_dir
= os
.path
.dirname(cat_dir
)
62 'basic-extra-pc-ft-members',
65 if cat
not in valid_cats
or os
.path
.basename(configs_dir
) != 'configs':
66 # not a YAML configuration test
69 # create C source, expectation file, and support directory paths
70 base_dir
= os
.path
.dirname(configs_dir
)
71 base_name
= file_name
.replace(yaml_ext
, '')
72 subcat_rel_dir
= os
.path
.join(cat
, subcat
)
73 src_path
= os
.path
.join(base_dir
, 'src', subcat_rel_dir
, f
'{base_name}.c')
74 data_expect_path
= os
.path
.join(base_dir
, 'expect', subcat_rel_dir
, f
'{base_name}.data.expect')
75 metadata_expect_path
= os
.path
.join(base_dir
, 'expect', subcat_rel_dir
,
76 f
'{base_name}.metadata.expect')
77 support_dir_path
= os
.path
.join(base_dir
, 'support', cat
)
79 # create the file node
80 return _YamlFile
.from_parent(parent
, fspath
=path
, src_path
=src_path
,
81 data_expect_path
=data_expect_path
,
82 metadata_expect_path
=metadata_expect_path
,
83 support_dir_path
=support_dir_path
,
84 name
=f
'test-{cat}-{subcat}-{base_name}')
87 class _YamlFile(pytest
.File
):
88 def __init__(self
, parent
, fspath
, src_path
, data_expect_path
, metadata_expect_path
,
89 support_dir_path
, name
):
90 super().__init
__(parent
=parent
, fspath
=fspath
)
92 self
._src
_path
= src_path
93 self
._data
_expect
_path
= data_expect_path
94 self
._metadata
_expect
_path
= metadata_expect_path
95 self
._support
_dir
_path
= support_dir_path
99 yield _YamlItem
.from_parent(self
, name
=self
._name
, src_path
=self
._src
_path
,
100 data_expect_path
=self
._data
_expect
_path
,
101 metadata_expect_path
=self
._metadata
_expect
_path
,
102 support_dir_path
=self
._support
_dir
_path
)
105 class _YamlItem(pytest
.Item
):
106 def __init__(self
, parent
, name
, src_path
, data_expect_path
, metadata_expect_path
,
108 super().__init
__(parent
=parent
, name
=name
)
109 self
._src
_path
= src_path
110 self
._data
_expect
_path
= data_expect_path
111 self
._metadata
_expect
_path
= metadata_expect_path
112 self
._support
_dir
_path
= support_dir_path
115 # create a temporary directory
116 tmpdir
= tempfile
.TemporaryDirectory(prefix
='pytest-barectf')
118 # create barectf configuration
119 with
open(self
.fspath
) as f
:
120 cfg
= barectf
.configuration_from_file(f
, inclusion_directories
=[self
._support
_dir
_path
])
122 # generate and write C code files
123 cg
= barectf
.CodeGenerator(cfg
)
124 files
= cg
.generate_c_headers()
125 files
+= cg
.generate_c_sources()
128 with
open(os
.path
.join(tmpdir
.name
, file.name
), 'w') as f
:
129 f
.write(file.contents
)
131 # generate metadata stream, stripping the version and date
132 file = cg
.generate_metadata_stream()
133 lines
= file.contents
.split('\n')
137 'The following code was generated',
139 'barectf_gen_date =',
149 for pattern
in discard_patterns
:
156 new_lines
.append(line
)
158 actual_metadata
= '\n'.join(new_lines
)
160 # copy Makefile to build directory
161 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'Makefile'), tmpdir
.name
)
163 # copy platform files to build directory
164 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'test-platform.c'), tmpdir
.name
)
165 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'test-platform.h'), tmpdir
.name
)
167 # copy specific source code file to build directory
168 shutil
.copy(self
._src
_path
, os
.path
.join(tmpdir
.name
, 'test.c'))
171 subprocess
.check_output(['make'], cwd
=tmpdir
.name
)
173 # run the test (produce the data stream)
174 subprocess
.check_output(['./test'], cwd
=tmpdir
.name
)
177 with
open(os
.path
.join(tmpdir
.name
, 'stream'), 'rb') as f
:
178 actual_stream
= f
.read()
180 # read data stream expectation file
181 with
open(self
._data
_expect
_path
, 'rb') as f
:
182 expected_stream
= f
.read()
184 # read metadata stream expectation file
185 with
open(self
._metadata
_expect
_path
, 'r') as f
:
186 expected_metadata
= f
.read()
189 assert actual_metadata
== expected_metadata
190 assert actual_stream
== expected_stream
192 # delete temporary directory
195 def repr_failure(self
, excinfo
, style
=None):
196 return f
'`{self.fspath}` failed: {excinfo}.'
198 def reportinfo(self
):
199 return self
.fspath
, None, self
.name
This page took 0.034418 seconds and 3 git commands to generate.