From ef32431682e1ae965010345c9a2d4c1a314dd363 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 29 Aug 2013 18:26:03 -0700 Subject: [PATCH] GetActualCasing can partially fix the path for non-existing paths. --- NzbDrone.Common.Test/PathExtensionFixture.cs | 21 ++++++++++---------- NzbDrone.Common/PathExtensions.cs | 14 ++++++++----- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/NzbDrone.Common.Test/PathExtensionFixture.cs b/NzbDrone.Common.Test/PathExtensionFixture.cs index 739344727..907d13d4c 100644 --- a/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -102,30 +102,31 @@ namespace NzbDrone.Common.Test } [Test] - public void get_actual_casing_for_none_existing_file_should_throw() + public void get_actual_casing_for_none_existing_file_return_partially_fixed_result() { WindowsOnly(); - Assert.Throws(() => "C:\\InValidFolder\\invalidfile.exe".GetActualCasing()); + "C:\\WINDOWS\\invalidfile.exe".GetActualCasing().Should().Be("C:\\Windows\\invalidfile.exe"); } + [Test] - public void get_actual_casing_should_return_actual_casing_for_local_file_in_windows() + public void get_actual_casing_for_none_existing_folder_return_partially_fixed_result() { WindowsOnly(); - var path = Process.GetCurrentProcess().MainModule.FileName; - path.ToUpper().GetActualCasing().Should().Be(path); - path.ToLower().GetActualCasing().Should().Be(path); + "C:\\WINDOWS\\SYSTEM32\\FAKEFOLDER\\invalidfile.exe".GetActualCasing().Should().Be("C:\\Windows\\System32\\FAKEFOLDER\\invalidfile.exe"); } [Test] - public void get_actual_casing_should_return_origibal_value_in_linux() + public void get_actual_casing_should_return_actual_casing_for_local_file_in_windows() { - LinuxOnly(); + WindowsOnly(); var path = Process.GetCurrentProcess().MainModule.FileName; - path.GetActualCasing().Should().Be(path); - path.GetActualCasing().Should().Be(path); + path.ToUpper().GetActualCasing().Should().Be(path); + path.ToLower().GetActualCasing().Should().Be(path); } + + [Test] public void get_actual_casing_should_return_actual_casing_for_local_dir_in_windows() { diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 15e3b8a08..b5e899eba 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -63,23 +63,27 @@ namespace NzbDrone.Common public static string GetActualCasing(this string path) { - var attributes = File.GetAttributes(path); - if (OsInfo.IsLinux || path.StartsWith("\\")) { return path; } - if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) + if (Directory.Exists(path) && (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory) { return GetProperCapitalization(new DirectoryInfo(path)); } var fileInfo = new FileInfo(path); + var dirInfo = fileInfo.Directory; + + var fileName = fileInfo.Name; + if (dirInfo != null && fileInfo.Exists) + { + fileName = dirInfo.GetFiles(fileInfo.Name)[0].Name; + } - DirectoryInfo dirInfo = fileInfo.Directory; - return Path.Combine(GetProperCapitalization(dirInfo), dirInfo.GetFiles(fileInfo.Name)[0].Name); + return Path.Combine(GetProperCapitalization(dirInfo), fileName); } public static string GetAppDataPath(this IAppFolderInfo appFolderInfo)