소스 검색

Use open() to test for file existance and readability

Signed-off-by: Felix Kaechele <felix@kaechele.ca>
Felix Kaechele 2 년 전
부모
커밋
c61d63b235
2개의 변경된 파일22개의 추가작업 그리고 4개의 파일을 삭제
  1. 1 3
      borgmatic/config/validate.py
  2. 21 1
      tests/unit/config/test_validate.py

+ 1 - 3
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):
     '''

+ 21 - 1
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():