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
)
60 if cat
not in {'basic'} or os
.path
.basename(configs_dir
) != 'configs':
61 # not a YAML configuration test
64 # create C source, expectation file, and support directory paths
65 base_dir
= os
.path
.dirname(configs_dir
)
66 base_name
= file_name
.replace(yaml_ext
, '')
67 subcat_rel_dir
= os
.path
.join(cat
, subcat
)
68 src_path
= os
.path
.join(base_dir
, 'src', subcat_rel_dir
, f
'{base_name}.c')
69 data_expect_path
= os
.path
.join(base_dir
, 'expect', subcat_rel_dir
, f
'{base_name}.data.expect')
70 metadata_expect_path
= os
.path
.join(base_dir
, 'expect', subcat_rel_dir
,
71 f
'{base_name}.metadata.expect')
72 support_dir_path
= os
.path
.join(base_dir
, 'support', cat
)
74 # create the file node
75 return _YamlFile
.from_parent(parent
, fspath
=path
, src_path
=src_path
,
76 data_expect_path
=data_expect_path
,
77 metadata_expect_path
=metadata_expect_path
,
78 support_dir_path
=support_dir_path
,
79 name
=f
'test-{cat}-{subcat}-{base_name}')
82 class _YamlFile(pytest
.File
):
83 def __init__(self
, parent
, fspath
, src_path
, data_expect_path
, metadata_expect_path
,
84 support_dir_path
, name
):
85 super().__init
__(parent
=parent
, fspath
=fspath
)
87 self
._src
_path
= src_path
88 self
._data
_expect
_path
= data_expect_path
89 self
._metadata
_expect
_path
= metadata_expect_path
90 self
._support
_dir
_path
= support_dir_path
94 yield _YamlItem
.from_parent(self
, name
=self
._name
, src_path
=self
._src
_path
,
95 data_expect_path
=self
._data
_expect
_path
,
96 metadata_expect_path
=self
._metadata
_expect
_path
,
97 support_dir_path
=self
._support
_dir
_path
)
100 class _YamlItem(pytest
.Item
):
101 def __init__(self
, parent
, name
, src_path
, data_expect_path
, metadata_expect_path
,
103 super().__init
__(parent
=parent
, name
=name
)
104 self
._src
_path
= src_path
105 self
._data
_expect
_path
= data_expect_path
106 self
._metadata
_expect
_path
= metadata_expect_path
107 self
._support
_dir
_path
= support_dir_path
110 # create a temporary directory
111 tmpdir
= tempfile
.TemporaryDirectory(prefix
='pytest-barectf')
113 # create barectf configuration
114 with
open(self
.fspath
) as f
:
115 cfg
= barectf
.configuration_from_file(f
, inclusion_directories
=[self
._support
_dir
_path
])
117 # generate and write C code files
118 cg
= barectf
.CodeGenerator(cfg
)
119 files
= cg
.generate_c_headers()
120 files
+= cg
.generate_c_sources()
123 with
open(os
.path
.join(tmpdir
.name
, file.name
), 'w') as f
:
124 f
.write(file.contents
)
126 # generate metadata stream, stripping the version and date
127 file = cg
.generate_metadata_stream()
128 lines
= file.contents
.split('\n')
132 'The following code was generated',
134 'barectf_gen_date =',
144 for pattern
in discard_patterns
:
151 new_lines
.append(line
)
153 actual_metadata
= '\n'.join(new_lines
)
155 # copy Makefile to build directory
156 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'Makefile'), tmpdir
.name
)
158 # copy platform files to build directory
159 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'test-platform.c'), tmpdir
.name
)
160 shutil
.copy(os
.path
.join(self
._support
_dir
_path
, 'test-platform.h'), tmpdir
.name
)
162 # copy specific source code file to build directory
163 shutil
.copy(self
._src
_path
, os
.path
.join(tmpdir
.name
, 'test.c'))
166 subprocess
.check_output(['make'], cwd
=tmpdir
.name
)
168 # run the test (produce the data stream)
169 subprocess
.check_output(['./test'], cwd
=tmpdir
.name
)
172 with
open(os
.path
.join(tmpdir
.name
, 'stream'), 'rb') as f
:
173 actual_stream
= f
.read()
175 # read data stream expectation file
176 with
open(self
._data
_expect
_path
, 'rb') as f
:
177 expected_stream
= f
.read()
179 # read metadata stream expectation file
180 with
open(self
._metadata
_expect
_path
, 'r') as f
:
181 expected_metadata
= f
.read()
184 assert actual_metadata
== expected_metadata
185 assert actual_stream
== expected_stream
187 # delete temporary directory
190 def repr_failure(self
, excinfo
, style
=None):
191 return f
'`{self.fspath}` failed: {excinfo}.'
193 def reportinfo(self
):
194 return self
.fspath
, None, self
.name
This page took 0.033376 seconds and 4 git commands to generate.