comparison tests/test-parseindex2.py @ 39045:88b04bd2cbb4

tests: port remaining bits of test-parseindex2 to unittest asserts Differential Revision: https://phab.mercurial-scm.org/D4181
author Augie Fackler <augie@google.com>
date Thu, 09 Aug 2018 13:26:28 -0400
parents ee0720e82257
children a450d460774e
comparison
equal deleted inserted replaced
39044:83cee1af747f 39045:88b04bd2cbb4
128 # (from http://docs.python.org/2/library/functions.html?#reload). 128 # (from http://docs.python.org/2/library/functions.html?#reload).
129 p = subprocess.Popen(cmd, shell=True, 129 p = subprocess.Popen(cmd, shell=True,
130 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 130 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
131 return p.communicate() # returns stdout, stderr 131 return p.communicate() # returns stdout, stderr
132 132
133 def printhexfail(testnumber, hexversion, stdout, expected): 133 def hexfailmsg(testnumber, hexversion, stdout, expected):
134 try: 134 try:
135 hexstring = hex(hexversion) 135 hexstring = hex(hexversion)
136 except TypeError: 136 except TypeError:
137 hexstring = None 137 hexstring = None
138 print("FAILED: version test #%s with Python %s and patched " 138 return ("FAILED: version test #%s with Python %s and patched "
139 "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" % 139 "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" %
140 (testnumber, sys.version_info, hexversion, hexstring, expected, 140 (testnumber, sys.version_info, hexversion, hexstring, expected,
141 stdout)) 141 stdout))
142
143 def testversionokay(testnumber, hexversion):
144 stdout, stderr = importparsers(hexversion)
145 if stdout:
146 printhexfail(testnumber, hexversion, stdout, expected="no stdout")
147
148 def testversionfail(testnumber, hexversion):
149 stdout, stderr = importparsers(hexversion)
150 # We include versionerrortext to distinguish from other ImportErrors.
151 errtext = b"ImportError: %s" % pycompat.sysbytes(parsers.versionerrortext)
152 if errtext not in stdout:
153 printhexfail(testnumber, hexversion, stdout,
154 expected="stdout to contain %r" % errtext)
155 142
156 def makehex(major, minor, micro): 143 def makehex(major, minor, micro):
157 return int("%x%02x%02x00" % (major, minor, micro), 16) 144 return int("%x%02x%02x00" % (major, minor, micro), 16)
158 145
159 class parseindex2tests(unittest.TestCase): 146 class parseindex2tests(unittest.TestCase):
147
148 def assertversionokay(self, testnumber, hexversion):
149 stdout, stderr = importparsers(hexversion)
150 self.assertFalse(
151 stdout, hexfailmsg(testnumber, hexversion, stdout, 'no stdout'))
152
153 def assertversionfail(self, testnumber, hexversion):
154 stdout, stderr = importparsers(hexversion)
155 # We include versionerrortext to distinguish from other ImportErrors.
156 errtext = b"ImportError: %s" % pycompat.sysbytes(
157 parsers.versionerrortext)
158 self.assertIn(errtext, stdout,
159 hexfailmsg(testnumber, hexversion, stdout,
160 expected="stdout to contain %r" % errtext))
161
160 def testversiondetection(self): 162 def testversiondetection(self):
161 """Check the version-detection logic when importing parsers.""" 163 """Check the version-detection logic when importing parsers."""
162 # Only test the version-detection logic if it is present. 164 # Only test the version-detection logic if it is present.
163 try: 165 try:
164 parsers.versionerrortext 166 parsers.versionerrortext
165 except AttributeError: 167 except AttributeError:
166 return 168 return
167 info = sys.version_info 169 info = sys.version_info
168 major, minor, micro = info[0], info[1], info[2] 170 major, minor, micro = info[0], info[1], info[2]
169 # Test same major-minor versions. 171 # Test same major-minor versions.
170 testversionokay(1, makehex(major, minor, micro)) 172 self.assertversionokay(1, makehex(major, minor, micro))
171 testversionokay(2, makehex(major, minor, micro + 1)) 173 self.assertversionokay(2, makehex(major, minor, micro + 1))
172 # Test different major-minor versions. 174 # Test different major-minor versions.
173 testversionfail(3, makehex(major + 1, minor, micro)) 175 self.assertversionfail(3, makehex(major + 1, minor, micro))
174 testversionfail(4, makehex(major, minor + 1, micro)) 176 self.assertversionfail(4, makehex(major, minor + 1, micro))
175 testversionfail(5, "'foo'") 177 self.assertversionfail(5, "'foo'")
176 178
177 def testbadargs(self): 179 def testbadargs(self):
178 # Check that parse_index2() raises TypeError on bad arguments. 180 # Check that parse_index2() raises TypeError on bad arguments.
179 with self.assertRaises(TypeError): 181 with self.assertRaises(TypeError):
180 parse_index2(0, True) 182 parse_index2(0, True)