Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Semantics.Paths/Implementations/RelativeDirectoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public sealed record RelativeDirectoryPath : SemanticDirectoryPath<RelativeDirec
/// </summary>
/// <value>
/// A <see cref="RelativeDirectoryPath"/> naming the directory that contains this one: the path with its last
/// segment removed, or an empty path for a single-segment path. Paths are normalized lexically first, so the
/// segment removed, or <c>.</c> (the starting directory, as <see cref="Normalize"/> writes it)
/// for a single-segment path. Paths are normalized lexically first, so the
/// parent of <c>a/..</c> is <c>..</c>, and a path that names the starting directory or one above it climbs
/// further instead: the parent of <c>.</c> (or the empty path) is <c>..</c>, and the parent of <c>..</c> is
/// <c>../..</c>.
Expand All @@ -62,7 +63,11 @@ private RelativeDirectoryPath ComputeParent()
return Create<RelativeDirectoryPath>(normalized + separator + "..");
}

return Create<RelativeDirectoryPath>(string.Join(separator, segments, 0, segments.Length - 1));
// One segment leaves the starting directory, which is "." rather than "": the empty path is
// rejected by Path.GetFullPath and reads as missing to Directory.Exists.
return segments.Length == 1
? Create<RelativeDirectoryPath>(".")
: Create<RelativeDirectoryPath>(string.Join(separator, segments, 0, segments.Length - 1));
}

// Cache for directory name
Expand Down
4 changes: 3 additions & 1 deletion Semantics.Paths/Implementations/RelativeFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public RelativeDirectoryPath RelativeDirectoryPath
{
get
{
// A bare file name lives in the starting directory, written "." so that AsAbsolute and
// IsDirectory can resolve it; the BCL rejects or misreads the empty path.
return _cachedDirectoryPath ??= RelativeDirectoryPath.Create<RelativeDirectoryPath>(
Path.GetDirectoryName(WeakString) ?? "");
Path.GetDirectoryName(WeakString) is { Length: > 0 } directory ? directory : ".");
}
}

Expand Down
11 changes: 7 additions & 4 deletions Semantics.Paths/Polyfills.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ public static string GetFullPath(string path, string basePath)
return Path.GetFullPath(path, basePath);
#else
// Fallback implementation for netstandard2.0 and netcoreapp2.0
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("Path cannot be empty.", nameof(path));
}
Ensure.NotNull(path);

if (string.IsNullOrEmpty(basePath))
{
Expand All @@ -153,6 +150,12 @@ public static string GetFullPath(string path, string basePath)

basePath = Path.GetFullPath(basePath);

// The BCL overload on newer frameworks resolves an empty path to the base path itself.
if (path.Length == 0)
{
return basePath;
}

if (IsPathFullyQualified(path))
{
return Path.GetFullPath(path);
Expand Down
7 changes: 6 additions & 1 deletion Semantics.Paths/SemanticFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,10 @@ private protected string WeakStringWithExtension(FileExtension newExtension)
/// <summary>
/// Gets the directory portion of the path
/// </summary>
public DirectoryPath DirectoryPath => DirectoryPath.Create<DirectoryPath>(Path.GetDirectoryName(WeakString) ?? "");
/// <remarks>A bare file name's directory is the starting directory, written <c>.</c>.</remarks>
public DirectoryPath DirectoryPath => DirectoryPath.Create<DirectoryPath>(Path.GetDirectoryName(WeakString) switch
{
"" => ".",
var directory => directory ?? "",
});
}
8 changes: 7 additions & 1 deletion Semantics.Paths/Utilities/PooledStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public static string CombinePaths(params string[] components)
StringBuilder sb = Get();
try
{
sb.Append(components[0]);
// The starting directory, ".", contributes nothing either, so the parent of a single-segment
// path combined with its name gives back that path ("a", not "./a").
if (components[0] != ".")
{
sb.Append(components[0]);
}

for (int i = 1; i < components.Length; i++)
{
// An empty leading component contributes nothing, so a separator after it would root the
Expand Down
2 changes: 1 addition & 1 deletion Semantics.Test/Paths/PathIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void PathHierarchy_ParentTraversal_WorksCorrectly()
Assert.Contains("c", parent1.WeakString);
Assert.Contains("b", parent2.WeakString);
Assert.AreEqual("a", parent3.WeakString);
Assert.AreEqual("", parent4.WeakString); // Should be empty at root
Assert.AreEqual(".", parent4.WeakString); // The starting directory
}

[TestMethod]
Expand Down
14 changes: 11 additions & 3 deletions Semantics.Test/Paths/PathOperatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,20 @@ public void DirectoryNameOperator_WithSpecialCharacters_HandlesCorrectly()
Assert.Contains("my-app_v2 (beta)", result.WeakString);
}

[TestMethod]
public void RelativeDirectoryOperators_SingleSegmentParent_RoundTrips()
{
RelativeDirectoryPath directory = RelativeDirectoryPath.Create<RelativeDirectoryPath>("a");
RelativeFilePath file = RelativeFilePath.Create<RelativeFilePath>("x.txt");

Assert.AreEqual("a", (directory.Parent / directory.Name).WeakString);
Assert.AreEqual("x.txt", (file.RelativeDirectoryPath / file.FileName).WeakString);
}

[TestMethod]
public void RelativeDirectoryOperators_EmptyLeftOperand_StayRelative()
{
// Parent of a single-segment path is the empty path, the usual source of an empty left operand.
RelativeDirectoryPath empty = RelativeDirectoryPath.Create<RelativeDirectoryPath>("a").Parent;
Assert.AreEqual(string.Empty, empty.WeakString);
RelativeDirectoryPath empty = RelativeDirectoryPath.Create<RelativeDirectoryPath>(string.Empty);

RelativeFilePath file = empty / FileName.Create<FileName>("x.txt");
Assert.AreEqual("x.txt", file.WeakString);
Expand Down
36 changes: 27 additions & 9 deletions Semantics.Test/Paths/RelativePathPropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,25 @@ public void RelativeDirectoryPath_Parent_ReturnsCorrectParent()
}

[TestMethod]
public void RelativeDirectoryPath_Parent_WithSingleComponent_ReturnsEmpty()
public void RelativeDirectoryPath_Parent_WithSingleComponent_ReturnsStartingDirectory()
{
// Test Parent property with single directory
RelativeDirectoryPath path = RelativeDirectoryPath.Create<RelativeDirectoryPath>("myapp");

RelativeDirectoryPath parent = path.Parent;

Assert.IsNotNull(parent);
Assert.AreEqual("", parent.WeakString);
Assert.AreEqual(".", parent.WeakString);
Assert.AreEqual(Path.GetFullPath("."), parent.AsAbsolute().WeakString);
Assert.IsTrue(parent.IsDirectory);
}

[TestMethod]
public void RelativeDirectoryPath_Parent_WithSingleComponent_ResolvesAgainstABase()
{
AbsoluteDirectoryPath baseDirectory = AbsoluteDirectoryPath.Create<AbsoluteDirectoryPath>(Path.GetTempPath());

RelativeDirectoryPath parent = RelativeDirectoryPath.Create<RelativeDirectoryPath>("myapp").Parent;

Assert.AreEqual(Path.GetFullPath(Path.GetTempPath()).TrimEnd(Path.DirectorySeparatorChar), parent.AsAbsolute(baseDirectory).WeakString.TrimEnd(Path.DirectorySeparatorChar));
}

[TestMethod]
Expand Down Expand Up @@ -108,15 +118,23 @@ public void RelativeFilePath_RelativeDirectoryPath_ReturnsCorrectDirectory()
}

