1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

GetActualCasing can partially fix the path for non-existing paths.

This commit is contained in:
kay.one 2013-08-29 18:26:03 -07:00
parent 5dc3a3223d
commit ef32431682
2 changed files with 20 additions and 15 deletions

View File

@ -102,10 +102,18 @@ public void normalize_path_exception_null()
} }
[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(); WindowsOnly();
Assert.Throws<DirectoryNotFoundException>(() => "C:\\InValidFolder\\invalidfile.exe".GetActualCasing()); "C:\\WINDOWS\\invalidfile.exe".GetActualCasing().Should().Be("C:\\Windows\\invalidfile.exe");
}
[Test]
public void get_actual_casing_for_none_existing_folder_return_partially_fixed_result()
{
WindowsOnly();
"C:\\WINDOWS\\SYSTEM32\\FAKEFOLDER\\invalidfile.exe".GetActualCasing().Should().Be("C:\\Windows\\System32\\FAKEFOLDER\\invalidfile.exe");
} }
[Test] [Test]
@ -117,14 +125,7 @@ public void get_actual_casing_should_return_actual_casing_for_local_file_in_wind
path.ToLower().GetActualCasing().Should().Be(path); path.ToLower().GetActualCasing().Should().Be(path);
} }
[Test]
public void get_actual_casing_should_return_origibal_value_in_linux()
{
LinuxOnly();
var path = Process.GetCurrentProcess().MainModule.FileName;
path.GetActualCasing().Should().Be(path);
path.GetActualCasing().Should().Be(path);
}
[Test] [Test]
public void get_actual_casing_should_return_actual_casing_for_local_dir_in_windows() public void get_actual_casing_should_return_actual_casing_for_local_dir_in_windows()

View File

@ -63,23 +63,27 @@ private static string GetProperCapitalization(DirectoryInfo dirInfo)
public static string GetActualCasing(this string path) public static string GetActualCasing(this string path)
{ {
var attributes = File.GetAttributes(path);
if (OsInfo.IsLinux || path.StartsWith("\\")) if (OsInfo.IsLinux || path.StartsWith("\\"))
{ {
return path; return path;
} }
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) if (Directory.Exists(path) && (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory)
{ {
return GetProperCapitalization(new DirectoryInfo(path)); return GetProperCapitalization(new DirectoryInfo(path));
} }
var fileInfo = new FileInfo(path); var fileInfo = new FileInfo(path);
var dirInfo = fileInfo.Directory;
var fileName = fileInfo.Name;
DirectoryInfo dirInfo = fileInfo.Directory; if (dirInfo != null && fileInfo.Exists)
return Path.Combine(GetProperCapitalization(dirInfo), dirInfo.GetFiles(fileInfo.Name)[0].Name); {
fileName = dirInfo.GetFiles(fileInfo.Name)[0].Name;
}
return Path.Combine(GetProperCapitalization(dirInfo), fileName);
} }
public static string GetAppDataPath(this IAppFolderInfo appFolderInfo) public static string GetAppDataPath(this IAppFolderInfo appFolderInfo)