Skip to content

Commit dfe6d44

Browse files
committed
reporter class refactor
1 parent aa732d3 commit dfe6d44

File tree

4 files changed

+70
-56
lines changed

4 files changed

+70
-56
lines changed

Ghpr.Controller.ts/report/src/js/ghpr.controller.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Ghpr.Core/Ghpr.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="EmbeddedResources\ResourceExtractor.cs" />
8383
<Compile Include="Enums\ResourceType.cs" />
8484
<Compile Include="Properties\AssemblyInfo.cs" />
85+
<Compile Include="Utils\ActionHelper.cs" />
8586
<Compile Include="Utils\GuidConverter.cs" />
8687
<Compile Include="Utils\Ids.cs" />
8788
<Compile Include="Utils\Log.cs" />

Ghpr.Core/Reporter.cs

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,106 +21,100 @@ public class Reporter
2121
public static bool TakeScreenshotAfterFail => Properties.Settings.Default.TakeScreenshotAfterFail;
2222
public static string Sprint => Properties.Settings.Default.Sprint;
2323
public static string RunName => Properties.Settings.Default.RunName;
24-
public static bool RealTime => Properties.Settings.Default.RealTime;
24+
public static bool RealTimeGeneration => Properties.Settings.Default.RealTime;
2525

26-
public const string TestsFolder = "tests";
27-
public const string RunsFolder = "runs";
26+
public const string TestsFolderName = "tests";
27+
public const string RunsFolderName = "runs";
2828

29-
private void CleanUp()
29+
private void SetUp()
3030
{
31-
_currentRun = new Run(Guid.NewGuid())
31+
ActionHelper.SafeAction(() =>
3232
{
33-
TestRunFiles = new List<string>(),
34-
RunSummary = new RunSummary()
35-
};
36-
_currentRunningTests = new List<ITestRun>();
37-
}
38-
39-
public void RunStarted()
40-
{
41-
try
42-
{
43-
CleanUp();
33+
_currentRun = new Run(Guid.NewGuid())
34+
{
35+
TestRunFiles = new List<string>(),
36+
RunSummary = new RunSummary()
37+
};
38+
_currentRunningTests = new List<ITestRun>();
39+
4440
_currentRun.Name = RunName;
4541
_currentRun.Sprint = Sprint;
4642
Extractor.ExtractReportBase();
4743
_currentRun.RunInfo.Start = DateTime.Now;
48-
}
49-
catch (Exception ex)
44+
});
45+
}
46+
47+
private void UpdateCurrentRunSummary(ITestRun finalTest)
48+
{
49+
ActionHelper.SafeAction(() =>
5050
{
51-
Log.Exception(ex, "Exception in RunStarted");
52-
}
51+
_currentRun.RunSummary = _currentRun.RunSummary.Update(finalTest);
52+
});
5353
}
5454

55-
public void RunFinished()
55+
private void GenerateReport()
5656
{
57-
try
57+
ActionHelper.SafeAction(() =>
5858
{
5959
_currentRun.RunInfo.Finish = DateTime.Now;
60-
var runsPath = Path.Combine(OutputPath, RunsFolder);
60+
var runsPath = Path.Combine(OutputPath, RunsFolderName);
6161
_currentRun.Save(runsPath);
6262
RunsHelper.SaveCurrentRunInfo(runsPath, _currentRun.RunInfo);
63-
}
64-
catch (Exception ex)
65-
{
66-
Log.Exception(ex, "Exception in RunFinished");
67-
}
63+
});
64+
}
65+
66+
public void RunStarted()
67+
{
68+
SetUp();
69+
}
70+
71+
public void RunFinished()
72+
{
73+
GenerateReport();
6874
}
6975

7076
public void TestStarted(ITestRun testRun)
7177
{
72-
try
78+
ActionHelper.SafeAction(() =>
7379
{
7480
_currentRunningTests.Add(testRun);
75-
}
76-
catch (Exception ex)
77-
{
78-
Log.Exception(ex, "Exception in TestStarted");
79-
}
81+
});
8082
}
8183

8284
public void TestFinished(ITestRun testRun)
8385
{
84-
_currentRun.RunSummary.Total++;
85-
try
86+
ActionHelper.SafeAction(() =>
8687
{
88+
_currentRun.RunSummary.Total++;
89+
8790
var finishDateTime = DateTime.Now;
8891
var currentTest = _currentRunningTests.GetTest(testRun);
8992
var finalTest = testRun.Update(currentTest);
9093
var testGuid = finalTest.TestInfo.Guid.ToString();
91-
92-
_currentRun.RunSummary = _currentRun.RunSummary.Update(finalTest);
93-
94-
var testsPath = Path.Combine(OutputPath, TestsFolder);
94+
var testsPath = Path.Combine(OutputPath, TestsFolderName);
9595
var testPath = Path.Combine(testsPath, testGuid);
9696
var fileName = finishDateTime.GetTestName();
97+
98+
UpdateCurrentRunSummary(finalTest);
99+
97100
finalTest.TestInfo.FileName = fileName;
98101
finalTest.RunGuid = _currentRun.RunInfo.Guid;
99-
if (finalTest.TestInfo.Start.Equals(default(DateTime)))
100-
{
101-
finalTest.TestInfo.Start = finishDateTime;
102-
}
103-
if (finalTest.TestInfo.Finish.Equals(default(DateTime)))
104-
{
105-
finalTest.TestInfo.Finish = finishDateTime;
106-
}
102+
finalTest.TestInfo.Start = finalTest.TestInfo.Start.Equals(default(DateTime)) ? finishDateTime : finalTest.TestInfo.Start;
103+
finalTest.TestInfo.Finish = finalTest.TestInfo.Finish.Equals(default(DateTime)) ? finishDateTime : finalTest.TestInfo.Finish;
107104
finalTest
108105
.TakeScreenshot(testPath, TakeScreenshotAfterFail)
109106
.Save(testPath, fileName);
110107
_currentRunningTests.Remove(currentTest);
111108
_currentRun.TestRunFiles.Add($"{testGuid}\\{fileName}");
112109
Extractor.ExtractTestPage(testsPath);
110+
113111
TestRunsHelper.SaveCurrentTestInfo(testPath, finalTest.TestInfo);
114112

115-
if (RealTime)
113+
if (RealTimeGeneration)
116114
{
117-
RunFinished();
115+
GenerateReport();
118116
}
119-
}
120-
catch (Exception ex)
121-
{
122-
Log.Exception(ex, $"Exception in TestFinished {testRun.FullName}");
123-
}
117+
});
124118
}
125119
}
126120
}

Ghpr.Core/Utils/ActionHelper.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace Ghpr.Core.Utils
4+
{
5+
public static class ActionHelper
6+
{
7+
public static void SafeAction(Action a)
8+
{
9+
try
10+
{
11+
a.Invoke();
12+
}
13+
catch (Exception ex)
14+
{
15+
Log.Exception(ex, $"Exception in method '{a.Method.Name}'");
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)