Skip to content

Commit b4f25a5

Browse files
authored
Merge pull request #343 from Hau-Hau/195-Migrate-from-Fluent-Assertions
Migrate to shouldly
2 parents ed93454 + 469cfc2 commit b4f25a5

18 files changed

Lines changed: 88 additions & 85 deletions

.csharpierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# .csharpierignore
2+
!**/*.cs

EncryptedConfigValue.AspNetCore.Test/EncryptedConfigValue.AspNetCore.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentAssertions" Version="8.8.0" />
10+
<PackageReference Include="Shouldly" Version="4.3.0" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1212
<PackageReference Include="NSubstitute" Version="5.3.0" />
1313
<PackageReference Include="xunit" Version="2.9.3" />
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using EncryptedConfigValue.AspNetCore.Test.Util;
22
using EncryptedConfigValue.Crypto;
33
using EncryptedConfigValue.Crypto.Util;
4-
using FluentAssertions;
54
using Microsoft.Extensions.Configuration;
65
using System.Collections.Generic;
76
using System.IO;
87
using System.Linq;
98
using Xunit;
9+
using Shouldly;
10+
using System.Text.RegularExpressions;
1011

1112
namespace EncryptedConfigValue.AspNetCore.Test
1213
{
@@ -25,25 +26,25 @@ public void TestDecryptionSucceeds()
2526
.AddEncryptedConfigValueProvider()
2627
.Build();
2728

28-
configuration["Unencrypted"].Should().Be("value");
29-
configuration["Encrypted"].Should().Be("value");
30-
configuration["EncryptedWithSingleQuote"].Should().Be("don't use quotes");
31-
configuration["EncryptedWithDoubleQuote"].Should().Be("double quote is \"");
32-
configuration["EncryptedMalformedYaml"].Should().Be("[oh dear");
29+
configuration["Unencrypted"].ShouldBe("value");
30+
configuration["Encrypted"].ShouldBe("value");
31+
configuration["EncryptedWithSingleQuote"].ShouldBe("don't use quotes");
32+
configuration["EncryptedWithDoubleQuote"].ShouldBe("double quote is \"");
33+
configuration["EncryptedMalformedYaml"].ShouldBe("[oh dear");
3334

3435
configuration
3536
.GetSection("ArrayWithSomeEncryptedValues")
3637
.GetChildren()
3738
.Select(x => x.Value)
38-
.Should()
39-
.BeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
39+
.ToList()
40+
.ShouldBeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
4041

4142
var person = new Person();
4243
configuration
4344
.GetSection("PocoWithEncryptedValues")
4445
.Bind(person);
45-
person.Username.Should().BeEquivalentTo("some-user");
46-
person.Password.Should().BeEquivalentTo("value");
46+
person.Username.ShouldBeEquivalentTo("some-user");
47+
person.Password.ShouldBeEquivalentTo("value");
4748
}
4849

4950
[Fact]
@@ -53,11 +54,10 @@ public void TestDecryptionFailsWithNiceMessage()
5354
.AddJsonFile(Path.Combine("Resources", "testConfigWithError.json"), optional: false, reloadOnChange: false)
5455
.AddEncryptedConfigValueProvider()
5556
.Build();
56-
act.Should()
57-
.Throw<ConfigurationDecryptionException>()
58-
.WithMessage($"Configuration decryption error at {Path.Combine("Resources", "testConfigWithError.json")} (*)")
59-
.WithInnerExceptionExactly<StringSubstitutionException>()
60-
.WithMessage("The value 'enc:ERROR' for field 'arrayWithSomeEncryptedValues:3' could not be replaced");
57+
var ex = Should.Throw<ConfigurationDecryptionException>(act);
58+
ex.Message.ShouldMatch($@"Configuration decryption error at {Regex.Escape(Path.Combine("Resources", "testConfigWithError.json"))} \(.*\)");
59+
ex.InnerException.ShouldBeOfType<StringSubstitutionException>();
60+
ex.InnerException.Message.ShouldBe("The value 'enc:ERROR' for field 'ArrayWithSomeEncryptedValues:3' could not be replaced");
6161
}
6262
}
6363
}

EncryptedConfigValue.AspNetCore.Test/VariableSubstitutionTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using EncryptedConfigValue.AspNetCore.Test.Util;
22
using EncryptedConfigValue.Crypto;
3-
using FluentAssertions;
3+
using Shouldly;
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.Extensions.Configuration;
66
using System;
@@ -31,25 +31,25 @@ private static WebApplication CreateWebApplication()
3131
[Fact]
3232
public void TestCanDecryptValueInConfig()
3333
{
34-
aspnet.Configuration["Unencrypted"].Should().Be("value");
35-
aspnet.Configuration["Encrypted"].Should().Be("value");
36-
aspnet.Configuration["EncryptedWithSingleQuote"].Should().Be("don't use quotes");
37-
aspnet.Configuration["EncryptedWithDoubleQuote"].Should().Be("double quote is \"");
38-
aspnet.Configuration["EncryptedMalformedYaml"].Should().Be("[oh dear");
34+
aspnet.Configuration["Unencrypted"].ShouldBe("value");
35+
aspnet.Configuration["Encrypted"].ShouldBe("value");
36+
aspnet.Configuration["EncryptedWithSingleQuote"].ShouldBe("don't use quotes");
37+
aspnet.Configuration["EncryptedWithDoubleQuote"].ShouldBe("double quote is \"");
38+
aspnet.Configuration["EncryptedMalformedYaml"].ShouldBe("[oh dear");
3939

4040
aspnet.Configuration
4141
.GetSection("ArrayWithSomeEncryptedValues")
4242
.GetChildren()
4343
.Select(x => x.Value)
44-
.Should()
45-
.BeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
44+
.ToList()
45+
.ShouldBeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
4646

4747
var person = new Person();
4848
aspnet.Configuration
4949
.GetSection("PocoWithEncryptedValues")
5050
.Bind(person);
51-
person.Username.Should().BeEquivalentTo("some-user");
52-
person.Password.Should().BeEquivalentTo("value");
51+
person.Username.ShouldBeEquivalentTo("some-user");
52+
person.Password.ShouldBeEquivalentTo("value");
5353
}
5454
}
5555
}

EncryptedConfigValue.Cli.Test/EncryptConfigValueCommandTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using EncryptedConfigValue.Crypto;
22
using EncryptedConfigValue.Crypto.Algorithm;
3-
using FluentAssertions;
3+
using Shouldly;
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
@@ -69,7 +69,7 @@ internal void WeEncryptAndPrintAValue(Algorithm algorithm)
6969
var decryptionKey = keyPair.DecryptionKey;
7070
var decryptedValue = configValue.Decrypt(decryptionKey);
7171

72-
decryptedValue.Should().BeEquivalentTo(plaintext);
72+
decryptedValue.ShouldBeEquivalentTo(plaintext);
7373
}
7474

7575
[Fact]
@@ -100,7 +100,7 @@ internal void WeFailIfTheKeyfileDoesNotExist()
100100
command.Execute("--keyfile", tempFilePath, "--value", plaintext);
101101
};
102102

103-
act.Should().Throw<FileNotFoundException>();
103+
act.ShouldThrow<FileNotFoundException>();
104104
}
105105

106106
public static IEnumerable<object[]> Data() =>

EncryptedConfigValue.Cli.Test/EncryptedConfigValue.Cli.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="FluentAssertions" Version="8.8.0" />
19-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
18+
<PackageReference Include="Shouldly" Version="4.3.0" />
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
2020
<PackageReference Include="xunit" Version="2.9.3" />
2121
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
2222
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

EncryptedConfigValue.Cli.Test/GenerateKeyCommandTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using EncryptedConfigValue.Crypto;
22
using EncryptedConfigValue.Crypto.Algorithm;
3-
using FluentAssertions;
3+
using Shouldly;
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
@@ -42,7 +42,7 @@ public void WeGenerateAValidKey(Algorithm algorithm)
4242
command.Execute("--algorithm", algorithm.ToString(), "--file", tempFilePath);
4343

4444
var keyPair = KeyFileUtils.KeyPairFromPath(tempFilePath);
45-
keyPair.EncryptionKey.Type.Algorithm.Should().BeEquivalentTo(algorithm);
45+
keyPair.EncryptionKey.Type.Algorithm.ShouldBeEquivalentTo(algorithm);
4646
}
4747

4848
[Fact]
@@ -62,8 +62,8 @@ public void WeDoNotOverwriteAnExistingKeyfile()
6262
{
6363
command.Execute("--algorithm", algorithm.ToString(), "--file", tempFilePath);
6464
};
65-
66-
act.Should().Throw<IOException>().WithMessage("*already exists*");
65+
var ex = Should.Throw<IOException>(act);
66+
ex.Message.ShouldMatch("(.*)already exists*");
6767
}
6868

6969
public static IEnumerable<object[]> Data() =>

EncryptedConfigValue.Module.Test/DecryptingVariableSubstitutorTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using EncryptedConfigValue.Crypto.Algorithm;
33
using EncryptedConfigValue.Crypto.Util;
44
using EncryptedConfigValue.Module;
5-
using FluentAssertions;
5+
using Shouldly;
66
using System;
77
using System.Collections.Generic;
88
using System.IO;
@@ -63,7 +63,7 @@ public void Dispose()
6363
public void ConstantsAreNotModified()
6464
{
6565
substitutor.TryReplace("abc", out var output);
66-
output.Should().BeEquivalentTo("abc");
66+
output.ShouldBeEquivalentTo("abc");
6767
}
6868

6969
[Fact]
@@ -76,30 +76,30 @@ public void InvalidEncryptedVariablesThrowStringSubstitutionException()
7676
}
7777
catch (StringSubstitutionException e)
7878
{
79-
e.Value.Should().BeEquivalentTo("enc:invalid-contents");
80-
e.Field.Should().BeEmpty();
79+
e.Value.ShouldBeEquivalentTo("enc:invalid-contents");
80+
e.Field.ShouldBeEmpty();
8181
}
8282
}
8383

8484
[Fact]
8585
public void NonEncryptedVariablesAreNotModified()
8686
{
8787
substitutor.TryReplace("${abc}", out var output);
88-
output.Should().BeEquivalentTo("${abc}");
88+
output.ShouldBeEquivalentTo("${abc}");
8989
}
9090

9191
[Fact]
9292
public void VariableIsDecrypted()
9393
{
9494
substitutor.TryReplace("${" + Encrypt("abc") + "}", out var output);
95-
output.Should().BeEquivalentTo("abc");
95+
output.ShouldBeEquivalentTo("abc");
9696
}
9797

9898
[Fact]
9999
public void VariableIsDecryptedWithRegex()
100100
{
101101
substitutor.TryReplace("${" + Encrypt("$5") + "}", out var output);
102-
output.Should().Be("$5");
102+
output.ShouldBe("$5");
103103
}
104104

105105
[Fact]
@@ -110,7 +110,7 @@ public void DecryptsMultiple()
110110
var hello = "${" + Encrypt("enc:hello") + "}";
111111
var source = abc + ":" + def + '.' + hello;
112112
substitutor.TryReplace(source, out var output);
113-
output.Should().Be("abc:def.enc:hello");
113+
output.ShouldBe("abc:def.enc:hello");
114114
}
115115

116116
[Fact]
@@ -120,7 +120,7 @@ public void DecryptsWithPlaceholders()
120120
var def = "${" + Encrypt("${enc:test}") + "}";
121121
var source = abc + ":" + def;
122122
substitutor.TryReplace(source, out var output);
123-
output.Should().BeEquivalentTo("abc:${enc:test}");
123+
output.ShouldBeEquivalentTo("abc:${enc:test}");
124124
}
125125

126126
[SkippableTheory]
@@ -131,7 +131,7 @@ public void PropertyTestValues(string plaintext)
131131
Skip.If(Encoding.UTF8.GetBytes(plaintext).Length > 190);
132132
EnsureTestKeysExist();
133133
substitutor.TryReplace("${" + Encrypt(plaintext) + "}", out var output);
134-
output.Should().BeEquivalentTo(plaintext);
134+
output.ShouldBeEquivalentTo(plaintext);
135135
}
136136

137137
public static IEnumerable<object[]> PropertyTestData(int tries)

EncryptedConfigValue.Module.Test/EncryptedConfigValue.Module.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentAssertions" Version="8.8.0" />
10+
<PackageReference Include="Shouldly" Version="4.3.0" />
1111
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0" />
1212
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1515
<PackageReference Include="NSubstitute" Version="5.3.0" />
1616
<PackageReference Include="xunit" Version="2.9.3" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">

EncryptedConfigValue.Test/Aes/AesKeyTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using EncryptedConfigValue.Crypto.Algorithm.Aes;
2-
using FluentAssertions;
2+
using Shouldly;
33
using Org.BouncyCastle.Crypto;
44
using Org.BouncyCastle.Security;
55
using Xunit;
@@ -19,7 +19,7 @@ public static byte[] NewSecretKey()
1919
public void TestEqualityFromSameSecretKey()
2020
{
2121
var secretKey = NewSecretKey();
22-
new AesKey(secretKey).Should().BeEquivalentTo(new AesKey(secretKey));
22+
new AesKey(secretKey).ShouldBeEquivalentTo(new AesKey(secretKey));
2323
}
2424
}
2525
}

0 commit comments

Comments
 (0)