Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#38) add CurrentWorkingDirectory method to AbsolutePath #68

Merged
merged 3 commits into from
Jun 20, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
23 changes: 23 additions & 0 deletions TruePath.Tests/AbsolutePathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
8 changes: 8 additions & 0 deletions TruePath/AbsolutePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ private AbsolutePath(string value, bool checkAbsoluteness)
/// <inheritdoc cref="IPath.FileName"/>
public string FileName => Underlying.FileName;

/// <summary>
/// Gets the current working directory as an AbsolutePath instance.
/// </summary>
/// <value>
/// The current working directory.
/// </value>
public static AbsolutePath CurrentWorkingDirectory => new(Environment.CurrentDirectory);

/// <summary>
/// Calculates the relative path from a base path to this path.
/// </summary>
Expand Down
Loading