Check the scan `result_file` JSON results against the `expected_file` expected JSON results. If `regen` is True the expected_file WILL BE overwritten with the new scan results from `results_file`. This is convenient for updating tests expectations. But use with caution. If
(
expected_file,
result_file,
regen=False,
remove_file_date=False,
check_headers=False,
remove_uuid=True,
)
| 168 | |
| 169 | |
| 170 | def check_json_scan( |
| 171 | expected_file, |
| 172 | result_file, |
| 173 | regen=False, |
| 174 | remove_file_date=False, |
| 175 | check_headers=False, |
| 176 | remove_uuid=True, |
| 177 | ): |
| 178 | """ |
| 179 | Check the scan `result_file` JSON results against the `expected_file` |
| 180 | expected JSON results. |
| 181 | |
| 182 | If `regen` is True the expected_file WILL BE overwritten with the new scan |
| 183 | results from `results_file`. This is convenient for updating tests |
| 184 | expectations. But use with caution. |
| 185 | |
| 186 | If `remove_file_date` is True, the file.date attribute is removed. |
| 187 | If `check_headers` is True, the scan headers attribute is not removed. |
| 188 | If `remove_uuid` is True, removes UUID from Package and Dependency. |
| 189 | and if also `regen` is True then regenerate expected file with old UUIDs present already. |
| 190 | """ |
| 191 | results = load_json_result(location=result_file, remove_file_date=remove_file_date) |
| 192 | if remove_uuid: |
| 193 | results = remove_uuid_from_scan(results) |
| 194 | |
| 195 | if not check_headers: |
| 196 | results.pop('headers', None) |
| 197 | |
| 198 | if regen: |
| 199 | with open(expected_file, 'w') as reg: |
| 200 | json.dump(results, reg, indent=2, separators=(',', ': ')) |
| 201 | expected = results |
| 202 | else: |
| 203 | expected = load_json_result(location=expected_file, remove_file_date=remove_file_date) |
| 204 | if remove_uuid: |
| 205 | expected = remove_uuid_from_scan(expected) |
| 206 | if not check_headers: |
| 207 | expected.pop('headers', None) |
| 208 | |
| 209 | # NOTE we redump the JSON as a YAML string for easier display of |
| 210 | # the failures comparison/diff |
| 211 | if results != expected: |
| 212 | expected = saneyaml.dump(expected) |
| 213 | results = saneyaml.dump(results) |
| 214 | assert results == expected |
| 215 | |
| 216 | |
| 217 | def remove_uuid_from_scan(results): |
no test coverage detected