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
40 # At the end of this loop, if `path` is
41 # `/home/jo/barectf/tests/tracing/configs/succeed/static-array/of-str.yaml`,
42 # for example, then `elems` is:
51 elem
= os
.path
.basename(path_str
)
57 path_str
= os
.path
.dirname(path_str
)
59 # create C source, expectation file, and support directory paths
60 base_dir
= os
.path
.dirname(path_str
)
61 base_name
= elems
[0].replace(yaml_ext
, '')
62 rel_dir
= os
.path
.join(*list(reversed(elems
[1:])))
63 src_path
= os
.path
.join(*[base_dir
, 'src', rel_dir
, f
'{base_name}.c'])
64 data_expect_path
= os
.path
.join(*([base_dir
, 'expect', rel_dir
, f
'{base_name}.data.expect']))
65 metadata_expect_path
= os
.path
.join(*([base_dir
, 'expect', rel_dir
, f
'{base_name}.metadata.expect']))
66 support_dir_path
= os
.path
.join(base_dir
, 'support')
68 # create a unique test name
69 name
= f
'test-{"-".join(reversed(elems))}'.replace(yaml_ext
, '')
71 # create the file node
72 return _YamlFile
.from_parent(parent
, fspath
=path
, src_path
=src_path
,
73 data_expect_path
=data_expect_path
,
74 metadata_expect_path
=metadata_expect_path
,
75 support_dir_path
=support_dir_path
, name
=name
)
78 class _YamlFile(pytest
.File
):
79 def __init__(self
, parent
, fspath
, src_path
, data_expect_path
, metadata_expect_path
,
80 support_dir_path
, name
):
81 super().__init
__(parent
=parent
, fspath
=fspath
)
83 self
._src
_path
= src_path
84 self
._data
_expect
_path
= data_expect_path
85 self
._metadata
_expect
_path
= metadata_expect_path
86 self
._support
_dir
_path
= support_dir_path
90 yield _YamlItem
.from_parent(self
, name
=self
._name
, src_path
=self
._src
_path
,
91 data_expect_path
=self
._data
_expect
_path
,
92 metadata_expect_path
=self
._metadata
_expect
_path
,
93 support_dir_path
=self
._support
_dir
_path
)
96 class _YamlItem(pytest
.Item
):
97 def __init__(self
, parent
, name
, src_path
, data_expect_path
, metadata_expect_path
,
99 super().__init
__(parent
=parent
, name
=name
)
100 self
._src
_path
= src_path
101 self
._data
_expect
_path
= data_expect_path
102 self
._metadata
_expect
_path
= metadata_expect_path
103 self
._support
_dir
_path
= support_dir_path
106 # create a temporary directory
107 tmpdir
= tempfile
.TemporaryDirectory(prefix
='pytest-barectf')
109 # create barectf configuration
110 with
open(self
.fspath
) as f
:
111 cfg
= barectf
.configuration_from_file(f
)
113 # generate and write C code files
114 cg
= barectf
.CodeGenerator(cfg
)
115 files
= cg
.generate_c_headers()
116 files
+= cg
.generate_c_sources()
119 with
open(os
.path
.join(tmpdir
.name
, file.name
), 'w') as f
:
120 f
.write(file.contents
)
122 # generate metadata stream, stripping the version and date
123 file = cg
.generate_metadata_stream()
124 lines
= file.contents
.split('\n')
128 'The following code was generated',
130 'barectf_gen_date =',
139 for pattern
in discard_patterns
:
146 new_lines
.append(line
)
148 actual_metadata
= '\n'.join(new_lines
)
150 # copy Makefile to build directory
151 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'Makefile'), tmpdir
.name
)
153 # copy platform files to build directory
154 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'test-platform.c'), tmpdir
.name
)
155 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'test-platform.h'), tmpdir
.name
)
157 # copy specific source code file to build directory
158 shutil
.copy(self
._src
_path
, os
.path
.join(tmpdir
.name
, 'test.c'))
161 subprocess
.check_output(['make'], cwd
=tmpdir
.name
)
163 # run the test (produce the data stream)
164 subprocess
.check_output(['./test'], cwd
=tmpdir
.name
)
167 with
open(os
.path
.join(tmpdir
.name
, 'stream'), 'rb') as f
:
168 actual_stream
= f
.read()
170 # read data stream expectation file
171 with
open(self
._data
_expect
_path
, 'rb') as f
:
172 expected_stream
= f
.read()
174 # read metadata stream expectation file
175 with
open(self
._metadata
_expect
_path
, 'r') as f
:
176 expected_metadata
= f
.read()
179 assert actual_metadata
== expected_metadata
180 assert actual_stream
== expected_stream
182 # delete temporary directory
185 def repr_failure(self
, excinfo
, style
=None):
186 return f
'`{self.fspath}` failed: {excinfo}.'
188 def reportinfo(self
):
189 return self
.fspath
, None, self
.name
This page took 0.032553 seconds and 4 git commands to generate.