Skip to content

Commit

Permalink
Fix error loading configuration schema on Fedora Linux (#703).
Browse files Browse the repository at this point in the history
  • Loading branch information
witten committed Jun 4, 2023
2 parents caf6543 + c61d63b commit b3f7043
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
19 changes: 4 additions & 15 deletions borgmatic/config/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import jsonschema
import ruamel.yaml

try:
import importlib_metadata
except ModuleNotFoundError: # pragma: nocover
import importlib.metadata as importlib_metadata

import borgmatic.config
from borgmatic.config import environment, load, normalize, override

Expand All @@ -19,16 +14,10 @@ def schema_filename():
Raise FileNotFoundError when the schema path does not exist.
'''
try:
return next(
str(path.locate())
for path in importlib_metadata.files('borgmatic')
if path.match('config/schema.yaml')
)
except StopIteration:
# If the schema wasn't found in the package's files, this is probably a pip editable
# install, so try a different approach to get the schema.
return os.path.join(os.path.dirname(borgmatic.config.__file__), 'schema.yaml')
schema_path = os.path.join(os.path.dirname(borgmatic.config.__file__), 'schema.yaml')

with open(schema_path):
return schema_path


def format_json_error_path_element(path_element):
Expand Down
27 changes: 15 additions & 12 deletions tests/unit/config/test_validate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
import sys
from io import StringIO

import pytest
from flexmock import flexmock

Expand All @@ -7,22 +11,21 @@
def test_schema_filename_finds_schema_path():
schema_path = '/var/borgmatic/config/schema.yaml'

flexmock(module.importlib_metadata).should_receive('files').and_return(
flexmock(match=lambda path: False, locate=lambda: None),
flexmock(match=lambda path: True, locate=lambda: schema_path),
flexmock(match=lambda path: False, locate=lambda: None),
)

flexmock(os.path).should_receive('dirname').and_return("/var/borgmatic/config")
builtins = flexmock(sys.modules['builtins'])
builtins.should_receive('open').with_args(schema_path).and_return(StringIO())
assert module.schema_filename() == schema_path


def test_schema_filename_with_missing_schema_path_in_package_still_finds_it_in_config_directory():
flexmock(module.importlib_metadata).should_receive('files').and_return(
flexmock(match=lambda path: False, locate=lambda: None),
flexmock(match=lambda path: False, locate=lambda: None),
)
def test_schema_filename_raises_filenotfounderror():
schema_path = '/var/borgmatic/config/schema.yaml'

flexmock(os.path).should_receive('dirname').and_return("/var/borgmatic/config")
builtins = flexmock(sys.modules['builtins'])
builtins.should_receive('open').with_args(schema_path).and_raise(FileNotFoundError)

assert module.schema_filename().endswith('/borgmatic/config/schema.yaml')
with pytest.raises(FileNotFoundError):
module.schema_filename()


def test_format_json_error_path_element_formats_array_index():
Expand Down

0 comments on commit b3f7043

Please sign in to comment.