[PATCH 1 of 3] debuginstall: convert unified test to pure unit test (issue4128)

Chris Jerdonek chris.jerdonek at gmail.com
Fri Dec 27 19:20:00 CST 2013


# HG changeset patch
# User Chris Jerdonek <chris.jerdonek at gmail.com>
# Date 1388190507 28800
#      Fri Dec 27 16:28:27 2013 -0800
# Node ID 3a91ff8b2a83bcfbad000f0140c9df67b0022522
# Parent  4274eda143cb1025be1130ffdaaf62370a2a6961
debuginstall: convert unified test to pure unit test (issue4128)

This change converts the test file for debuginstall from a unified test to
a pure unit test.  This gives us more flexibility in testing the output
of "hg debuginstall".  In particular, this will let us test in a
straightforward way the changes to debuginstall for issue4128.  For example,
the change lets us use Python to calculate the expected value of substrings
of the output, and then compare them.

diff --git a/tests/test-install.t b/tests/test-debuginstall.py
rename from tests/test-install.t
rename to tests/test-debuginstall.py
--- a/tests/test-install.t
+++ b/tests/test-debuginstall.py
@@ -1,22 +1,80 @@
-hg debuginstall
-  $ hg debuginstall
-  checking encoding (ascii)...
-  checking Python lib (*lib*)... (glob)
-  checking installed modules (*mercurial)... (glob)
-  checking templates (*mercurial?templates)... (glob)
-  checking commit editor...
-  checking username...
-  no problems detected
+import collections
+import fnmatch
+import os
+import subprocess
+import textwrap
+import unittest
 
-hg debuginstall with no username
-  $ HGUSER= hg debuginstall
-  checking encoding (ascii)...
-  checking Python lib (*lib*)... (glob)
-  checking installed modules (*mercurial)... (glob)
-  checking templates (*mercurial?templates)... (glob)
-  checking commit editor...
-  checking username...
-   no username supplied (see "hg help config")
-   (specify a username in your configuration file)
-  1 problems detected, please check your install!
-  [1]
+import silenttestrunner
+
+def calldebuginstall(env=None):
+    if env is not None:
+        new_env = os.environ.copy()
+        new_env.update(env)
+        env = new_env
+    cmd = "hg debuginstall"
+    p = subprocess.Popen(cmd, shell=True, env=env,
+                         stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    stdout, stderr = p.communicate()
+    return stdout, stderr, p.returncode
+
+def tolines(s):
+    return textwrap.dedent(s).splitlines()
+
+class testdebuginstall(unittest.TestCase):
+
+    def checklines(self, actual, expected, wildcard=False):
+        if wildcard:
+            match = fnmatch.fnmatchcase
+        else:
+            match = lambda s, t: s == t
+        for eline in expected:
+            aline = actual.popleft()
+            if not match(aline, eline):
+                msg = ("line mismatch (wildcard=%s):\n"
+                       "  actual:   %r\n"
+                       "  expected: %r\n"
+                       "remaining lines-->\n%s"%
+                       (wildcard, aline, eline, "\n".join(actual)))
+                raise AssertionError(msg)
+
+    def test_basic(self):
+        stdout, stderr, returncode = calldebuginstall()
+        lines = stdout.splitlines()
+        self.assertEqual(0, returncode)
+
+        # We use a deque so we can call popleft() and compare lines without
+        # keeping track of a list index.
+        lines = collections.deque(lines)
+        self.checklines(lines, ["checking encoding (ascii)..."])
+        expected = tolines("""\
+        checking Python lib (*lib*)...
+        checking installed modules (*mercurial)...
+        checking templates (*mercurial?templates)...
+        """)
+        self.checklines(lines, expected, wildcard=True)
+        expected = tolines("""\
+        checking commit editor...
+        checking username...
+        no problems detected
+        """)
+        self.checklines(lines, expected)
+        self.assertFalse(lines, msg=("there were extra lines-->\n%s" %
+                                     "\n".join(lines)))
+
+    def test_nousername(self):
+        stdout, err, returncode = calldebuginstall(env={'HGUSER': ''})
+        lines = stdout.splitlines()
+        self.assertEqual(1, returncode)
+
+        # Only check the username-specific output.
+        expected = tolines("""\
+        checking username...
+         no username supplied (see "hg help config")
+         (specify a username in your configuration file)
+        1 problems detected, please check your install!
+        """)
+        self.assertEqual(lines[-4:], expected)
+
+if __name__ == '__main__':
+    silenttestrunner.main(__name__)


More information about the Mercurial-devel mailing list