Skip to content

Commit

Permalink
Fixes #22 issue with folder to folder relative path (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre authored Feb 27, 2019
1 parent 6ef9d67 commit 6a9b4e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/OSPSuite.Utility/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,25 +329,25 @@ public static string CreateRelativePath(string originalPath, string relativeToPa

private static string sanitizePath(string path, bool isRelativeToPath)
{
var endsWithSeparator = path.EndsWith(Path.DirectorySeparatorChar.ToString());
var sanitizedPath = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
var endsWithSeparator = sanitizedPath.EndsWith(Path.DirectorySeparatorChar.ToString());

//The relative path should always end with a separator
if (isRelativeToPath)
{
if (endsWithSeparator)
return path;

return appendDirectorySeparator(path);
}
return appendDirectorySeparator(sanitizedPath);

//Absolute argument should not end with separator
if (endsWithSeparator)
return path.Remove(path.Length - 1);
return sanitizedPath.Remove(sanitizedPath.Length - 1);

return path;
return sanitizedPath;
}

private static string appendDirectorySeparator(string path) => $"{path}{Path.DirectorySeparatorChar}";
private static string appendDirectorySeparator(string path)
{
var endsWithSeparator = path.EndsWith(Path.DirectorySeparatorChar.ToString());
return endsWithSeparator ? path : $"{path}{Path.DirectorySeparatorChar}";
}

/// <summary>
/// Try to shorten a path to a given max length (e.g. replace the sub folder with ...)
Expand Down
19 changes: 19 additions & 0 deletions tests/OSPSuite.Utility.Tests/FileHelperSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ public void should_return_the_expected_relative_path_between_a_folder_and_a_fold
FileHelper.CreateRelativePath("c:/A/B/C/D", "c:/A/B").ShouldBeEqualTo(@"C\D\");
}

[Observation]
public void should_return_the_expected_relative_path_between_a_folder_and_a_folder_ending_with_a_folder_char()
{
FileHelper.CreateRelativePath("c:/A/B/C/D", "c:/A/B/").ShouldBeEqualTo(@"C\D\");
}

[Observation]
public void should_return_the_expected_relative_path_between_a_folder_ending_with_a_folder_char_and_a_folder()
{
FileHelper.CreateRelativePath("c:/A/B/C/D/", "c:/A/B").ShouldBeEqualTo(@"C\D\");
}

[Observation]
public void should_return_the_expected_relative_path_between_a_folder_ending_with_a_folder_char_and_a_folder_ending_with_a_folder_char()
{
FileHelper.CreateRelativePath("c:/A/B/C/D/", "c:/A/B/").ShouldBeEqualTo(@"C\D\");
}


[Observation]
public void should_return_the_expected_relative_path_between_a_folder_and_a_file()
{
Expand Down

0 comments on commit 6a9b4e7

Please sign in to comment.