Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path dependency in convert_to_path test #749

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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