Skip to content

Commit

Permalink
Working for netstandard2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-martinsmith committed Aug 21, 2024
1 parent 1f7d1a5 commit c13dc93
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 168 deletions.
42 changes: 40 additions & 2 deletions src/DNX.Extensions/IO/DirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -90,12 +91,49 @@ public static string GetRelativePath(this DirectoryInfo directoryInfo, Directory
{
var relativePath = owningDirectoryInfo == null || directoryInfo == null
? null
: ""; //Path.GetRelativePath(owningDirectoryInfo.FullName, directoryInfo.FullName)
//.RemoveStartsWith($".{Path.DirectorySeparatorChar}");
: GetRelativePath(directoryInfo.FullName, owningDirectoryInfo.FullName)
.RemoveStartsWith($".{Path.DirectorySeparatorChar}");

if (relativePath == ".")
{
relativePath = string.Empty;
}

return relativePath;
}
/// <summary>
/// Returns a relative path string from a full path based on a base path
/// provided.
/// </summary>
/// <param name="fullPath">The path to convert. Can be either a file or a directory</param>
/// <param name="basePath">The base path on which relative processing is based. Should be a directory.</param>
/// <returns>
/// String of the relative path.
///
/// Examples of returned values:
/// test.txt, ..\test.txt, ..\..\..\test.txt, ., .., subdir\test.txt
/// </returns>
/// <remarks>
/// From : http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
/// </remarks>
private static string GetRelativePath(string fullPath, string basePath)
{
#if NETSTANDARD2_1_OR_GREATER
return Path.GetRelativePath(basePath, fullPath);
#else
// Require trailing path separator for path
fullPath = fullPath.EnsureEndsWith(Path.DirectorySeparatorChar.ToString());
basePath = basePath.EnsureEndsWith(Path.DirectorySeparatorChar.ToString());

var baseUri = new Uri(basePath);
var fullUri = new Uri(fullPath);

var relativeUri = baseUri.MakeRelativeUri(fullUri);

// Uri's use forward slashes so convert back to OS slashes
return relativeUri.ToString()
.Replace("/", Path.DirectorySeparatorChar.ToString())
.RemoveEndsWith(Path.DirectorySeparatorChar.ToString());
#endif
}
}
7 changes: 7 additions & 0 deletions src/DNX.Extensions/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"DNX.Extensions": {
"commandName": "Project"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,46 @@ public void GetHashCode_compares_as_expected(string x, string y, StringCompariso

#region TestData

public static IEnumerable<object[]> StringComparisonValues_Data()
public static TheoryData<StringComparison> StringComparisonValues_Data()
{
return Enum.GetValues(typeof(StringComparison))
return new TheoryData<StringComparison>(
Enum.GetValues(typeof(StringComparison))
.Cast<StringComparison>()
.Select(x => new object[] { x });
);
}

public static IEnumerable<object[]> Equals_Data()
public static TheoryData<string, string, StringComparison, bool> Equals_Data()
{
return new List<object[]>()
return new TheoryData<string, string, StringComparison, bool>
{
new object[] { null, null, StringComparison.CurrentCulture, true },
new object[] { "", "", StringComparison.CurrentCulture, true },
new object[] { "ClearBank", "", StringComparison.CurrentCulture, false },
new object[] { "ClearBank", "", StringComparison.CurrentCulture, false },
new object[] { "", "ClearBank", StringComparison.CurrentCulture, false },
new object[] { "ClearBank", null, StringComparison.CurrentCulture, false },
new object[] { null, "ClearBank", StringComparison.CurrentCulture, false },

new object[] { "Clear", "Bank", StringComparison.CurrentCulture, false },
new object[] { "Clear", "Bank", StringComparison.CurrentCultureIgnoreCase, false },
new object[] { "Clear", "Bank", StringComparison.InvariantCulture, false },
new object[] { "Clear", "Bank", StringComparison.InvariantCultureIgnoreCase, false },
new object[] { "Clear", "Bank", StringComparison.Ordinal, false },
new object[] { "Clear", "Bank", StringComparison.OrdinalIgnoreCase, false },

new object[] { "ClearBank", "ClearBank", StringComparison.CurrentCulture, true },
new object[] { "ClearBank", "ClearBank", StringComparison.CurrentCultureIgnoreCase, true },
new object[] { "ClearBank", "ClearBank", StringComparison.InvariantCulture, true },
new object[] { "ClearBank", "ClearBank", StringComparison.InvariantCultureIgnoreCase, true },
new object[] { "ClearBank", "ClearBank", StringComparison.Ordinal, true },
new object[] { "ClearBank", "ClearBank", StringComparison.OrdinalIgnoreCase, true },

new object[] { "ClearBank", "CLEARBANK", StringComparison.CurrentCulture, false },
new object[] { "ClearBank", "CLEARBANK", StringComparison.CurrentCultureIgnoreCase, true },
new object[] { "ClearBank", "CLEARBANK", StringComparison.InvariantCulture, false },
new object[] { "ClearBank", "CLEARBANK", StringComparison.InvariantCultureIgnoreCase, true },
new object[] { "ClearBank", "CLEARBANK", StringComparison.Ordinal, false },
new object[] { "ClearBank", "CLEARBANK", StringComparison.OrdinalIgnoreCase, true },
{ null, null, StringComparison.CurrentCulture, true },
{ "", "", StringComparison.CurrentCulture, true },
{ "ClearBank", "", StringComparison.CurrentCulture, false },
{ "ClearBank", "", StringComparison.CurrentCulture, false },
{ "", "ClearBank", StringComparison.CurrentCulture, false },
{ "ClearBank", null, StringComparison.CurrentCulture, false },
{ null, "ClearBank", StringComparison.CurrentCulture, false },

{ "Clear", "Bank", StringComparison.CurrentCulture, false },
{ "Clear", "Bank", StringComparison.CurrentCultureIgnoreCase, false },
{ "Clear", "Bank", StringComparison.InvariantCulture, false },
{ "Clear", "Bank", StringComparison.InvariantCultureIgnoreCase, false },
{ "Clear", "Bank", StringComparison.Ordinal, false },
{ "Clear", "Bank", StringComparison.OrdinalIgnoreCase, false },

{ "ClearBank", "ClearBank", StringComparison.CurrentCulture, true },
{ "ClearBank", "ClearBank", StringComparison.CurrentCultureIgnoreCase, true },
{ "ClearBank", "ClearBank", StringComparison.InvariantCulture, true },
{ "ClearBank", "ClearBank", StringComparison.InvariantCultureIgnoreCase, true },
{ "ClearBank", "ClearBank", StringComparison.Ordinal, true },
{ "ClearBank", "ClearBank", StringComparison.OrdinalIgnoreCase, true },

{ "ClearBank", "CLEARBANK", StringComparison.CurrentCulture, false },
{ "ClearBank", "CLEARBANK", StringComparison.CurrentCultureIgnoreCase, true },
{ "ClearBank", "CLEARBANK", StringComparison.InvariantCulture, false },
{ "ClearBank", "CLEARBANK", StringComparison.InvariantCultureIgnoreCase, true },
{ "ClearBank", "CLEARBANK", StringComparison.Ordinal, false },
{ "ClearBank", "CLEARBANK", StringComparison.OrdinalIgnoreCase, true },
};
}

Expand Down
20 changes: 10 additions & 10 deletions tests/DNX.Extensions.Tests/DateTimes/DateTimeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ public void SetMilliseconds_can_operate_as_expected(DateTime dateTime)
result.Millisecond.Should().Be(value);
}

public static IEnumerable<object[]> ResetDateTime_Data()
public static TheoryData<DateTime> ResetDateTime_Data()
{
return new List<object[]>()
return new TheoryData<DateTime>
{
new object[] { DateTime.UtcNow },
new object[] { DateTime.Now },
new object[] { DateTime.UnixEpoch },
new object[] { DateTime.Parse("2021-11-05 20:53:44.12345") },
new object[] { DateTime.Parse("2021-11-05 20:53:44") },
new object[] { DateTime.Parse("2021-11-05 20:53") },
new object[] { DateTime.Parse("2021-11-05 00:00") },
new object[] { DateTime.Parse("2021-11-05") },
{ DateTime.UtcNow },
{ DateTime.Now },
{ DateTime.UnixEpoch },
{ DateTime.Parse("2021-11-05 20:53:44.12345") },
{ DateTime.Parse("2021-11-05 20:53:44") },
{ DateTime.Parse("2021-11-05 20:53") },
{ DateTime.Parse("2021-11-05 00:00") },
{ DateTime.Parse("2021-11-05") },
};
}
}
Expand Down
29 changes: 16 additions & 13 deletions tests/DNX.Extensions.Tests/IO/DirectoryInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using FluentAssertions;
using Xunit;

