1 # SPDX-License-Identifier: GPL-2.0 2 # 3 # Copyright (C) 2018 Masahiro Yamada <yamada.ma 4 # 5 6 """ 7 Kconfig unit testing framework. 8 9 This provides fixture functions commonly used 10 """ 11 12 import os 13 import pytest 14 import shutil 15 import subprocess 16 import tempfile 17 18 CONF_PATH = os.path.abspath(os.path.join('scri 19 20 21 class Conf: 22 """Kconfig runner and result checker. 23 24 This class provides methods to run text-ba 25 (scripts/kconfig/conf) and retrieve the re 26 stdout, and stderr. It also provides meth 27 results with expectations. 28 """ 29 30 def __init__(self, request): 31 """Create a new Conf instance. 32 33 request: object to introspect the requ 34 """ 35 # the directory of the test being run 36 self._test_dir = os.path.dirname(str(r 37 38 # runners 39 def _run_conf(self, mode, dot_config=None, 40 interactive=False, in_keys=N 41 """Run text-based Kconfig executable a 42 43 mode: input mode option (--oldaskconfi 44 dot_config: .config file to use for co 45 out_file: file name to contain the out 46 interactive: flag to specify the inter 47 in_keys: key inputs for interactive mo 48 extra_env: additional environments 49 returncode: exit status of the Kconfig 50 """ 51 command = [CONF_PATH, mode, 'Kconfig'] 52 53 # Override 'srctree' environment to ma 54 extra_env['srctree'] = self._test_dir 55 56 # Clear KCONFIG_DEFCONFIG_LIST to keep 57 # by the user's environment. 58 extra_env['KCONFIG_DEFCONFIG_LIST'] = 59 60 # Run Kconfig in a temporary directory 61 # This directory is automatically remo 62 with tempfile.TemporaryDirectory() as 63 64 # if .config is given, copy it to 65 if dot_config: 66 shutil.copyfile(os.path.join(s 67 os.path.join(t 68 69 ps = subprocess.Popen(command, 70 stdin=subpro 71 stdout=subpr 72 stderr=subpr 73 cwd=temp_dir 74 env=dict(os. 75 76 # If input key sequence is given, 77 if in_keys: 78 ps.stdin.write(in_keys.encode( 79 80 while ps.poll() is None: 81 # For interactive modes such a 82 # send 'Enter' key until the p 83 if interactive: 84 ps.stdin.write(b'\n') 85 86 self.retcode = ps.returncode 87 self.stdout = ps.stdout.read().dec 88 self.stderr = ps.stderr.read().dec 89 90 # Retrieve the resulted config dat 91 # to exist. If the command fails, 92 # 'listnewconfig' does not produce 93 if self.retcode == 0 and out_file: 94 with open(os.path.join(temp_di 95 self.config = f.read() 96 else: 97 self.config = None 98 99 # Logging: 100 # Pytest captures the following inform 101 # of tests, the captured log will be d 102 # figure out what has happened. 103 104 print("[command]\n{}\n".format(' '.joi 105 106 print("[retcode]\n{}\n".format(self.re 107 108 print("[stdout]") 109 print(self.stdout) 110 111 print("[stderr]") 112 print(self.stderr) 113 114 if self.config is not None: 115 print("[output for '{}']".format(o 116 print(self.config) 117 118 return self.retcode 119 120 def oldaskconfig(self, dot_config=None, in 121 """Run oldaskconfig. 122 123 dot_config: .config file to use for co 124 in_key: key inputs (optional) 125 returncode: exit status of the Kconfig 126 """ 127 return self._run_conf('--oldaskconfig' 128 interactive=True 129 130 def oldconfig(self, dot_config=None, in_ke 131 """Run oldconfig. 132 133 dot_config: .config file to use for co 134 in_key: key inputs (optional) 135 returncode: exit status of the Kconfig 136 """ 137 return self._run_conf('--oldconfig', d 138 interactive=True 139 140 def olddefconfig(self, dot_config=None): 141 """Run olddefconfig. 142 143 dot_config: .config file to use for co 144 returncode: exit status of the Kconfig 145 """ 146 return self._run_conf('--olddefconfig' 147 148 def defconfig(self, defconfig): 149 """Run defconfig. 150 151 defconfig: defconfig file for input 152 returncode: exit status of the Kconfig 153 """ 154 defconfig_path = os.path.join(self._te 155 return self._run_conf('--defconfig={}' 156 157 def _allconfig(self, mode, all_config, ext 158 if all_config: 159 all_config_path = os.path.join(sel 160 extra_env['KCONFIG_ALLCONFIG'] = a 161 162 return self._run_conf('--{}config'.for 163 164 def allyesconfig(self, all_config=None): 165 """Run allyesconfig. 166 167 all_config: fragment config file for K 168 returncode: exit status of the Kconfig 169 """ 170 return self._allconfig('allyes', all_c 171 172 def allmodconfig(self, all_config=None): 173 """Run allmodconfig. 174 175 all_config: fragment config file for K 176 returncode: exit status of the Kconfig 177 """ 178 return self._allconfig('allmod', all_c 179 180 def allnoconfig(self, all_config=None): 181 """Run allnoconfig. 182 183 all_config: fragment config file for K 184 returncode: exit status of the Kconfig 185 """ 186 return self._allconfig('allno', all_co 187 188 def alldefconfig(self, all_config=None): 189 """Run alldefconfig. 190 191 all_config: fragment config file for K 192 returncode: exit status of the Kconfig 193 """ 194 return self._allconfig('alldef', all_c 195 196 def randconfig(self, all_config=None, seed 197 """Run randconfig. 198 199 all_config: fragment config file for K 200 seed: the seed for randconfig (optiona 201 returncode: exit status of the Kconfig 202 """ 203 if seed is not None: 204 extra_env = {'KCONFIG_SEED': hex(s 205 else: 206 extra_env = {} 207 208 return self._allconfig('rand', all_con 209 210 def savedefconfig(self, dot_config): 211 """Run savedefconfig. 212 213 dot_config: .config file for input 214 returncode: exit status of the Kconfig 215 """ 216 return self._run_conf('--savedefconfig 217 218 def listnewconfig(self, dot_config=None): 219 """Run listnewconfig. 220 221 dot_config: .config file to use for co 222 returncode: exit status of the Kconfig 223 """ 224 return self._run_conf('--listnewconfig 225 out_file=None) 226 227 # checkers 228 def _read_and_compare(self, compare, expec 229 """Compare the result with expectation 230 231 compare: function to compare the resul 232 expected: file that contains the expec 233 """ 234 with open(os.path.join(self._test_dir, 235 expected_data = f.read() 236 return compare(self, expected_data) 237 238 def _contains(self, attr, expected): 239 return self._read_and_compare( 240 lambda s, 241 expected) 242 243 def _matches(self, attr, expected): 244 return self._read_and_compare(lambda s 245 expected 246 247 def config_contains(self, expected): 248 """Check if resulted configuration con 249 250 expected: file that contains the expec 251 returncode: True if result contains th 252 """ 253 return self._contains('config', expect 254 255 def config_matches(self, expected): 256 """Check if resulted configuration exa 257 258 expected: file that contains the expec 259 returncode: True if result matches the 260 """ 261 return self._matches('config', expecte 262 263 def stdout_contains(self, expected): 264 """Check if resulted stdout contains e 265 266 expected: file that contains the expec 267 returncode: True if result contains th 268 """ 269 return self._contains('stdout', expect 270 271 def stdout_matches(self, expected): 272 """Check if resulted stdout exactly ma 273 274 expected: file that contains the expec 275 returncode: True if result matches the 276 """ 277 return self._matches('stdout', expecte 278 279 def stderr_contains(self, expected): 280 """Check if resulted stderr contains e 281 282 expected: file that contains the expec 283 returncode: True if result contains th 284 """ 285 return self._contains('stderr', expect 286 287 def stderr_matches(self, expected): 288 """Check if resulted stderr exactly ma 289 290 expected: file that contains the expec 291 returncode: True if result matches the 292 """ 293 return self._matches('stderr', expecte 294 295 296 @pytest.fixture(scope="module") 297 def conf(request): 298 """Create a Conf instance and provide it t 299 return Conf(request)
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.