Skip to content

Commit dffa814

Browse files
unittest_config_checks debugging facility
Set the environment variable `UNITTEST_CONFIG_CHECKS_DEBUG` to dump the preprocessor output to a file if a test case fails. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent 25b4587 commit dffa814

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

scripts/mbedtls_framework/unittest_config_checks.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ class TestConfigChecks(unittest.TestCase):
3333
# to say.
3434
maxDiff = 9999
3535

36+
def setUp(self) -> None:
37+
self.cpp_output = None #type: Optional[str]
38+
39+
def tearDown(self) -> None:
40+
"""Log the preprocessor output to a file, if available and desired.
41+
42+
This is intended for debugging. It only happens if the environment
43+
variable UNITTEST_CONFIG_CHECKS_DEBUG is non-empty.
44+
"""
45+
if os.getenv('UNITTEST_CONFIG_CHECKS_DEBUG'):
46+
if self.cpp_output is not None:
47+
basename = os.path.splitext(os.path.basename(sys.argv[0]))[0]
48+
filename = f'{basename}.{self._testMethodName}.out.txt'
49+
with open(filename, 'w') as out:
50+
out.write(self.cpp_output)
51+
3652
def user_config_file_name(self, variant: str) -> str:
3753
"""Construct a unique temporary file name for a user config header."""
3854
name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
@@ -69,6 +85,8 @@ def run_with_config_files(self,
6985
stdout and stderr.
7086
"""
7187
cmd = ['cpp']
88+
if os.getenv('UNITTEST_CONFIG_CHECKS_DEBUG'):
89+
cmd += ['-dD']
7290
if crypto_user_config_file is not None:
7391
cmd.append(f'-DTF_PSA_CRYPTO_USER_CONFIG_FILE="{crypto_user_config_file}"')
7492
if mbedtls_user_config_file is not None:
@@ -135,8 +153,10 @@ def good_case(self,
135153
"""
136154
cp = self.run_with_config(crypto_user_config, mbedtls_user_config,
137155
extra_options=extra_options)
156+
self.cpp_output = cp.stdout
138157
self.assertEqual(cp.stderr, '')
139158
self.assertEqual(cp.returncode, 0)
159+
self.cpp_output = None
140160

141161
def bad_case(self,
142162
crypto_user_config: Optional[str],
@@ -152,10 +172,12 @@ def bad_case(self,
152172
"""
153173
cp = self.run_with_config(crypto_user_config, mbedtls_user_config,
154174
extra_options=extra_options)
175+
self.cpp_output = cp.stdout
155176
if error is not None:
156177
self.assertRegex(cp.stderr, error)
157178
self.assertGreater(cp.returncode, 0)
158179
self.assertLess(cp.returncode, 126)
180+
self.cpp_output = None
159181

160182
def test_nominal(self) -> None:
161183
self.good_case(None)

0 commit comments

Comments
 (0)