Skip to content

Commit ae7c4cd

Browse files
committed
Nullable properties returning ChainablePath now return ChainablePath.Empty
1 parent e82032f commit ae7c4cd

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Pathy
33
{
44
public readonly struct ChainablePath : System.IEquatable<Pathy.ChainablePath>
55
{
6-
public Pathy.ChainablePath? Directory { get; }
6+
public Pathy.ChainablePath Directory { get; }
77
public bool DirectoryExists { get; }
88
public string DirectoryName { get; }
99
public bool Exists { get; }
@@ -16,6 +16,7 @@ namespace Pathy
1616
public Pathy.ChainablePath Parent { get; }
1717
public Pathy.ChainablePath Root { get; }
1818
public static Pathy.ChainablePath Current { get; }
19+
public static Pathy.ChainablePath Empty { get; }
1920
public static Pathy.ChainablePath New { get; }
2021
public static Pathy.ChainablePath Temp { get; }
2122
public System.IO.DirectoryInfo ToDirectoryInfo() { }
@@ -38,4 +39,4 @@ namespace Pathy
3839
{
3940
public static Pathy.ChainablePath ToPath(this string path) { }
4041
}
41-
}
42+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Pathy
33
{
44
public readonly struct ChainablePath : System.IEquatable<Pathy.ChainablePath>
55
{
6-
public Pathy.ChainablePath? Directory { get; }
6+
public Pathy.ChainablePath Directory { get; }
77
public bool DirectoryExists { get; }
88
public string DirectoryName { get; }
99
public bool Exists { get; }
@@ -16,6 +16,7 @@ namespace Pathy
1616
public Pathy.ChainablePath Parent { get; }
1717
public Pathy.ChainablePath Root { get; }
1818
public static Pathy.ChainablePath Current { get; }
19+
public static Pathy.ChainablePath Empty { get; }
1920
public static Pathy.ChainablePath New { get; }
2021
public static Pathy.ChainablePath Temp { get; }
2122
public Pathy.ChainablePath AsRelativeTo(Pathy.ChainablePath basePath) { }
@@ -39,4 +40,4 @@ namespace Pathy
3940
{
4041
public static Pathy.ChainablePath ToPath(this string path) { }
4142
}
42-
}
43+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Pathy
33
{
44
public readonly struct ChainablePath : System.IEquatable<Pathy.ChainablePath>
55
{
6-
public Pathy.ChainablePath? Directory { get; }
6+
public Pathy.ChainablePath Directory { get; }
77
public bool DirectoryExists { get; }
88
public string DirectoryName { get; }
99
public bool Exists { get; }
@@ -16,6 +16,7 @@ namespace Pathy
1616
public Pathy.ChainablePath Parent { get; }
1717
public Pathy.ChainablePath Root { get; }
1818
public static Pathy.ChainablePath Current { get; }
19+
public static Pathy.ChainablePath Empty { get; }
1920
public static Pathy.ChainablePath New { get; }
2021
public static Pathy.ChainablePath Temp { get; }
2122
public System.IO.DirectoryInfo ToDirectoryInfo() { }
@@ -38,4 +39,4 @@ namespace Pathy
3839
{
3940
public static Pathy.ChainablePath ToPath(this string path) { }
4041
}
41-
}
42+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Pathy
33
{
44
public readonly struct ChainablePath : System.IEquatable<Pathy.ChainablePath>
55
{
6-
public Pathy.ChainablePath? Directory { get; }
6+
public Pathy.ChainablePath Directory { get; }
77
public bool DirectoryExists { get; }
88
public string DirectoryName { get; }
99
public bool Exists { get; }
@@ -16,6 +16,7 @@ namespace Pathy
1616
public Pathy.ChainablePath Parent { get; }
1717
public Pathy.ChainablePath Root { get; }
1818
public static Pathy.ChainablePath Current { get; }
19+
public static Pathy.ChainablePath Empty { get; }
1920
public static Pathy.ChainablePath New { get; }
2021
public static Pathy.ChainablePath Temp { get; }
2122
public Pathy.ChainablePath AsRelativeTo(Pathy.ChainablePath basePath) { }
@@ -39,4 +40,4 @@ namespace Pathy
3940
{
4041
public static Pathy.ChainablePath ToPath(this string path) { }
4142
}
42-
}
43+
}

Pathy.Specs/ChainablePathSpecs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ChainablePathSpecs
1313

1414
public ChainablePathSpecs()
1515
{
16-
testFolder = ChainablePath.Temp / nameof(ChainablePathSpecs);
16+
testFolder = ChainablePath.Temp / nameof(ChainablePathSpecs) / Environment.Version.ToString();
1717
testFolder.DeleteFileOrDirectory();
1818
testFolder.CreateDirectoryRecursively();
1919
}
@@ -310,7 +310,8 @@ public void The_root_does_not_have_a_parent_directory()
310310
var path = ChainablePath.From("C://");
311311

312312
// Assert
313-
path.Directory.Should().BeNull();
313+
path.Directory.Should().Be(ChainablePath.Empty);
314+
path.Directory.ToString().Should().BeEmpty();
314315
}
315316

316317
[Fact]

Pathy/ChainablePath.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ public readonly record struct ChainablePath
1717
internal readonly record struct ChainablePath
1818
#endif
1919
{
20-
private readonly string path;
20+
private readonly string path = string.Empty;
2121

2222
private ChainablePath(string path)
2323
{
2424
this.path = path;
2525
}
2626

27+
/// <summary>
28+
/// Represents an empty <see cref="ChainablePath"/> instance.
29+
/// </summary>
30+
public static ChainablePath Empty { get; } = new(string.Empty);
31+
2732
/// <summary>
2833
/// Gets a default, empty <see cref="ChainablePath"/> instance.
2934
/// </summary>
@@ -158,39 +163,39 @@ private static string NormalizeSlashes(string path)
158163
/// <summary>
159164
/// If the current path represents a file, gets the directory of that file.
160165
/// Or, if the current path represents a directory, gets the parent directory.
161-
/// Returns <c>null</c> if the path represents the root of a file system.
166+
/// Returns <see cref="Empty"/> if the path represents the root of a file system.
162167
/// </summary>
163-
public ChainablePath? Directory
168+
public ChainablePath Directory
164169
{
165170
get
166171
{
167172
string directory = DirectoryName;
168-
if (directory is not null)
173+
if (directory.Length > 0)
169174
{
170175
return From(directory);
171176
}
172177

173-
return null;
178+
return Empty;
174179
}
175180
}
176181

177182
/// <summary>
178183
/// If the current path represents a file, gets the directory of that file.
179184
/// Or, if the current path represents a directory, gets the parent directory.
180-
/// Returns <c>null</c> if the path represents the root of a file system.
185+
/// Returns <see cref="Empty"/> if the path represents the root of a file system.
181186
/// </summary>
182187
public ChainablePath Parent => From(DirectoryName);
183188

184189
/// <summary>
185190
/// If the current path represents a file, gets the directory of that file.
186191
/// Or, if the current path represents a directory, gets the parent directory.
187-
/// Returns <c>null</c> if the path represents the root of a file system.
192+
/// Returns an empty string if the path represents the root of a file system.
188193
/// </summary>
189194
public string DirectoryName
190195
{
191196
get
192197
{
193-
return Path.GetDirectoryName(path.TrimEnd(Path.DirectorySeparatorChar));
198+
return Path.GetDirectoryName(path.TrimEnd(Path.DirectorySeparatorChar)) ?? "";
194199
}
195200
}
196201

0 commit comments

Comments
 (0)