Skip to content

Commit 96b80da

Browse files
authored
Switch to AwesomeAssertions (#113425)
* Switch to AwesomeAssertions * React to brekaing API changes
1 parent 066ee97 commit 96b80da

File tree

9 files changed

+55
-44
lines changed

9 files changed

+55
-44
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
<NewtonsoftJsonBsonVersion>1.0.2</NewtonsoftJsonBsonVersion>
202202
<SQLitePCLRawbundle_greenVersion>2.0.4</SQLitePCLRawbundle_greenVersion>
203203
<MoqVersion>4.18.4</MoqVersion>
204-
<FluentAssertionsVersion>6.7.0</FluentAssertionsVersion>
204+
<AwesomeAssertionsVersion>8.0.2</AwesomeAssertionsVersion>
205205
<FsCheckVersion>2.14.3</FsCheckVersion>
206206
<CommandLineParserVersion>2.9.1</CommandLineParserVersion>
207207
<!-- Android gRPC client tests -->

src/installer/tests/HostActivation.Tests/DependencyResolution/DependencyResolutionCommandResultExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ public static class DependencyResolutionCommandResultExtensions
1818
public static AndConstraint<CommandResultAssertions> HaveRuntimePropertyContaining(this CommandResultAssertions assertion, string propertyName, params string[] values)
1919
{
2020
string propertyValue = GetAppMockPropertyValue(assertion, propertyName);
21+
AssertionChain assertionChain = AssertionChain.GetOrCreate();
2122

2223
foreach (string value in values)
2324
{
24-
Execute.Assertion.ForCondition(propertyValue != null && propertyValue.Contains(value))
25+
assertionChain.ForCondition(propertyValue != null && propertyValue.Contains(value))
2526
.FailWith($"The property {propertyName} doesn't contain expected value: '{value}'{Environment.NewLine}" +
2627
$"{propertyName}='{propertyValue}'" +
2728
$"{assertion.GetDiagnosticsInfo()}");
@@ -33,10 +34,11 @@ public static AndConstraint<CommandResultAssertions> HaveRuntimePropertyContaini
3334
public static AndConstraint<CommandResultAssertions> NotHaveRuntimePropertyContaining(this CommandResultAssertions assertion, string propertyName, params string[] values)
3435
{
3536
string propertyValue = GetAppMockPropertyValue(assertion, propertyName);
37+
AssertionChain assertionChain = AssertionChain.GetOrCreate();
3638

3739
foreach (string value in values)
3840
{
39-
Execute.Assertion.ForCondition(propertyValue != null && !propertyValue.Contains(value))
41+
assertionChain.ForCondition(propertyValue != null && !propertyValue.Contains(value))
4042
.FailWith($"The property {propertyName} contains unexpected value: '{value}'{Environment.NewLine}" +
4143
$"{propertyName}='{propertyValue}'" +
4244
$"{assertion.GetDiagnosticsInfo()}");
@@ -80,10 +82,11 @@ public static AndConstraint<CommandResultAssertions> HaveResolvedComponentDepend
8082
params string[] values)
8183
{
8284
string propertyValue = GetComponentMockPropertyValue(assertion, propertyName);
85+
AssertionChain assertionChain = AssertionChain.GetOrCreate();
8386

8487
foreach (string value in values)
8588
{
86-
Execute.Assertion.ForCondition(propertyValue != null && propertyValue.Contains(value))
89+
assertionChain.ForCondition(propertyValue != null && propertyValue.Contains(value))
8790
.FailWith($"The resolved {propertyName} doesn't contain expected value: '{value}'{Environment.NewLine}" +
8891
$"{propertyName}='{propertyValue}'" +
8992
$"{assertion.GetDiagnosticsInfo()}");
@@ -98,10 +101,11 @@ public static AndConstraint<CommandResultAssertions> NotHaveResolvedComponentDep
98101
params string[] values)
99102
{
100103
string propertyValue = GetComponentMockPropertyValue(assertion, propertyName);
104+
AssertionChain assertionChain = AssertionChain.GetOrCreate();
101105

102106
foreach (string value in values)
103107
{
104-
Execute.Assertion.ForCondition(propertyValue != null && !propertyValue.Contains(value))
108+
assertionChain.ForCondition(propertyValue != null && !propertyValue.Contains(value))
105109
.FailWith($"The resolved {propertyName} contains unexpected value: '{value}'{Environment.NewLine}" +
106110
$"{propertyName}='{propertyValue}'" +
107111
$"{assertion.GetDiagnosticsInfo()}");

src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public class CommandResultAssertions
1414
{
1515
public CommandResult Result { get; }
1616

17-
public CommandResultAssertions(CommandResult commandResult)
17+
public AssertionChain CurrentAssertionChain { get; }
18+
19+
public CommandResultAssertions(CommandResult commandResult, AssertionChain assertionChain)
1820
{
1921
Result = commandResult;
22+
CurrentAssertionChain = assertionChain;
2023
}
2124

2225
public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
@@ -25,128 +28,128 @@ public AndConstraint<CommandResultAssertions> ExitWith(int expectedExitCode)
2528
if (!OperatingSystem.IsWindows())
2629
expectedExitCode = expectedExitCode & 0xFF;
2730

28-
Execute.Assertion.ForCondition(Result.ExitCode == expectedExitCode)
31+
CurrentAssertionChain.ForCondition(Result.ExitCode == expectedExitCode)
2932
.FailWith($"Expected command to exit with {expectedExitCode} but it did not.{GetDiagnosticsInfo()}");
3033
return new AndConstraint<CommandResultAssertions>(this);
3134
}
3235

3336
public AndConstraint<CommandResultAssertions> Pass()
3437
{
35-
Execute.Assertion.ForCondition(Result.ExitCode == 0)
38+
CurrentAssertionChain.ForCondition(Result.ExitCode == 0)
3639
.FailWith($"Expected command to pass but it did not.{GetDiagnosticsInfo()}");
3740
return new AndConstraint<CommandResultAssertions>(this);
3841
}
3942

4043
public AndConstraint<CommandResultAssertions> Fail()
4144
{
42-
Execute.Assertion.ForCondition(Result.ExitCode != 0)
45+
CurrentAssertionChain.ForCondition(Result.ExitCode != 0)
4346
.FailWith($"Expected command to fail but it did not.{GetDiagnosticsInfo()}");
4447
return new AndConstraint<CommandResultAssertions>(this);
4548
}
4649

4750
public AndConstraint<CommandResultAssertions> HaveStdOut()
4851
{
49-
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(Result.StdOut))
52+
CurrentAssertionChain.ForCondition(!string.IsNullOrEmpty(Result.StdOut))
5053
.FailWith($"Command did not output anything to stdout{GetDiagnosticsInfo()}");
5154
return new AndConstraint<CommandResultAssertions>(this);
5255
}
5356

5457
public AndConstraint<CommandResultAssertions> HaveStdOut(string expectedOutput)
5558
{
56-
Execute.Assertion.ForCondition(Result.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
59+
CurrentAssertionChain.ForCondition(Result.StdOut.Equals(expectedOutput, StringComparison.Ordinal))
5760
.FailWith($"Command did not output with Expected Output. Expected: '{expectedOutput}'{GetDiagnosticsInfo()}");
5861
return new AndConstraint<CommandResultAssertions>(this);
5962
}
6063

6164
public AndConstraint<CommandResultAssertions> HaveStdOutContaining(string pattern)
6265
{
63-
Execute.Assertion.ForCondition(Result.StdOut.Contains(pattern))
66+
CurrentAssertionChain.ForCondition(Result.StdOut.Contains(pattern))
6467
.FailWith($"The command output did not contain expected result: '{pattern}'{GetDiagnosticsInfo()}");
6568
return new AndConstraint<CommandResultAssertions>(this);
6669
}
6770

6871
public AndConstraint<CommandResultAssertions> NotHaveStdOutContaining(string pattern)
6972
{
70-
Execute.Assertion.ForCondition(!Result.StdOut.Contains(pattern))
73+
CurrentAssertionChain.ForCondition(!Result.StdOut.Contains(pattern))
7174
.FailWith($"The command output contained a result it should not have contained: '{pattern}'{GetDiagnosticsInfo()}");
7275
return new AndConstraint<CommandResultAssertions>(this);
7376
}
7477

7578
public AndConstraint<CommandResultAssertions> HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
7679
{
77-
Execute.Assertion.ForCondition(Regex.IsMatch(Result.StdOut, pattern, options))
80+
CurrentAssertionChain.ForCondition(Regex.IsMatch(Result.StdOut, pattern, options))
7881
.FailWith($"Matching the command output failed. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
7982
return new AndConstraint<CommandResultAssertions>(this);
8083
}
8184

8285
public AndConstraint<CommandResultAssertions> NotHaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
8386
{
84-
Execute.Assertion.ForCondition(!Regex.IsMatch(Result.StdOut, pattern, options))
87+
CurrentAssertionChain.ForCondition(!Regex.IsMatch(Result.StdOut, pattern, options))
8588
.FailWith($"The command output matched a pattern is should not have matched. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
8689
return new AndConstraint<CommandResultAssertions>(this);
8790
}
8891

8992
public AndConstraint<CommandResultAssertions> HaveStdErr()
9093
{
91-
Execute.Assertion.ForCondition(!string.IsNullOrEmpty(Result.StdErr))
94+
CurrentAssertionChain.ForCondition(!string.IsNullOrEmpty(Result.StdErr))
9295
.FailWith($"Command did not output anything to stderr.{GetDiagnosticsInfo()}");
9396
return new AndConstraint<CommandResultAssertions>(this);
9497
}
9598

9699
public AndConstraint<CommandResultAssertions> HaveStdErrContaining(string pattern)
97100
{
98-
Execute.Assertion.ForCondition(Result.StdErr.Contains(pattern))
101+
CurrentAssertionChain.ForCondition(Result.StdErr.Contains(pattern))
99102
.FailWith($"The command error output did not contain expected result: '{pattern}'{GetDiagnosticsInfo()}");
100103
return new AndConstraint<CommandResultAssertions>(this);
101104
}
102105

103106
public AndConstraint<CommandResultAssertions> NotHaveStdErrContaining(string pattern)
104107
{
105-
Execute.Assertion.ForCondition(!Result.StdErr.Contains(pattern))
108+
CurrentAssertionChain.ForCondition(!Result.StdErr.Contains(pattern))
106109
.FailWith($"The command error output contained a result it should not have contained: '{pattern}'{GetDiagnosticsInfo()}");
107110
return new AndConstraint<CommandResultAssertions>(this);
108111
}
109112

110113
public AndConstraint<CommandResultAssertions> HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None)
111114
{
112-
Execute.Assertion.ForCondition(Regex.IsMatch(Result.StdErr, pattern, options))
115+
CurrentAssertionChain.ForCondition(Regex.IsMatch(Result.StdErr, pattern, options))
113116
.FailWith($"Matching the command error output failed. Pattern: '{pattern}'{GetDiagnosticsInfo()}");
114117
return new AndConstraint<CommandResultAssertions>(this);
115118
}
116119

117120
public AndConstraint<CommandResultAssertions> NotHaveStdOut()
118121
{
119-
Execute.Assertion.ForCondition(string.IsNullOrEmpty(Result.StdOut))
122+
CurrentAssertionChain.ForCondition(string.IsNullOrEmpty(Result.StdOut))
120123
.FailWith($"Expected command to not output to stdout but it did:{GetDiagnosticsInfo()}");
121124
return new AndConstraint<CommandResultAssertions>(this);
122125
}
123126

124127
public AndConstraint<CommandResultAssertions> NotHaveStdErr()
125128
{
126-
Execute.Assertion.ForCondition(string.IsNullOrEmpty(Result.StdErr))
129+
CurrentAssertionChain.ForCondition(string.IsNullOrEmpty(Result.StdErr))
127130
.FailWith($"Expected command to not output to stderr but it did:{GetDiagnosticsInfo()}");
128131
return new AndConstraint<CommandResultAssertions>(this);
129132
}
130133

131134
public AndConstraint<CommandResultAssertions> FileExists(string path)
132135
{
133-
Execute.Assertion.ForCondition(System.IO.File.Exists(path))
136+
CurrentAssertionChain.ForCondition(System.IO.File.Exists(path))
134137
.FailWith($"The command did not write the expected file: '{path}'{GetDiagnosticsInfo()}");
135138
return new AndConstraint<CommandResultAssertions>(this);
136139
}
137140

138141
public AndConstraint<CommandResultAssertions> FileContains(string path, string pattern)
139142
{
140143
string fileContent = System.IO.File.ReadAllText(path);
141-
Execute.Assertion.ForCondition(fileContent.Contains(pattern))
144+
CurrentAssertionChain.ForCondition(fileContent.Contains(pattern))
142145
.FailWith($"The command did not write the expected result '{pattern}' to the file: '{path}'{GetDiagnosticsInfo()}{Environment.NewLine}file content: >>{fileContent}<<");
143146
return new AndConstraint<CommandResultAssertions>(this);
144147
}
145148

146149
public AndConstraint<CommandResultAssertions> NotFileContains(string path, string pattern)
147150
{
148151
string fileContent = System.IO.File.ReadAllText(path);
149-
Execute.Assertion.ForCondition(!fileContent.Contains(pattern))
152+
CurrentAssertionChain.ForCondition(!fileContent.Contains(pattern))
150153
.FailWith($"The command did not write the expected result '{pattern}' to the file: '{path}'{GetDiagnosticsInfo()}{Environment.NewLine}file content: >>{fileContent}<<");
151154
return new AndConstraint<CommandResultAssertions>(this);
152155
}

src/installer/tests/TestUtils/Assertions/CommandResultExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using FluentAssertions;
5+
using FluentAssertions.Execution;
56
using Microsoft.DotNet.Cli.Build.Framework;
67
using System;
78

@@ -11,13 +12,13 @@ public static class CommandResultExtensions
1112
{
1213
public static CommandResultAssertions Should(this CommandResult commandResult)
1314
{
14-
return new CommandResultAssertions(commandResult);
15+
return new CommandResultAssertions(commandResult, AssertionChain.GetOrCreate());
1516
}
1617

1718
public static CommandResult StdErrAfter(this CommandResult commandResult, string pattern)
1819
{
1920
int i = commandResult.StdErr.IndexOf(pattern);
20-
i.Should().BeGreaterOrEqualTo(
21+
i.Should().BeGreaterThanOrEqualTo(
2122
0,
2223
"Trying to filter StdErr after '{0}', but such string can't be found in the StdErr.{1}{2}",
2324
pattern,

src/installer/tests/TestUtils/Assertions/DirectoryInfoAssertions.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,35 @@ namespace Microsoft.DotNet.CoreSetup.Test
1313
public class DirectoryInfoAssertions
1414
{
1515
private DirectoryInfo _dirInfo;
16+
private AssertionChain _assertionChain;
1617

17-
public DirectoryInfoAssertions(DirectoryInfo dir)
18+
public DirectoryInfoAssertions(DirectoryInfo dir, AssertionChain assertionChain)
1819
{
1920
_dirInfo = dir;
21+
_assertionChain = assertionChain;
2022
}
2123

2224
public DirectoryInfo DirectoryInfo => _dirInfo;
2325

2426
public AndConstraint<DirectoryInfoAssertions> Exist()
2527
{
26-
Execute.Assertion.ForCondition(_dirInfo.Exists)
28+
_assertionChain.ForCondition(_dirInfo.Exists)
2729
.FailWith($"Expected directory '{_dirInfo.FullName}' does not exist.");
2830
return new AndConstraint<DirectoryInfoAssertions>(this);
2931
}
3032

3133
public AndConstraint<DirectoryInfoAssertions> HaveFile(string expectedFile)
3234
{
3335
var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
34-
Execute.Assertion.ForCondition(file != null)
36+
_assertionChain.ForCondition(file != null)
3537
.FailWith($"Expected File '{expectedFile}' cannot be found in directory '{_dirInfo.FullName}.");
3638
return new AndConstraint<DirectoryInfoAssertions>(this);
3739
}
3840

3941
public AndConstraint<DirectoryInfoAssertions> NotHaveFile(string expectedFile)
4042
{
4143
var file = _dirInfo.EnumerateFiles(expectedFile, SearchOption.TopDirectoryOnly).SingleOrDefault();
42-
Execute.Assertion.ForCondition(file == null)
44+
_assertionChain.ForCondition(file == null)
4345
.FailWith($"File '{expectedFile}' should not be found in directory '{_dirInfo.FullName}'.");
4446
return new AndConstraint<DirectoryInfoAssertions>(this);
4547
}
@@ -67,19 +69,19 @@ public AndConstraint<DirectoryInfoAssertions> NotHaveFiles(IEnumerable<string> e
6769
public AndConstraint<DirectoryInfoAssertions> HaveDirectory(string expectedDir)
6870
{
6971
var dir = _dirInfo.EnumerateDirectories(expectedDir, SearchOption.TopDirectoryOnly).SingleOrDefault();
70-
Execute.Assertion.ForCondition(dir != null)
72+
_assertionChain.ForCondition(dir != null)
7173
.FailWith($"Expected directory '{expectedDir}' cannot be found inside directory '{_dirInfo.FullName}'.");
7274

73-
return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir));
75+
return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir, _assertionChain));
7476
}
7577

7678
public AndConstraint<DirectoryInfoAssertions> NotHaveDirectory(string expectedDir)
7779
{
7880
var dir = _dirInfo.EnumerateDirectories(expectedDir, SearchOption.TopDirectoryOnly).SingleOrDefault();
79-
Execute.Assertion.ForCondition(dir == null)
81+
_assertionChain.ForCondition(dir == null)
8082
.FailWith($"Directory '{expectedDir}' should not be found in found inside directory '{_dirInfo.FullName}'.");
8183

82-
return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir));
84+
return new AndConstraint<DirectoryInfoAssertions>(new DirectoryInfoAssertions(dir, _assertionChain));
8385
}
8486

8587
public AndConstraint<DirectoryInfoAssertions> OnlyHaveFiles(IEnumerable<string> expectedFiles)
@@ -89,10 +91,10 @@ public AndConstraint<DirectoryInfoAssertions> OnlyHaveFiles(IEnumerable<string>
8991
var extraFiles = Enumerable.Except(actualFiles, expectedFiles);
9092
var nl = Environment.NewLine;
9193

92-
Execute.Assertion.ForCondition(!missingFiles.Any())
94+
_assertionChain.ForCondition(!missingFiles.Any())
9395
.FailWith($"Following files cannot be found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, missingFiles)}");
9496

95-
Execute.Assertion.ForCondition(!extraFiles.Any())
97+
_assertionChain.ForCondition(!extraFiles.Any())
9698
.FailWith($"Following extra files are found inside directory {_dirInfo.FullName} {nl} {string.Join(nl, extraFiles)}");
9799

98100
return new AndConstraint<DirectoryInfoAssertions>(this);
@@ -103,7 +105,7 @@ public AndConstraint<DirectoryInfoAssertions> NotBeModifiedAfter(DateTime timeUt
103105
_dirInfo.Refresh();
104106
DateTime writeTime = _dirInfo.LastWriteTimeUtc;
105107

106-
Execute.Assertion.ForCondition(writeTime <= timeUtc)
108+
_assertionChain.ForCondition(writeTime <= timeUtc)
107109
.FailWith($"Directory '{_dirInfo.FullName}' should not be modified after {timeUtc}, but is modified at {writeTime}.");
108110

109111
return new AndConstraint<DirectoryInfoAssertions>(this);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using FluentAssertions.Execution;
45
using System.IO;
56

67
namespace Microsoft.DotNet.CoreSetup.Test
@@ -9,7 +10,7 @@ public static class DirectoryInfoExtensions
910
{
1011
public static DirectoryInfoAssertions Should(this DirectoryInfo dir)
1112
{
12-
return new DirectoryInfoAssertions(dir);
13+
return new DirectoryInfoAssertions(dir, AssertionChain.GetOrCreate());
1314
}
1415
}
1516
}

src/installer/tests/TestUtils/TestUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
18+
<PackageReference Include="AwesomeAssertions" Version="$(AwesomeAssertionsVersion)" />
1919
<PackageReference Include="Microsoft.DotNet.XUnitExtensions" Version="$(MicrosoftDotNetXUnitExtensionsVersion)" />
2020
<PackageReference Include="xunit.core" Version="$(XUnitVersion)" ExcludeAssets="build" />
2121
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(MicrosoftExtensionsDependencyModelVersion)" />

0 commit comments

Comments
 (0)