Skip to content

Commit 5364317

Browse files
authored
refactor: error message when no file or directory was found (#48)
When no matching file or directory was found in a directory, improve the error message to specify that "none" was found instead of the simple count (0).
1 parent 2474d0c commit 5364317

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

Source/Testably.Abstractions.FluentAssertions/DirectoryAssertions.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public AndConstraint<DirectoryAssertions> HasDirectories(
6262
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirectory(
6363
string searchPattern = "*", string because = "", params object[] becauseArgs)
6464
{
65+
var subdirectory = Subject?.GetDirectories(searchPattern).FirstOrDefault();
6566
Execute.Assertion
6667
.WithDefaultIdentifier(Identifier)
6768
.BecauseOf(because, becauseArgs)
@@ -75,16 +76,22 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirector
7576
.Then
7677
.Given(() => Subject!)
7778
.ForCondition(directoryInfo
78-
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
79+
=> directoryInfo.GetDirectories(searchPattern).Length <= 1)
7980
.FailWith(
8081
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
8182
_ => searchPattern,
8283
directoryInfo => directoryInfo.Name,
83-
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);
84+
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length)
85+
.Then
86+
.ForCondition(_ => subdirectory != null)
87+
.FailWith(
88+
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found none.",
89+
_ => searchPattern,
90+
directoryInfo => directoryInfo.Name);
8491

8592
return new AndWhichConstraint<FileSystemAssertions, DirectoryAssertions>(
8693
new FileSystemAssertions(Subject!.FileSystem),
87-
new DirectoryAssertions(Subject!.GetDirectories(searchPattern).Single()));
94+
new DirectoryAssertions(subdirectory));
8895
}
8996

9097
/// <summary>
@@ -93,6 +100,7 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirector
93100
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
94101
string searchPattern = "*", string because = "", params object[] becauseArgs)
95102
{
103+
var file = Subject?.GetFiles(searchPattern).FirstOrDefault();
96104
Execute.Assertion
97105
.WithDefaultIdentifier(Identifier)
98106
.BecauseOf(because, becauseArgs)
@@ -106,16 +114,22 @@ public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
106114
.Then
107115
.Given(() => Subject!)
108116
.ForCondition(directoryInfo
109-
=> directoryInfo.GetFiles(searchPattern).Length == 1)
117+
=> directoryInfo.GetFiles(searchPattern).Length <= 1)
110118
.FailWith(
111119
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
112120
_ => searchPattern,
113121
directoryInfo => directoryInfo.Name,
114-
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);
122+
directoryInfo => directoryInfo.GetFiles(searchPattern).Length)
123+
.Then
124+
.ForCondition(_ => file != null)
125+
.FailWith(
126+
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found none.",
127+
_ => searchPattern,
128+
directoryInfo => directoryInfo.Name);
115129

116130
return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
117131
new FileSystemAssertions(Subject!.FileSystem),
118-
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
132+
new FileAssertions(file));
119133
}
120134

121135
/// <summary>

Tests/Testably.Abstractions.FluentAssertions.Tests/DirectoryAssertionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void HasDirectory_WithoutMatchingDirectory_ShouldThrow(
183183
exception.Should().NotBeNull();
184184
exception!.Message.Should()
185185
.Be(
186-
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found 0.");
186+
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found none.");
187187
}
188188

189189
[Theory]
@@ -268,7 +268,7 @@ public void HasFile_WithoutMatchingFile_ShouldThrow(
268268
exception.Should().NotBeNull();
269269
exception!.Message.Should()
270270
.Be(
271-
$"Expected directory \"{directoryName}\" to contain exactly one file matching \"{fileName}\" {because}, but found 0.");
271+
$"Expected directory \"{directoryName}\" to contain exactly one file matching \"{fileName}\" {because}, but found none.");
272272
}
273273

274274
[Theory]

Tests/Testably.Abstractions.FluentAssertions.Tests/DirectoryInfoAssertionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void HaveDirectory_WithoutMatchingDirectory_ShouldThrow(
182182
exception.Should().NotBeNull();
183183
exception!.Message.Should()
184184
.Be(
185-
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found 0.");
185+
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found none.");
186186
}
187187

188188
[Theory]
@@ -282,7 +282,7 @@ public void HaveFile_WithoutMatchingFile_ShouldThrow(
282282
exception.Should().NotBeNull();
283283
exception!.Message.Should()
284284
.Be(
285-
$"Expected directory \"{directoryName}\" to contain exactly one file matching \"{fileName}\" {because}, but found 0.");
285+
$"Expected directory \"{directoryName}\" to contain exactly one file matching \"{fileName}\" {because}, but found none.");
286286
}
287287

288288
[Theory]

0 commit comments

Comments
 (0)