mirror of
https://github.com/borgbackup/borg.git
synced 2026-07-15 13:06:55 -04:00
json: support BORG_JSON_INDENT env var for JSON output formatting
Some checks are pending
CI / lint (push) Waiting to run
CI / asan_ubsan (push) Blocked by required conditions
CI / native_tests (push) Blocked by required conditions
CI / vm_tests (Haiku, false, haiku, r1beta5) (push) Blocked by required conditions
CI / vm_tests (NetBSD, false, netbsd, 10.1) (push) Blocked by required conditions
CI / vm_tests (OpenBSD, false, openbsd, 7.7) (push) Blocked by required conditions
CI / vm_tests (borg-freebsd-14-x86_64-gh, FreeBSD, true, freebsd, 14.3) (push) Blocked by required conditions
CodeQL / Analyze (push) Waiting to run
Windows CI / msys2-ucrt64 (push) Waiting to run
Some checks are pending
CI / lint (push) Waiting to run
CI / asan_ubsan (push) Blocked by required conditions
CI / native_tests (push) Blocked by required conditions
CI / vm_tests (Haiku, false, haiku, r1beta5) (push) Blocked by required conditions
CI / vm_tests (NetBSD, false, netbsd, 10.1) (push) Blocked by required conditions
CI / vm_tests (OpenBSD, false, openbsd, 7.7) (push) Blocked by required conditions
CI / vm_tests (borg-freebsd-14-x86_64-gh, FreeBSD, true, freebsd, 14.3) (push) Blocked by required conditions
CodeQL / Analyze (push) Waiting to run
Windows CI / msys2-ucrt64 (push) Waiting to run
This commit is contained in:
parent
8d715e030b
commit
0ee07f0cda
2 changed files with 45 additions and 1 deletions
|
|
@ -1039,9 +1039,25 @@ def basic_json_data(manifest, *, cache=None, extra=None):
|
|||
return data
|
||||
|
||||
|
||||
def _json_indent_from_env():
|
||||
"""Parse BORG_JSON_INDENT env var for json.dumps indent parameter."""
|
||||
value = os.environ.get("BORG_JSON_INDENT")
|
||||
if value is None:
|
||||
return 4
|
||||
if value.lower() == "none":
|
||||
return None
|
||||
if value == "":
|
||||
return ""
|
||||
try:
|
||||
return int(value)
|
||||
except ValueError:
|
||||
return value
|
||||
|
||||
|
||||
def json_dump(obj):
|
||||
"""Dump using BorgJSONEncoder."""
|
||||
return json.dumps(obj, sort_keys=True, indent=4, cls=BorgJsonEncoder)
|
||||
indent = _json_indent_from_env()
|
||||
return json.dumps(obj, sort_keys=True, indent=indent, cls=BorgJsonEncoder)
|
||||
|
||||
|
||||
def json_print(obj):
|
||||
|
|
|
|||
|
|
@ -1239,3 +1239,31 @@ def test_ec_invalid():
|
|||
))
|
||||
def test_max_ec(ec1, ec2, ec_max):
|
||||
assert max_ec(ec1, ec2) == ec_max
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'env_value, expect_newlines',
|
||||
[
|
||||
(None, True),
|
||||
('none', False),
|
||||
('0', True),
|
||||
('4', True),
|
||||
('', True),
|
||||
('\t', True),
|
||||
],
|
||||
)
|
||||
def test_json_dump_indent(monkeypatch, env_value, expect_newlines):
|
||||
from ..helpers import json_dump
|
||||
import json
|
||||
|
||||
obj = {'key': 'value', 'number': 42}
|
||||
if env_value is not None:
|
||||
monkeypatch.setenv('BORG_JSON_INDENT', env_value)
|
||||
else:
|
||||
monkeypatch.delenv('BORG_JSON_INDENT', raising=False)
|
||||
|
||||
result = json_dump(obj)
|
||||
if expect_newlines:
|
||||
assert '\n' in result
|
||||
else:
|
||||
assert '\n' not in result
|
||||
assert json.loads(result) == obj
|
||||
|
|
|
|||
Loading…
Reference in a new issue