Skip to content

Commit c5b23fe

Browse files
committed
Fix netstandard 2.0 test error of CodeDOM
1 parent 700d58c commit c5b23fe

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

test/MsgPack.UnitTest/Serialization/SerializerGeneratorTest.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,25 @@
1919
#endregion -- License Terms --
2020

2121
using System;
22+
#if !NETSTANDARD2_0
2223
using System.CodeDom.Compiler;
24+
#endif // !NETSTANDARD2_0
2325
using System.Collections;
2426
using System.Collections.Generic;
27+
#if NETSTANDARD2_0
28+
using System.Globalization;
29+
#endif // NETSTANDARD2_0
2530
using System.IO;
2631
using System.Linq;
2732
using System.Reflection;
2833
using System.Text.RegularExpressions;
2934

35+
#if NETSTANDARD2_0
36+
using Microsoft.CodeAnalysis;
37+
using Microsoft.CodeAnalysis.CSharp;
38+
using Microsoft.CodeAnalysis.Emit;
39+
#endif // NETSTANDARD2_0
40+
3041
using MsgPack.Serialization.Reflection;
3142

3243
using NUnit.Framework;
@@ -1708,6 +1719,8 @@ private static void TestOnWorkerAppDomainForMultiple( string geneartedAssemblyFi
17081719

17091720
private static void AssertValidCode( IEnumerable<SerializerCodeGenerationResult> results )
17101721
{
1722+
#if !NETSTANDARD2_0
1723+
17111724
var result =
17121725
CodeDomProvider
17131726
.CreateProvider( "C#" )
@@ -1735,14 +1748,88 @@ private static void AssertValidCode( IEnumerable<SerializerCodeGenerationResult>
17351748
{
17361749
File.Delete( result.PathToAssembly );
17371750
}
1751+
1752+
#else // !NETSTANDARD2_0
1753+
1754+
var assemblyName = "CodeGenerationAssembly" + DateTime.UtcNow.ToString( "yyyyMMddHHmmssfff" );
1755+
var metadataList =
1756+
new TempFileDependentAssemblyManager( TestContext.CurrentContext.TestDirectory ).CodeSerializerDependentAssemblies
1757+
.Concat( new[] { Assembly.GetExecutingAssembly().Location } )
1758+
.Select(
1759+
a =>
1760+
a is string
1761+
? AssemblyMetadata.CreateFromFile( a as string )
1762+
: AssemblyMetadata.CreateFromImage( a as byte[] )
1763+
).ToArray();
1764+
try
1765+
{
1766+
var compilation =
1767+
CSharpCompilation.Create(
1768+
assemblyName,
1769+
results.Select( r => CSharpSyntaxTree.ParseText( File.ReadAllText( r.FilePath ) ) ),
1770+
metadataList.Select( m => m.GetReference() ),
1771+
new CSharpCompilationOptions(
1772+
OutputKind.DynamicallyLinkedLibrary,
1773+
optimizationLevel: OptimizationLevel.Debug,
1774+
// Suppress CS0436 because gen/*.cs will conflict with testing serializers.
1775+
specificDiagnosticOptions: new[] { new KeyValuePair<string, ReportDiagnostic>( "CS0436", ReportDiagnostic.Suppress ) }
1776+
)
1777+
);
1778+
1779+
var emitOptions = new EmitOptions( runtimeMetadataVersion: "v4.0.30319" );
1780+
EmitResult result;
1781+
using ( var buffer = new MemoryStream() )
1782+
{
1783+
result = compilation.Emit( buffer, options: emitOptions );
1784+
}
1785+
1786+
Assert.That(
1787+
result.Diagnostics.Any( d => d.Severity == DiagnosticSeverity.Error || d.Severity == DiagnosticSeverity.Warning ),
1788+
Is.False,
1789+
String.Join( Environment.NewLine, GetCompileErrorLines( result.Diagnostics ).ToArray() )
1790+
);
1791+
}
1792+
finally
1793+
{
1794+
foreach ( var metadata in metadataList )
1795+
{
1796+
metadata.Dispose();
1797+
}
1798+
}
1799+
#endif // !NETSTANDARD2_0
17381800
}
17391801

1802+
#if !NETSTANDARD2_0
1803+
17401804
private static IEnumerable<string> GetCompileErrorLines( CompilerError error )
17411805
{
17421806
yield return error.ToString();
17431807
yield return File.ReadAllLines( error.FileName ).Skip( error.Line - 1 ).First();
17441808
}
17451809

1810+
#else // !NETSTANDARD2_0
1811+
1812+
private static IEnumerable<string> GetCompileErrorLines( IEnumerable<Diagnostic> diagnostics )
1813+
{
1814+
return
1815+
diagnostics.Select(
1816+
( diagnostic, i ) =>
1817+
String.Format(
1818+
CultureInfo.InvariantCulture,
1819+
"[{0}]{1}:{2}:(File:{3}, Line:{4}, Column:{5}):{6}",
1820+
i,
1821+
diagnostic.Severity,
1822+
diagnostic.Id,
1823+
diagnostic.Location.GetLineSpan().Path,
1824+
diagnostic.Location.GetLineSpan().StartLinePosition.Line,
1825+
diagnostic.Location.GetLineSpan().StartLinePosition.Character,
1826+
diagnostic.GetMessage()
1827+
)
1828+
);
1829+
}
1830+
1831+
#endif // !NETSTANDARD2_0
1832+
17461833
public sealed class Tester : MarshalByRefObject
17471834
{
17481835
public void DoTest( string testAssemblyFile, int packerCompatiblityOptions, int serializationMethod, byte[] bytesValue, byte[] expectedPackedValue, int expectedSerializerTypeCounts, TestType testType )

test/MsgPack.UnitTest/Serialization/TempFileDependentAssemblyManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ protected override IEnumerable<string> GetRuntimeAssemblies()
103103
// .NET Standard 2.0 library should refer netstandard.dll
104104
yield return Path.Combine( coreSdkAssemblyDirectory, "netstandard.dll" );
105105
yield return typeof( object ).Assembly.Location; // System.Private.Corelib.dll
106+
yield return typeof( Uri ).Assembly.Location; // System.Private.Uri.dll
106107
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Runtime.dll" );
107108
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Collections.dll" );
108109
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Collections.NonGeneric.dll" );
@@ -112,10 +113,12 @@ protected override IEnumerable<string> GetRuntimeAssemblies()
112113
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Globalization.dll" );
113114
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Linq.dll" );
114115
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Numerics.Vectors.dll" );
116+
yield return Path.Combine( coreSdkAssemblyDirectory, "System.ObjectModel.dll" );
115117
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Reflection.dll" );
116118
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Reflection.Extensions.dll" );
117119
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Reflection.Primitives.dll" );
118120
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Runtime.Extensions.dll" );
121+
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Runtime.InteropServices.dll" );
119122
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Runtime.Numerics.dll" );
120123
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Runtime.Serialization.Primitives.dll" );
121124
yield return Path.Combine( coreSdkAssemblyDirectory, "System.Threading.dll" );

0 commit comments

Comments
 (0)