// ReSharper disable StringLiteralTypo

namespace DNX.Extensions.Tests.IO
{
public class DirectoryInfoExtensionsTests : IDisposable
Expand All @@ -20,6 +22,7 @@ public DirectoryInfoExtensionsTests()
public void Dispose()
{
_directoryInfo.Delete(true);
GC.SuppressFinalize(this);
}

internal static void CreateFile(DirectoryInfo directoryInfo, string fileName)
Expand Down Expand Up @@ -218,31 +221,31 @@ public void FindDirectories_for_multiple_patterns_with_recursion_finds_expected_
[MemberData(nameof(GetRelativePath_Data))]
public void GetRelativePath_can_extract_relative_path_correctly(string dirName, string relativeToDirName, string expected)
{
var dirInfo = dirName == null ? null : new DirectoryInfo(dirName);
var dirInfo = dirName == null ? null : new DirectoryInfo(dirName);
var relativeToDirInfo = relativeToDirName == null ? null : new DirectoryInfo(relativeToDirName);

var result = dirInfo.GetRelativePath(relativeToDirInfo);

result.Should().Be(expected);
result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(relativeToDirInfo)}: {relativeToDirInfo}");
}

public static IEnumerable<object[]> GetRelativePath_Data()
public static TheoryData<string, string, string> GetRelativePath_Data()
{
var guid1 = Guid.NewGuid().ToString();
var guid2 = Guid.NewGuid().ToString();
var guid3 = Guid.NewGuid().ToString();

return new List<object[]>()
return new TheoryData<string, string, string>()
{
new object[] { Path.Combine(Path.GetTempPath(), guid1), null, null },
new object[] { null, Path.Combine(Path.GetTempPath(), guid1), null },
new object[] { Path.Combine(Path.GetTempPath(), guid1), Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Join("..", guid1) },
new object[] { Path.Combine(Path.GetTempPath(), guid2), Path.Combine(Path.GetTempPath(), guid3), Path.Join("..", guid2) },
new object[] { Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Combine(Path.GetTempPath(), "abcdefg"), "" },
new object[] { Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg"), "dir3" },
new object[] { Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), "" },
new object[] { Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine(Path.GetTempPath(), "folder2"), Path.Combine("..", "folder1") },
new object[] { Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine("D:", "folder2"), Path.Combine(Path.GetTempPath(), "folder1") },
{ Path.Combine(Path.GetTempPath(), guid1), null, null },
{ null, Path.Combine(Path.GetTempPath(), guid1), null },
{ Path.Combine(Path.GetTempPath(), guid1), Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Join("..", guid1) },
{ Path.Combine(Path.GetTempPath(), guid2), Path.Combine(Path.GetTempPath(), guid3), Path.Join("..", guid2) },
{ Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Combine(Path.GetTempPath(), "abcdefg"), "" },
{ Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg"), "dir3" },
{ Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), "" },
{ Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine(Path.GetTempPath(), "folder2"), Path.Combine("..", "folder1") },
{ Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine("D:", "folder2"), Path.Combine(Path.GetTempPath(), "folder1") },
};
}
}
Expand Down
66 changes: 32 additions & 34 deletions tests/DNX.Extensions.Tests/IO/FileInfoExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,50 @@ public void GetFriendlyFileSize_given_a_valid_FileInfo_should_return_expected_te
fileInfo.Delete();
}

