-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
DeveCoolLib.Tests/PathHelpers/RelativePathFinderHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using DeveCoolLib.PathHelpers; | ||
using System; | ||
using Xunit; | ||
|
||
namespace DeveCoolLib.Tests.PathHelpers | ||
{ | ||
public class RelativePathFinderHelperTests | ||
{ | ||
[Fact] | ||
public void MakesARelativePath() | ||
{ | ||
string dirPath; | ||
string filePath; | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
dirPath = @"C:\WOP\DeveMazeGenerator"; | ||
filePath = @"C:\WOP\DevemazeGenerator\Images\TestImage.png"; | ||
} | ||
else | ||
{ | ||
dirPath = @"/home/WOP/DeveMazeGenerator"; | ||
filePath = @"/home/WOP/DeveMazeGenerator/Images/TestImage.png"; | ||
} | ||
|
||
var expectedPath = @"Images/TestImage.png"; | ||
|
||
var relativePath = RelativePathFinderHelper.GetRelativePath(dirPath, filePath); | ||
Assert.Equal(expectedPath, relativePath); | ||
} | ||
|
||
[Fact] | ||
public void WorksForNullPath() | ||
{ | ||
string dirPath = null; | ||
string filePath; | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
filePath = @"C:\WOP\DevemazeGenerator\Images\TestImage.png"; | ||
} | ||
else | ||
{ | ||
filePath = @"/home/WOP/DeveMazeGenerator/Images/TestImage.png"; | ||
} | ||
|
||
var expectedPath = filePath; | ||
|
||
var relativePath = RelativePathFinderHelper.GetRelativePath(dirPath, filePath); | ||
Assert.Equal(expectedPath, relativePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace DeveCoolLib.PathHelpers | ||
{ | ||
public static class RelativePathFinderHelper | ||
{ | ||
public static string GetRelativePath(string originDirectory, string fullPathOfFile) | ||
{ | ||
return GetRelativePath(originDirectory != null ? new DirectoryInfo(originDirectory) : null, fullPathOfFile != null ? new FileInfo(fullPathOfFile) : null); | ||
} | ||
|
||
public static string GetRelativePath(FileSystemInfo? destinationPath, FileSystemInfo? originPath) | ||
{ | ||
if (originPath == null) | ||
{ | ||
throw new ArgumentNullException(nameof(originPath)); | ||
} | ||
|
||
if (destinationPath == null) | ||
{ | ||
return originPath.FullName; | ||
} | ||
|
||
string path1FullName = GetFullName(destinationPath); | ||
string path2FullName = GetFullName(originPath); | ||
|
||
Uri uri1 = new Uri(path1FullName); | ||
Uri uri2 = new Uri(path2FullName); | ||
Uri relativeUri = uri1.MakeRelativeUri(uri2); | ||
|
||
return relativeUri.OriginalString; | ||
} | ||
|
||
private static string GetFullName(FileSystemInfo path) | ||
{ | ||
string fullName = path.FullName; | ||
|
||
if (path is DirectoryInfo && fullName[fullName.Length - 1] != Path.DirectorySeparatorChar) | ||
{ | ||
fullName += Path.DirectorySeparatorChar; | ||
} | ||
return fullName; | ||
} | ||
} | ||
} |