Add ToUri and FromUri to ChainablePath - #157
Merged
Merged
Conversation
Implements the API proposal in issue #146. - ToUri() converts an absolute ChainablePath to a file:// Uri by delegating to the built-in Uri(string) constructor, which already handles escaping and UNC paths correctly. Throws InvalidOperationException for relative paths instead of silently resolving against the current directory, since that would be an implicit and surprising side effect for a path library. - FromUri(Uri) validates the Uri is absolute and uses the file scheme (throwing ArgumentException otherwise, and ArgumentNullException for a null Uri), then builds a ChainablePath from uri.LocalPath, which already handles UNC paths and percent-decoding. - Added unit tests covering round-tripping absolute paths, UNC paths, and paths with spaces/special characters, plus the relative path / non-file-scheme / relative Uri / null Uri failure cases. - Updated the approved API snapshots for all four target frameworks. Fixes #146 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This was referenced Aug 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the API proposal in #146:
Design decisions
ToUri()throws on relative paths instead of silently resolving against the current working directory. Implicitly resolving againstcwdwould be a surprising side effect for a path library — the caller should make that resolution explicit (e.g. viaToAbsolute()) before callingToUri(). ThrowsInvalidOperationExceptionwith a message stating the path must be absolute.ToUri()delegates tonew Uri(string)for absolute paths rather than hand-rolling escaping logic. The built-in constructor already produces correctfile://URIs (with proper percent-escaping) for both regular absolute paths and UNC paths.FromUri(Uri)restricts to thefilescheme and requires an absoluteUri, throwingArgumentExceptionotherwise (mentioning the rejected scheme), andArgumentNullExceptionfor a nullUri. It builds theChainablePathfromuri.LocalPath, which already handles UNC paths and percent-decoding correctly.Uriwas added (per the issue's rejected alternatives), andFromUrionly accepts aUri, not astring, keeping the surface minimal as scoped in the issue.Testing
Pathy.Specs/ChainablePathSpecs.cscovering: absolute path round-trip throughToUri/FromUri, relative path throwing onToUri, UNC path handling, rejection of non-file schemes, rejection of relative URIs, null-uri check, and paths with spaces/special characters.dotnet test Pathy.Specs— 117 passed (net8.0).dotnet test Pathy.ApiVerificationTests— 8 passed across all four target frameworks (net47, net8.0, netstandard2.0, netstandard2.1) after regenerating the approved API snapshots viaAcceptApiChanges.ps1.Fixes #146