Skip to content

Commit 6479533

Browse files
authored
v5.6.3 (#96)
- *Fixed:* `TestSetUp` fixed to load _all_ assemblies on start up (versus selective) to ensure all `OneOffTestSetUpAttribute` implementations are executed prior to any test executions. - *Fixed:* Added `IJsonSerializer.Clone()` and `JsonElementComparerOptions.Clone` to ensure cross test contamination does not occur.
1 parent 6ae6f07 commit 6479533

File tree

9 files changed

+32
-13
lines changed

9 files changed

+32
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Represents the **NuGet** versions.
44

5+
## v5.6.3
6+
- *Fixed:* `TestSetUp` fixed to load _all_ assemblies on start up (versus selective) to ensure all `OneOffTestSetUpAttribute` implementations are executed prior to any test executions.
7+
- *Fixed:* Added `IJsonSerializer.Clone()` and `JsonElementComparerOptions.Clone` to ensure cross test contamination does not occur.
8+
59
## v5.6.2
610
- *Fixed:* Republish packages with a new version; last publish was incomplete.
711

Common.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>5.6.2</Version>
3+
<Version>5.6.3</Version>
44
<LangVersion>preview</LangVersion>
55
<Authors>Avanade</Authors>
66
<Company>Avanade</Company>

src/UnitTestEx/Abstractions/OneOffTestSetUpAttribute.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ namespace UnitTestEx.Abstractions
1414
[AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
1515
public class OneOffTestSetUpAttribute(Type type) : Attribute
1616
{
17-
private static readonly object _oneOffSetUpLock = new();
18-
private static readonly HashSet<Type> _oneOffSetUpTypes = [];
19-
2017
/// <summary>
2118
/// Performs the set up for the specified <paramref name="assembly"/> where configured using the <see cref="OneOffTestSetUpAttribute"/>.
2219
/// </summary>

src/UnitTestEx/Abstractions/TesterBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public abstract class TesterBase
3535
/// </summary>
3636
static TesterBase()
3737
{
38+
TestSetUp.Force();
39+
3840
try
3941
{
4042
var fi = new FileInfo(Path.Combine(Environment.CurrentDirectory, "appsettings.unittest.json"));

src/UnitTestEx/Json/IJsonSerializer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,11 @@ public interface IJsonSerializer
4545
/// <param name="json">The JSON <see cref="string"/>.</param>
4646
/// <returns>The corresponding typed value.</returns>
4747
T? Deserialize<T>(string json);
48+
49+
/// <summary>
50+
/// Clones the <see cref="IJsonSerializer"/>.
51+
/// </summary>
52+
/// <returns>The cloned <see cref="IJsonSerializer"/>.</returns>
53+
IJsonSerializer Clone();
4854
}
4955
}

src/UnitTestEx/Json/JsonElementComparerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static JsonElementComparerOptions Default
7777
MaxDifferences = MaxDifferences,
7878
ValueComparison = ValueComparison,
7979
NullComparison = NullComparison,
80-
JsonSerializer = JsonSerializer
80+
JsonSerializer = JsonSerializer?.Clone()
8181
};
8282
}
8383
}

src/UnitTestEx/Json/JsonSerializer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,8 @@ public JsonSerializer(Stj.JsonSerializerOptions? options = null)
6464

6565
/// <inheritdoc/>
6666
public string Serialize<T>(T value, JsonWriteFormat? format = null) => Stj.JsonSerializer.Serialize(value, format == null || format.Value == JsonWriteFormat.None ? Options : IndentedOptions);
67+
68+
/// <inheritdoc/>
69+
public IJsonSerializer Clone() => new JsonSerializer(new Stj.JsonSerializerOptions(Options));
6770
}
6871
}

src/UnitTestEx/TestSetUp.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class TestSetUp : ICloneable
2525
{
2626
private static readonly SemaphoreSlim _semaphore = new(1, 1);
2727
private static readonly ConcurrentQueue<string> _autoSetUpOutputs = new();
28+
private static readonly ConcurrentDictionary<string, object?> _registeredAssemblies = new();
2829

2930
private TestSetUp? _clonedFrom;
3031
private bool _setUpSet = false;
@@ -40,13 +41,16 @@ public class TestSetUp : ICloneable
4041
/// <remarks>Wires up the <see cref="OneOffTestSetUpAttribute.SetUp()"/> invocation whenever an <see cref="Assembly"/> is <see cref="AppDomain.AssemblyLoad">loaded.</see></remarks>
4142
static TestSetUp()
4243
{
43-
// Load dependent UnitTestEx assemblies as they may not have been loaded yet!
44-
foreach (var fi in new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).EnumerateFiles("*.dll").Where(f => f.Name.StartsWith("UnitTestEx.")))
44+
// Load all dependent assemblies to ensure all one off test set up(s) execute before any test exection is perfomed.
45+
foreach (var fi in new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).EnumerateFiles("*.dll"))
4546
Assembly.LoadFrom(fi.FullName);
4647

4748
// Wire up for any assemblies already loaded.
4849
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
49-
OneOffTestSetUpAttribute.SetUp(assembly);
50+
{
51+
if (_registeredAssemblies.TryAdd(assembly.FullName!, null))
52+
OneOffTestSetUpAttribute.SetUp(assembly);
53+
}
5054

5155
// Wire up for any future assembly loading.
5256
AppDomain.CurrentDomain.AssemblyLoad += (_, e) => OneOffTestSetUpAttribute.SetUp(e.LoadedAssembly);
@@ -199,8 +203,8 @@ public static void LogAutoSetUpOutputs(TestFrameworkImplementor implementor)
199203
/// <remarks>The <see cref="RegisterSetUp"/> and <see cref="RegisterAutoSetUp"/> will reference the originating unless explicitly registered (overridden) for the cloned instance.</remarks>
200204
public TestSetUp Clone() => new()
201205
{
202-
JsonSerializer = JsonSerializer,
203-
JsonComparerOptions = JsonComparerOptions,
206+
JsonSerializer = JsonSerializer.Clone(),
207+
JsonComparerOptions = JsonComparerOptions.Clone(),
204208
Properties = new Dictionary<string, object?>(Properties),
205209
DefaultUserName = DefaultUserName,
206210
UserNameConverter = UserNameConverter,
@@ -255,7 +259,7 @@ public void RegisterAutoSetUp(Func<int, object?, CancellationToken, Task<(bool,
255259
public async Task<bool> SetUpAsync(object? data = null, CancellationToken cancellationToken = default)
256260
{
257261
if (SetUpFunc == null && AutoSetUpFunc == null)
258-
throw new InvalidOperationException("Set up can not be invoked as no set up function has been registered; please use RegisterSetUp() ot AutoRegisterSetUp() to enable.");
262+
throw new InvalidOperationException("Set up can not be invoked as no set up function has been registered; please use RegisterSetUp() or AutoRegisterSetUp() to enable.");
259263

260264
await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
261265
try

tests/UnitTestEx.MSTest.Test/NewtonsoftJsonSerializer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class NewtonsoftJsonSerializer : IJsonSerializer
6666
/// <summary>
6767
/// Copies the settings.
6868
/// </summary>
69-
private Nsj.JsonSerializerSettings CopySettings(JsonWriteFormat format)
69+
private Nsj.JsonSerializerSettings CopySettings(JsonWriteFormat? format)
7070
{
7171
var s = new Nsj.JsonSerializerSettings
7272
{
@@ -89,7 +89,7 @@ private Nsj.JsonSerializerSettings CopySettings(JsonWriteFormat format)
8989
Context = Settings.Context,
9090
DateFormatString = Settings.DateFormatString,
9191
MaxDepth = Settings.MaxDepth,
92-
Formatting = format == JsonWriteFormat.None ? Nsj.Formatting.None : Nsj.Formatting.Indented,
92+
Formatting = format is null ? Settings.Formatting : format == JsonWriteFormat.None ? Nsj.Formatting.None : Nsj.Formatting.Indented,
9393
DateFormatHandling = Settings.DateFormatHandling,
9494
DateTimeZoneHandling = Settings.DateTimeZoneHandling,
9595
DateParseHandling = Settings.DateParseHandling,
@@ -105,6 +105,9 @@ private Nsj.JsonSerializerSettings CopySettings(JsonWriteFormat format)
105105

106106
return s;
107107
}
108+
109+
/// <inheritdoc/>
110+
public IJsonSerializer Clone() => new NewtonsoftJsonSerializer(CopySettings(null));
108111
}
109112
}
110113

0 commit comments

Comments
 (0)