Ejemplo n.º 1
0
    public static int Main(string[] args)
    {
        var    arguments = new Stack <string> ();
        string host      = null;
        int    port      = 0;
        bool   closeAfterTestRun;
        bool   failed;

        for (var i = args.Length - 1; i >= 0; i--)
        {
            arguments.Push(args[i]);
        }

        // First argument is the connection string if we're driven by harness.exe, otherwise we're driven by UITests
        if (arguments.Count > 0 && arguments.Peek().StartsWith("tcp:", StringComparison.Ordinal))
        {
            var parts = arguments.Pop().Split(':');
            if (parts.Length != 3)
            {
                throw new Exception();
            }
            host = parts [1];
            port = Int32.Parse(parts [2]);
            closeAfterTestRun = true;
        }
        else
        {
            closeAfterTestRun = false;
        }

        // Make sure the TLS subsystem including the DependencyInjector is initialized.
        // This would normally happen on system startup in
        // `xamarin-macios/src/ObjcRuntime/Runtime.cs`.
        MonoTlsProviderFactory.Initialize();

        // some tests assert having a SynchronizationContext for MONOTOUCH, provide a default one
        SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

#if XUNIT_RUNNER
        var writer           = new TcpWriter(host, port);
        var assemblyFileName = arguments.Pop();
        var filters          = XunitArgumentsParser.ParseArgumentsToFilter(arguments);
        var configuration    = new TestAssemblyConfiguration()
        {
            ShadowCopy = false
        };
        var discoveryOptions = TestFrameworkOptions.ForDiscovery(configuration);
        var discoverySink    = new TestDiscoverySink();
        var diagnosticSink   = new DiagnosticTextWriterMessageSink(writer);
        var testOptions      = TestFrameworkOptions.ForExecution(configuration);
        var testSink         = new TestMessageSink();
        var controller       = new XunitFrontController(AppDomainSupport.Denied, assemblyFileName, configFileName: null, shadowCopy: false, diagnosticMessageSink: diagnosticSink);

        Interop.mono_sdks_ui_set_test_summary_message($"Running {assemblyFileName}...");

        writer.WriteLine($"Discovering tests for {assemblyFileName}");
        controller.Find(includeSourceInformation: false, discoverySink, discoveryOptions);
        discoverySink.Finished.WaitOne();
        var testCasesToRun = discoverySink.TestCases.Where(filters.Filter).ToList();
        writer.WriteLine($"Discovery finished.");

        var summarySink        = new DelegatingExecutionSummarySink(testSink, () => false, (completed, summary) => { writer.WriteLine($"Tests run: {summary.Total}, Errors: 0, Failures: {summary.Failed}, Skipped: {summary.Skipped}{Environment.NewLine}Time: {TimeSpan.FromSeconds ((double)summary.Time).TotalSeconds}s"); });
        var resultsXmlAssembly = new XElement("assembly");
        var resultsSink        = new DelegatingXmlCreationSink(summarySink, resultsXmlAssembly);

        testSink.Execution.TestPassedEvent  += args => { writer.WriteLine($"[PASS] {args.Message.Test.DisplayName}"); Interop.mono_sdks_ui_increment_testcase_result(0); };
        testSink.Execution.TestSkippedEvent += args => { writer.WriteLine($"[SKIP] {args.Message.Test.DisplayName}"); Interop.mono_sdks_ui_increment_testcase_result(1); };
        testSink.Execution.TestFailedEvent  += args => { writer.WriteLine($"[FAIL] {args.Message.Test.DisplayName}{Environment.NewLine}{ExceptionUtility.CombineMessages (args.Message)}{Environment.NewLine}{ExceptionUtility.CombineStackTraces (args.Message)}"); Interop.mono_sdks_ui_increment_testcase_result(2); };

        testSink.Execution.TestAssemblyStartingEvent += args => { writer.WriteLine($"Running tests for {args.Message.TestAssembly.Assembly}"); };
        testSink.Execution.TestAssemblyFinishedEvent += args => { writer.WriteLine($"Finished {args.Message.TestAssembly.Assembly}{Environment.NewLine}"); };

        controller.RunTests(testCasesToRun, resultsSink, testOptions);
        resultsSink.Finished.WaitOne();

        var resultsXml = new XElement("assemblies");
        resultsXml.Add(resultsXmlAssembly);
        resultsXml.Save(resultsXmlPath);

        if (host != null)
        {
            writer.WriteLine($"STARTRESULTXML");
            resultsXml.Save(((TcpWriter)writer).RawStream);
            writer.WriteLine();
            writer.WriteLine($"ENDRESULTXML");
        }

        failed = resultsSink.ExecutionSummary.Failed > 0 || resultsSink.ExecutionSummary.Errors > 0;
#else
        MonoSdksTextUI runner;
        TextWriter     writer           = null;
        string         resultsXmlPath   = Path.GetTempFileName();
        string         assemblyFileName = arguments.Peek();

        if (File.Exists("nunit-excludes.txt"))
        {
            var excludes = File.ReadAllLines("nunit-excludes.txt");
            arguments.Push("-exclude:" + String.Join(",", excludes));
        }

        arguments.Push("-labels");
        arguments.Push("-format:xunit");
        arguments.Push($"-result:{resultsXmlPath}");

        if (host != null)
        {
            Console.WriteLine($"Connecting to harness at {host}:{port}.");
            writer = new TcpWriter(host, port);
        }
        else
        {
            writer = ConsoleWriter.Out;
        }

        Interop.mono_sdks_ui_set_test_summary_message($"Running {assemblyFileName}...");

        runner = new MonoSdksTextUI(writer);
        runner.Execute(arguments.ToArray());

        if (host != null)
        {
            writer.WriteLine($"STARTRESULTXML");
            using (var resultsXmlStream = File.OpenRead(resultsXmlPath)) resultsXmlStream.CopyTo(((TcpWriter)writer).RawStream);
            writer.WriteLine();
            writer.WriteLine($"ENDRESULTXML");
        }

        failed = runner.Failure;
#endif

        Interop.mono_sdks_ui_set_test_summary_message($"Summary: {(failed ? "Failed" : "Succeeded")} for {assemblyFileName}.");

        if (!closeAfterTestRun)
        {
            Thread.Sleep(Int32.MaxValue);
        }

        return(failed ? 1 : 0);
    }
