Skip to content

Commit 4d501d9

Browse files
authored
BinaryFormatter tests should be skipped only on AOT, WASM and Mobile (#106858)
* respect AppContext switch (which is currently enabled for all projects in the root Directory.Build.props file) * add project reference to all test projects that need working BF (and were being skipped for a while) * adjust to changes from #104202: EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
1 parent b234aef commit 4d501d9

File tree

22 files changed

+109
-16
lines changed

22 files changed

+109
-16
lines changed

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -737,22 +737,8 @@ private static bool DetermineBinaryFormatterSupport()
737737
return false;
738738
}
739739

740-
Assembly assembly = typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter).Assembly;
741-
AssemblyName name = assembly.GetName();
742-
Version assemblyVersion = name.Version;
743-
744-
bool isSupported = true;
745-
746-
// Version 8.1 is the version in the shared runtime (.NET 9+) that has the type disabled with no config.
747-
// Assembly versions beyond 8.1 are the fully functional version from NuGet.
748-
// Assembly versions before 8.1 probably won't be encountered, since that's the past.
749-
750-
if (assemblyVersion.Major == 8 && assemblyVersion.Minor == 1)
751-
{
752-
isSupported = false;
753-
}
754-
755-
return isSupported;
740+
return AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out bool isBinaryFormatterEnabled)
741+
&& isBinaryFormatterEnabled;
756742
}
757743
}
758744
}

src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Microsoft.Extensions.Hosting.Unit.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@
1919
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Hosting\src\Microsoft.Extensions.Hosting.csproj" />
2020
</ItemGroup>
2121

22+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
23+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
24+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
25+
Private="true"
26+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
27+
</ItemGroup>
28+
2229
</Project>

src/libraries/System.Collections.NonGeneric/tests/System.Collections.NonGeneric.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,10 @@
4444
<Compile Include="CaseInsensitiveHashCodeProviderTests.cs" />
4545
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs"
4646
Link="Common\System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs" />
47+
48+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
49+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
50+
Private="true"
51+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
4752
</ItemGroup>
4853
</Project>

src/libraries/System.Collections.Specialized/tests/System.Collections.Specialized.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,10 @@
7575
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs"
7676
Link="Common\System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs" />
7777
<Compile Include="NameObjectCollectionBase\NameObjectCollectionBase.ConstructorTests.cs" />
78+
79+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
80+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
81+
Private="true"
82+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
7883
</ItemGroup>
7984
</Project>

src/libraries/System.Collections/tests/Generic/Dictionary/Dictionary.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ private static void TestComparerSerialization<T>(IEqualityComparer<T> equalityCo
452452
s.Position = 0;
453453
dict = (Dictionary<T, T>)bf.Deserialize(s);
454454

455+
if (equalityComparer.Equals(EqualityComparer<string>.Default))
456+
{
457+
// EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
458+
Assert.Equal("System.Collections.Generic.GenericEqualityComparer`1[System.String]", dict.Comparer.GetType().ToString());
459+
return;
460+
}
461+
455462
if (internalTypeName == null)
456463
{
457464
Assert.IsType(equalityComparer.GetType(), dict.Comparer);

src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,13 @@ static void TestComparerSerialization<TCompared>(IEqualityComparer<TCompared> eq
886886
s.Position = 0;
887887
set = (HashSet<TCompared>)bf.Deserialize(s);
888888

889+
if (equalityComparer.Equals(EqualityComparer<string>.Default))
890+
{
891+
// EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
892+
Assert.Equal("System.Collections.Generic.GenericEqualityComparer`1[System.String]", set.Comparer.GetType().ToString());
893+
return;
894+
}
895+
889896
if (internalTypeName == null)
890897
{
891898
Assert.IsType(equalityComparer.GetType(), set.Comparer);

src/libraries/System.Collections/tests/System.Collections.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<TestRuntime>true</TestRuntime>
55
<DebuggerSupport Condition="'$(DebuggerSupport)' == '' and ('$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi')">true</DebuggerSupport>
66
</PropertyGroup>
7+
<ItemGroup>
8+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
9+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
10+
Private="true"
11+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
12+
</ItemGroup>
713
<ItemGroup>
814
<RdXmlFile Include="default.rd.xml" />
915
</ItemGroup>

src/libraries/System.ComponentModel.Annotations/tests/System.ComponentModel.Annotations.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@
4848
<Compile Include="System\ComponentModel\DataAnnotations\ValidationResultTests.cs" />
4949
<Compile Include="System\ComponentModel\DataAnnotations\ValidatorTests.cs" />
5050
<Compile Include="System\ComponentModel\DataAnnotations\DeniedValuesAttributeTests.cs" />
51+
52+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
53+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
54+
Private="true"
55+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
5156
</ItemGroup>
5257
</Project>

src/libraries/System.ComponentModel.Primitives/tests/System.ComponentModel.Primitives.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@
2626
<Compile Include="System\ComponentModel\ParenthesizePropertyNameAttributeTests.cs" />
2727
<Compile Include="System\ComponentModel\ReadOnlyAttributeTests.cs" />
2828
<Compile Include="System\ComponentModel\RefreshPropertiesAttributeTests.cs" />
29+
30+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
31+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
32+
Private="true"
33+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
2934
</ItemGroup>
3035
</Project>

src/libraries/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@
166166
<ItemGroup>
167167
<PackageReference Include="System.ComponentModel.TypeConverter.TestData" Version="$(SystemComponentModelTypeConverterTestDataVersion)" />
168168
<PackageReference Include="Moq" Version="$(MoqVersion)" />
169+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
170+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
171+
Private="true"
172+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
169173
</ItemGroup>
170174
<ItemGroup>
171175
<EmbeddedResource Include="Resources\TestResx.resx">

0 commit comments

Comments
 (0)