From ac1fbca16b8f2d458a9c6f2f637e4ff550890201 Mon Sep 17 00:00:00 2001 From: Joshua Larsen Date: Fri, 1 Oct 2021 16:13:39 -0700 Subject: [PATCH] fix(MFFileMgmt.string_to_file_path): updated to support UNC paths added test_mf6_string_to_file_path to t501_test.py --- autotest/t501_test.py | 33 +++++++++++++++++++++++++++++++++ flopy/mf6/mfbase.py | 7 ++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/autotest/t501_test.py b/autotest/t501_test.py index 620bb8c0b6..36c1a2e295 100644 --- a/autotest/t501_test.py +++ b/autotest/t501_test.py @@ -135,5 +135,38 @@ def test_mf6(): return +def test_mf6_string_to_file_path(): + from flopy.mf6.mfbase import MFFileMgmt + import platform + + if platform.system().lower() == "windows": + unc_path = r"\\server\path\path" + new_path = MFFileMgmt.string_to_file_path(unc_path) + if not unc_path == new_path: + raise AssertionError("UNC path error") + + abs_path = r"C:\Users\some_user\path" + new_path = MFFileMgmt.string_to_file_path(abs_path) + if not abs_path == new_path: + raise AssertionError("Absolute path error") + + rel_path = r"..\path\some_path" + new_path = MFFileMgmt.string_to_file_path(rel_path) + if not rel_path == new_path: + raise AssertionError("Relative path error") + + else: + abs_path = "/mnt/c/some_user/path" + new_path = MFFileMgmt.string_to_file_path(abs_path) + if not abs_path == new_path: + raise AssertionError("Absolute path error") + + rel_path = "../path/some_path" + new_path = MFFileMgmt.string_to_file_path(rel_path) + if not rel_path == new_path: + raise AssertionError("Relative path error") + + if __name__ == "__main__": test_mf6() + test_mf6_string_to_file_path() diff --git a/flopy/mf6/mfbase.py b/flopy/mf6/mfbase.py index c1e88447f8..ddc6291ae8 100644 --- a/flopy/mf6/mfbase.py +++ b/flopy/mf6/mfbase.py @@ -310,7 +310,12 @@ def string_to_file_path(fp_string): arr_string = new_string.split(delimiter) if len(arr_string) > 1: if os.path.isabs(fp_string): - new_string = f"{arr_string[0]}{delimiter}{arr_string[1]}" + if not arr_string[0] and not arr_string[1]: + new_string = f"{delimiter}{delimiter}" + else: + new_string = ( + f"{arr_string[0]}{delimiter}{arr_string[1]}" + ) else: new_string = os.path.join(arr_string[0], arr_string[1]) if len(arr_string) > 2: