Patchwork [2,of,5] run-tests: factor out json write code into another method

login
register
mail settings
Submitter Siddharth Agarwal
Date June 6, 2017, 9:39 p.m.
Message ID <737bb0771ca0bff0673f.1496785165@devvm31800.prn1.facebook.com>
Download mbox | patch
Permalink /patch/21228/
State Accepted
Headers show

Comments

Siddharth Agarwal - June 6, 2017, 9:39 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1496779855 25200
#      Tue Jun 06 13:10:55 2017 -0700
# Node ID 737bb0771ca0bff0673f861c068018c8f606cf7d
# Parent  2d78617dd0b04d927beff7bc8bb2a1329b92ed8e
run-tests: factor out json write code into another method

We're going to use this code to output a JSON-formatted listing of tests.
Yuya Nishihara - June 7, 2017, 1:04 p.m.
On Tue, 6 Jun 2017 14:39:25 -0700, Siddharth Agarwal wrote:
> # HG changeset patch
> # User Siddharth Agarwal <sid0@fb.com>
> # Date 1496779855 25200
> #      Tue Jun 06 13:10:55 2017 -0700
> # Node ID 737bb0771ca0bff0673f861c068018c8f606cf7d
> # Parent  2d78617dd0b04d927beff7bc8bb2a1329b92ed8e
> run-tests: factor out json write code into another method

> @@ -2060,6 +2031,40 @@ class TextTestRunner(unittest.TextTestRu
>              s.appendChild(t)
>          outf.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
>  
> +    @staticmethod
> +    def _writejson(result, outf):
> +        timesd = {}
> +        for tdata in result.times:
> +            test = tdata[0]
> +            timesd[test] = tdata[1:]
> +
> +        outcome = {}
> +        groups = [('success', ((tc, None)
> +                   for tc in result.successes)),
> +                  ('failure', result.failures),
> +                  ('skip', result.skipped)]
> +        for res, testcases in groups:
> +            for tc, __ in testcases:
> +                if tc.name in timesd:
> +                    diff = result.faildata.get(tc.name, b'')
> +                    tres = {'result': res,
> +                            'time': ('%0.3f' % timesd[tc.name][2]),
> +                            'cuser': ('%0.3f' % timesd[tc.name][0]),
> +                            'csys': ('%0.3f' % timesd[tc.name][1]),
> +                            'start': ('%0.3f' % timesd[tc.name][3]),
> +                            'end': ('%0.3f' % timesd[tc.name][4]),
> +                            'diff': diff.decode('unicode_escape'),
> +                            }
> +                else:
> +                    # blacklisted test
> +                    tres = {'result': res}
> +
> +                outcome[tc.name] = tres
> +        jsonout = json.dumps(outcome, sort_keys=True, indent=4,
> +                             separators=(',', ': '))
> +        outf.writelines(("testreport =", jsonout))
> +
> +

Drooped double empty lines to silence test-check-commit.t.

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -1938,36 +1938,7 @@  class TextTestRunner(unittest.TextTestRu
             if self._runner.options.json:
                 jsonpath = os.path.join(self._runner._testdir, b'report.json')
                 with open(jsonpath, 'w') as fp:
-                    timesd = {}
-                    for tdata in result.times:
-                        test = tdata[0]
-                        timesd[test] = tdata[1:]
-
-                    outcome = {}
-                    groups = [('success', ((tc, None)
-                               for tc in result.successes)),
-                              ('failure', result.failures),
-                              ('skip', result.skipped)]
-                    for res, testcases in groups:
-                        for tc, __ in testcases:
-                            if tc.name in timesd:
-                                diff = result.faildata.get(tc.name, b'')
-                                tres = {'result': res,
-                                        'time': ('%0.3f' % timesd[tc.name][2]),
-                                        'cuser': ('%0.3f' % timesd[tc.name][0]),
-                                        'csys': ('%0.3f' % timesd[tc.name][1]),
-                                        'start': ('%0.3f' % timesd[tc.name][3]),
-                                        'end': ('%0.3f' % timesd[tc.name][4]),
-                                        'diff': diff.decode('unicode_escape'),
-                                        }
-                            else:
-                                # blacklisted test
-                                tres = {'result': res}
-
-                            outcome[tc.name] = tres
-                    jsonout = json.dumps(outcome, sort_keys=True, indent=4,
-                                         separators=(',', ': '))
-                    fp.writelines(("testreport =", jsonout))
+                    self._writejson(result, fp)
 
             self._runner._checkhglib('Tested')
 
@@ -2060,6 +2031,40 @@  class TextTestRunner(unittest.TextTestRu
             s.appendChild(t)
         outf.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
 
+    @staticmethod
+    def _writejson(result, outf):
+        timesd = {}
+        for tdata in result.times:
+            test = tdata[0]
+            timesd[test] = tdata[1:]
+
+        outcome = {}
+        groups = [('success', ((tc, None)
+                   for tc in result.successes)),
+                  ('failure', result.failures),
+                  ('skip', result.skipped)]
+        for res, testcases in groups:
+            for tc, __ in testcases:
+                if tc.name in timesd:
+                    diff = result.faildata.get(tc.name, b'')
+                    tres = {'result': res,
+                            'time': ('%0.3f' % timesd[tc.name][2]),
+                            'cuser': ('%0.3f' % timesd[tc.name][0]),
+                            'csys': ('%0.3f' % timesd[tc.name][1]),
+                            'start': ('%0.3f' % timesd[tc.name][3]),
+                            'end': ('%0.3f' % timesd[tc.name][4]),
+                            'diff': diff.decode('unicode_escape'),
+                            }
+                else:
+                    # blacklisted test
+                    tres = {'result': res}
+
+                outcome[tc.name] = tres
+        jsonout = json.dumps(outcome, sort_keys=True, indent=4,
+                             separators=(',', ': '))
+        outf.writelines(("testreport =", jsonout))
+
+
 class TestRunner(object):
     """Holds context for executing tests.