Ejemplo n.º 2
0
    public static int Main(string[] args)
    {
        string host = null;
        int    port = 0;

        // First argument is the connection string
        if (args [0].StartsWith("tcp:"))
        {
            var parts = args [0].Split(':');
            if (parts.Length != 3)
            {
                throw new Exception();
            }
            host = parts [1];
            port = Int32.Parse(parts [2]);
            args = args.Skip(1).ToArray();
        }

        // Make sure the TLS subsystem including the DependencyInjector is initialized.
        // This would normally happen on system startup in
        // `xamarin-macios/src/ObjcRuntime/Runtime.cs`.
        MonoTlsProviderFactory.Initialize();

#if XUNIT_RUNNER
        var writer           = new TcpWriter(host, port);
        var assemblyFileName = args[0];
        var configuration    = new TestAssemblyConfiguration()
        {
            ShadowCopy = false
        };
        var discoveryOptions = TestFrameworkOptions.ForDiscovery(configuration);
        var discoverySink    = new TestDiscoverySink();
        var diagnosticSink   = new DiagnosticTextWriterMessageSink(writer);
        var testOptions      = TestFrameworkOptions.ForExecution(configuration);
        var testSink         = new TestMessageSink();
        var controller       = new XunitFrontController(AppDomainSupport.Denied, assemblyFileName, configFileName: null, shadowCopy: false, diagnosticMessageSink: diagnosticSink);

        writer.WriteLine($"Discovering tests for {assemblyFileName}");
        controller.Find(includeSourceInformation: false, discoverySink, discoveryOptions);
        discoverySink.Finished.WaitOne();
        writer.WriteLine($"Discovery finished.");

        var summarySink        = new DelegatingExecutionSummarySink(testSink, () => false, (completed, summary) => { writer.WriteLine($"Tests run: {summary.Total}, Errors: 0, Failures: {summary.Failed}, Skipped: {summary.Skipped}{Environment.NewLine}Time: {TimeSpan.FromSeconds ((double)summary.Time).TotalSeconds}s"); });
        var resultsXmlAssembly = new XElement("assembly");
        var resultsSink        = new DelegatingXmlCreationSink(summarySink, resultsXmlAssembly);

        testSink.Execution.TestPassedEvent  += args => { writer.WriteLine($"[PASS] {args.Message.Test.DisplayName}"); Interop.mono_sdks_ui_increment_testcase_result(0); };
        testSink.Execution.TestSkippedEvent += args => { writer.WriteLine($"[SKIP] {args.Message.Test.DisplayName}"); Interop.mono_sdks_ui_increment_testcase_result(1); };
        testSink.Execution.TestFailedEvent  += args => { writer.WriteLine($"[FAIL] {args.Message.Test.DisplayName}{Environment.NewLine}{ExceptionUtility.CombineMessages (args.Message)}{Environment.NewLine}{ExceptionUtility.CombineStackTraces (args.Message)}"); Interop.mono_sdks_ui_increment_testcase_result(2); };

        testSink.Execution.TestAssemblyStartingEvent += args => { writer.WriteLine($"Running tests for {args.Message.TestAssembly.Assembly}"); };
        testSink.Execution.TestAssemblyFinishedEvent += args => { writer.WriteLine($"Finished {args.Message.TestAssembly.Assembly}{Environment.NewLine}"); };

        controller.RunTests(discoverySink.TestCases, resultsSink, testOptions);
        resultsSink.Finished.WaitOne();

        writer.WriteLine($"STARTRESULTXML");
        var resultsXml = new XElement("assemblies");
        resultsXml.Add(resultsXmlAssembly);
        resultsXml.Save(writer.RawStream);
        writer.WriteLine();
        writer.WriteLine($"ENDRESULTXML");

        var failed = resultsSink.ExecutionSummary.Failed > 0 || resultsSink.ExecutionSummary.Errors > 0;
        return(failed ? 1 : 0);
#else
        MonoSdksTextUI runner;
        TcpWriter      writer     = null;
        string         resultsXml = null;

        if (host != null)
        {
            Console.WriteLine($"Connecting to harness at {host}:{port}.");
            resultsXml = Path.GetTempFileName();
            args       = args.Concat(new string[] { "-format:xunit", $"-result:{resultsXml}" }).ToArray();
            writer     = new TcpWriter(host, port);
            runner     = new MonoSdksTextUI(writer);
        }
        else
        {
            runner = new MonoSdksTextUI();
        }

        runner.Execute(args);

        if (resultsXml != null)
        {
            writer.WriteLine($"STARTRESULTXML");
            using (var resultsXmlStream = File.OpenRead(resultsXml)) resultsXmlStream.CopyTo(writer.RawStream);
            writer.WriteLine();
            writer.WriteLine($"ENDRESULTXML");
        }

        return(runner.Failure ? 1 : 0);
#endif
    }