Skip to content

Commit adcbb45

Browse files
committed
Allow building an absolute path from a relative one
1 parent 3b0dc2e commit adcbb45

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

Pathy.ApiVerificationTests/ApprovedApi/pathy.net47.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Pathy
2020
public static Pathy.ChainablePath New { get; }
2121
public static Pathy.ChainablePath Temp { get; }
2222
public bool HasExtension(string extension) { }
23+
public Pathy.ChainablePath ToAbsolute() { }
24+
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
2325
public System.IO.DirectoryInfo ToDirectoryInfo() { }
2426
public System.IO.FileInfo ToFileInfo() { }
2527
public override string ToString() { }

Pathy.ApiVerificationTests/ApprovedApi/pathy.net8.0.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace Pathy
2121
public static Pathy.ChainablePath Temp { get; }
2222
public Pathy.ChainablePath AsRelativeTo(Pathy.ChainablePath basePath) { }
2323
public bool HasExtension(string extension) { }
24+
public Pathy.ChainablePath ToAbsolute() { }
25+
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
2426
public System.IO.DirectoryInfo ToDirectoryInfo() { }
2527
public System.IO.FileInfo ToFileInfo() { }
2628
public override string ToString() { }

Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.0.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Pathy
2020
public static Pathy.ChainablePath New { get; }
2121
public static Pathy.ChainablePath Temp { get; }
2222
public bool HasExtension(string extension) { }
23+
public Pathy.ChainablePath ToAbsolute() { }
24+
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
2325
public System.IO.DirectoryInfo ToDirectoryInfo() { }
2426
public System.IO.FileInfo ToFileInfo() { }
2527
public override string ToString() { }

Pathy.ApiVerificationTests/ApprovedApi/pathy.netstandard2.1.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace Pathy
2121
public static Pathy.ChainablePath Temp { get; }
2222
public Pathy.ChainablePath AsRelativeTo(Pathy.ChainablePath basePath) { }
2323
public bool HasExtension(string extension) { }
24+
public Pathy.ChainablePath ToAbsolute() { }
25+
public object ToAbsolute(Pathy.ChainablePath parentPath) { }
2426
public System.IO.DirectoryInfo ToDirectoryInfo() { }
2527
public System.IO.FileInfo ToFileInfo() { }
2628
public override string ToString() { }

Pathy.Specs/ChainablePathSpecs.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,47 @@ public void Can_build_from_a_relative_path()
116116
path.IsRooted.Should().BeFalse();
117117
}
118118

119+
[Fact]
120+
public void Can_convert_the_relative_path_to_an_absolute_path_using_the_current_working_directory()
121+
{
122+
// Arrange
123+
var path = ChainablePath.From("temp/somefile.txt");
124+
125+
// Act
126+
var absolutePath = path.ToAbsolute();
127+
128+
// Assert
129+
absolutePath.ToString().Should().Be(Path.Combine(Environment.CurrentDirectory, "temp", "somefile.txt"));
130+
}
131+
132+
[Fact]
133+
public void Can_combine_a_relative_path_using_a_specific_absolute_path()
134+
{
135+
// Arrange
136+
var path = ChainablePath.From("temp/somefile.txt");
137+
138+
// Act
139+
var absolutePath = path.ToAbsolute(ChainablePath.Temp);
140+
141+
// Assert
142+
absolutePath.ToString().Should().Be(Path.Combine(Path.GetTempPath(), "temp", "somefile.txt"));
143+
}
144+
145+
[Theory]
146+
[InlineData("")]
147+
[InlineData(null)]
148+
public void The_absolute_path_must_be_valid(string absolutePath)
149+
{
150+
// Arrange
151+
var path = ChainablePath.From("temp/somefile.txt");
152+
153+
// Act
154+
Action act = () => path.ToAbsolute(absolutePath);
155+
156+
// Assert
157+
act.Should().Throw<ArgumentException>("*absolutePath*");
158+
}
159+
119160
[Fact]
120161
public void Can_start_with_an_empty_path()
121162
{

Pathy/ChainablePath.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,28 @@ public bool HasExtension(string extension)
341341

342342
return string.Equals(Extension, extension, StringComparison.OrdinalIgnoreCase);
343343
}
344+
345+
/// <summary>
346+
/// Converts the current <see cref="ChainablePath"/> to an absolute path using the current working directory.
347+
/// </summary>
348+
public ChainablePath ToAbsolute()
349+
{
350+
return Path.GetFullPath(this);
351+
}
352+
353+
/// <summary>
354+
/// Converts the current <see cref="ChainablePath"/> instance to an absolute path, using the specified parent
355+
/// path as the base.
356+
/// </summary>
357+
public object ToAbsolute(ChainablePath parentPath)
358+
{
359+
if (!parentPath.IsRooted)
360+
{
361+
throw new ArgumentException("Parent path must be an absolute path", nameof(parentPath));
362+
}
363+
364+
return From(Path.Combine(parentPath.ToString(), this.ToString()));
365+
}
344366
}
345367

346368
#if PATHY_PUBLIC

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Given an instance of `ChainablePath`, you can get a lot of useful information:
114114
* Want to know the delta between two paths? Use `AsRelativeTo`.
115115
* To determine if a file has a case-insensitive extension, use `HasExtension(".txt")` or `HasExtension("txt")`.
116116

117+
Other features
118+
* Build an absolute path from a relative path using `ToAbsolute` to use the current directory as the base or `ToAbsolute(parentPath)` to use something else as the base.
119+
117120
And if the built-in functionality really isn't enough, you can always call `ToDirectoryInfo` or `ToFileInfo` to continue with an instance of `DirectoryInfo` and `FileInfo`.
118121

119122
### Globbing

0 commit comments

Comments
 (0)