Details
Details
- Reviewers
pulkit - Group Reviewers
hg-reviewers - Commits
- rHG88b04bd2cbb4: tests: port remaining bits of test-parseindex2 to unittest asserts
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
( )
pulkit |
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
# version-checking code happens inside the module init function, and | # version-checking code happens inside the module init function, and | ||||
# when using reload() to reimport an extension module, "The init function | # when using reload() to reimport an extension module, "The init function | ||||
# of extension modules is not called a second time" | # of extension modules is not called a second time" | ||||
# (from http://docs.python.org/2/library/functions.html?#reload). | # (from http://docs.python.org/2/library/functions.html?#reload). | ||||
p = subprocess.Popen(cmd, shell=True, | p = subprocess.Popen(cmd, shell=True, | ||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||||
return p.communicate() # returns stdout, stderr | return p.communicate() # returns stdout, stderr | ||||
def printhexfail(testnumber, hexversion, stdout, expected): | def hexfailmsg(testnumber, hexversion, stdout, expected): | ||||
try: | try: | ||||
hexstring = hex(hexversion) | hexstring = hex(hexversion) | ||||
except TypeError: | except TypeError: | ||||
hexstring = None | hexstring = None | ||||
print("FAILED: version test #%s with Python %s and patched " | return ("FAILED: version test #%s with Python %s and patched " | ||||
"sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" % | "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" % | ||||
(testnumber, sys.version_info, hexversion, hexstring, expected, | (testnumber, sys.version_info, hexversion, hexstring, expected, | ||||
stdout)) | stdout)) | ||||
def testversionokay(testnumber, hexversion): | def makehex(major, minor, micro): | ||||
return int("%x%02x%02x00" % (major, minor, micro), 16) | |||||
class parseindex2tests(unittest.TestCase): | |||||
def assertversionokay(self, testnumber, hexversion): | |||||
stdout, stderr = importparsers(hexversion) | stdout, stderr = importparsers(hexversion) | ||||
if stdout: | self.assertFalse( | ||||
printhexfail(testnumber, hexversion, stdout, expected="no stdout") | stdout, hexfailmsg(testnumber, hexversion, stdout, 'no stdout')) | ||||
def testversionfail(testnumber, hexversion): | def assertversionfail(self, testnumber, hexversion): | ||||
stdout, stderr = importparsers(hexversion) | stdout, stderr = importparsers(hexversion) | ||||
# We include versionerrortext to distinguish from other ImportErrors. | # We include versionerrortext to distinguish from other ImportErrors. | ||||
errtext = b"ImportError: %s" % pycompat.sysbytes(parsers.versionerrortext) | errtext = b"ImportError: %s" % pycompat.sysbytes( | ||||
if errtext not in stdout: | parsers.versionerrortext) | ||||
printhexfail(testnumber, hexversion, stdout, | self.assertIn(errtext, stdout, | ||||
expected="stdout to contain %r" % errtext) | hexfailmsg(testnumber, hexversion, stdout, | ||||
expected="stdout to contain %r" % errtext)) | |||||
def makehex(major, minor, micro): | |||||
return int("%x%02x%02x00" % (major, minor, micro), 16) | |||||
class parseindex2tests(unittest.TestCase): | |||||
def testversiondetection(self): | def testversiondetection(self): | ||||
"""Check the version-detection logic when importing parsers.""" | """Check the version-detection logic when importing parsers.""" | ||||
# Only test the version-detection logic if it is present. | # Only test the version-detection logic if it is present. | ||||
try: | try: | ||||
parsers.versionerrortext | parsers.versionerrortext | ||||
except AttributeError: | except AttributeError: | ||||
return | return | ||||
info = sys.version_info | info = sys.version_info | ||||
major, minor, micro = info[0], info[1], info[2] | major, minor, micro = info[0], info[1], info[2] | ||||
# Test same major-minor versions. | # Test same major-minor versions. | ||||
testversionokay(1, makehex(major, minor, micro)) | self.assertversionokay(1, makehex(major, minor, micro)) | ||||
testversionokay(2, makehex(major, minor, micro + 1)) | self.assertversionokay(2, makehex(major, minor, micro + 1)) | ||||
# Test different major-minor versions. | # Test different major-minor versions. | ||||
testversionfail(3, makehex(major + 1, minor, micro)) | self.assertversionfail(3, makehex(major + 1, minor, micro)) | ||||
testversionfail(4, makehex(major, minor + 1, micro)) | self.assertversionfail(4, makehex(major, minor + 1, micro)) | ||||
testversionfail(5, "'foo'") | self.assertversionfail(5, "'foo'") | ||||
def testbadargs(self): | def testbadargs(self): | ||||
# Check that parse_index2() raises TypeError on bad arguments. | # Check that parse_index2() raises TypeError on bad arguments. | ||||
with self.assertRaises(TypeError): | with self.assertRaises(TypeError): | ||||
parse_index2(0, True) | parse_index2(0, True) | ||||
def testparseindexfile(self): | def testparseindexfile(self): | ||||
# Check parsers.parse_index2() on an index file against the | # Check parsers.parse_index2() on an index file against the |