diff --git a/Semantics.Paths/Implementations/RelativeDirectoryPath.cs b/Semantics.Paths/Implementations/RelativeDirectoryPath.cs index d7e40156..456f582c 100644 --- a/Semantics.Paths/Implementations/RelativeDirectoryPath.cs +++ b/Semantics.Paths/Implementations/RelativeDirectoryPath.cs @@ -36,7 +36,8 @@ public sealed record RelativeDirectoryPath : SemanticDirectoryPath /// /// A 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 . (the starting directory, as writes it) + /// for a single-segment path. Paths are normalized lexically first, so the /// parent of a/.. is .., and a path that names the starting directory or one above it climbs /// further instead: the parent of . (or the empty path) is .., and the parent of .. is /// ../... @@ -62,7 +63,11 @@ private RelativeDirectoryPath ComputeParent() return Create(normalized + separator + ".."); } - return Create(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(".") + : Create(string.Join(separator, segments, 0, segments.Length - 1)); } // Cache for directory name diff --git a/Semantics.Paths/Implementations/RelativeFilePath.cs b/Semantics.Paths/Implementations/RelativeFilePath.cs index 85f68e23..da879fe0 100644 --- a/Semantics.Paths/Implementations/RelativeFilePath.cs +++ b/Semantics.Paths/Implementations/RelativeFilePath.cs @@ -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( - Path.GetDirectoryName(WeakString) ?? ""); + Path.GetDirectoryName(WeakString) is { Length: > 0 } directory ? directory : "."); } } diff --git a/Semantics.Paths/Polyfills.cs b/Semantics.Paths/Polyfills.cs index f682b0f9..c8bb53a5 100644 --- a/Semantics.Paths/Polyfills.cs +++ b/Semantics.Paths/Polyfills.cs @@ -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)) { @@ -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); diff --git a/Semantics.Paths/SemanticFilePath.cs b/Semantics.Paths/SemanticFilePath.cs index a60bf135..b0c285b6 100644 --- a/Semantics.Paths/SemanticFilePath.cs +++ b/Semantics.Paths/SemanticFilePath.cs @@ -111,5 +111,10 @@ private protected string WeakStringWithExtension(FileExtension newExtension) /// /// Gets the directory portion of the path /// - public DirectoryPath DirectoryPath => DirectoryPath.Create(Path.GetDirectoryName(WeakString) ?? ""); + /// A bare file name's directory is the starting directory, written .. + public DirectoryPath DirectoryPath => DirectoryPath.Create(Path.GetDirectoryName(WeakString) switch + { + "" => ".", + var directory => directory ?? "", + }); } diff --git a/Semantics.Paths/Utilities/PooledStringBuilder.cs b/Semantics.Paths/Utilities/PooledStringBuilder.cs index 503d85ac..05568884 100644 --- a/Semantics.Paths/Utilities/PooledStringBuilder.cs +++ b/Semantics.Paths/Utilities/PooledStringBuilder.cs @@ -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 diff --git a/Semantics.Test/Paths/PathIntegrationTests.cs b/Semantics.Test/Paths/PathIntegrationTests.cs index 9bef792d..b8dab022 100644 --- a/Semantics.Test/Paths/PathIntegrationTests.cs +++ b/Semantics.Test/Paths/PathIntegrationTests.cs @@ -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] diff --git a/Semantics.Test/Paths/PathOperatorTests.cs b/Semantics.Test/Paths/PathOperatorTests.cs index 15336676..64036cab 100644 --- a/Semantics.Test/Paths/PathOperatorTests.cs +++ b/Semantics.Test/Paths/PathOperatorTests.cs @@ -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("a"); + RelativeFilePath file = RelativeFilePath.Create("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("a").Parent; - Assert.AreEqual(string.Empty, empty.WeakString); + RelativeDirectoryPath empty = RelativeDirectoryPath.Create(string.Empty); RelativeFilePath file = empty / FileName.Create("x.txt"); Assert.AreEqual("x.txt", file.WeakString); diff --git a/Semantics.Test/Paths/RelativePathPropertyTests.cs b/Semantics.Test/Paths/RelativePathPropertyTests.cs index e484363b..183ee2d6 100644 --- a/Semantics.Test/Paths/RelativePathPropertyTests.cs +++ b/Semantics.Test/Paths/RelativePathPropertyTests.cs @@ -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("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(Path.GetTempPath()); + + RelativeDirectoryPath parent = RelativeDirectoryPath.Create("myapp").Parent; + + Assert.AreEqual(Path.GetFullPath(Path.GetTempPath()).TrimEnd(Path.DirectorySeparatorChar), parent.AsAbsolute(baseDirectory).WeakString.TrimEnd(Path.DirectorySeparatorChar)); } [TestMethod] @@ -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("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("file.txt"); + + Assert.AreEqual(".", file.DirectoryPath.WeakString); + Assert.IsTrue(file.DirectoryPath.IsDirectory); } [TestMethod] @@ -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", "..")] diff --git a/Semantics.Test/SemanticPathTests.cs b/Semantics.Test/SemanticPathTests.cs index 9fd3de3b..80fc0bee 100644 --- a/Semantics.Test/SemanticPathTests.cs +++ b/Semantics.Test/SemanticPathTests.cs @@ -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("file.txt"); DirectoryPath directoryPath = filePath.DirectoryPath; - Assert.AreEqual("", directoryPath.ToString()); + Assert.AreEqual(".", directoryPath.ToString()); } [TestMethod]