Added code to:

compile a string to a platform library
collect the output of the compilation process
check for errors
Added a single unit test of the smoke test variety.
This commit is contained in:
Steve Hawley 2022-05-09 09:42:34 -04:00 коммит произвёл GitHub
Родитель 073c2c1047
Коммит 5631ca5c8e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 203 добавлений и 0 удалений

Просмотреть файл

@ -9,6 +9,7 @@
<AssemblyName>generate-frameworks-constants</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<OutputPath>bin\$(Configuration)</OutputPath>
<LangVersion>default</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|anycpu' ">
<DebugSymbols>true</DebugSymbols>

Просмотреть файл

@ -4,6 +4,8 @@ using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
#nullable disable
namespace Xamarin
{
// A class that creates temporary directories next to the test assembly, and cleans the output on startup

Просмотреть файл

@ -16,6 +16,8 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
#nullable disable
namespace Xamarin.Utils {
public class Execution {
public string FileName;

Просмотреть файл

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
#nullable disable
namespace Xamarin.Utils {
internal class StringUtils {
static StringUtils ()

Просмотреть файл

@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.810.18
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nnyeah", "nnyeah\nnyeah.csproj", "{772B2205-5CFA-41A0-ACB5-A98301BC86FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nnyeah-tests", "tests\nnyeah-tests.csproj", "{0CDCFD15-98C0-4669-A00B-E5287086D606}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{772B2205-5CFA-41A0-ACB5-A98301BC86FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{772B2205-5CFA-41A0-ACB5-A98301BC86FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{772B2205-5CFA-41A0-ACB5-A98301BC86FD}.Release|Any CPU.Build.0 = Release|Any CPU
{0CDCFD15-98C0-4669-A00B-E5287086D606}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CDCFD15-98C0-4669-A00B-E5287086D606}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CDCFD15-98C0-4669-A00B-E5287086D606}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CDCFD15-98C0-4669-A00B-E5287086D606}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Просмотреть файл

@ -0,0 +1,32 @@
using System;
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin;
namespace Microsoft.MaciOS.Nnyeah.Tests.SmokeTests {
[TestFixture]
public class CompileALibrary {
[Test]
public async Task BasicLibrary ()
{
var dir = Cache.CreateTemporaryDirectory ("BasicLibrary");
var code = @"
using System;
public class Foo {
public nint Ident (nint e) => e;
}
";
var output = await TestRunning.BuildLibrary (code, "NoName", dir);
var expectedOutputFile = Path.Combine (dir, "NoName.dll");
Assert.IsTrue (File.Exists (expectedOutputFile));
}
[Test]
public void BasicExecutable ()
{
var dir = Cache.CreateTemporaryDirectory ("BasicExecutable");
}
}
}

Просмотреть файл

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="../../common/Execution.cs" Link="Execution.cs" />
<Compile Include="../../common/StringUtils.cs" Link="StringUtils.cs" />
<Compile Include="../../../tests/mtouch/Cache.cs" Link="Cache.cs" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,88 @@
using System;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Xamarin;
using Xamarin.Utils;
using System.Collections.Generic;
namespace Microsoft.MaciOS.Nnyeah.Tests {
public enum PlatformName {
None, // desktop managed executable
macOS, // Xamarin.Mac app
iOS,
watchOS,
tvOS,
}
public class Compiler {
const string MonoCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc";
public static async Task<string> CompileText (string text, string outputFile, PlatformName platformName, bool isLibrary)
{
var dir = Cache.CreateTemporaryDirectory ();
var outputCSFile = Path.Combine (dir, "LibraryFile.cs");
File.WriteAllText (outputCSFile, text);
return await Compile (outputFile, platformName, isLibrary, dir, outputCSFile);
}
public static async Task<string> Compile (string outputFile, PlatformName platformName, bool isLibrary, string workingDirectory, params string[] sourceFiles)
{
var compilerArgs = BuildCompilerArgs (sourceFiles, outputFile, platformName, isLibrary);
Execution execution = await Execution.RunAsync(MonoCompiler, compilerArgs, mergeOutput: true, workingDirectory: workingDirectory);
return execution!.StandardOutput.ToString()!;
}
static List<string> BuildCompilerArgs (string[] sourceFiles, string outputFile, PlatformName platformName,
bool isLibrary)
{
var args = new List<string>();
args.Add ("/unsafe");
args.Add ("/nostdlib+");
AppendPlatformReference (args, platformName, "mscorlib");
AppendPlatformReference (args, platformName, XamarinLibName (platformName));
args.Add ("/debug+");
args.Add ("/debug:full");
args.Add ("/optimize-");
args.Add ("/out:" + outputFile);
args.Add ("/target:" + (isLibrary ? "library" : "exe"));
foreach (var file in sourceFiles) {
args.Add (file);
}
return args;
}
static void AppendPlatformReference (List<string> args, PlatformName platformName, string libName)
{
args.Add("/reference:" + PlatformLibPath (platformName, libName));
}
static string PlatformLibPath (PlatformName platformName, string libName)
{
return Path.Combine (PlatformLibDirectory (platformName), $"{libName}.dll");
}
static string PlatformLibDirectory (PlatformName platformName) =>
platformName switch {
PlatformName.macOS => "/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/mono/Xamarin.Mac/",
PlatformName.iOS => "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS",
PlatformName.tvOS => "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.TVOS",
PlatformName.watchOS => "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.WatchOS",
_ => throw new NotImplementedException (),
};
static string XamarinLibName (PlatformName platformName) =>
platformName switch {
PlatformName.macOS => "Xamarin.Mac",
PlatformName.iOS => "Xamarin.iOS",
PlatformName.tvOS => "Xamarin.TVOS",
PlatformName.watchOS => "Xamarin.WatchOS",
_ => throw new NotImplementedException (),
};
}
}

Просмотреть файл

@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin;
namespace Microsoft.MaciOS.Nnyeah.Tests {
public class TestRunning {
static string GetInvokingTestName (out string callingMethodClass, string callingMethodName)
{
var stackTrace = new System.Diagnostics.StackTrace ();
var callingMethod = stackTrace.GetFrame (2)!.GetMethod ();
Assert.NotNull (callingMethod, "unable to get calling test from stack frame.");
if (string.IsNullOrEmpty (callingMethodName)) {
if (!callingMethod!.CustomAttributes.Any (x => x.AttributeType.Name == "TestAttribute")) {
Assert.Fail ("TestRunning expect invocations without an explicit `testName` parameter to be invoked from the [Test] method directly. Consider passing an explicit `testName`.");
}
callingMethodName = callingMethod.Name;
}
callingMethodClass = callingMethod!.DeclaringType!.Name;
return callingMethodName;
}
public static async Task TestAndExecute (string libraryCode, string callingCode, string expectedOutput,
string callingMethodName = "")
{
var testName = GetInvokingTestName (out var nameSpace, callingMethodName);
var testClassName = "NnyeahTest" + testName;
var initialLibraryDir = Cache.CreateTemporaryDirectory ();
var finalLibraryDir = Cache.CreateTemporaryDirectory ();
var libCompilerOutput = await BuildLibrary (libraryCode, testName, initialLibraryDir);
}
public static Task<string> BuildLibrary (string libraryCode, string libName, string outputDirectory, PlatformName platformName = PlatformName.macOS)
{
var outputPath = Path.Combine (outputDirectory, $"{libName}.dll");
return Compiler.CompileText (libraryCode, outputPath, platformName, isLibrary: true);
}
}
}

Просмотреть файл

@ -8,6 +8,7 @@
<RootNamespace>xibuild</RootNamespace>
<AssemblyName>xibuild</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<LangVersion>default</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>