Skip to content

Commit

Permalink
refactor: error message when no file or directory was found (#48)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
vbreuss authored Sep 5, 2023
1 parent 2474d0c commit 5364317
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public AndConstraint<DirectoryAssertions> HasDirectories(
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirectory(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
var subdirectory = Subject?.GetDirectories(searchPattern).FirstOrDefault();
Execute.Assertion
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
Expand All @@ -75,16 +76,22 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirector
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
=> directoryInfo.GetDirectories(searchPattern).Length <= 1)
.FailWith(
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length)
.Then
.ForCondition(_ => subdirectory != null)
.FailWith(
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found none.",
_ => searchPattern,
directoryInfo => directoryInfo.Name);

return new AndWhichConstraint<FileSystemAssertions, DirectoryAssertions>(
new FileSystemAssertions(Subject!.FileSystem),
new DirectoryAssertions(Subject!.GetDirectories(searchPattern).Single()));
new DirectoryAssertions(subdirectory));
}

/// <summary>
Expand All @@ -93,6 +100,7 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirector
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
var file = Subject?.GetFiles(searchPattern).FirstOrDefault();
Execute.Assertion
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
Expand All @@ -106,16 +114,22 @@ public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetFiles(searchPattern).Length == 1)
=> directoryInfo.GetFiles(searchPattern).Length <= 1)
.FailWith(
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);
directoryInfo => directoryInfo.GetFiles(searchPattern).Length)
.Then
.ForCondition(_ => file != null)
.FailWith(
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found none.",
_ => searchPattern,
directoryInfo => directoryInfo.Name);

return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
new FileSystemAssertions(Subject!.FileSystem),
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
new FileAssertions(file));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void HasDirectory_WithoutMatchingDirectory_ShouldThrow(
exception.Should().NotBeNull();
exception!.Message.Should()
.Be(
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found 0.");
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found none.");
}

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

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void HaveDirectory_WithoutMatchingDirectory_ShouldThrow(
exception.Should().NotBeNull();
exception!.Message.Should()
.Be(
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found 0.");
$"Expected directory \"{directoryName}\" to contain exactly one directory matching \"{subdirectoryName}\" {because}, but found none.");
}

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

[Theory]
Expand Down

0 comments on commit 5364317

Please sign in to comment.