Skip to content

Commit

Permalink
Improvement in Path (#96)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Apr 8, 2024
1 parent da66c66 commit b3ea15f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
24 changes: 24 additions & 0 deletions System.IO.FileSystem.UnitTests/PathUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace System.IO.FileSystem.UnitTests
[TestClass]
internal class PathUnitTests
{
private string RootDirectory => "\\";

[TestMethod]
public void ChangeExtension_adds_extension()
{
Expand Down Expand Up @@ -311,6 +313,28 @@ public void GetPathRoot_returns_root_UNC_paths()
}
}

[TestMethod]
public void GetFullPath0()
{
string fullPath = Path.GetFullPath(@"dir1\dir2\file.ext");

Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void GetFullPath1()
{
string fullPath = Path.GetFullPath(RootDirectory + @"\dir1\dir2\file.ext");
Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void GetFullPath2()
{
string fullPath = Path.GetFullPath(RootDirectory + @"\dir1\..\dir2\file.ext");
Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void HasExtension_returns_false()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,18 @@
<ItemGroup>
<Reference Include="mscorlib, Version=1.15.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.2.54.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.TestFramework, Version=2.1.93.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.TestFramework.2.1.93\lib\nanoFramework.TestFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.TestFramework.2.1.93\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Streams, Version=1.1.56.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.IO.Streams.1.1.56\lib\System.IO.Streams.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
Expand Down
60 changes: 39 additions & 21 deletions System.IO.FileSystem/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,8 @@ public static string Combine(string path1, string path2)

private static string CombineInternal(string first, string second)
{
if (string.IsNullOrEmpty(first))
{
return second;
}

if (string.IsNullOrEmpty(second))
{
return first;
}
ValidateNullOrEmpty(first);
ValidateNullOrEmpty(second);

if (IsPathRooted(second))
{
Expand Down Expand Up @@ -193,10 +186,7 @@ internal static int GetDirectoryNameOffset(string path)
[return: NotNullIfNotNull("path")]
public static string GetExtension(string path)
{
if (path is null)
{
return null;
}
ValidateNullOrEmpty(path);

var length = path.Length;

Expand Down Expand Up @@ -235,10 +225,7 @@ public static string GetExtension(string path)
[return: NotNullIfNotNull("path")]
public static string GetFileName(string path)
{
if (path is null)
{
return null;
}
ValidateNullOrEmpty(path);

var root = GetPathRoot(path).Length;

Expand All @@ -264,10 +251,7 @@ public static string GetFileName(string path)
[return: NotNullIfNotNull("path")]
public static string GetFileNameWithoutExtension(string path)
{
if (path is null)
{
return null;
}
ValidateNullOrEmpty(path);

var fileName = GetFileName(path);
var lastPeriod = fileName.LastIndexOf('.');
Expand All @@ -277,6 +261,27 @@ public static string GetFileNameWithoutExtension(string path)
fileName.Substring(0, lastPeriod);
}

/// <summary>
/// Returns the absolute path for the specified path string.
/// </summary>
/// <param name="path">The file or directory for which to obtain absolute path information.</param>
/// <returns>The fully qualified location of <paramref name="path"/>, such as "C:\MyFile.txt".</returns>
public static string GetFullPath(string path)
{
ValidateNullOrEmpty(path);

if (!IsPathRooted(path))
{
// TODO: will be implemented in next PR
// string currDir = Directory.GetCurrentDirectory();
// path = Combine(currDir, path);
}

return Combine(
GetDirectoryName(path),
GetFileName(path));
}

/// <summary>
/// Gets an array containing the characters that are not allowed in file names.
/// </summary>
Expand Down Expand Up @@ -392,5 +397,18 @@ private static string JoinInternal(string first, string second)
string.Concat(first, second) :
string.Concat(first, PathInternal.DirectorySeparatorCharAsString, second);
}

private static void ValidateNullOrEmpty(string str)
{
if (str == null)
{
throw new ArgumentNullException();
}

if (str.Length == 0)
{
throw new ArgumentException();
}
}
}
}

0 comments on commit b3ea15f

Please sign in to comment.