Add static array tracing tests
[barectf.git] / tests / tracing / conftest.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
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.
23
24 import pytest
25 import os
26 import os.path
27 import barectf
28 import shutil
29 import subprocess
30
31
32 @pytest.fixture
33 def tracing_succeed_test(yaml_cfg_path, request, tmpdir):
34 def func():
35 test_dir = os.path.dirname(request.fspath)
36
37 # Use the test's module and function names to automatically find
38 # the test-specific expectation files.
39 #
40 # For:
41 #
42 # Test module name:
43 # `test_succeed_hello_there.py`
44 #
45 # Test function name:
46 # `test_how_are_you`
47 #
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('_', '-')
60 elems.append(func)
61 expect_base_path = os.path.join(*elems)
62
63 # Use the test's module and function names to automatically find
64 # the test-specific C source file.
65 #
66 # For:
67 #
68 # Test module name:
69 # `test_succeed_hello_there.py`
70 #
71 # Test function name:
72 # `test_how_are_you`
73 #
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)
88
89 # create barectf configuration
90 with open(yaml_cfg_path) as f:
91 cfg = barectf.configuration_from_file(f)
92
93 # generate and write C code files
94 cg = barectf.CodeGenerator(cfg)
95 files = cg.generate_c_headers()
96 files += cg.generate_c_sources()
97
98 for file in files:
99 with open(os.path.join(tmpdir, file.name), 'w') as f:
100 f.write(file.contents)
101
102 # generate metadata stream, stripping the version and date
103 file = cg.generate_metadata_stream()
104 lines = file.contents.split('\n')
105 new_lines = []
106 discard_patterns = [
107 'Copyright (c)',
108 'The following code was generated',
109 '* on ',
110 'barectf_gen_date =',
111 'tracer_major =',
112 'tracer_minor =',
113 'tracer_patch =',
114 ]
115
116 for line in lines:
117 skip = False
118
119 for pattern in discard_patterns:
120 if pattern in line:
121 skip = True
122
123 if skip:
124 continue
125
126 new_lines.append(line)
127
128 actual_metadata = '\n'.join(new_lines)
129
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)
133
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)
137
138 # copy specific source code file to build directory
139 shutil.copy(src_path, os.path.join(tmpdir, 'test.c'))
140
141 # build the test
142 subprocess.check_output(['make'], cwd=tmpdir)
143
144 # run the test (produce the data stream)
145 subprocess.check_output(['./test'], cwd=tmpdir)
146
147 # read actual stream
148 with open(os.path.join(tmpdir, 'stream'), 'rb') as f:
149 actual_stream = f.read()
150
151 # read data stream expectation file
152 with open(f'{expect_base_path}.data.expect', 'rb') as f:
153 expected_stream = f.read()
154
155 # read metadata stream expectation file
156 with open(f'{expect_base_path}.metadata.expect', 'r') as f:
157 expected_metadata = f.read()
158
159 # validate streams
160 assert actual_metadata == expected_metadata
161 assert actual_stream == expected_stream
162
163 return func
This page took 0.058191 seconds and 4 git commands to generate.