2024-04-05 21:23:50 +02:00
|
|
|
import pytest
|
2023-12-20 18:17:41 +01:00
|
|
|
|
|
|
|
from borgmatic.actions import json as module
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_json_loads_json_from_string():
|
|
|
|
assert module.parse_json('{"repository": {"id": "foo"}}', label=None) == {
|
|
|
|
'repository': {'id': 'foo', 'label': ''}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-05 21:23:50 +02:00
|
|
|
def test_parse_json_skips_non_json_warnings_and_loads_subsequent_json():
|
|
|
|
assert module.parse_json(
|
|
|
|
'/non/existent/path: stat: [Errno 2] No such file or directory: /non/existent/path\n{"repository":\n{"id": "foo"}}',
|
|
|
|
label=None,
|
|
|
|
) == {'repository': {'id': 'foo', 'label': ''}}
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_json_skips_with_invalid_json_raises():
|
|
|
|
with pytest.raises(module.json.JSONDecodeError):
|
|
|
|
module.parse_json('this is not valid JSON }', label=None)
|
2023-12-20 18:17:41 +01:00
|
|
|
|
2024-04-05 21:23:50 +02:00
|
|
|
|
|
|
|
def test_parse_json_injects_label_into_parsed_data():
|
2023-12-20 18:17:41 +01:00
|
|
|
assert module.parse_json('{"repository": {"id": "foo"}}', label='bar') == {
|
|
|
|
'repository': {'id': 'foo', 'label': 'bar'}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_json_injects_nothing_when_repository_missing():
|
|
|
|
assert module.parse_json('{"stuff": {"id": "foo"}}', label='bar') == {'stuff': {'id': 'foo'}}
|