diff --git a/floris/type_dec.py b/floris/type_dec.py index 830f6c6ab..e2cec3ce6 100644 --- a/floris/type_dec.py +++ b/floris/type_dec.py @@ -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. @@ -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" diff --git a/tests/type_dec_unit_test.py b/tests/type_dec_unit_test.py index 43bf5bc3a..df2c1b1ff 100644 --- a/tests/type_dec_unit_test.py +++ b/tests/type_dec_unit_test.py @@ -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):