Skip to content

Commit 42dbd78

Browse files
committed
ability to run as a sanity test
1 parent be5e620 commit 42dbd78

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: dotnet pack -p:PackageOutputPath="${GITHUB_WORKSPACE}/packages" -p:IncludeSymbols=false -p:RepositoryCommit=${GITHUB_SHA} -p:PackageVersion="0.0.0.1"
3030

3131
- name: Test
32-
run: dotnet test --no-build --verbosity normal
32+
run: dotnet test --framework net8.0 --no-build --verbosity normal
3333

3434
- name: Upload Binary Log
3535
uses: actions/upload-artifact@v3

Src/Basic.Reference.Assemblies.UnitTests/Basic.Reference.Assemblies.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;net472</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
<RollForward>LatestMajor</RollForward>
77
</PropertyGroup>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Basic.Reference.Assemblies.UnitTests;
12+
internal static class CompilationUtil
13+
{
14+
public static MemoryStream CompileToLibrary(string code, string assemblyName)
15+
{
16+
var references =
17+
#if NET
18+
Net80.References.All;
19+
#else
20+
Net461.References.All;
21+
#endif
22+
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
23+
var compilation = CSharpCompilation.Create(
24+
assemblyName,
25+
[CSharpSyntaxTree.ParseText(code)],
26+
references,
27+
options);
28+
29+
var peStream = new MemoryStream();
30+
var emitResult = compilation.Emit(peStream);
31+
if (!emitResult.Success)
32+
{
33+
throw new Exception(GetMessage(emitResult.Diagnostics));
34+
}
35+
36+
peStream.Position = 0;
37+
return peStream;
38+
39+
static string GetMessage(IEnumerable<Diagnostic> diagnostics)
40+
{
41+
var builder = new StringBuilder();
42+
builder.AppendLine("Compilation failed with the following errors:");
43+
foreach (var d in diagnostics)
44+
{
45+
builder.AppendLine(d.ToString());
46+
}
47+
return builder.ToString();
48+
}
49+
}
50+
51+
public static Assembly CompileToLibraryAndLoad(string code, string assemblyName)
52+
{
53+
var stream = CompileToLibrary(code, assemblyName);
54+
return Load(stream, assemblyName);
55+
}
56+
57+
/// <summary>
58+
/// Compile and run the code expecting to find a static Lib.Go method
59+
/// </summary>
60+
public static string? CompileAndRun(string code, string assemblyName)
61+
{
62+
var assembly = CompileToLibraryAndLoad(code, assemblyName);
63+
var libType = assembly
64+
.GetTypes()
65+
.Where(x => x.Name == "Lib")
66+
.Single();
67+
var method = libType.GetMethod("Go", BindingFlags.Static | BindingFlags.NonPublic);
68+
var obj = method!.Invoke(null, null);
69+
return (string?)obj;
70+
}
71+
72+
public static Assembly Load(MemoryStream stream, string assemblyName)
73+
{
74+
stream.Position = 0;
75+
#if NET
76+
var alc = new System.Runtime.Loader.AssemblyLoadContext(assemblyName);
77+
return alc.LoadFromStream(stream);
78+
#else
79+
return Assembly.Load(stream.ToArray());
80+
#endif
81+
}
82+
}

Src/Basic.Reference.Assemblies.UnitTests/Extensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
23
using System;
34
using System.Collections.Generic;
5+
using System.IO;
46
using System.Linq;
7+
using System.Reflection;
58
using System.Text;
69
using System.Threading.Tasks;
710

Src/Basic.Reference.Assemblies.UnitTests/SanityUnitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,21 @@ static void Main()
176176
Assert.True(emitResult.Success);
177177
Assert.Empty(emitResult.Diagnostics);
178178
}
179+
180+
[Fact]
181+
public void RunTuple()
182+
{
183+
var source = """
184+
static class Lib
185+
{
186+
public static string Go()
187+
{
188+
var tuple = (1, 2);
189+
return tuple.ToString();
190+
}
191+
}
192+
""";
193+
var actual = CompilationUtil.CompileAndRun(source, nameof(RunTuple));
194+
Assert.Equal("(1, 2)", actual);
195+
}
179196
}

0 commit comments

Comments
 (0)