From 30b61038e8eeb7e32fbff9126ddb5841b20abf55 Mon Sep 17 00:00:00 2001 From: fskalkan Date: Sun, 13 Sep 2026 17:31:27 +0300 Subject: [PATCH] feat: add AbsolutePath.PathRoot() API --- TruePath.Tests/AbsolutePathTests.cs | 17 +++++++++++++++++ TruePath/AbsolutePath.cs | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/TruePath.Tests/AbsolutePathTests.cs b/TruePath.Tests/AbsolutePathTests.cs index c7f784e..e0efe21 100644 --- a/TruePath.Tests/AbsolutePathTests.cs +++ b/TruePath.Tests/AbsolutePathTests.cs @@ -16,6 +16,23 @@ public void ConstructionTest() Assert.Equal($"{root}...", path.Value); } + [Fact] + public void PathRootReturnsRoot() + { + var root = new AbsolutePath(OperatingSystem.IsWindows() ? @"A:\" : "/"); + var path = root / "foo" / "bar"; + + Assert.Equal(root, path.PathRoot()); + } + + [Fact] + public void PathRootOfRootReturnsItself() + { + var root = new AbsolutePath(OperatingSystem.IsWindows() ? @"A:\" : "/"); + + Assert.Equal(root, root.PathRoot()); + } + [Fact] public void ReadKind_NonExistent() { diff --git a/TruePath/AbsolutePath.cs b/TruePath/AbsolutePath.cs index 23ed853..79f306d 100644 --- a/TruePath/AbsolutePath.cs +++ b/TruePath/AbsolutePath.cs @@ -74,6 +74,10 @@ public AbsolutePath(LocalPath localPath) : this(localPath.Value, checkAbsolutene /// public AbsolutePath? Parent => Underlying.Parent is { } path ? new(path.Value, checkAbsoluteness: false) : null; + /// Gets the root of this path. + public AbsolutePath PathRoot() => + new(Path.GetPathRoot(Value)!, checkAbsoluteness: false); + /// IPath? IPath.Parent => Parent;