-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathReportTxtFileWriter.cs
140 lines (116 loc) · 4.45 KB
/
ReportTxtFileWriter.cs
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
133
134
135
136
137
138
139
140
using Meadow.CoverageReport.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
namespace Meadow.CoverageReport
{
class ReportTxtFileWriter
{
public ReportTxtFileWriter()
{
}
public void WriteTestResults(IGrouping<string, UnitTestResult>[] unitTestOutcome, IndexViewModel indexView, string outputDirectory, string filename)
{
var sb = new StringBuilder();
if (unitTestOutcome != null)
{
WriteTestOutcomes(sb, unitTestOutcome);
}
WriteCoverageTable(sb, indexView);
WriteFileHashes(sb, indexView);
var rawUnitTestOutcomeReport = sb.ToString().Trim();
string outputFile = Path.Combine(outputDirectory, filename);
File.WriteAllText(outputFile, rawUnitTestOutcomeReport, new UTF8Encoding(false, false));
}
static void WriteTestOutcomes(StringBuilder sb, IGrouping<string, UnitTestResult>[] unitTestOutcome)
{
TimeSpan durationPassed = TimeSpan.Zero;
TimeSpan durationFailed = TimeSpan.Zero;
int countPassed = 0;
int countFailed = 0;
foreach (var testGroup in unitTestOutcome)
{
sb.AppendLine();
sb.AppendLine($"{testGroup.Key}");
foreach (var testResult in testGroup.OrderBy(t => t.TestName))
{
if (testResult.Passed)
{
durationPassed += testResult.Duration;
countPassed++;
}
else
{
durationFailed += testResult.Duration;
countFailed++;
}
var icon = testResult.Passed ? "√" : "✗";
sb.AppendLine($" {icon} {testResult.TestName} ({Math.Round(testResult.Duration.TotalMilliseconds)}ms)");
}
}
sb.AppendLine();
if (countPassed > 0)
{
sb.AppendLine($"{countPassed} passed ({Math.Round(durationPassed.TotalSeconds)}s)");
}
if (countFailed > 0)
{
sb.AppendLine($"{countFailed} failed ({Math.Round(durationFailed.TotalSeconds)}s)");
}
}
static void WriteCoverageTable(StringBuilder sb, IndexViewModel indexView)
{
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("--------");
sb.AppendLine("Coverage");
var rows = new List<string[]>();
string[] CreateRow(string rowName, CoverageStats entry)
{
return new string[]
{
rowName,
$"{entry.LineCoveredCount}/{entry.LineCount}",
$"{entry.LineCoveragePercent}%",
$"{entry.BranchCoveredCount}/{entry.BranchCount}",
$"{entry.BranchCoveragePercent}%",
$"{entry.FunctionCoveredCount}/{entry.FunctionCount}",
$"{entry.FunctionCoveragePercent}%"
};
}
foreach (var entry in indexView.SourceFileMaps)
{
rows.Add(CreateRow(entry.SourceFilePath, entry));
}
var coverageTable = new AsciiTable
{
Columns = new[] { "File", "Lines", string.Empty, "Branches", string.Empty, "Functions", string.Empty },
Rows = rows.ToArray(),
Footer = CreateRow("All files", indexView)
};
coverageTable.WriteToString(sb);
}
static void WriteFileHashes(StringBuilder sb, IndexViewModel indexView)
{
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("--------");
sb.AppendLine("Source File Hashes");
var rows = new List<string[]>();
foreach (var entry in indexView.SourceFileMaps)
{
rows.Add(new string[] { entry.SourceFilePath, entry.SourceHashSha256 });
}
var hashTable = new AsciiTable
{
Columns = new[] { "File", "Fingerprint (SHA256)" },
Rows = rows.ToArray(),
SplitterChar = ' '
};
hashTable.WriteToString(sb);
}
}
}