Skip to content

Commit

Permalink
Fix path dependency in convert_to_path test (#749)
Browse files Browse the repository at this point in the history
* fix third-case to be correct with new conversion logic

* Test the value of the result, implicitly the type

* Test a file search

* Use a more obviously bad path

* udpate to be inclusive of file checking

* Add more context in the docstring

---------

Co-authored-by: Rafael M Mudafort <[email protected]>
Co-authored-by: Rafael M Mudafort <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2023
1 parent e1dd6b7 commit 599f226
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
14 changes: 9 additions & 5 deletions floris/type_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ def iter_validator(iter_type, item_types: Union[Any, Tuple[Any]]) -> Callable:
return validator

def convert_to_path(fn: str | Path) -> Path:
"""Converts an input string or pathlib.Path object to a fully resolved ``pathlib.Path``
object.
"""
Converts an input string or ``pathlib.Path`` object to a fully resolved ``pathlib.Path``
object. If the input is a string, it is converted to a pathlib.Path object.
The function then checks if the path exists as an absolute path, a relative path from
the script, or a relative path from the system location. If the path does not exist in
any of these locations, a FileExistsError is raised.
Args:
fn (str | Path): The user input file path or file name.
Expand All @@ -113,11 +117,11 @@ def convert_to_path(fn: str | Path) -> Path:
absolute_fn = fn.resolve()
relative_fn_script = (base_fn_script / fn).resolve()
relative_fn_sys = (base_fn_sys / fn).resolve()
if absolute_fn.is_dir():
if absolute_fn.exists():
return absolute_fn
if relative_fn_script.is_dir():
if relative_fn_script.exists():
return relative_fn_script
if relative_fn_sys.is_dir():
if relative_fn_sys.exists():
return relative_fn_sys
raise FileExistsError(
f"{fn} could not be found as either a\n"
Expand Down
29 changes: 21 additions & 8 deletions tests/type_dec_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,36 @@ def test_attrs_array_converter():


def test_convert_to_path():
# Test that a string works
str_input = "../tests"
expected_path = (Path(__file__).parent / str_input).resolve()

# Test that a string works
test_str_input = convert_to_path(str_input)
assert isinstance(test_str_input, Path)
assert test_str_input == expected_path

# Test that a pathlib.Path works
path_input = Path("../tests")
path_input = Path(str_input)
test_path_input = convert_to_path(path_input)
assert isinstance(test_path_input, Path)
assert test_path_input == expected_path

# Test that both of those inputs are the same
# NOTE These first three asserts tests the relative path search
assert test_str_input == test_path_input

# Test that a non-existent folder also works even though it's a valid data type
str_input = "tests"
test_str_input = convert_to_path(str_input)
assert isinstance(test_str_input, Path)
# Test absolute path
abs_path = expected_path
test_abs_path = convert_to_path(abs_path)
assert test_abs_path == expected_path

# Test a file
file_input = Path(__file__)
test_file = convert_to_path(file_input)
assert test_file == file_input

# Test that a non-existent folder fails, now that the conversion has a multi-pronged search
str_input = str(Path(__file__).parent / "bad_path")
with pytest.raises(FileExistsError):
convert_to_path(str_input)

# Test that invalid data types fail
with pytest.raises(TypeError):
Expand Down

0 comments on commit 599f226

Please sign in to comment.