From 32d4cbc615810adbbefc5efabce6d72cdd944a6f Mon Sep 17 00:00:00 2001 From: Stephen Hawley Date: Wed, 2 Feb 2022 14:17:37 -0500 Subject: [PATCH 01/10] Remove existing attributes --- src/SensorKit/SRAbsoluteTime.cs | 6 ------ src/SensorKit/SRAmbientLightChromaticity.cs | 6 ------ 2 files changed, 12 deletions(-) diff --git a/src/SensorKit/SRAbsoluteTime.cs b/src/SensorKit/SRAbsoluteTime.cs index abf6f55593ae..73b5dcdd7b0e 100644 --- a/src/SensorKit/SRAbsoluteTime.cs +++ b/src/SensorKit/SRAbsoluteTime.cs @@ -7,15 +7,9 @@ namespace SensorKit { -#if NET - [SupportedOSPlatform ("ios14.0")] - [UnsupportedOSPlatform ("tvos")] - [UnsupportedOSPlatform ("macos")] -#else [NoWatch, NoTV, NoMac] [iOS (14,0)] [MacCatalyst (14,0)] -#endif public static class SRAbsoluteTime { [DllImport (Constants.SensorKitLibrary, EntryPoint = "SRAbsoluteTimeGetCurrent")] diff --git a/src/SensorKit/SRAmbientLightChromaticity.cs b/src/SensorKit/SRAmbientLightChromaticity.cs index a12c95d4320d..1e44a42b6f22 100644 --- a/src/SensorKit/SRAmbientLightChromaticity.cs +++ b/src/SensorKit/SRAmbientLightChromaticity.cs @@ -7,14 +7,8 @@ namespace SensorKit { -#if NET - [SupportedOSPlatform ("ios14.0")] - [UnsupportedOSPlatform ("tvos")] - [UnsupportedOSPlatform ("macos")] -#else [NoWatch, NoTV, NoMac] [iOS (14,0)] -#endif [StructLayout (LayoutKind.Sequential)] public struct SRAmbientLightChromaticity { public float X; From 5988de75af9e4f116ceb6f4202a28876516d7cbf Mon Sep 17 00:00:00 2001 From: Stephen Hawley Date: Wed, 2 Feb 2022 14:18:28 -0500 Subject: [PATCH 02/10] Attribute Conversion --- src/SensorKit/SRAbsoluteTime.cs | 11 ++++++++++- src/SensorKit/SRAmbientLightChromaticity.cs | 10 +++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/SensorKit/SRAbsoluteTime.cs b/src/SensorKit/SRAbsoluteTime.cs index 73b5dcdd7b0e..20f96ef69c90 100644 --- a/src/SensorKit/SRAbsoluteTime.cs +++ b/src/SensorKit/SRAbsoluteTime.cs @@ -7,9 +7,18 @@ namespace SensorKit { - [NoWatch, NoTV, NoMac] +#if NET + [SupportedOSPlatform ("ios14.0")] + [SupportedOSPlatform ("maccatalyst14.0")] + [UnsupportedOSPlatform ("tvos")] + [UnsupportedOSPlatform ("macos")] +#else + [NoWatch] + [NoTV] + [NoMac] [iOS (14,0)] [MacCatalyst (14,0)] +#endif public static class SRAbsoluteTime { [DllImport (Constants.SensorKitLibrary, EntryPoint = "SRAbsoluteTimeGetCurrent")] diff --git a/src/SensorKit/SRAmbientLightChromaticity.cs b/src/SensorKit/SRAmbientLightChromaticity.cs index 1e44a42b6f22..bb698adb3829 100644 --- a/src/SensorKit/SRAmbientLightChromaticity.cs +++ b/src/SensorKit/SRAmbientLightChromaticity.cs @@ -7,8 +7,16 @@ namespace SensorKit { - [NoWatch, NoTV, NoMac] +#if NET + [SupportedOSPlatform ("ios14.0")] + [UnsupportedOSPlatform ("tvos")] + [UnsupportedOSPlatform ("macos")] +#else + [NoWatch] + [NoTV] + [NoMac] [iOS (14,0)] +#endif [StructLayout (LayoutKind.Sequential)] public struct SRAmbientLightChromaticity { public float X; From b036949ecd9179a9e2b7207168122ad672d0c5f4 Mon Sep 17 00:00:00 2001 From: Stephen Hawley Date: Thu, 5 May 2022 15:59:55 -0400 Subject: [PATCH 03/10] Added the start of testing --- tools/nnyeah/NNyeahTests/Compiler.cs | 73 +++++++++++ .../NNyeahTests/DisposableTempDirectory.cs | 81 ++++++++++++ tools/nnyeah/NNyeahTests/Enums.cs | 10 ++ tools/nnyeah/NNyeahTests/ExecAndCollect.cs | 117 ++++++++++++++++++ tools/nnyeah/NNyeahTests/NNyeahTests.csproj | 46 +++++++ .../Smoke Tests/CompileALibrary.cs | 29 +++++ tools/nnyeah/NNyeahTests/TestRunning.cs | 42 +++++++ tools/nnyeah/nnyeah.sln | 6 + 8 files changed, 404 insertions(+) create mode 100644 tools/nnyeah/NNyeahTests/Compiler.cs create mode 100644 tools/nnyeah/NNyeahTests/DisposableTempDirectory.cs create mode 100644 tools/nnyeah/NNyeahTests/Enums.cs create mode 100644 tools/nnyeah/NNyeahTests/ExecAndCollect.cs create mode 100644 tools/nnyeah/NNyeahTests/NNyeahTests.csproj create mode 100644 tools/nnyeah/NNyeahTests/Smoke Tests/CompileALibrary.cs create mode 100644 tools/nnyeah/NNyeahTests/TestRunning.cs diff --git a/tools/nnyeah/NNyeahTests/Compiler.cs b/tools/nnyeah/NNyeahTests/Compiler.cs new file mode 100644 index 000000000000..98895fd0c34c --- /dev/null +++ b/tools/nnyeah/NNyeahTests/Compiler.cs @@ -0,0 +1,73 @@ +using System; +using System.Text; +using System.IO; + +namespace Microsoft.MaciOS.Nnyeah.Tests { + public class Compiler { + const string kCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc"; + + public static string CompileText (string text, string outputFile, PlatformName platformName, bool isLibrary) + { + using var provider = new DisposableTempDirectory (); + var outputCSFile = Path.Combine (provider.DirectoryPath, "LibraryFile.cs"); + using var writer = new StreamWriter (outputCSFile); + writer.Write (text); + writer.Close (); + return Compile (outputFile, platformName, isLibrary, provider.DirectoryPath, outputCSFile); + } + + public static string Compile (string outputFile, PlatformName platformName, bool isLibrary, string workingDirectory, params string[] sourceFiles) + { + var compilerArgs = BuildCompilerArgs (sourceFiles, outputFile, platformName, isLibrary); + return ExecAndCollect.Run (kCompiler, compilerArgs, workingDirectory); + } + + static string BuildCompilerArgs (string[] sourceFiles, string outputFile, PlatformName platformName, + bool isLibrary) + { + var sb = new StringBuilder (); + + sb.Append ("/unsafe ").Append ("/out:").Append (outputFile); + sb.Append (" /nostdlib+"); + AppendPlatformReference (sb, platformName, "mscorlib"); + AppendPlatformReference (sb, platformName, XamarinLibName (platformName)); + sb.Append (" /debug+ /debug:full /optimize-"); + sb.Append (" /out:").Append (outputFile); + sb.Append (" /target:").Append (isLibrary ? "library" : "exe"); + + foreach (var file in sourceFiles) { + sb.Append (' ').Append (file); + } + + return sb.ToString (); + } + + static StringBuilder AppendPlatformReference (StringBuilder sb, PlatformName platformName, string libName) + { + return sb.Append (" /reference:").Append (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 (), + }; + } +} diff --git a/tools/nnyeah/NNyeahTests/DisposableTempDirectory.cs b/tools/nnyeah/NNyeahTests/DisposableTempDirectory.cs new file mode 100644 index 000000000000..83429b442d8f --- /dev/null +++ b/tools/nnyeah/NNyeahTests/DisposableTempDirectory.cs @@ -0,0 +1,81 @@ +using System; +using System.IO; +using System.Reflection; +using System.Threading; + +namespace Microsoft.MaciOS.Nnyeah.Tests { + public class DisposableTempDirectory : IDisposable { + // If directories are deleted at process startup (for previous + // processes), or when the DisposableTempDirectory instance is + // disposed/collected. Deleting at process startup is useful when + // debugging tests: you can examine temporary files after the test has + // completed. + static readonly bool delete_on_launch; + // If paths are deterministic (the nth request always gets the same + // path). Deterministic paths are useful when debugging tests: + // temporary files end up in the same location every time you run a + // test from the IDE. + static readonly bool deterministic; + static int counter; + static readonly string root; + + static DisposableTempDirectory () + { + root = Path.Combine (Path.GetTempPath (), "nnyeah"); +#if DEBUG + // Default to the helpful values when running unit tests in the IDE. + // This is only something we want possible in DEBUG mode + if (Assembly.GetEntryAssembly () is null) { + delete_on_launch = true; + deterministic = true; + var assemblyPath = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) ?? throw new NotSupportedException (); + root = Path.Combine (assemblyPath, "nnyeah-tests"); + } + if (delete_on_launch && Directory.Exists (root)) + Directory.Delete (root, true); +#endif + Directory.CreateDirectory (root); + } + + public DisposableTempDirectory (string? directoryName = null, bool prependGuid = true) + { + var unique = deterministic ? Interlocked.Increment (ref counter).ToString () : Guid.NewGuid ().ToString (); + if (directoryName is not null) + directoryName = unique + directoryName; + + directoryName = directoryName ?? unique; + + this.DirectoryPath = Path.Combine (root, directoryName); + + Directory.CreateDirectory (this.DirectoryPath); + } + + #region IDisposable implementation + + ~DisposableTempDirectory () + { + Dispose (false); + } + + public void Dispose () + { + Dispose (true); + GC.SuppressFinalize (this); + } + + protected virtual void Dispose (bool disposing) + { + if (!delete_on_launch) + RemoveTempDirectoryAndContents (); + } + + #endregion + + void RemoveTempDirectoryAndContents () + { + Directory.Delete (DirectoryPath, true); + } + + public string DirectoryPath { get; private set; } + } +} diff --git a/tools/nnyeah/NNyeahTests/Enums.cs b/tools/nnyeah/NNyeahTests/Enums.cs new file mode 100644 index 000000000000..dfb4fe2fe7ef --- /dev/null +++ b/tools/nnyeah/NNyeahTests/Enums.cs @@ -0,0 +1,10 @@ +using System; +namespace Microsoft.MaciOS.Nnyeah.Tests { + public enum PlatformName { + None, // desktop managed executable + macOS, // Xamarin.Mac app + iOS, + watchOS, + tvOS, + } +} diff --git a/tools/nnyeah/NNyeahTests/ExecAndCollect.cs b/tools/nnyeah/NNyeahTests/ExecAndCollect.cs new file mode 100644 index 000000000000..85e15d8359fb --- /dev/null +++ b/tools/nnyeah/NNyeahTests/ExecAndCollect.cs @@ -0,0 +1,117 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using System.Threading; + +namespace Microsoft.MaciOS.Nnyeah.Tests { + public class ExecAndCollect { + public static string Run (string path, string args, string workingDirectory = "", bool verbose = false) + { + var output = new StringBuilder (); + var exitCode = RunCommand (path, args, output: output, verbose: verbose, workingDirectory: workingDirectory); + if (exitCode != 0) + throw new Exception ($"Failed to execute (exit code {exitCode}): {path} {string.Join (" ", args)}\n{output.ToString ()}"); + return output.ToString (); + } + + static void ReadStream (Stream stream, StringBuilder sb, ManualResetEvent completed) + { + var encoding = Encoding.UTF8; + var decoder = encoding.GetDecoder (); + var buffer = new byte [1024]; + var characters = new char [encoding.GetMaxCharCount (buffer.Length)]; + + AsyncCallback? callback = null; + callback = new AsyncCallback ((IAsyncResult ar) => { + var read = stream.EndRead (ar); + + var chars = decoder.GetChars (buffer, 0, read, characters, 0); + lock (sb) + sb.Append (characters, 0, chars); + + if (read > 0) { + stream.BeginRead (buffer, 0, buffer.Length, callback, null); + } else { + completed.Set (); + } + }); + stream.BeginRead (buffer, 0, buffer.Length, callback, null); + } + + public static int RunCommand (string path, string args, IDictionary? env = null, StringBuilder? output = null, bool verbose = false, string? workingDirectory = null) + { + var info = new ProcessStartInfo (path, args); + info.UseShellExecute = false; + info.RedirectStandardInput = false; + info.RedirectStandardOutput = true; + info.RedirectStandardError = true; + if (workingDirectory is not null) + info.WorkingDirectory = workingDirectory; + + if (output is null) + output = new StringBuilder (); + + if (env != null) { + foreach (var kvp in env) { + if (kvp.Value == null) { + if (info.EnvironmentVariables.ContainsKey (kvp.Key)) + info.EnvironmentVariables.Remove (kvp.Key); + } else { + info.EnvironmentVariables [kvp.Key] = kvp.Value; + } + } + } + + if (info.EnvironmentVariables.ContainsKey ("XCODE_DEVELOPER_DIR_PATH")) { + // VSfM adds this key, which confuses Xcode mightily if it doesn't match the value of xcode-select. + // So just remove it, we don't need it for anything. + info.EnvironmentVariables.Remove ("XCODE_DEVELOPER_DIR_PATH"); + } + + + if (verbose) { + var envOut = new StringBuilder (); + foreach (var key in info.EnvironmentVariables.Keys) { + if (key is string keyString) { + var value = info.EnvironmentVariables [keyString]; + envOut.AppendLine ($"export {keyString}={value}"); + } + } + var pathArgs = $"{path} {args}"; + envOut.AppendLine (pathArgs); + Console.Write (envOut.ToString ()); + Console.WriteLine (pathArgs); + } + + var p = Process.Start (info); + if (p is not null) { + var error_output = new StringBuilder (); + var stdout_completed = new ManualResetEvent (false); + var stderr_completed = new ManualResetEvent (false); + + ReadStream (p.StandardOutput.BaseStream, output, stdout_completed); + ReadStream (p.StandardError.BaseStream, error_output, stderr_completed); + + p.WaitForExit (); + + stderr_completed.WaitOne (TimeSpan.FromMinutes (1)); + stdout_completed.WaitOne (TimeSpan.FromMinutes (1)); + + if (verbose) { + if (output.Length > 0) + Console.WriteLine (output); + if (error_output.Length > 0) + Console.WriteLine (error_output); + if (p.ExitCode != 0) + Console.Error.WriteLine ($"Process exited with code {p.ExitCode}"); + } + if (p.ExitCode != 0 && error_output.Length > 0) + output.Append (error_output); + return p.ExitCode; + } + return -2; + } + } +} diff --git a/tools/nnyeah/NNyeahTests/NNyeahTests.csproj b/tools/nnyeah/NNyeahTests/NNyeahTests.csproj new file mode 100644 index 000000000000..cab6704885ec --- /dev/null +++ b/tools/nnyeah/NNyeahTests/NNyeahTests.csproj @@ -0,0 +1,46 @@ + + + + + Debug + AnyCPU + {21568B5B-B904-49FC-8A40-A8752E042D00} + Library + Microsoft.MaciOS.Nnyeah.Tests + NNyeahTests + v4.7.2 + enable + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + latest + + + true + bin\Release + prompt + 4 + latest + + + + + ..\..\..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll + + + + + + + + + + + + \ No newline at end of file diff --git a/tools/nnyeah/NNyeahTests/Smoke Tests/CompileALibrary.cs b/tools/nnyeah/NNyeahTests/Smoke Tests/CompileALibrary.cs new file mode 100644 index 000000000000..cf633f4e5e3a --- /dev/null +++ b/tools/nnyeah/NNyeahTests/Smoke Tests/CompileALibrary.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; +using NUnit.Framework; + +namespace Microsoft.MaciOS.Nnyeah.Tests.SmokeTests { + [TestFixture] + public class CompileALibrary { + [Test] + public void BasicLibrary () + { + using var provider = new DisposableTempDirectory (); + var code = @" +using System; +public class Foo { + public nint Ident (nint e) => e; +} +"; + var output = TestRunning.BuildLibrary (code, "NoName", provider.DirectoryPath); + var expectedOutputFile = Path.Combine (provider.DirectoryPath, "NoName.dll"); + Assert.IsTrue (File.Exists (expectedOutputFile)); + } + + [Test] + public void BasicExecutable () + { + using var provider = new DisposableTempDirectory (); + } + } +} diff --git a/tools/nnyeah/NNyeahTests/TestRunning.cs b/tools/nnyeah/NNyeahTests/TestRunning.cs new file mode 100644 index 000000000000..2ee876f44b2f --- /dev/null +++ b/tools/nnyeah/NNyeahTests/TestRunning.cs @@ -0,0 +1,42 @@ +using System; +using System.IO; +using System.Linq; +using NUnit.Framework; + +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 (); + if (callingMethod is null) + Assert.Fail ("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 void TestAndExecute (string libraryCode, string callingCode, string expectedOutput, + string callingMethodName = "") + { + var testName = GetInvokingTestName (out var nameSpace, callingMethodName); + var testClassName = "NnyeahTest" + testName; + + using var initialLibraryDir = new DisposableTempDirectory (); + using var finalLibraryDir = new DisposableTempDirectory (); + + var libCompilerOutput = BuildLibrary (libraryCode, testName, initialLibraryDir.DirectoryPath); + } + + public static 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); + } + } +} diff --git a/tools/nnyeah/nnyeah.sln b/tools/nnyeah/nnyeah.sln index 0aa4738a4944..b1c66028a4b9 100644 --- a/tools/nnyeah/nnyeah.sln +++ b/tools/nnyeah/nnyeah.sln @@ -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}") = "NNyeahTests", "NNyeahTests\NNyeahTests.csproj", "{21568B5B-B904-49FC-8A40-A8752E042D00}" +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 + {21568B5B-B904-49FC-8A40-A8752E042D00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {21568B5B-B904-49FC-8A40-A8752E042D00}.Debug|Any CPU.Build.0 = Debug|Any CPU + {21568B5B-B904-49FC-8A40-A8752E042D00}.Release|Any CPU.ActiveCfg = Release|Any CPU + {21568B5B-B904-49FC-8A40-A8752E042D00}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 50ded52ce57f5faace364b45aab85978683203bd Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Fri, 6 May 2022 11:50:17 -0500 Subject: [PATCH 04/10] Move csproj to NET6 and more standard directory name --- tools/nnyeah/NNyeahTests/NNyeahTests.csproj | 46 ------------------- tools/nnyeah/nnyeah.sln | 10 ++-- .../nnyeah/{NNyeahTests => tests}/Compiler.cs | 0 .../DisposableTempDirectory.cs | 0 tools/nnyeah/{NNyeahTests => tests}/Enums.cs | 0 .../{NNyeahTests => tests}/ExecAndCollect.cs | 0 .../Smoke Tests/CompileALibrary.cs | 0 .../{NNyeahTests => tests}/TestRunning.cs | 0 tools/nnyeah/tests/nnyeah-tests.csproj | 17 +++++++ 9 files changed, 22 insertions(+), 51 deletions(-) delete mode 100644 tools/nnyeah/NNyeahTests/NNyeahTests.csproj rename tools/nnyeah/{NNyeahTests => tests}/Compiler.cs (100%) rename tools/nnyeah/{NNyeahTests => tests}/DisposableTempDirectory.cs (100%) rename tools/nnyeah/{NNyeahTests => tests}/Enums.cs (100%) rename tools/nnyeah/{NNyeahTests => tests}/ExecAndCollect.cs (100%) rename tools/nnyeah/{NNyeahTests => tests}/Smoke Tests/CompileALibrary.cs (100%) rename tools/nnyeah/{NNyeahTests => tests}/TestRunning.cs (100%) create mode 100644 tools/nnyeah/tests/nnyeah-tests.csproj diff --git a/tools/nnyeah/NNyeahTests/NNyeahTests.csproj b/tools/nnyeah/NNyeahTests/NNyeahTests.csproj deleted file mode 100644 index cab6704885ec..000000000000 --- a/tools/nnyeah/NNyeahTests/NNyeahTests.csproj +++ /dev/null @@ -1,46 +0,0 @@ - - - - - Debug - AnyCPU - {21568B5B-B904-49FC-8A40-A8752E042D00} - Library - Microsoft.MaciOS.Nnyeah.Tests - NNyeahTests - v4.7.2 - enable - - - true - full - false - bin\Debug - DEBUG; - prompt - 4 - latest - - - true - bin\Release - prompt - 4 - latest - - - - - ..\..\..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll - - - - - - - - - - - - \ No newline at end of file diff --git a/tools/nnyeah/nnyeah.sln b/tools/nnyeah/nnyeah.sln index b1c66028a4b9..0e798d57cb1e 100644 --- a/tools/nnyeah/nnyeah.sln +++ b/tools/nnyeah/nnyeah.sln @@ -5,7 +5,7 @@ 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}") = "NNyeahTests", "NNyeahTests\NNyeahTests.csproj", "{21568B5B-B904-49FC-8A40-A8752E042D00}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nnyeah-tests", "tests\nnyeah-tests.csproj", "{0CDCFD15-98C0-4669-A00B-E5287086D606}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +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 - {21568B5B-B904-49FC-8A40-A8752E042D00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {21568B5B-B904-49FC-8A40-A8752E042D00}.Debug|Any CPU.Build.0 = Debug|Any CPU - {21568B5B-B904-49FC-8A40-A8752E042D00}.Release|Any CPU.ActiveCfg = Release|Any CPU - {21568B5B-B904-49FC-8A40-A8752E042D00}.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 diff --git a/tools/nnyeah/NNyeahTests/Compiler.cs b/tools/nnyeah/tests/Compiler.cs similarity index 100% rename from tools/nnyeah/NNyeahTests/Compiler.cs rename to tools/nnyeah/tests/Compiler.cs diff --git a/tools/nnyeah/NNyeahTests/DisposableTempDirectory.cs b/tools/nnyeah/tests/DisposableTempDirectory.cs similarity index 100% rename from tools/nnyeah/NNyeahTests/DisposableTempDirectory.cs rename to tools/nnyeah/tests/DisposableTempDirectory.cs diff --git a/tools/nnyeah/NNyeahTests/Enums.cs b/tools/nnyeah/tests/Enums.cs similarity index 100% rename from tools/nnyeah/NNyeahTests/Enums.cs rename to tools/nnyeah/tests/Enums.cs diff --git a/tools/nnyeah/NNyeahTests/ExecAndCollect.cs b/tools/nnyeah/tests/ExecAndCollect.cs similarity index 100% rename from tools/nnyeah/NNyeahTests/ExecAndCollect.cs rename to tools/nnyeah/tests/ExecAndCollect.cs diff --git a/tools/nnyeah/NNyeahTests/Smoke Tests/CompileALibrary.cs b/tools/nnyeah/tests/Smoke Tests/CompileALibrary.cs similarity index 100% rename from tools/nnyeah/NNyeahTests/Smoke Tests/CompileALibrary.cs rename to tools/nnyeah/tests/Smoke Tests/CompileALibrary.cs diff --git a/tools/nnyeah/NNyeahTests/TestRunning.cs b/tools/nnyeah/tests/TestRunning.cs similarity index 100% rename from tools/nnyeah/NNyeahTests/TestRunning.cs rename to tools/nnyeah/tests/TestRunning.cs diff --git a/tools/nnyeah/tests/nnyeah-tests.csproj b/tools/nnyeah/tests/nnyeah-tests.csproj new file mode 100644 index 000000000000..e59718eece00 --- /dev/null +++ b/tools/nnyeah/tests/nnyeah-tests.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + + false + + + + + + + + + + From c5e2099d65007301408160c596cc00a41e754ca6 Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Fri, 6 May 2022 13:17:34 -0500 Subject: [PATCH 05/10] Move to common execution and temporary directory services --- tests/mtouch/Cache.cs | 2 + tools/common/Execution.cs | 2 + tools/common/StringUtils.cs | 2 + .../{Smoke Tests => }/CompileALibrary.cs | 13 +- tools/nnyeah/tests/Compiler.cs | 73 ----------- tools/nnyeah/tests/DisposableTempDirectory.cs | 81 ------------ tools/nnyeah/tests/Enums.cs | 10 -- tools/nnyeah/tests/ExecAndCollect.cs | 117 ------------------ tools/nnyeah/tests/nnyeah-tests.csproj | 8 ++ tools/nnyeah/tests/utils/Compiler.cs | 90 ++++++++++++++ tools/nnyeah/tests/{ => utils}/TestRunning.cs | 14 ++- 11 files changed, 121 insertions(+), 291 deletions(-) rename tools/nnyeah/tests/{Smoke Tests => }/CompileALibrary.cs (51%) delete mode 100644 tools/nnyeah/tests/Compiler.cs delete mode 100644 tools/nnyeah/tests/DisposableTempDirectory.cs delete mode 100644 tools/nnyeah/tests/Enums.cs delete mode 100644 tools/nnyeah/tests/ExecAndCollect.cs create mode 100644 tools/nnyeah/tests/utils/Compiler.cs rename tools/nnyeah/tests/{ => utils}/TestRunning.cs (69%) diff --git a/tests/mtouch/Cache.cs b/tests/mtouch/Cache.cs index 75e6fd655faa..5ce3bfa21cef 100644 --- a/tests/mtouch/Cache.cs +++ b/tests/mtouch/Cache.cs @@ -4,6 +4,8 @@ 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 diff --git a/tools/common/Execution.cs b/tools/common/Execution.cs index 040bdb79f005..472ad106df86 100644 --- a/tools/common/Execution.cs +++ b/tools/common/Execution.cs @@ -16,6 +16,8 @@ using System.Threading; using System.Threading.Tasks; +#nullable disable + namespace Xamarin.Utils { public class Execution { public string FileName; diff --git a/tools/common/StringUtils.cs b/tools/common/StringUtils.cs index baf9fef5df31..8b09d4913111 100644 --- a/tools/common/StringUtils.cs +++ b/tools/common/StringUtils.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Text; +#nullable disable + namespace Xamarin.Utils { internal class StringUtils { static StringUtils () diff --git a/tools/nnyeah/tests/Smoke Tests/CompileALibrary.cs b/tools/nnyeah/tests/CompileALibrary.cs similarity index 51% rename from tools/nnyeah/tests/Smoke Tests/CompileALibrary.cs rename to tools/nnyeah/tests/CompileALibrary.cs index cf633f4e5e3a..7e9c776bcdd5 100644 --- a/tools/nnyeah/tests/Smoke Tests/CompileALibrary.cs +++ b/tools/nnyeah/tests/CompileALibrary.cs @@ -1,29 +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 void BasicLibrary () + public async Task BasicLibrary () { - using var provider = new DisposableTempDirectory (); + var dir = Cache.CreateTemporaryDirectory ("BasicLibrary"); var code = @" using System; public class Foo { public nint Ident (nint e) => e; } "; - var output = TestRunning.BuildLibrary (code, "NoName", provider.DirectoryPath); - var expectedOutputFile = Path.Combine (provider.DirectoryPath, "NoName.dll"); + var output = await TestRunning.BuildLibrary (code, "NoName", dir); + var expectedOutputFile = Path.Combine (dir, "NoName.dll"); Assert.IsTrue (File.Exists (expectedOutputFile)); } [Test] public void BasicExecutable () { - using var provider = new DisposableTempDirectory (); + var dir = Cache.CreateTemporaryDirectory ("BasicExecutable"); } } } diff --git a/tools/nnyeah/tests/Compiler.cs b/tools/nnyeah/tests/Compiler.cs deleted file mode 100644 index 98895fd0c34c..000000000000 --- a/tools/nnyeah/tests/Compiler.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Text; -using System.IO; - -namespace Microsoft.MaciOS.Nnyeah.Tests { - public class Compiler { - const string kCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc"; - - public static string CompileText (string text, string outputFile, PlatformName platformName, bool isLibrary) - { - using var provider = new DisposableTempDirectory (); - var outputCSFile = Path.Combine (provider.DirectoryPath, "LibraryFile.cs"); - using var writer = new StreamWriter (outputCSFile); - writer.Write (text); - writer.Close (); - return Compile (outputFile, platformName, isLibrary, provider.DirectoryPath, outputCSFile); - } - - public static string Compile (string outputFile, PlatformName platformName, bool isLibrary, string workingDirectory, params string[] sourceFiles) - { - var compilerArgs = BuildCompilerArgs (sourceFiles, outputFile, platformName, isLibrary); - return ExecAndCollect.Run (kCompiler, compilerArgs, workingDirectory); - } - - static string BuildCompilerArgs (string[] sourceFiles, string outputFile, PlatformName platformName, - bool isLibrary) - { - var sb = new StringBuilder (); - - sb.Append ("/unsafe ").Append ("/out:").Append (outputFile); - sb.Append (" /nostdlib+"); - AppendPlatformReference (sb, platformName, "mscorlib"); - AppendPlatformReference (sb, platformName, XamarinLibName (platformName)); - sb.Append (" /debug+ /debug:full /optimize-"); - sb.Append (" /out:").Append (outputFile); - sb.Append (" /target:").Append (isLibrary ? "library" : "exe"); - - foreach (var file in sourceFiles) { - sb.Append (' ').Append (file); - } - - return sb.ToString (); - } - - static StringBuilder AppendPlatformReference (StringBuilder sb, PlatformName platformName, string libName) - { - return sb.Append (" /reference:").Append (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 (), - }; - } -} diff --git a/tools/nnyeah/tests/DisposableTempDirectory.cs b/tools/nnyeah/tests/DisposableTempDirectory.cs deleted file mode 100644 index 83429b442d8f..000000000000 --- a/tools/nnyeah/tests/DisposableTempDirectory.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.IO; -using System.Reflection; -using System.Threading; - -namespace Microsoft.MaciOS.Nnyeah.Tests { - public class DisposableTempDirectory : IDisposable { - // If directories are deleted at process startup (for previous - // processes), or when the DisposableTempDirectory instance is - // disposed/collected. Deleting at process startup is useful when - // debugging tests: you can examine temporary files after the test has - // completed. - static readonly bool delete_on_launch; - // If paths are deterministic (the nth request always gets the same - // path). Deterministic paths are useful when debugging tests: - // temporary files end up in the same location every time you run a - // test from the IDE. - static readonly bool deterministic; - static int counter; - static readonly string root; - - static DisposableTempDirectory () - { - root = Path.Combine (Path.GetTempPath (), "nnyeah"); -#if DEBUG - // Default to the helpful values when running unit tests in the IDE. - // This is only something we want possible in DEBUG mode - if (Assembly.GetEntryAssembly () is null) { - delete_on_launch = true; - deterministic = true; - var assemblyPath = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location) ?? throw new NotSupportedException (); - root = Path.Combine (assemblyPath, "nnyeah-tests"); - } - if (delete_on_launch && Directory.Exists (root)) - Directory.Delete (root, true); -#endif - Directory.CreateDirectory (root); - } - - public DisposableTempDirectory (string? directoryName = null, bool prependGuid = true) - { - var unique = deterministic ? Interlocked.Increment (ref counter).ToString () : Guid.NewGuid ().ToString (); - if (directoryName is not null) - directoryName = unique + directoryName; - - directoryName = directoryName ?? unique; - - this.DirectoryPath = Path.Combine (root, directoryName); - - Directory.CreateDirectory (this.DirectoryPath); - } - - #region IDisposable implementation - - ~DisposableTempDirectory () - { - Dispose (false); - } - - public void Dispose () - { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) - { - if (!delete_on_launch) - RemoveTempDirectoryAndContents (); - } - - #endregion - - void RemoveTempDirectoryAndContents () - { - Directory.Delete (DirectoryPath, true); - } - - public string DirectoryPath { get; private set; } - } -} diff --git a/tools/nnyeah/tests/Enums.cs b/tools/nnyeah/tests/Enums.cs deleted file mode 100644 index dfb4fe2fe7ef..000000000000 --- a/tools/nnyeah/tests/Enums.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -namespace Microsoft.MaciOS.Nnyeah.Tests { - public enum PlatformName { - None, // desktop managed executable - macOS, // Xamarin.Mac app - iOS, - watchOS, - tvOS, - } -} diff --git a/tools/nnyeah/tests/ExecAndCollect.cs b/tools/nnyeah/tests/ExecAndCollect.cs deleted file mode 100644 index 85e15d8359fb..000000000000 --- a/tools/nnyeah/tests/ExecAndCollect.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.IO; -using System.Collections.Generic; -using System.Diagnostics; -using System.Text; -using System.Threading; - -namespace Microsoft.MaciOS.Nnyeah.Tests { - public class ExecAndCollect { - public static string Run (string path, string args, string workingDirectory = "", bool verbose = false) - { - var output = new StringBuilder (); - var exitCode = RunCommand (path, args, output: output, verbose: verbose, workingDirectory: workingDirectory); - if (exitCode != 0) - throw new Exception ($"Failed to execute (exit code {exitCode}): {path} {string.Join (" ", args)}\n{output.ToString ()}"); - return output.ToString (); - } - - static void ReadStream (Stream stream, StringBuilder sb, ManualResetEvent completed) - { - var encoding = Encoding.UTF8; - var decoder = encoding.GetDecoder (); - var buffer = new byte [1024]; - var characters = new char [encoding.GetMaxCharCount (buffer.Length)]; - - AsyncCallback? callback = null; - callback = new AsyncCallback ((IAsyncResult ar) => { - var read = stream.EndRead (ar); - - var chars = decoder.GetChars (buffer, 0, read, characters, 0); - lock (sb) - sb.Append (characters, 0, chars); - - if (read > 0) { - stream.BeginRead (buffer, 0, buffer.Length, callback, null); - } else { - completed.Set (); - } - }); - stream.BeginRead (buffer, 0, buffer.Length, callback, null); - } - - public static int RunCommand (string path, string args, IDictionary? env = null, StringBuilder? output = null, bool verbose = false, string? workingDirectory = null) - { - var info = new ProcessStartInfo (path, args); - info.UseShellExecute = false; - info.RedirectStandardInput = false; - info.RedirectStandardOutput = true; - info.RedirectStandardError = true; - if (workingDirectory is not null) - info.WorkingDirectory = workingDirectory; - - if (output is null) - output = new StringBuilder (); - - if (env != null) { - foreach (var kvp in env) { - if (kvp.Value == null) { - if (info.EnvironmentVariables.ContainsKey (kvp.Key)) - info.EnvironmentVariables.Remove (kvp.Key); - } else { - info.EnvironmentVariables [kvp.Key] = kvp.Value; - } - } - } - - if (info.EnvironmentVariables.ContainsKey ("XCODE_DEVELOPER_DIR_PATH")) { - // VSfM adds this key, which confuses Xcode mightily if it doesn't match the value of xcode-select. - // So just remove it, we don't need it for anything. - info.EnvironmentVariables.Remove ("XCODE_DEVELOPER_DIR_PATH"); - } - - - if (verbose) { - var envOut = new StringBuilder (); - foreach (var key in info.EnvironmentVariables.Keys) { - if (key is string keyString) { - var value = info.EnvironmentVariables [keyString]; - envOut.AppendLine ($"export {keyString}={value}"); - } - } - var pathArgs = $"{path} {args}"; - envOut.AppendLine (pathArgs); - Console.Write (envOut.ToString ()); - Console.WriteLine (pathArgs); - } - - var p = Process.Start (info); - if (p is not null) { - var error_output = new StringBuilder (); - var stdout_completed = new ManualResetEvent (false); - var stderr_completed = new ManualResetEvent (false); - - ReadStream (p.StandardOutput.BaseStream, output, stdout_completed); - ReadStream (p.StandardError.BaseStream, error_output, stderr_completed); - - p.WaitForExit (); - - stderr_completed.WaitOne (TimeSpan.FromMinutes (1)); - stdout_completed.WaitOne (TimeSpan.FromMinutes (1)); - - if (verbose) { - if (output.Length > 0) - Console.WriteLine (output); - if (error_output.Length > 0) - Console.WriteLine (error_output); - if (p.ExitCode != 0) - Console.Error.WriteLine ($"Process exited with code {p.ExitCode}"); - } - if (p.ExitCode != 0 && error_output.Length > 0) - output.Append (error_output); - return p.ExitCode; - } - return -2; - } - } -} diff --git a/tools/nnyeah/tests/nnyeah-tests.csproj b/tools/nnyeah/tests/nnyeah-tests.csproj index e59718eece00..962ec523d910 100644 --- a/tools/nnyeah/tests/nnyeah-tests.csproj +++ b/tools/nnyeah/tests/nnyeah-tests.csproj @@ -14,4 +14,12 @@ + + + + + + + + diff --git a/tools/nnyeah/tests/utils/Compiler.cs b/tools/nnyeah/tests/utils/Compiler.cs new file mode 100644 index 000000000000..e61a28c5d769 --- /dev/null +++ b/tools/nnyeah/tests/utils/Compiler.cs @@ -0,0 +1,90 @@ +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 kCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc"; + + public static async Task CompileText (string text, string outputFile, PlatformName platformName, bool isLibrary) + { + var dir = Cache.CreateTemporaryDirectory ("compile-text"); + + var outputCSFile = Path.Combine (dir, "LibraryFile.cs"); + using var writer = new StreamWriter (outputCSFile); + writer.Write (text); + writer.Close (); + return await Compile (outputFile, platformName, isLibrary, dir, outputCSFile); + } + + public static async Task Compile (string outputFile, PlatformName platformName, bool isLibrary, string workingDirectory, params string[] sourceFiles) + { + var compilerArgs = BuildCompilerArgs (sourceFiles, outputFile, platformName, isLibrary); + Execution execution = await Execution.RunAsync(kCompiler, compilerArgs, mergeOutput: true, workingDirectory: workingDirectory); + return execution!.StandardOutput.ToString()!; + } + + static List BuildCompilerArgs (string[] sourceFiles, string outputFile, PlatformName platformName, + bool isLibrary) + { + var args = new List(); + + 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 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 (), + }; + } +} diff --git a/tools/nnyeah/tests/TestRunning.cs b/tools/nnyeah/tests/utils/TestRunning.cs similarity index 69% rename from tools/nnyeah/tests/TestRunning.cs rename to tools/nnyeah/tests/utils/TestRunning.cs index 2ee876f44b2f..c3070c8c57e4 100644 --- a/tools/nnyeah/tests/TestRunning.cs +++ b/tools/nnyeah/tests/utils/TestRunning.cs @@ -1,8 +1,12 @@ 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) @@ -21,19 +25,19 @@ static string GetInvokingTestName (out string callingMethodClass, string calling return callingMethodName; } - public static void TestAndExecute (string libraryCode, string callingCode, string expectedOutput, + public static async Task TestAndExecute (string libraryCode, string callingCode, string expectedOutput, string callingMethodName = "") { var testName = GetInvokingTestName (out var nameSpace, callingMethodName); var testClassName = "NnyeahTest" + testName; - using var initialLibraryDir = new DisposableTempDirectory (); - using var finalLibraryDir = new DisposableTempDirectory (); + var initialLibraryDir = Cache.CreateTemporaryDirectory ("initial-library"); + var finalLibraryDir = Cache.CreateTemporaryDirectory ("final-library"); - var libCompilerOutput = BuildLibrary (libraryCode, testName, initialLibraryDir.DirectoryPath); + var libCompilerOutput = await BuildLibrary (libraryCode, testName, initialLibraryDir); } - public static string BuildLibrary (string libraryCode, string libName, string outputDirectory, PlatformName platformName = PlatformName.macOS) + public static Task 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); From 231945c4754060ea94e7cfca270b7fe1f986401c Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Fri, 6 May 2022 13:22:09 -0500 Subject: [PATCH 06/10] More tweaks and simplifications --- tools/nnyeah/tests/CompileALibrary.cs | 4 ++-- tools/nnyeah/tests/utils/Compiler.cs | 11 ++++------- tools/nnyeah/tests/utils/TestRunning.cs | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/tools/nnyeah/tests/CompileALibrary.cs b/tools/nnyeah/tests/CompileALibrary.cs index 7e9c776bcdd5..41ac1e669cd9 100644 --- a/tools/nnyeah/tests/CompileALibrary.cs +++ b/tools/nnyeah/tests/CompileALibrary.cs @@ -11,7 +11,7 @@ public class CompileALibrary { [Test] public async Task BasicLibrary () { - var dir = Cache.CreateTemporaryDirectory ("BasicLibrary"); + var dir = Cache.CreateTemporaryDirectory ("BasicLibrary"); var code = @" using System; public class Foo { @@ -26,7 +26,7 @@ public class Foo { [Test] public void BasicExecutable () { - var dir = Cache.CreateTemporaryDirectory ("BasicExecutable"); + var dir = Cache.CreateTemporaryDirectory ("BasicExecutable"); } } } diff --git a/tools/nnyeah/tests/utils/Compiler.cs b/tools/nnyeah/tests/utils/Compiler.cs index e61a28c5d769..412f23911b7b 100644 --- a/tools/nnyeah/tests/utils/Compiler.cs +++ b/tools/nnyeah/tests/utils/Compiler.cs @@ -17,23 +17,20 @@ public enum PlatformName { tvOS, } public class Compiler { - const string kCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc"; + const string MonoCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc"; public static async Task CompileText (string text, string outputFile, PlatformName platformName, bool isLibrary) { - var dir = Cache.CreateTemporaryDirectory ("compile-text"); - + var dir = Cache.CreateTemporaryDirectory (); var outputCSFile = Path.Combine (dir, "LibraryFile.cs"); - using var writer = new StreamWriter (outputCSFile); - writer.Write (text); - writer.Close (); + File.WriteAllText (outputCSFile, text); return await Compile (outputFile, platformName, isLibrary, dir, outputCSFile); } public static async Task Compile (string outputFile, PlatformName platformName, bool isLibrary, string workingDirectory, params string[] sourceFiles) { var compilerArgs = BuildCompilerArgs (sourceFiles, outputFile, platformName, isLibrary); - Execution execution = await Execution.RunAsync(kCompiler, compilerArgs, mergeOutput: true, workingDirectory: workingDirectory); + Execution execution = await Execution.RunAsync(MonoCompiler, compilerArgs, mergeOutput: true, workingDirectory: workingDirectory); return execution!.StandardOutput.ToString()!; } diff --git a/tools/nnyeah/tests/utils/TestRunning.cs b/tools/nnyeah/tests/utils/TestRunning.cs index c3070c8c57e4..19f59e8b3a09 100644 --- a/tools/nnyeah/tests/utils/TestRunning.cs +++ b/tools/nnyeah/tests/utils/TestRunning.cs @@ -31,8 +31,8 @@ public static async Task TestAndExecute (string libraryCode, string callingCode, var testName = GetInvokingTestName (out var nameSpace, callingMethodName); var testClassName = "NnyeahTest" + testName; - var initialLibraryDir = Cache.CreateTemporaryDirectory ("initial-library"); - var finalLibraryDir = Cache.CreateTemporaryDirectory ("final-library"); + var initialLibraryDir = Cache.CreateTemporaryDirectory (); + var finalLibraryDir = Cache.CreateTemporaryDirectory (); var libCompilerOutput = await BuildLibrary (libraryCode, testName, initialLibraryDir); } From 307fcf610103dd12508b35df166abe8b099611db Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Fri, 6 May 2022 13:28:23 -0500 Subject: [PATCH 07/10] Move to explict assert --- tools/nnyeah/tests/utils/TestRunning.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/nnyeah/tests/utils/TestRunning.cs b/tools/nnyeah/tests/utils/TestRunning.cs index 19f59e8b3a09..94a86d50f105 100644 --- a/tools/nnyeah/tests/utils/TestRunning.cs +++ b/tools/nnyeah/tests/utils/TestRunning.cs @@ -13,8 +13,8 @@ static string GetInvokingTestName (out string callingMethodClass, string calling { var stackTrace = new System.Diagnostics.StackTrace (); var callingMethod = stackTrace.GetFrame (2)!.GetMethod (); - if (callingMethod is null) - Assert.Fail ("unable to get calling test from stack frame."); + 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`."); From 27694be29db516cb908db2a9b38589636d726151 Mon Sep 17 00:00:00 2001 From: Chris Hamons Date: Fri, 6 May 2022 13:32:35 -0500 Subject: [PATCH 08/10] Fix whitespace --- tools/nnyeah/tests/nnyeah-tests.csproj | 2 -- tools/nnyeah/tests/utils/Compiler.cs | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/nnyeah/tests/nnyeah-tests.csproj b/tools/nnyeah/tests/nnyeah-tests.csproj index 962ec523d910..fb76a5e54e38 100644 --- a/tools/nnyeah/tests/nnyeah-tests.csproj +++ b/tools/nnyeah/tests/nnyeah-tests.csproj @@ -20,6 +20,4 @@ - - diff --git a/tools/nnyeah/tests/utils/Compiler.cs b/tools/nnyeah/tests/utils/Compiler.cs index 412f23911b7b..34548f277b9d 100644 --- a/tools/nnyeah/tests/utils/Compiler.cs +++ b/tools/nnyeah/tests/utils/Compiler.cs @@ -16,6 +16,7 @@ public enum PlatformName { watchOS, tvOS, } + public class Compiler { const string MonoCompiler = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/csc"; From a9c8c2005a436b57cd2a26d9bd83330c9f21f2d8 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Fri, 6 May 2022 15:11:39 -0400 Subject: [PATCH 09/10] Use the default language to avoid issues with nullability. --- .../generate-frameworks-constants.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/generate-frameworks-constants/generate-frameworks-constants.csproj b/src/generate-frameworks-constants/generate-frameworks-constants.csproj index d0edfe68ae18..43e3b74d9191 100644 --- a/src/generate-frameworks-constants/generate-frameworks-constants.csproj +++ b/src/generate-frameworks-constants/generate-frameworks-constants.csproj @@ -9,6 +9,7 @@ generate-frameworks-constants v4.7.2 bin\$(Configuration) + default true From 20ebca72790fa9146ddb5dc7d56e7c2d827d73d5 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Fri, 6 May 2022 15:42:15 -0400 Subject: [PATCH 10/10] Use the default language to avoid issues with nullability. --- tools/xibuild/xibuild.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/xibuild/xibuild.csproj b/tools/xibuild/xibuild.csproj index af31fa48891c..70e07f2f606e 100644 --- a/tools/xibuild/xibuild.csproj +++ b/tools/xibuild/xibuild.csproj @@ -8,6 +8,7 @@ xibuild xibuild v4.7 + default true