Commit feb03692 authored by Matt Clarkson's avatar Matt Clarkson

Issue #8: Fix validate_is_file error messages, improve unit tests.

parent 330b17a5
Pipeline #698 passed with stages
in 13 minutes and 30 seconds
......@@ -13,16 +13,19 @@ def validate_is_file(file_name):
Check if file_name is a file.
:param file_name: string containing path name
:raises: ValueError if not
:raises: TypeError if not string, ValueError if not file
:returns: True
"""
if not file_name:
raise ValueError("Empty parameter file_name")
raise ValueError("Empty file_name.")
if not isinstance(file_name, str):
raise TypeError("file_name is not a string.")
if os.path.isfile(file_name):
return True
raise ValueError('File `' + file_name + '` is not a file')
raise ValueError('File `' + str(file_name) + '` is not a file')
def validate_is_writable_file(file_name):
......@@ -30,7 +33,7 @@ def validate_is_writable_file(file_name):
Check if file_name is a writable file.
:param file_name: string containing path name
:raises: ValueError if not
:raises: TypeError if not string, ValueError if not file
:returns: True
"""
validate_is_file(file_name)
......@@ -38,4 +41,4 @@ def validate_is_writable_file(file_name):
if os.access(file_name, os.W_OK):
return True
raise ValueError('File `' + file_name + '` is not a writeable file.')
raise ValueError('File `' + str(file_name) + '` is not a writeable file.')
......@@ -8,13 +8,31 @@ import pytest
from sksurgerycore.utilities import validate_file as f
def test_invalid_file_name_because_its_null():
with pytest.raises(ValueError):
f.validate_is_file(None)
def test_invalid_file_name_because_its_empty():
with pytest.raises(ValueError):
f.validate_is_file("")
def test_invalid_file_name_because_its_an_integer():
with pytest.raises(TypeError):
f.validate_is_file(1)
def test_invalid_file_name_because_non_existent():
with pytest.raises(ValueError):
f.validate_is_file("/invalid/file/name")
def test_invalid_file_name_because_directory():
def test_invalid_file_name_because_its_a_directory():
with pytest.raises(ValueError):
f.validate_is_file(".")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment