-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[nnyeah] Start testing #14913
[nnyeah] Start testing #14913
Changes from all commits
32d4cbc
5988de7
81ca9d9
ce4949e
f435edf
e227ef1
1e2d7f9
80bdf38
53f4dc3
4a19011
aebce8d
8208d2d
16c3f5b
a0b273f
277e2af
3db326e
b036949
9ef5961
50ded52
c5e2099
231945c
307fcf6
27694be
a9c8c20
20ebca7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#nullable disable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll take care of this in a diff PR. |
||
|
||
namespace Xamarin.Utils { | ||
public class Execution { | ||
public string FileName; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
using System.Linq; | ||
using System.Text; | ||
|
||
#nullable disable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll take care of this in a diff PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done here #14944 |
||
|
||
namespace Xamarin.Utils { | ||
internal class StringUtils { | ||
static StringUtils () | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this need of ! in a follow up PR. |
||
} | ||
|
||
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 (), | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take care of this in a diff PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done here https://github.com/xamarin/xamarin-macios/pull/14913/files