You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/01/09 17:42:38 UTC

[couchdb] 01/06: Add a JUnit report to JavaScript tests

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch feat/improve-javascript-test-reports
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit a1f44e3f878566209ff614d4149d56b1c9f775da
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Jan 9 11:12:03 2020 -0600

    Add a JUnit report to JavaScript tests
---
 .gitignore          |  1 +
 test/javascript/run | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore
index 9406d9b..5eec70f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -111,6 +111,7 @@ src/global_changes/ebin/
 src/mango/ebin/
 src/mango/test/*.pyc
 src/mango/venv/
+test/javascript/junit.xml
 
 /_build/
 /src/bunt
diff --git a/test/javascript/run b/test/javascript/run
index a465a7b..761fa45 100755
--- a/test/javascript/run
+++ b/test/javascript/run
@@ -17,7 +17,9 @@ import optparse as op
 import os
 import subprocess as sp
 import sys
+import time
 import re
+import xml.dom.minidom as md
 
 
 USAGE = "%prog [options] [command to run...]"
@@ -83,16 +85,66 @@ def run_couchjs(test, fmt):
         + SCRIPTS
         + [test, RUNNER]
     )
-    p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sys.stderr)
+    p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)
+    output = []
     while True:
         line = p.stdout.readline()
         if not line:
             break
         line = line.decode()
+        output.append(line)
         sys.stderr.write(line)
     p.wait()
     fmt(p.returncode)
-    return p.returncode
+    return (p.returncode, "".join(output))
+
+
+def write_junit(filename, total_time, results):
+    failures = 0
+    skipped = 0
+    for (_, rc, _, _) in results:
+        if rc == 2 or rc == 3:
+            skipped += 1
+        else:
+            failures += 1
+
+    doc = md.Document()
+    root = doc.createElement("testsuite")
+    root.setAttribute("name", "JavaScript tests")
+    root.setAttribute("time", "%0.3f" % total_time)
+    root.setAttribute("tests", str(len(results)))
+    root.setAttribute("failures", str(failures))
+    root.setAttribute("errors", "0")
+    root.setAttribute("skipped", str(skipped))
+    doc.appendChild(root)
+
+    for (path, rc, output, test_time) in results:
+        sys.stderr.write("WHUT? %r\n" % output)
+        name = os.path.split(path)[-1]
+        tc = doc.createElement("testcase")
+        tc.setAttribute("name", name)
+        tc.setAttribute("time", "%0.3f" % test_time)
+        if rc == 0:
+            pass
+        elif rc == 2:
+            skipped = doc.createElement("skipped")
+            skipped.setAttribute("message", "disabled")
+            tc.appendChild(skipped)
+        elif rc == 3:
+            skipped = doc.createElement("skipped")
+            skipped.setAttribute("message", "ported to elixir")
+            tc.appendChild(skipped)
+        else:
+            failure = doc.createElement("failure")
+            failure.setAttribute("message", "failed: %d" % rc)
+            failure_text = "Exit Code: %d" % rc + "\n\n" + output
+            message = doc.createTextNode(failure_text)
+            failure.appendChild(message)
+            tc.appendChild(failure)
+        root.appendChild(tc)
+
+    with open(filename, "w") as handle:
+        doc.writexml(handle, addindent="  ", newl=os.linesep)
 
 
 def options():
@@ -102,7 +154,7 @@ def options():
             "--start",
             metavar="FILENAME",
             default=None,
-            help="Start from the given filename if multiple files " "are passed",
+            help="Start from the given filename if multiple files are passed",
         ),
         op.make_option(
             "-a",
@@ -139,6 +191,14 @@ def options():
             dest="test_path",
             help="Path where the tests are located",
         ),
+        op.make_option(
+            "-j",
+            "--junit-report",
+            type="string",
+            default="test/javascript/junit.xml",
+            dest="junit_report",
+            help="Write a JUnit compatible test report",
+        ),
     ]
 
 
@@ -162,12 +222,16 @@ def main():
                 tmp.append(name)
         tests = tmp
 
+    results = []
+    begin = time.time()
     passed = 0
     failed = 0
     if len(tests) > 0:
         fmt = mkformatter(tests)
         for test in tests:
-            result = run_couchjs(test, fmt)
+            tbefore = time.time()
+            (result, output) = run_couchjs(test, fmt)
+            results.append((test, result, output, time.time() - tbefore))
             if result == 0 or result == 2 or result == 3:
                 passed += 1
             else:
@@ -175,6 +239,10 @@ def main():
                 if not opts.all:
                     break
 
+    total_time = time.time() - begin
+    if opts.junit_report:
+        write_junit(opts.junit_report, total_time, results)
+
     sys.stderr.write(
         "=======================================================" + os.linesep
     )