Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape some characters in xunit_results.xml #252

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace TcUnit.Verifier
{
class FB_TestXUnitPublisher : TestFunctionBlockAssert
{
public FB_TestXUnitPublisher(IEnumerable<ErrorList.Error> errors, string xmlTestResultsFilePath, string testFunctionBlockInstance = null)
: base(errors, testFunctionBlockInstance)
{
Test_ParceXml(xmlTestResultsFilePath);
Test_EscapedFailedMessage();
Test_EscapedFunctionName();
}
private void Test_ParceXml(string xmlTestResultsFilePath)
{
try
{
XDocument _ = XDocument.Load(xmlTestResultsFilePath);
}
catch (Exception)
{
log.Info("Test suite " + _testFunctionBlockInstance + " could not parse xml file: " + xmlTestResultsFilePath);
}

}
private void Test_EscapedFailedMessage()
{
string testMessage = "PRG_TEST." + _testFunctionBlockInstance + "@Test_EscapedFailedMessage";
AssertContainsMessage(testMessage, EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelHigh);
}

private void Test_EscapedFunctionName()
{
string testMessage = "PRG_TEST." + _testFunctionBlockInstance + "@Test_EscapedFunctionName";
AssertDoesNotContainMessage(testMessage, EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelHigh);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NDesk.Options;
using NDesk.Options;
using log4net;
using System;
using System.Collections.Generic;
Expand All @@ -16,9 +16,10 @@ class Program
{
private static string tcUnitVerifierPath = null;
private static string tcUnitTargetNetId = "127.0.0.1.1.1";
private static string tcUnitXUnitFilePath = @"C:\TwinCAT\3.1\Boot\tcunit_xunit_testresults.xml";
private static VisualStudioInstance vsInstance = null;
private static ILog log = LogManager.GetLogger("TcUnit-Verifier");
private static int expectedNumberOfFailedTests = 121; // Update this if you add intentionally failing tests
private static int expectedNumberOfFailedTests = 122; // Update this if you add intentionally failing tests

[STAThread]
static void Main(string[] args)
Expand All @@ -31,6 +32,7 @@ static void Main(string[] args)
OptionSet options = new OptionSet()
.Add("v=|TcUnitVerifierPath=", "Path to TcUnit-Verifier TwinCAT solution", v => tcUnitVerifierPath = v)
.Add("t=|TcUnitTargetNetId=", "[OPTIONAL] Target NetId of TwinCAT runtime to run TcUnit-Verifier", t => tcUnitTargetNetId = t)
.Add("x=|TcUnitXUnitFilePath=", "[OPTIONAL] path and filename for the TcUnit-Verifier xunit testresults", t => tcUnitXUnitFilePath = t)
.Add("?|h|help", h => showHelp = h != null);

try
Expand Down Expand Up @@ -213,6 +215,7 @@ static void Main(string[] args)
new FB_TestDurationMeasurement(errors);
new FB_EmptyAssertionMessage(errors);
new FB_AssertCountExceedsMaxNumber(errors);
new FB_TestXUnitPublisher(errors, tcUnitXUnitFilePath);

log.Info("Done.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="FB_AssertCountExceedsMaxNumber.cs" />
<Compile Include="FB_EmptyAssertionMessage.cs" />
<Compile Include="FB_TestDurationMeasurement.cs" />
<Compile Include="FB_TestXUnitPublisher.cs" />
<Compile Include="FB_TestXmlControl.cs" />
<Compile Include="FB_TestFileControl.cs" />
<Compile Include="FB_TestStreamBuffer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestFunctionBlockAssert
{
private IEnumerable<ErrorList.Error> _errors;
protected string _testFunctionBlockInstance;
private static ILog log = LogManager.GetLogger("TcUnit-Verifier");
protected static ILog log = LogManager.GetLogger("TcUnit-Verifier");

private string DefaultFunctionBlockInstance
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<Compile Include="Test\FB_TestXmlControl.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="Test\FB_TestXUnitPublisher.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="Test\FB_WriteProtectedFunctions.TcPOU">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -146,6 +149,12 @@
<PlaceholderReference Include="TcUnit">
<DefaultResolution>TcUnit, * (www.tcunit.org)</DefaultResolution>
<Namespace>TcUnit</Namespace>
<Parameters>
<Parameter ListName="GVL_PARAM_TCUNIT" xmlns="">
<Key>XUNITENABLEPUBLISH</Key>
<Value>TRUE</Value>
</Parameter>
</Parameters>
</PlaceholderReference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
<POU Name="FB_TestXUnitPublisher" Id="{c7819ac2-5de4-4840-89ae-815e54b5cb69}" SpecialFunc="None">
<Declaration><![CDATA[// Contains tests that verify the XmlControl function block methods
FUNCTION_BLOCK FB_TestXUnitPublisher EXTENDS TcUnit.FB_TestSuite]]></Declaration>
<Implementation>
<ST><![CDATA[Test_EscapedFailedMessage();
Test_EscapedFunctionName();]]></ST>
</Implementation>
<Method Name="Test_EscapedFailedMessage" Id="{8aca4c27-6db1-484c-a76f-a529f691497e}">
<Declaration><![CDATA[METHOD PRIVATE Test_EscapedFailedMessage]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('Test_EscapedFailedMessage');

AssertTrue(FALSE, 'This "string" <should> be $'escaped$' & parsed properly');

TEST_FINISHED();]]></ST>
</Implementation>
</Method>
<Method Name="Test_EscapedFunctionName" Id="{da2058a6-4b6f-43f7-8364-e0cf3494d96b}">
<Declaration><![CDATA[METHOD PRIVATE Test_EscapedFunctionName]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('Test_EscapedFunctionName$'"<>&');

TEST_FINISHED();]]></ST>
</Implementation>
</Method>
</POU>
</TcPlcObject>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ VAR
TestFinishedNamed : FB_TestFinishedNamed;
EmptyAssertionMessage : FB_EmptyAssertionMessage;
AssertCountExceedsMaxNumber : FB_AssertCountExceedsMaxNumber;
TestXUnitPublisher : FB_TestXUnitPublisher;
(* The testsuite below is not active, as it will make TcUnit to abort. Uncomment if you want
to test the function of where a test with a name that doesn't exist is set to finished *)
//TestFinishedNamedDoesNotExist : FB_TestFinishedNamedDoesNotExist;
Expand Down
4 changes: 2 additions & 2 deletions TcUnit/TcUnit/POUs/FB_xUnitXmlPublisher.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ IF PublishTrigger.Q THEN
FOR CurrentTestCount := 1 TO UnitTestResults.TestSuiteResults[CurrentSuiteNumber].NumberOfTests BY 1 DO
// <testcase>
Xml.NewTag('testcase');
Xml.NewParameter('name', UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].TestName);
Xml.NewParameter('name', F_XmlEscapeString(UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].TestName));
Xml.NewParameter('classname', UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].TestClassName);
Xml.NewParameter('time', LREAL_TO_STRING(UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].Duration));

Expand All @@ -133,7 +133,7 @@ IF PublishTrigger.Q THEN
<failure message="Values differ" type="BYTE" />
*)
Xml.NewTag('failure');
Xml.NewParameter('message', UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].FailureMessage);
Xml.NewParameter('message', F_XmlEscapeString(UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].FailureMessage));
Xml.NewParameter('type', F_AssertionTypeToString(UnitTestResults.TestSuiteResults[CurrentSuiteNumber].TestCaseResults[CurrentTestCount].FailureType));
// Close failure tag
Xml.CloseTag();
Expand Down
35 changes: 35 additions & 0 deletions TcUnit/TcUnit/POUs/Functions/F_XmlEscapeString.TcPOU
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
<POU Name="F_XmlEscapeString" Id="{5bc10c6a-24ec-0a65-0666-70c1e69087d6}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION F_XmlEscapeString : T_MaxString
VAR_INPUT
Message : T_MaxString;
END_VAR
VAR
EscapedMessage : T_MaxString;
EscapedMessageSize : UDINT := SIZEOF(T_MaxString);
END_VAR
VAR CONSTANT
QUOTE : STRING(1) := '"';
APOS : STRING(2) := '$'';
_LT : STRING(1) := '<';
_GT : STRING(1) := '>';
AMP : STRING(1) := '&';

