| 203 | expected_failure = license_test.expected_failure |
| 204 | |
| 205 | def closure_test_function(*args, **kwargs): |
| 206 | idx = cache.get_index() |
| 207 | matches = idx.match( |
| 208 | location=test_file, |
| 209 | min_score=0, |
| 210 | unknown_licenses=unknown_detection, |
| 211 | ) |
| 212 | if not matches: |
| 213 | matches = [] |
| 214 | |
| 215 | detected_expressions = [match.rule.license_expression for match in matches] |
| 216 | |
| 217 | # use detection as expected and dump test back |
| 218 | if regen: |
| 219 | if not expected_failure: |
| 220 | license_test.license_expressions = detected_expressions |
| 221 | license_test.dump() |
| 222 | return |
| 223 | |
| 224 | if detected_expressions != expected_expressions: |
| 225 | # On failure, we compare against more result data to get additional |
| 226 | # failure details, including the test_file and full match details |
| 227 | expected = expected_expressions + ['======================', ''] |
| 228 | results_failure_trace = ( |
| 229 | detected_expressions[:] |
| 230 | +['======================', ''] |
| 231 | ) |
| 232 | |
| 233 | for match in matches: |
| 234 | qtext, itext = get_texts(match) |
| 235 | rule_file = match.rule.rule_file() |
| 236 | results_failure_trace.extend(['', |
| 237 | '======= MATCH ====', repr(match), |
| 238 | '======= Matched Query Text for:', |
| 239 | f'file://{test_file}' |
| 240 | ]) |
| 241 | if test_data_file: |
| 242 | results_failure_trace.append(f'file://{test_data_file}') |
| 243 | |
| 244 | results_failure_trace.append('') |
| 245 | results_failure_trace.append(qtext) |
| 246 | results_failure_trace.extend(['', |
| 247 | '======= Matched Rule Text for:', |
| 248 | f'file://{rule_file}', |
| 249 | '', |
| 250 | itext, |
| 251 | ]) |
| 252 | if not matches: |
| 253 | results_failure_trace.extend(['', |
| 254 | '======= NO MATCH ====', |
| 255 | '======= Not Matched Query Text for:', |
| 256 | f'file://{test_file}' |
| 257 | ]) |
| 258 | if test_data_file: |
| 259 | results_failure_trace.append(f'file://{test_data_file}') |
| 260 | |
| 261 | # this assert will always fail and provide a detailed failure trace |
| 262 | assert '\n'.join(results_failure_trace) == '\n'.join(expected) |