Use open() to test for file existance and readability

Signed-off-by: Felix Kaechele <felix@kaechele.ca>
This commit is contained in:
Felix Kaechele 2023-06-04 00:50:35 -04:00
parent ba0899660d
commit c61d63b235
2 changed files with 22 additions and 4 deletions

View File

@ -16,11 +16,9 @@ def schema_filename():
'''
schema_path = os.path.join(os.path.dirname(borgmatic.config.__file__), 'schema.yaml')
if os.path.exists(schema_path) and os.path.isfile(schema_path):
with open(schema_path):
return schema_path
raise FileNotFoundError
def format_json_error_path_element(path_element):
'''

View File

@ -1,3 +1,7 @@
import os
import sys
from io import StringIO
import pytest
from flexmock import flexmock
@ -5,7 +9,23 @@ from borgmatic.config import validate as module
def test_schema_filename_finds_schema_path():
module.schema_filename().endswith('/borgmatic/config/schema.yaml')
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_return(StringIO())
assert module.schema_filename() == schema_path
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)
with pytest.raises(FileNotFoundError):
module.schema_filename()
def test_format_json_error_path_element_formats_array_index():