From c61d63b235f6e2bcd59a88baea85012704453983 Mon Sep 17 00:00:00 2001 From: Felix Kaechele Date: Sun, 4 Jun 2023 00:50:35 -0400 Subject: [PATCH] Use open() to test for file existance and readability Signed-off-by: Felix Kaechele --- borgmatic/config/validate.py | 4 +--- tests/unit/config/test_validate.py | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/borgmatic/config/validate.py b/borgmatic/config/validate.py index 1917eea..5835ead 100644 --- a/borgmatic/config/validate.py +++ b/borgmatic/config/validate.py @@ -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): ''' diff --git a/tests/unit/config/test_validate.py b/tests/unit/config/test_validate.py index fdb13b6..701ba51 100644 --- a/tests/unit/config/test_validate.py +++ b/tests/unit/config/test_validate.py @@ -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():