Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: pretty-print JSON in snapshots #647

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ lint-fix: virtual-env-check ## Fix linting issues (ruff)
.PHONY: format
format: virtual-env-check ## Format all code (black)
black src tests
poetry run python scripts/format-json-snapshots.py

.PHONY: check-types
check-types: virtual-env-check ## Run type checks (mypy)
Expand Down
Empty file added scripts/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions scripts/format-json-snapshots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

import orjson

root_directory = "tests/unit/providers"


def pretty_print_json_file(file_path):
try:
with open(file_path, "rb") as file:
existing = file.read()
data = orjson.loads(existing)

formatted = orjson.dumps(
data,
option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS | orjson.OPT_APPEND_NEWLINE)
if formatted == existing:
return

with open(file_path, "wb") as file:
file.write(formatted)
print(f"Formatted: {file_path}")
except (OSError, orjson.JSONDecodeError) as e:
print(f"Error processing {file_path}: {e}")


def main():
for dirpath, _, filenames in os.walk(root_directory):
if "test-fixtures/snapshots" in dirpath:
for filename in filenames:
if filename.endswith(".json"):
file_path = os.path.join(dirpath, filename)
pretty_print_json_file(file_path)

print("JSON formatting complete.")
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shutil

import jsonschema
import orjson
import pytest


Expand Down Expand Up @@ -93,7 +94,11 @@ def assert_result_snapshots(self):
if not self.snapshot._snapshot_update and not os.path.exists(snapshot_abs_path):
missing_snapshot_files.append(snapshot_abs_path)
else:
self.snapshot.assert_match(f.read() + "\n", snapshot_path)
d = orjson.loads(f.read())
expected_bytes = orjson.dumps(
d, option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS
)
self.snapshot.assert_match(expected_bytes, snapshot_path)

