diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e65098..646e842 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] (1.3.0) +### Added +- [#38: Introduce `AbsolutePath::CurrentWorkingDirectory`](https://github.com/ForNeVeR/TruePath/issues/38). + ### Changed - [#17: `AbsolutePath::Parent` should not re-check the path's absoluteness](https://github.com/ForNeVeR/TruePath/issues/17) (a performance optimization). diff --git a/README.md b/README.md index 35695a1..54450c8 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,9 @@ This functions basically the same as the `LocalPath`, but it is _always_ an abso To convert from `LocalPath` to `AbsolutePath` and vice versa, you can use the constructors of `AbsolutePath` and `LocalPath` respectively. Any `AbsolutePath` constructor (from either a string or a `LocalPath`) has same check for absolute path, and any `LocalPath` constructor (from either a string or an `AbsolutePath`) doesn't have any checks. +#### Static Members +- `AbsolutePath.CurrentWorkingDirectory`: returns `Environment.CurrentDirectory` as an `AbsolutePath` instance. + ### `IPath` This is an interface that is implemented by both `LocalPath` and `AbsolutePath`. It allows to process any paths in a polymorphic way. diff --git a/TruePath.Tests/AbsolutePathTests.cs b/TruePath.Tests/AbsolutePathTests.cs index 9bb446b..0620c84 100644 --- a/TruePath.Tests/AbsolutePathTests.cs +++ b/TruePath.Tests/AbsolutePathTests.cs @@ -6,6 +6,29 @@ namespace TruePath.Tests; public class AbsolutePathTests { + [Fact] + public void CurrentWorkingDirectoryShouldReturnCorrectAbsolutePath() + { + // Arrange + var expectedPath = Directory.GetCurrentDirectory(); + + // Act + var actualPath = AbsolutePath.CurrentWorkingDirectory; + + // Assert + Assert.Equal(expectedPath, actualPath.Value); + } + + [Fact] + public void CurrentWorkingDirectoryShouldBeAbsolute() + { + // Act + var path = AbsolutePath.CurrentWorkingDirectory; + + // Assert + Assert.True(Path.IsPathRooted(path.Value)); + } + [Fact] public void PathIsNormalizedOnCreation() { diff --git a/TruePath/AbsolutePath.cs b/TruePath/AbsolutePath.cs index 1f9304a..8f82864 100644 --- a/TruePath/AbsolutePath.cs +++ b/TruePath/AbsolutePath.cs @@ -54,6 +54,14 @@ public AbsolutePath(LocalPath localPath) : this(localPath.Value, checkAbsolutene /// public string FileName => Underlying.FileName; + /// + /// Gets the current working directory as an AbsolutePath instance. + /// + /// + /// The current working directory. + /// + public static AbsolutePath CurrentWorkingDirectory => new(Environment.CurrentDirectory); + /// /// Calculates the relative path from a base path to this path. ///