Add script to convert buck test report into junit report

Buck test report format is not supported by CI tools like Jenkins. This
conversion will allow to use buck built plugins in CI infrastructure
without having to wait until CI tools support buck.

Change-Id: Ia9eecb585ce02556c86aed7733d86b457f2e4d18
Signed-off-by: Hugo Arès <hugo.ares@ericsson.com>
Signed-off-by: Hector Oswaldo Caballero <hector.caballero@ericsson.com>
diff --git a/tools/buckToJUnit.xsl b/tools/buckToJUnit.xsl
new file mode 100644
index 0000000..424c050
--- /dev/null
+++ b/tools/buckToJUnit.xsl
@@ -0,0 +1,60 @@
+<?xml version="1.0"?>
+<!--
+Copyright (C) 2015 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:func="com.google.gerrit" xmlns:xs="http://www.w3.org/2001/XMLSchema"
+    exclude-result-prefixes="func" version="2.0">
+  <xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8"/>
+  <xsl:strip-space elements="*"/>
+
+  <xsl:template match="/tests">
+    <xsl:apply-templates/>
+  </xsl:template>
+
+  <xsl:template match="test">
+    <xsl:variable name="filename" select="concat('TEST-', @name, '.xml')"/>
+    <xsl:result-document href="{$filename}" method="xml">
+      <xsl:variable name="testCount" select="count(testresult)"/>
+      <xsl:variable name="nonEmptyStacks" select="count(testresult[stacktrace != ''])"/>
+      <xsl:variable name="failures"
+          select="count(testresult[contains(stacktrace, 'java.lang.AssertionError')])"/>
+      <xsl:variable name="errors" select="$nonEmptyStacks - $failures"/>
+      <testsuite failures="{$failures}" time="{func:toMS(@time)}" errors="{$errors}" skipped="0"
+          tests="{$testCount}" name="{@name}">
+        <xsl:apply-templates/>
+      </testsuite>
+    </xsl:result-document>
+  </xsl:template>
+
+  <xsl:template match="testresult">
+    <testcase time="{func:toMS(@time)}" classname="{../@name}" name="{@name}">
+      <xsl:apply-templates/>
+    </testcase>
+  </xsl:template>
+
+  <xsl:template match="message"/>
+
+  <xsl:template match="stacktrace[. != '']">
+    <failure message="{../message}" type="{substring-before(., ':')}">
+      <xsl:value-of select="."/>
+    </failure>
+  </xsl:template>
+
+  <xsl:function name="func:toMS">
+    <xsl:param name="sec" as="xs:decimal"/>
+    <xsl:value-of select="$sec div 1000"/>
+  </xsl:function>
+</xsl:stylesheet>
diff --git a/tools/buck_to_junit.py b/tools/buck_to_junit.py
new file mode 100755
index 0000000..bbf3e67
--- /dev/null
+++ b/tools/buck_to_junit.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+# Copyright (C) 2015 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from optparse import OptionParser
+from os import path, chdir
+from os.path import abspath
+from shutil import rmtree
+from subprocess import check_call, CalledProcessError
+from tempfile import mkdtemp
+
+opts = OptionParser()
+opts.add_option('-t', help='test report to convert')
+opts.add_option('-o', help='output directory')
+args, _ = opts.parse_args()
+temp_dir = mkdtemp()
+try:
+  try:
+    check_call(
+      ['curl', '--proxy-anyauth', '-sfo', path.join(temp_dir, 'saxon.jar'),
+       'http://central.maven.org/maven2/net/sf/saxon/Saxon-HE/9.6.0-6/Saxon-HE-9.6.0-6.jar'])
+  except OSError as err:
+    print('could not invoke curl: %s\nis curl installed?' % err)
+    exit(1)
+  except CalledProcessError as err:
+    print('error using curl: %s' % err)
+    exit(1)
+
+  buck_report = abspath(args.t)
+  buck_to_junit_xsl = abspath(
+    path.join(path.abspath(path.dirname(__file__)), 'buckToJUnit.xsl'))
+
+  chdir(args.o)
+  try:
+    check_call(
+      ['java', '-jar', path.join(temp_dir, 'saxon.jar'), '-s:' + buck_report,
+       '-xsl:' + buck_to_junit_xsl])
+  except CalledProcessError as err:
+    print('error converting to junit: %s' % err)
+    exit(1)
+finally:
+  rmtree(temp_dir, ignore_errors=True)