Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ResultFormatter.cs « XUnit « TestUtils « src « runtest « CoreFX « tests - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a1b10ee137d543abab03d92d22232cf8e4d8bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/// <summary>
/// This helper class reads xunit xml log files and prints the tallied test results to the console
/// </summary>
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using Newtonsoft.Json;

namespace CoreFX.TestUtils.XUnit
{
    public static class ResultFormatter
    {
        private static string logDir;
        private static string logPattern;

        public static void Main(string[] args)
        {
            ArgumentSyntax syntax = ParseCommandLine(args);
            IEnumerable<string> logFiles = DiscoverLogs(logDir, logPattern);
            PrintTotals(logFiles);
        }

        private static void PrintTotals(IEnumerable<string> logFiles)
        {
            int total = 0;
            int passed = 0;
            int failed = 0;
            int skipped = 0;
            ulong timeElapsed = 0;
            bool printTotals = false;

            foreach (string logFile in logFiles)
            {
                try
                {

                    // XMLReader escapes the character sequence \\.. as just a single backslash \ - Is this intended behavior? 
                    using (XmlReader reader = XmlReader.Create(logFile.Replace(@"\\..", @"\..")))
                    {
                        reader.MoveToContent();
                        reader.ReadToDescendant("collection");
                        do
                        {
                            // Get total tests in current element
                            string totalAttr = reader.GetAttribute("total");
                            int currentTotal;
                            Int32.TryParse(totalAttr, out currentTotal);

                            // Get passed tests 
                            string passedAttr = reader.GetAttribute("passed");
                            int currentPassed;
                            Int32.TryParse(passedAttr, out currentPassed);

                            // Get failed tests
                            string failedAttr = reader.GetAttribute("failed");
                            int currentFailed;
                            Int32.TryParse(failedAttr, out currentFailed);

                            // Get skipped tests
                            string skippedAttr = reader.GetAttribute("skipped");
                            int currentSkipped;
                            Int32.TryParse(skippedAttr, out currentSkipped);

                            // Get time elapsed
                            string timeAttr = reader.GetAttribute("time");
                            ulong currentTime;
                            UInt64.TryParse(timeAttr, out currentTime);

                            // Update running total only once current element has been parsed
                            total += currentTotal;
                            passed += currentPassed;
                            failed += currentFailed;
                            skipped += currentSkipped;
                            timeElapsed += currentTime;

                        } while (reader.ReadToNextSibling("collection"));

                        // If we've fully parsed a log, print totals
                        printTotals = true;
                    }
                }
                catch (XmlException exc)
                {
                    Console.WriteLine("Malformed Log: {0} ", logFile);
                    Console.WriteLine("Reason: {0} ", exc.Message);
                    continue;
                }
            }

            if (printTotals)
            {
                Console.WriteLine("=== CoreFX TEST EXECUTION SUMMARY ===: ");
                Console.WriteLine(String.Format("Total: {0}, Passed: {1}, Failed: {2}, Skipped: {3}", total, passed, failed, timeElapsed));
                Console.WriteLine("Detailed logs written to: " + logDir);
            }
        }

        private static ArgumentSyntax ParseCommandLine(string[] args)
        {

            ArgumentSyntax argSyntax = ArgumentSyntax.Parse(args, syntax =>
            {
                syntax.DefineOption("log|logDirectory|logDir", ref logDir, "Path to directory of xml test results");
                syntax.DefineOption("pattern|p", ref logPattern, "Pattern of XUnit log filenames for which to search");
            });

            return argSyntax;
        }

        public static IEnumerable<string> DiscoverLogs(string logDirectory, string logPattern)
        {
            Debug.Assert(Directory.Exists(logDirectory));
            Console.WriteLine(logDirectory);
            var logFiles = Directory.EnumerateFiles(logDirectory, logPattern, SearchOption.AllDirectories);

            return logFiles;
        }


    }
}