Skip to content

Commit

Permalink
Trx Parsing Improvements (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst authored Sep 5, 2024
1 parent bb1cc09 commit 2e87c1b
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 59 deletions.
1 change: 0 additions & 1 deletion .idea/.idea.ModularPipelines/.idea/.name

This file was deleted.

2 changes: 2 additions & 0 deletions src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- The namespace for the TrxParser has changed
- The TrxParser now parses the ResultSummary tag
7 changes: 3 additions & 4 deletions src/ModularPipelines.DotNet/DotNetTestResult.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using ModularPipelines.DotNet.Enums;
using ModularPipelines.DotNet.Parsers.NUnitTrx;
using ModularPipelines.DotNet.Parsers.Trx;

namespace ModularPipelines.DotNet;

public record DotNetTestResult(IReadOnlyList<UnitTestResult> UnitTestResults)
public record DotNetTestResult(IReadOnlyList<UnitTestResult> UnitTestResults, ResultSummary ResultSummary)
{
public bool Successful => UnitTestResults.All(x => x.Outcome != TestOutcome.Failed);
public bool Successful => ResultSummary.Outcome is "Passed" or "Completed";
}
2 changes: 1 addition & 1 deletion src/ModularPipelines.DotNet/Extensions/DotNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ModularPipelines.Context;
using ModularPipelines.DotNet.Parsers.NUnitTrx;
using ModularPipelines.DotNet.Parsers.Trx;
using ModularPipelines.DotNet.Services;
using ModularPipelines.Engine;

Expand Down
49 changes: 0 additions & 49 deletions src/ModularPipelines.DotNet/Parsers/NUnitTrx/TrxParser.cs

This file was deleted.

19 changes: 19 additions & 0 deletions src/ModularPipelines.DotNet/Parsers/Trx/Counters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace ModularPipelines.DotNet.Parsers.Trx;

public record Counters(
int Total,
int Executed,
int Passed,
int Failed,
int Error,
int Timeout,
int Aborted,
int Inconclusive,
int PassedButRunAborted,
int NotRunnable,
int NotExecuted,
int Disconnected,
int Warning,
int Completed,
int InProgress,
int Pending);
3 changes: 3 additions & 0 deletions src/ModularPipelines.DotNet/Parsers/Trx/ResultSummary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ModularPipelines.DotNet.Parsers.Trx;

public record ResultSummary(string Outcome, Counters Counters);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Xml.Serialization;

namespace ModularPipelines.DotNet.Parsers.NUnitTrx;
namespace ModularPipelines.DotNet.Parsers.Trx;

[XmlRoot(ElementName = "Output")]
[ExcludeFromCodeCoverage]
Expand Down
88 changes: 88 additions & 0 deletions src/ModularPipelines.DotNet/Parsers/Trx/TrxParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Xml.Linq;
using ModularPipelines.DotNet.Enums;

namespace ModularPipelines.DotNet.Parsers.Trx;

public class TrxParser : ITrxParser
{
public DotNetTestResult ParseTrxContents(string input)
{
var xDocument = XDocument.Load(new StringReader(input));
return new DotNetTestResult(GetUnitTestResults(xDocument), GetResultSummary(xDocument));
}

private ResultSummary GetResultSummary(XDocument document)
{
return ParseResultSummary(document
.Descendants()
.First(e => e.Name.LocalName == "ResultSummary"));
}

private ResultSummary ParseResultSummary(XElement element)
{
var outcome = element.Attribute("outcome")!.Value;

var counters = ParseCounters(element.Descendants().First(e => e.Name.LocalName == "Counters"));

return new ResultSummary(outcome, counters);
}

private Counters ParseCounters(XElement element)
{
return new Counters(
int.Parse(element.Attribute("total")!.Value),
int.Parse(element.Attribute("executed")!.Value),
int.Parse(element.Attribute("passed")!.Value),
int.Parse(element.Attribute("failed")!.Value),
int.Parse(element.Attribute("error")!.Value),
int.Parse(element.Attribute("timeout")!.Value),
int.Parse(element.Attribute("aborted")!.Value),
int.Parse(element.Attribute("inconclusive")!.Value),
int.Parse(element.Attribute("passedButRunAborted")!.Value),
int.Parse(element.Attribute("notRunnable")!.Value),
int.Parse(element.Attribute("notExecuted")!.Value),
int.Parse(element.Attribute("disconnected")!.Value),
int.Parse(element.Attribute("warning")!.Value),
int.Parse(element.Attribute("completed")!.Value),
int.Parse(element.Attribute("inProgress")!.Value),
int.Parse(element.Attribute("pending")!.Value)
);
}

private List<UnitTestResult> GetUnitTestResults(XDocument xDocument)
{
return xDocument.Descendants()
.Where(d => d.Name.LocalName == "UnitTestResult")
.Select(ParseUnitTestResult)
.ToList();
}

private UnitTestResult ParseUnitTestResult(XElement element)
{
var errorInfo = element.Descendants().FirstOrDefault(x => x.Name.LocalName == "ErrorInfo");

return new UnitTestResult
{
ExecutionId = element.Attribute("executionId")!.Value,
TestId = element.Attribute("testId")!.Value,
TestName = element.Attribute("testName")!.Value,
ComputerName = element.Attribute("computerName")!.Value,
Duration = TimeSpan.Parse(element.Attribute("duration")!.Value),
StartTime = DateTimeOffset.Parse(element.Attribute("startTime")!.Value),
EndTime = DateTimeOffset.Parse(element.Attribute("endTime")!.Value),
TestType = element.Attribute("testType")!.Value,
Outcome = Enum.Parse<TestOutcome>(element.Attribute("outcome")!.Value),
TestListId = element.Attribute("testListId")!.Value,
RelativeResultsDirectory = element.Attribute("relativeResultsDirectory")!.Value,
Output = new TestOutput
{
StdOut = element.Descendants().FirstOrDefault(x => x.Name.LocalName == "StdOut")?.Value,
ErrorInfo = errorInfo == null ? null : new ErrorInfo
{
Message = errorInfo.Descendants().FirstOrDefault(x => x.Name.LocalName == "Message")?.Value,
StackTrace = errorInfo.Descendants().FirstOrDefault(x => x.Name.LocalName == "StackTrace")?.Value,
},
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Xml.Serialization;
using ModularPipelines.DotNet.Enums;

namespace ModularPipelines.DotNet.Parsers.NUnitTrx;
namespace ModularPipelines.DotNet.Parsers.Trx;

[Serializable]
[XmlRoot(ElementName = "UnitTestResult")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ModularPipelines.Context;
using ModularPipelines.DotNet.Extensions;
using ModularPipelines.DotNet.Options;
using ModularPipelines.DotNet.Parsers.NUnitTrx;
using ModularPipelines.DotNet.Parsers.Trx;
using ModularPipelines.Enums;
using ModularPipelines.Exceptions;
using ModularPipelines.Git.Extensions;
Expand Down
2 changes: 1 addition & 1 deletion test/ModularPipelines.UnitTests/TrxParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using ModularPipelines.DotNet.Enums;
using ModularPipelines.DotNet.Extensions;
using ModularPipelines.DotNet.Options;
using ModularPipelines.DotNet.Parsers.NUnitTrx;
using ModularPipelines.DotNet.Parsers.Trx;
using ModularPipelines.Enums;
using ModularPipelines.Git.Extensions;
using ModularPipelines.Modules;
Expand Down

0 comments on commit 2e87c1b

Please sign in to comment.