Skip to content

Commit

Permalink
Added relativepathfinderhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed Jun 17, 2023
1 parent eb0f641 commit 737b7f0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
51 changes: 51 additions & 0 deletions DeveCoolLib.Tests/PathHelpers/RelativePathFinderHelperTests.cs
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);
}
}
}
2 changes: 1 addition & 1 deletion DeveCoolLib/Collections/EqualityComparerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace DeveCoolLib
namespace DeveCoolLib.Collections
{
/// <summary>
/// Utility class for creating <see cref="IEqualityComparer{T}"/> instances
Expand Down
46 changes: 46 additions & 0 deletions DeveCoolLib/PathHelpers/RelativePathFinderHelper.cs
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;
}
}
}

0 comments on commit 737b7f0

Please sign in to comment.