public static IEnumerable<object[]> FileSizeData()
public static TheoryData<long, string> FileSizeData()
{
var data = new List<object[]>
return new TheoryData<long, string>
{
new object[] { 0, "0B" },
new object[] { 1000, "1000B" },
new object[] { 1023, "1023B" },
new object[] { 1024, "1KB" },
new object[] { 1536, "1.5KB" },
new object[] { 1792, "1.8KB" },
new object[] { 2048, "2KB" },
new object[] { 10240, "10KB" },
new object[] { 102400, "100KB" },
new object[] { 1024000, "1000KB" },
new object[] { 1048500, "1023.9KB" },
new object[] { 1048575, "1024KB" },
new object[] { 1048576, "1MB" },
new object[] { 2097152, "2MB" },
new object[] { 10485760, "10MB" },
{ 0, "0B" },
{ 1000, "1000B" },
{ 1023, "1023B" },
{ 1024, "1KB" },
{ 1536, "1.5KB" },
{ 1792, "1.8KB" },
{ 2048, "2KB" },
{ 10240, "10KB" },
{ 102400, "100KB" },
{ 1024000, "1000KB" },
{ 1048500, "1023.9KB" },
{ 1048575, "1024KB" },
{ 1048576, "1MB" },
{ 2097152, "2MB" },
{ 10485760, "10MB" },
};

return data;
}

