Patchwork [4,of,4] test-doctest: normalize b'', u'' and exception name on Python 3

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 24, 2017, 3:20 p.m.
Message ID <098312faf83c46ea590c.1503588043@mimosa>
Download mbox | patch
Permalink /patch/23305/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 24, 2017, 3:20 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1503581608 -32400
#      Thu Aug 24 22:33:28 2017 +0900
# Node ID 098312faf83c46ea590c0c865874ccfc2f1b4df7
# Parent  a9b6c464c13b6a6d7dce4e8adf88d0e80dc96003
test-doctest: normalize b'', u'' and exception name on Python 3

Still most doctests fail because string literals in doctests aren't converted
to bytes. The idea is described in the following page.

https://dirkjan.ochtman.nl/writing/2014/07/06/single-source-python-23-doctests.html

# no-check-commit
Yuya Nishihara - Aug. 25, 2017, 2:15 p.m.
On Fri, 25 Aug 2017 00:20:43 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1503581608 -32400
> #      Thu Aug 24 22:33:28 2017 +0900
> # Node ID 098312faf83c46ea590c0c865874ccfc2f1b4df7
> # Parent  a9b6c464c13b6a6d7dce4e8adf88d0e80dc96003
> test-doctest: normalize b'', u'' and exception name on Python 3

> +class py3docchecker(doctest.OutputChecker):
> +    def check_output(self, want, got, optionflags):
> +        want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want)  # py2: u''
> +        got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got)  # py3: b''
> +        got2 = re.sub(r'^mercurial\.error\.', '', got2)  # exception name

Perhaps this should be flagged as re.MULTILINE. I'll resend this. The other
patches can be queued separately.

Patch

diff --git a/tests/test-doctest.py b/tests/test-doctest.py
--- a/tests/test-doctest.py
+++ b/tests/test-doctest.py
@@ -4,6 +4,7 @@  from __future__ import absolute_import
 
 import doctest
 import os
+import re
 import sys
 
 ispy3 = (sys.version_info[0] >= 3)
@@ -11,6 +12,14 @@  ispy3 = (sys.version_info[0] >= 3)
 if 'TERM' in os.environ:
     del os.environ['TERM']
 
+class py3docchecker(doctest.OutputChecker):
+    def check_output(self, want, got, optionflags):
+        want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want)  # py2: u''
+        got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got)  # py3: b''
+        got2 = re.sub(r'^mercurial\.error\.', '', got2)  # exception name
+        return any(doctest.OutputChecker.check_output(self, w, g, optionflags)
+                   for w, g in [(want, got), (want2, got2)])
+
 # TODO: migrate doctests to py3 and enable them on both versions
 def testmod(name, optionflags=0, testtarget=None, py2=True, py3=False):
     if not (not ispy3 and py2 or ispy3 and py3):
@@ -19,7 +28,16 @@  def testmod(name, optionflags=0, testtar
     mod = sys.modules[name]
     if testtarget is not None:
         mod = getattr(mod, testtarget)
-    doctest.testmod(mod, optionflags=optionflags)
+
+    # minimal copy of doctest.testmod()
+    finder = doctest.DocTestFinder()
+    checker = None
+    if ispy3:
+        checker = py3docchecker()
+    runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags)
+    for test in finder.find(mod, name):
+        runner.run(test)
+    runner.summarize()
 
 testmod('mercurial.changegroup')
 testmod('mercurial.changelog')
@@ -42,7 +60,7 @@  testmod('mercurial.pycompat', py3=True)
 testmod('mercurial.revsetlang')
 testmod('mercurial.smartset')
 testmod('mercurial.store')
-testmod('mercurial.subrepo')
+testmod('mercurial.subrepo', py3=True)
 testmod('mercurial.templatefilters')
 testmod('mercurial.templater')
 testmod('mercurial.ui')