Skip to content

Commit

Permalink
[JENKINS-28871] CppTest 9.5 report file seems not valid for xUnit whe…
Browse files Browse the repository at this point in the history
…n no tests was executed

Improve CppTest XSL to better handle 7.x and 9.x reports. Cover also the case no test was run.
  • Loading branch information
nfalco79 committed May 20, 2018
1 parent 6aed82b commit cd5367f
Show file tree
Hide file tree
Showing 16 changed files with 695 additions and 626 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.jenkinsci.lib.dtkit.model.OutputMetric;
import org.jenkinsci.plugins.xunit.types.model.JUnitModel;

public class CppTestUnit extends InputMetricXSL {
public class CppTest extends InputMetricXSL {

@Override
public InputType getToolType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CppTestJunitHudsonTestType(String pattern, boolean skipNoTestFiles, boole
public static class DescriptorImpl extends TestTypeDescriptor<CppTestJunitHudsonTestType> {

public DescriptorImpl() {
super(CppTestJunitHudsonTestType.class, CppTestUnit.class);
super(CppTestJunitHudsonTestType.class, CppTest.class);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,108 +1,170 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*******************************************************************************
* Copyright (c) 2011 Thales Corporate Services SAS *
* Author : Gregory Boissinot, Aravindan Mahendran *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal*
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
*******************************************************************************/
The MIT License (MIT)
Copyright (c) 2011, Gregory Boissinot, Aravindan Mahendran
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xunit="http://www.xunit.org">
<xsl:output method="xml" indent="yes" encoding="UTF-8" cdata-section-elements="system-out system-err failure"/>
<xsl:decimal-format decimal-separator="." grouping-separator=","/>

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:function name="xunit:junit-time" as="xs:string">
<xsl:param name="value" as="xs:double?" />

<xsl:variable name="time" as="xs:double">
<xsl:value-of select="translate(string($value),',','.')" />
</xsl:variable>
<xsl:value-of select="format-number($time, '0.000')" />
</xsl:function>

<xsl:function name="xunit:millis-from-time" as="xs:double">
<xsl:param name="value" as="xs:string?" />

<xsl:variable name="formattedTime" select="xunit:if-empty(replace(translate($value,',','.'), '^(\d:.+)', '0$1'), '00:00:00')" />
<xsl:variable name="time" select="xs:time($formattedTime)" />
<xsl:value-of select="hours-from-time($time)*3600 + minutes-from-time($time)*60 + seconds-from-time($time)" />
</xsl:function>

<xsl:function name="xunit:if-empty" as="xs:string">
<xsl:param name="value" as="xs:string?" />
<xsl:param name="default" as="xs:anyAtomicType?" />
<xsl:value-of select="if (string($value) != '') then string($value) else $default" />
</xsl:function>

<xsl:function name="xunit:is-empty" as="xs:boolean">
<xsl:param name="value" as="xs:string?" />
<xsl:value-of select="string($value) != ''" />
</xsl:function>

<xsl:template match="/">
<testsuite name="{ResultsSession/Exec/Summary/Projects/Project/@name}" time="0"
tests="{Summary/Projects/Project/@testCases}"
failures="{ResultsSession/Exec/Summary/Projects/Project/@fail}">
<xsl:apply-templates select="ResultsSession/Exec"></xsl:apply-templates>
<xsl:apply-templates select="ResultsSession/ExecutedTestsDetails"></xsl:apply-templates>
</testsuite>
<xsl:variable name="testCount" select="ResultsSession/Exec/Summary/Projects/Project/@testCases" />
<xsl:variable name="failureCount" select="ResultsSession/Exec/Summary/Projects/Project/@fail" />
<xsl:variable name="suiteName" select="ResultsSession/Exec/Summary/Projects/Project/@name" />
<xsl:variable name="totalTime" select="xunit:junit-time(xunit:millis-from-time(ResultsSession/Exec/@time))" />

<xsl:choose>
<xsl:when test="ResultsSession/Exec/TestingProcessProblems">
<!-- No tests was run -->
<testsuite name="{ResultsSession/TestConfig/@name}"
time="{$totalTime}"
tests="0"
failures="0">
<xsl:element name="system-err">
<xsl:value-of select="ResultsSession/Exec/TestingProcessProblems/CppAnalysisProblem/ErrorList/Error/@val"/>
</xsl:element>
</testsuite>
</xsl:when>
<xsl:when test="ResultsSession/ExecutedTestsDetails">
<!-- CppTest 9.x -->
<testsuites name="{$suiteName}"
time="{$totalTime}"
tests="{$testCount}"
failures="{$failureCount}">
<xsl:apply-templates select="ResultsSession/ExecutedTestsDetails/Total/Project/TestSuite">
<xsl:with-param name="suiteName" select="$suiteName" />
</xsl:apply-templates>
</testsuites>
</xsl:when>
<xsl:otherwise>
<!-- CppTest 7.x -->
<testsuite name="{$suiteName}"
time="{$totalTime}"
tests="{$testCount}"
failures="{$failureCount}">
<xsl:call-template name="TestCase_7x">
<xsl:with-param name="violations" select="ResultsSession/Exec/ExecViols"/>
</xsl:call-template>
</testsuite>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="Exec">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template name="TestCase_7x">
<xsl:param name="violations" />

<xsl:template match="Goals">
<properties>
<xsl:apply-templates select="Goal"/>
</properties>
<xsl:for-each select="$violations/ExecViol">
<xsl:variable name="suiteName" select="substring-before(@testName, '::')" />
<xsl:variable name="testName" select="substring-after(@testName, '::')" />

<testcase classname="{$suiteName}" name="{$testName}" time="{xunit:junit-time(0)}">
<xsl:apply-templates select="Thr" />
</testcase>
</xsl:for-each>
</xsl:template>

<xsl:template match="Goal">
<property name="{@name}" value="{@type}"/>
<xsl:template match="TestSuite">
<xsl:param name="suiteName" as="xs:string?" />

<xsl:variable name="suiteName" select="concat($suiteName, '.', @name)" />
<xsl:choose>
<xsl:when test="count(TestSuite) > 0">
<xsl:apply-templates select="TestSuite">
<xsl:with-param name="suiteName" select="$suiteName" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<testsuite name="{$suiteName}"
time="{xunit:junit-time(0)}"
tests="{@pass + @fail}"
failures="{@fail}">
<xsl:apply-templates select="Test">
<xsl:with-param name="fullSuiteName" select="$suiteName" />
<xsl:with-param name="suiteName" select="@name" />
</xsl:apply-templates>
</testsuite>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="ExecViols">
<xsl:apply-templates select="ExecViol"/>
<xsl:template match="Test">
<xsl:param name="suiteName" as="xs:string?" />
<xsl:param name="fullSuiteName" as="xs:string?" />

<xsl:variable name="testName" select="if (starts-with(@name, $suiteName)) then substring(@name, string-length($suiteName) + 3) else @name" />
<xsl:variable name="testId" select="@id" />
<testcase name="{$testName}"
classname="{xunit:if-empty($fullSuiteName, $suiteName)}"
time="{xunit:junit-time(xunit:millis-from-time(@time))}">
<xsl:if test="@pass != 1">
<xsl:apply-templates select="//Exec/ExecViols/ExecViol[@testId = $testId]" />
</xsl:if>
</testcase>
</xsl:template>

<xsl:template match="ExecViol">
<xsl:if test="@cat!=6">
<testcase classname="{@locFile}" name="{@testName}" time="0">
<xsl:apply-templates select="Thr"/>
</testcase>
</xsl:if>
<xsl:apply-templates select="Thr"/>
</xsl:template>

<xsl:template match="Thr">
<xsl:apply-templates select="ThrPart"/>
</xsl:template>

<xsl:template match="ThrPart">
<failure type="{@clName}" message="{@detMsg}"/>
<system-err>
<xsl:text>Trace </xsl:text>
<failure type="{@clName}" message="{@detMsg}">
<xsl:apply-templates select="Trace"/>
</system-err>
</failure>
</xsl:template>

<xsl:template match="Trace">
<xsl:text>Line :</xsl:text>
<xsl:value-of select="@ln"/>
<xsl:text> File :</xsl:text>
<xsl:value-of select="@fileName"/>
</xsl:template>

<xsl:template match="ExecutedTestsDetails">
<xsl:apply-templates select="Total"/>
</xsl:template>

<xsl:template match="Total">
<xsl:apply-templates select="Project"/>
</xsl:template>

<xsl:template match="Project">
<xsl:apply-templates select="TestSuite"/>
</xsl:template>

<xsl:template match="TestSuite">
<xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="Test">
<xsl:variable name="fullTestName"><xsl:value-of select="/ResultsSession/Exec/Summary/Projects/Project/@name"/>JUnitTestSuite
</xsl:variable>
<xsl:if test="@pass=1">
<testcase name="{@name}" classname="{$fullTestName}" time="0"/>
</xsl:if>
at <xsl:value-of select="@fileName"/>:<xsl:value-of select="@ln"/>
</xsl:template>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,8 @@ THE SOFTWARE.
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="timestamp" type="xs:string" use="optional"/>
<xs:attribute name="classname" type="xs:string" use="optional"/>
<xs:attribute name="status" type="xs:string" use="optional"/>
<xs:attribute name="class" type="xs:string" use="optional"/>
<xs:attribute name="file" type="xs:string" use="optional"/>
<xs:attribute name="log" type="xs:string" use="optional"/>
<xs:attribute name="group" type="xs:string" use="optional"/>
<xs:attribute name="url" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>

Expand All @@ -129,15 +123,12 @@ THE SOFTWARE.
<xs:attribute name="failures" type="xs:string" use="optional"/>
<xs:attribute name="errors" type="xs:string" use="optional"/>
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="disabled" type="xs:string" use="optional"/>
<xs:attribute name="skipped" type="xs:string" use="optional"/>
<xs:attribute name="skips" type="xs:string" use="optional"/>
<xs:attribute name="timestamp" type="xs:string" use="optional"/>
<xs:attribute name="hostname" type="xs:string" use="optional"/>
<xs:attribute name="id" type="xs:string" use="optional"/>
<xs:attribute name="package" type="xs:string" use="optional"/>
<xs:attribute name="file" type="xs:string" use="optional"/>
<xs:attribute name="skip" type="xs:string" use="optional"/>
<xs:attribute name="log" type="xs:string" use="optional"/>
<xs:attribute name="url" type="xs:string" use="optional"/>
</xs:complexType>
Expand All @@ -152,7 +143,6 @@ THE SOFTWARE.
<xs:attribute name="time" type="xs:string" use="optional"/>
<xs:attribute name="tests" type="xs:string" use="optional"/>
<xs:attribute name="failures" type="xs:string" use="optional"/>
<xs:attribute name="disabled" type="xs:string" use="optional"/>
<xs:attribute name="errors" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected void convertAndValidate(Class<? extends InputMetric> metricClass, Stri

inputMetric.convert(inputXMLFile, outputXMLFile);
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(readXmlAsString(outputXMLFile), readXmlAsString(new File(this.getClass().getResource(expectedResultPath).toURI())));
Diff myDiff = new Diff(readXmlAsString(new File(this.getClass().getResource(expectedResultPath).toURI())), readXmlAsString(outputXMLFile));
Assert.assertTrue("XSL transformation did not work " + myDiff, myDiff.similar());

//The generated output file must be valid
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/org/jenkinsci/plugins/xunit/types/CppTestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017, Gregory Boissinot, Falco Nikolas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.xunit.types;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class CppTestTest extends AbstractTest {

@Parameters(name = "testcase{1}: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { "testcase1", 1 }, //
{ "mix execution of pass and fail tests", 2 }, //
{ "testcase3", 3 } //
});
}

public CppTestTest(String testName, int testNumber) {
super(CppTest.class, resolveInput("cpptest", testNumber), resolveOutput("cpptest", testNumber));
}

@Override
@Test
public void verifyXSLT() throws Exception {
super.verifyXSLT();
}

}
Loading

0 comments on commit cd5367f

Please sign in to comment.