public static IEnumerable<object[]> GetRelativeFileName_Data()
public static TheoryData<string, string, string> GetRelativeFileName_Data()
{
return new List<object[]>()
return new TheoryData<string, string, string>
{
new object[] { Path.Combine("C:", "Temp", "abcdefg", "file.txt"), Path.Combine("C:", "Temp", "abcdefg"), "file.txt" },
new object[] { Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg"), Path.Combine("dir3", "file1.tf") },
new object[] { Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg", "dir3"), "file1.tf" },
new object[] { Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg", "dir3"), "file1.tf" },
new object[] { Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("D:", "folder2"), Path.Combine("C:", "Temp", "folder1", "file.txt") },
new object[] { Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("C:", "Temp", "folder2"), Path.Combine("..", "folder1", "file.txt") },
{ Path.Combine("C:", "Temp", "abcdefg", "file.txt"), Path.Combine("C:", "Temp", "abcdefg"), "file.txt" },
{ Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg"), Path.Combine("dir3", "file1.tf") },
{ Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg", "dir3"), "file1.tf" },
{ Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg", "dir3"), "file1.tf" },
{ Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("D:", "folder2"), Path.Combine("C:", "Temp", "folder1", "file.txt") },
{ Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("C:", "Temp", "folder2"), Path.Combine("..", "folder1", "file.txt") },
};
}

public static IEnumerable<object[]> GetRelativeFilePath_Data()
public static TheoryData<string, string, string> GetRelativeFilePath_Data()
{
return new List<object[]>()
return new TheoryData<string, string, string>
{
new object[] { Path.Combine("C:", "Temp", "abcdefg", "file.txt"), Path.Combine("C:", "Temp", "abcdefg"), "" },
new object[] { Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg"), "dir3" },
new object[] { Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg", "dir3"), "" },
new object[] { Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("C:", "Temp", "folder2"), Path.Combine("..", "folder1") },
new object[] { Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("D:", "folder2"), Path.Combine("C:", "Temp", "folder1") },
{ Path.Combine("C:", "Temp", "abcdefg", "file.txt"), Path.Combine("C:", "Temp", "abcdefg"), "" },
{ Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg"), "dir3" },
{ Path.Combine("C:", "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine("C:", "Temp", "abcdefg", "dir3"), "" },
{ Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("C:", "Temp", "folder2"), Path.Combine("..", "folder1") },
{ Path.Combine("C:", "Temp", "folder1", "file.txt"), Path.Combine("D:", "folder2"), Path.Combine("C:", "Temp", "folder1") },
};
}
}
Expand Down
Loading

0 comments on commit c13dc93

Please sign in to comment.