示例#1
0
        public bool TestsSucceeded(Log listener_log, bool timed_out, bool crashed)
        {
            string log;

            using (var reader = listener_log.GetReader())
                log = reader.ReadToEnd();
            // parsing the result is different if we are in jenkins or not.
            // When in Jenkins, Touch.Unit produces an xml file instead of a console log (so that we can get better test reporting).
            // However, for our own reporting, we still want the console-based log. This log is embedded inside the xml produced
            // by Touch.Unit, so we need to extract it and write it to disk. We also need to re-save the xml output, since Touch.Unit
            // wraps the NUnit xml output with additional information, which we need to unwrap so that Jenkins understands it.
            if (Harness.InJenkins)
            {
                // we have to parse the xml result
                crashed = false;
                var xmldoc = new XmlDocument();
                try {
                    xmldoc.LoadXml(log);

                    var nunit_output = xmldoc.SelectSingleNode("/TouchUnitTestRun/NUnitOutput");
                    var xmllog       = nunit_output.InnerXml;
                    var extra_output = xmldoc.SelectSingleNode("/TouchUnitTestRun/TouchUnitExtraData");
                    log = extra_output.InnerText;

                    File.WriteAllText(listener_log.FullPath, log);

                    var testsResults = new XmlDocument();
                    testsResults.LoadXml(xmllog);

                    var mainResultNode = testsResults.SelectSingleNode("test-results");
                    if (mainResultNode == null)
                    {
                        Harness.LogWrench($"Node is null.");
                    }
                    else
                    {
                        // update the information of the main node to add information about the mode and the test that is excuted. This will later create
                        // nicer reports in jenkins
                        mainResultNode.Attributes ["name"].Value = Target.AsString();
                        // store a clean version of the logs, later this will be used by the bots to show results in github/web
                        var path = listener_log.FullPath;
                        path = Path.ChangeExtension(path, "xml");
                        testsResults.Save(path);
                        Logs.AddFile(path, "Test xml");
                    }
                } catch (Exception e) {
                    main_log.WriteLine("Could not parse xml result file: {0}", e);

                    if (timed_out)
                    {
                        Harness.LogWrench($"@MonkeyWrench: AddSummary: <b><i>{mode} timed out</i></b><br/>");
                        return(false);
                    }
                    else
                    {
                        Harness.LogWrench($"@MonkeyWrench: AddSummary: <b><i>{mode} crashed</i></b><br/>");
                        main_log.WriteLine("Test run crashed");
                        crashed = true;
                        return(false);
                    }
                }
            }

            // parsing the human readable results
            if (log.Contains("Tests run"))
            {
                var tests_run = string.Empty;
                var log_lines = log.Split('\n');
                var failed    = false;
                foreach (var line in log_lines)
                {
                    if (line.Contains("Tests run:"))
                    {
                        Console.WriteLine(line);
                        tests_run = line.Replace("Tests run: ", "");
                        break;
                    }
                    else if (line.Contains("FAIL"))
                    {
                        Console.WriteLine(line);
                        failed = true;
                    }
                }

                if (failed)
                {
                    Harness.LogWrench("@MonkeyWrench: AddSummary: <b>{0} failed: {1}</b><br/>", mode, tests_run);
                    main_log.WriteLine("Test run failed");
                    return(false);
                }
                else
                {
                    Harness.LogWrench("@MonkeyWrench: AddSummary: {0} succeeded: {1}<br/>", mode, tests_run);
                    main_log.WriteLine("Test run succeeded");
                    return(true);
                }
            }
            else if (timed_out)
            {
                Harness.LogWrench("@MonkeyWrench: AddSummary: <b><i>{0} timed out</i></b><br/>", mode);
                return(false);
            }
            else
            {
                Harness.LogWrench("@MonkeyWrench: AddSummary: <b><i>{0} crashed</i></b><br/>", mode);
                main_log.WriteLine("Test run crashed");
                crashed = true;
                return(false);
            }
        }