if snapshot_abs_path in expected_files_to_test:
expected_files_to_test.remove(snapshot_abs_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2018-1071","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zsh","Version":"5.4.2-r1"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2018-1071","Description":"","Metadata":{},"Name":"CVE-2018-1071","CVSS":[]}}}
{
"identifier": "3.15/cve-2018-1071",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zsh",
"NamespaceName": "alpine:3.15",
"Version": "5.4.2-r1",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2018-1071",
"Metadata": {},
"Name": "CVE-2018-1071",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2018-1083","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zsh","Version":"5.4.2-r1"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2018-1083","Description":"","Metadata":{},"Name":"CVE-2018-1083","CVSS":[]}}}
{
"identifier": "3.15/cve-2018-1083",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zsh",
"NamespaceName": "alpine:3.15",
"Version": "5.4.2-r1",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2018-1083",
"Metadata": {},
"Name": "CVE-2018-1083",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2018-25032","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zlib","Version":"1.2.12-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2018-25032","Description":"","Metadata":{},"Name":"CVE-2018-25032","CVSS":[]}}}
{
"identifier": "3.15/cve-2018-25032",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zlib",
"NamespaceName": "alpine:3.15",
"Version": "1.2.12-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2018-25032",
"Metadata": {},
"Name": "CVE-2018-25032",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2019-11922","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zstd","Version":"1.3.8-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2019-11922","Description":"","Metadata":{},"Name":"CVE-2019-11922","CVSS":[]}}}
{
"identifier": "3.15/cve-2019-11922",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zstd",
"NamespaceName": "alpine:3.15",
"Version": "1.3.8-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2019-11922",
"Metadata": {},
"Name": "CVE-2019-11922",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2019-13132","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zeromq","Version":"4.3.2-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2019-13132","Description":"","Metadata":{},"Name":"CVE-2019-13132","CVSS":[]}}}
{
"identifier": "3.15/cve-2019-13132",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zeromq",
"NamespaceName": "alpine:3.15",
"Version": "4.3.2-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2019-13132",
"Metadata": {},
"Name": "CVE-2019-13132",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2019-20044","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zsh","Version":"5.8-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2019-20044","Description":"","Metadata":{},"Name":"CVE-2019-20044","CVSS":[]}}}
{
"identifier": "3.15/cve-2019-20044",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zsh",
"NamespaceName": "alpine:3.15",
"Version": "5.8-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2019-20044",
"Metadata": {},
"Name": "CVE-2019-20044",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2019-6250","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zeromq","Version":"4.3.1-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2019-6250","Description":"","Metadata":{},"Name":"CVE-2019-6250","CVSS":[]}}}
{
"identifier": "3.15/cve-2019-6250",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zeromq",
"NamespaceName": "alpine:3.15",
"Version": "4.3.1-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2019-6250",
"Metadata": {},
"Name": "CVE-2019-6250",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2019-9210","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"advancecomp","Version":"2.1-r2"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2019-9210","Description":"","Metadata":{},"Name":"CVE-2019-9210","CVSS":[]}}}
{
"identifier": "3.15/cve-2019-9210",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "advancecomp",
"NamespaceName": "alpine:3.15",
"Version": "2.1-r2",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2019-9210",
"Metadata": {},
"Name": "CVE-2019-9210",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2020-14929","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"alpine","Version":"2.23-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2020-14929","Description":"","Metadata":{},"Name":"CVE-2020-14929","CVSS":[]}}}
{
"identifier": "3.15/cve-2020-14929",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "alpine",
"NamespaceName": "alpine:3.15",
"Version": "2.23-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2020-14929",
"Metadata": {},
"Name": "CVE-2020-14929",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2020-15166","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zeromq","Version":"4.3.3-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2020-15166","Description":"","Metadata":{},"Name":"CVE-2020-15166","CVSS":[]}}}
{
"identifier": "3.15/cve-2020-15166",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zeromq",
"NamespaceName": "alpine:3.15",
"Version": "4.3.3-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2020-15166",
"Metadata": {},
"Name": "CVE-2020-15166",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2021-24031","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zstd","Version":"1.4.1-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2021-24031","Description":"","Metadata":{},"Name":"CVE-2021-24031","CVSS":[]}}}
{
"identifier": "3.15/cve-2021-24031",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zstd",
"NamespaceName": "alpine:3.15",
"Version": "1.4.1-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2021-24031",
"Metadata": {},
"Name": "CVE-2021-24031",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2021-24032","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"zstd","Version":"1.4.9-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2021-24032","Description":"","Metadata":{},"Name":"CVE-2021-24032","CVSS":[]}}}
{
"identifier": "3.15/cve-2021-24032",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "zstd",
"NamespaceName": "alpine:3.15",
"Version": "1.4.9-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2021-24032",
"Metadata": {},
"Name": "CVE-2021-24032",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
{"schema":"https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json","identifier":"3.15/cve-2021-38370","item":{"Vulnerability":{"Severity":"Unknown","NamespaceName":"alpine:3.15","FixedIn":[{"VersionFormat":"apk","NamespaceName":"alpine:3.15","Name":"alpine","Version":"2.25-r0"}],"Link":"https://www.cve.org/CVERecord?id=CVE-2021-38370","Description":"","Metadata":{},"Name":"CVE-2021-38370","CVSS":[]}}}
{
"identifier": "3.15/cve-2021-38370",
"item": {
"Vulnerability": {
"CVSS": [],
"Description": "",
"FixedIn": [
{
"Name": "alpine",
"NamespaceName": "alpine:3.15",
"Version": "2.25-r0",
"VersionFormat": "apk"
}
],
"Link": "https://www.cve.org/CVERecord?id=CVE-2021-38370",
"Metadata": {},
"Name": "CVE-2021-38370",
"NamespaceName": "alpine:3.15",
"Severity": "Unknown"
}
},
"schema": "https://raw.githubusercontent.com/anchore/vunnel/main/schema/vulnerability/os/schema-1.0.0.json"
}
Loading
Loading