Skip to content
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

Merged
merged 25 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
32d4cbc
Remove existing attributes
stephen-hawley Feb 2, 2022
5988de7
Attribute Conversion
stephen-hawley Feb 2, 2022
81ca9d9
Merge branch 'xamarin:main' into main
stephen-hawley Feb 3, 2022
ce4949e
Merge branch 'xamarin:main' into main
stephen-hawley Feb 4, 2022
f435edf
Merge branch 'xamarin:main' into main
stephen-hawley Mar 15, 2022
e227ef1
Merge branch 'xamarin:main' into main
stephen-hawley Mar 25, 2022
1e2d7f9
Merge branch 'xamarin:main' into main
stephen-hawley Mar 28, 2022
80bdf38
Merge branch 'xamarin:main' into main
stephen-hawley Mar 30, 2022
53f4dc3
Merge branch 'xamarin:main' into main
stephen-hawley Mar 31, 2022
4a19011
Merge branch 'xamarin:main' into main
stephen-hawley Apr 7, 2022
aebce8d
Merge branch 'xamarin:main' into main
stephen-hawley Apr 8, 2022
8208d2d
Merge branch 'xamarin:main' into main
stephen-hawley Apr 27, 2022
16c3f5b
Merge branch 'xamarin:main' into main
stephen-hawley May 2, 2022
a0b273f
Merge branch 'xamarin:main' into main
stephen-hawley May 2, 2022
277e2af
Merge branch 'xamarin:main' into main
stephen-hawley May 3, 2022
3db326e
Merge branch 'xamarin:main' into main
stephen-hawley May 4, 2022
b036949
Added the start of testing
stephen-hawley May 5, 2022
9ef5961
Merge branch 'main' into StartTesting
chamons May 6, 2022
50ded52
Move csproj to NET6 and more standard directory name
chamons May 6, 2022
c5e2099
Move to common execution and temporary directory services
chamons May 6, 2022
231945c
More tweaks and simplifications
chamons May 6, 2022
307fcf6
Move to explict assert
chamons May 6, 2022
27694be
Fix whitespace
chamons May 6, 2022
a9c8c20
Use the default language to avoid issues with nullability.
mandel-macaque May 6, 2022
20ebca7
Use the default language to avoid issues with nullability.
mandel-macaque May 6, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions tests/mtouch/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Runtime.InteropServices;
using System.Threading;

#nullable disable
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


namespace Xamarin
{
// A class that creates temporary directories next to the test assembly, and cleans the output on startup
Expand Down
2 changes: 2 additions & 0 deletions tools/common/Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Threading;
using System.Threading.Tasks;

#nullable disable
Copy link
Member

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.


namespace Xamarin.Utils {
public class Execution {
public string FileName;
Expand Down
2 changes: 2 additions & 0 deletions tools/common/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text;

#nullable disable
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here #14944


namespace Xamarin.Utils {
internal class StringUtils {
static StringUtils ()
Expand Down
6 changes: 6 additions & 0 deletions tools/nnyeah/nnyeah.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tools/nnyeah/tests/CompileALibrary.cs
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");
}
}
}
23 changes: 23 additions & 0 deletions tools/nnyeah/tests/nnyeah-tests.csproj
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>
88 changes: 88 additions & 0 deletions tools/nnyeah/tests/utils/Compiler.cs
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()!;
Copy link
Member

Choose a reason for hiding this comment

The 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 (),
};
}
}
46 changes: 46 additions & 0 deletions tools/nnyeah/tests/utils/TestRunning.cs
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);
}
}
}
1 change: 1 addition & 0 deletions tools/xibuild/xibuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down