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 tracing_succeed_test(yaml_cfg_path
, request
, tmpdir
):
35 test_dir
= os
.path
.dirname(request
.fspath
)
37 # Use the test's module and function names to automatically find
38 # the test-specific expectation files.
43 # `test_succeed_hello_there.py`
48 # The corresponding base expectation file path is
49 # `expect/succeed/hello-there/how-are-you'.
50 elems
= [test_dir
, 'expect']
51 mod
= request
.module
.__name
__
52 mod
= mod
.replace('test_', '')
53 mod
= mod
.replace('_', '-')
54 parts
= mod
.split('-')
55 elems
.append(parts
[0])
56 elems
.append('-'.join(parts
[1:]))
57 func
= request
.function
.__name
__
58 func
= func
.replace('test_', '')
59 func
= func
.replace('_', '-')
61 expect_base_path
= os
.path
.join(*elems
)
63 # Use the test's module and function names to automatically find
64 # the test-specific C source file.
69 # `test_succeed_hello_there.py`
74 # The corresponding expectation file path is
75 # `src/succeed/hello-there/how-are-you.c'.
76 elems
= [test_dir
, 'src']
77 mod
= request
.module
.__name
__
78 mod
= mod
.replace('test_', '')
79 mod
= mod
.replace('_', '-')
80 parts
= mod
.split('-')
81 elems
.append(parts
[0])
82 elems
.append('-'.join(parts
[1:]))
83 func
= request
.function
.__name
__
84 func
= func
.replace('test_', '')
85 func
= func
.replace('_', '-')
86 elems
.append(f
'{func}.c')
87 src_path
= os
.path
.join(*elems
)
89 # create barectf configuration
90 with
open(yaml_cfg_path
) as f
:
91 cfg
= barectf
.configuration_from_file(f
)
93 # generate and write C code files
94 cg
= barectf
.CodeGenerator(cfg
)
95 files
= cg
.generate_c_headers()
96 files
+= cg
.generate_c_sources()
99 with
open(os
.path
.join(tmpdir
, file.name
), 'w') as f
:
100 f
.write(file.contents
)
102 # generate metadata stream, stripping the version and date
103 file = cg
.generate_metadata_stream()
104 lines
= file.contents
.split('\n')
108 'The following code was generated',
110 'barectf_gen_date =',
119 for pattern
in discard_patterns
:
126 new_lines
.append(line
)
128 actual_metadata
= '\n'.join(new_lines
)
130 # copy Makefile to build directory
131 support_dir
= os
.path
.join(test_dir
, 'support')
132 shutil
.copy(os
.path
.join(support_dir
, 'Makefile'), tmpdir
)
134 # copy platform files to build directory
135 shutil
.copy(os
.path
.join(support_dir
, 'test-platform.c'), tmpdir
)
136 shutil
.copy(os
.path
.join(support_dir
, 'test-platform.h'), tmpdir
)
138 # copy specific source code file to build directory
139 shutil
.copy(src_path
, os
.path
.join(tmpdir
, 'test.c'))
142 subprocess
.check_output(['make'], cwd
=tmpdir
)
144 # run the test (produce the data stream)
145 subprocess
.check_output(['./test'], cwd
=tmpdir
)
148 with
open(os
.path
.join(tmpdir
, 'stream'), 'rb') as f
:
149 actual_stream
= f
.read()
151 # read data stream expectation file
152 with
open(f
'{expect_base_path}.data.expect', 'rb') as f
:
153 expected_stream
= f
.read()
155 # read metadata stream expectation file
156 with
open(f
'{expect_base_path}.metadata.expect', 'r') as f
:
157 expected_metadata
= f
.read()
160 assert actual_metadata
== expected_metadata
161 assert actual_stream
== expected_stream
This page took 0.058191 seconds and 4 git commands to generate.