[TestMethod]
public void RelativeFilePath_RelativeDirectoryPath_WithFileInRoot_ReturnsEmpty()
public void RelativeFilePath_RelativeDirectoryPath_WithBareFileName_ReturnsStartingDirectory()
{
// Test RelativeDirectoryPath property with file in root
RelativeFilePath file = RelativeFilePath.Create<RelativeFilePath>("file.txt");

RelativeDirectoryPath dir = file.RelativeDirectoryPath;

Assert.IsNotNull(dir);
Assert.AreEqual("", dir.WeakString);
Assert.AreEqual(".", dir.WeakString);
Assert.AreEqual(Path.GetFullPath("."), dir.AsAbsolute().WeakString);
}

[TestMethod]
public void RelativeFilePath_DirectoryPath_WithBareFileName_ReturnsStartingDirectory()
{
RelativeFilePath file = RelativeFilePath.Create<RelativeFilePath>("file.txt");

Assert.AreEqual(".", file.DirectoryPath.WeakString);
Assert.IsTrue(file.DirectoryPath.IsDirectory);
}

[TestMethod]
Expand Down Expand Up @@ -310,7 +328,7 @@ public void RelativeFilePath_AsAbsoluteWithBase_WithNullBase_ThrowsException()
[DataRow("", "..")]
[DataRow("a/..", "..")]
[DataRow("a/../..", "../..")]
[DataRow("a", "")]
[DataRow("a", ".")]
[DataRow("a/b", "a")]
[DataRow("a/./b", "a")]
[DataRow("../a", "..")]
Expand Down
7 changes: 4 additions & 3 deletions Semantics.Test/SemanticPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,13 @@ public void SemanticFilePath_DirectoryPath_WithRootFile_ShouldReturnRootDirector
}

[TestMethod]
public void SemanticFilePath_DirectoryPath_WithEmptyResult_ShouldReturnEmpty()
public void SemanticFilePath_DirectoryPath_OfABareFileName_IsTheStartingDirectory()
{
// Test when GetDirectoryName returns null/empty
// GetDirectoryName returns "" for a bare file name; "." is what Path.GetFullPath and
// Directory.Exists can resolve.
FilePath filePath = FilePath.Create<FilePath>("file.txt");
DirectoryPath directoryPath = filePath.DirectoryPath;
Assert.AreEqual("", directoryPath.ToString());
Assert.AreEqual(".", directoryPath.ToString());
}

[TestMethod]
Expand Down
Loading