ESCAPED_QUOTE : STRING(6) := '&quot;';
ESCAPED_APOS : STRING(6) := '&apos;';
ESCAPED_LT : STRING(4) := '&lt;';
ESCAPED_GT : STRING(4) := '&gt;';
ESCAPED_AMP : STRING(5) := '&amp;';
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[EscapedMessage := Message;
FindAndReplace(ADR(EscapedMessage), ADR(AMP), ADR(ESCAPED_AMP), ADR(EscapedMessage), SIZEOF(T_MaxString));
FindAndReplace(ADR(EscapedMessage), ADR(QUOTE), ADR(ESCAPED_QUOTE), ADR(EscapedMessage), SIZEOF(T_MaxString));
FindAndReplace(ADR(EscapedMessage), ADR(APOS), ADR(ESCAPED_APOS), ADR(EscapedMessage), SIZEOF(T_MaxString));
FindAndReplace(ADR(EscapedMessage), ADR(_LT), ADR(ESCAPED_LT), ADR(EscapedMessage), SIZEOF(T_MaxString));
FindAndReplace(ADR(EscapedMessage), ADR(_GT), ADR(ESCAPED_GT), ADR(EscapedMessage), SIZEOF(T_MaxString));
F_XmlEscapeString := EscapedMessage;]]></ST>
</Implementation>
</POU>
</TcPlcObject>
5 changes: 4 additions & 1 deletion TcUnit/TcUnit/TcUnit.plcproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>-->
Expand Down Expand Up @@ -161,6 +161,9 @@ Documentation and examples are available at www.tcunit.org</Description>
<Compile Include="POUs\Functions\F_RemoveInstancePathAndProjectNameFromTestInstancePath.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="POUs\Functions\F_XmlEscapeString.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="POUs\Functions\IS_TEST_FINISHED.TcPOU">
<SubType>Code</SubType>
</Compile>
Expand Down