chdtest.py: Python 3 compat. (nw)

This commit is contained in:
Mike Naberezny 2015-03-06 13:08:58 -08:00
parent be5f14c495
commit 196e21d88d

View File

@ -7,6 +7,10 @@ import shutil
def runProcess(cmd): def runProcess(cmd):
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = process.communicate() (stdout, stderr) = process.communicate()
if not isinstance(stdout, str): # python 3
stdout = stdout.decode('latin-1')
if not isinstance(stderr, str): # python 3
stderr = stderr.decode('latin-1')
return process.returncode, stdout, stderr return process.returncode, stdout, stderr
def compareInfo(info1, info2): def compareInfo(info1, info2):
@ -23,7 +27,7 @@ def compareInfo(info1, info2):
continue continue
if not lines1[i] == lines2[i]: if not lines1[i] == lines2[i]:
mismatch = True mismatch = True
print lines1[i] + " - " + lines2[i] print(lines1[i] + " - " + lines2[i])
return mismatch == False return mismatch == False
@ -54,7 +58,7 @@ def extractcdAndCompare(type):
exitcode, stdout, stderr = runProcess([chdmanBin, "extractcd", "-f", "-i", outFile, "-o", extractFile]) exitcode, stdout, stderr = runProcess([chdmanBin, "extractcd", "-f", "-i", outFile, "-o", extractFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - extractcd (" + type + ") failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - extractcd (" + type + ") failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
sha1_extract = sha1sum(extractFile) sha1_extract = sha1sum(extractFile)
@ -69,20 +73,20 @@ def extractcdAndCompare(type):
exitcode, stdout, stderr = runProcess([chdmanBin, "extractcd", "-f", "-i", tempFile, "-o", extractFile]) exitcode, stdout, stderr = runProcess([chdmanBin, "extractcd", "-f", "-i", tempFile, "-o", extractFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - extractcd (" + type + ") failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - extractcd (" + type + ") failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
sha1_extract_2 = sha1sum(extractFile) sha1_extract_2 = sha1sum(extractFile)
sha1_extract_bin_2 = sha1sum(extractFileBin) sha1_extract_bin_2 = sha1sum(extractFileBin)
if not sha1_extract == sha1_extract_2: if not sha1_extract == sha1_extract_2:
print "expected: " + sha1_extract + " found: " + sha1_extract_2 print("expected: " + sha1_extract + " found: " + sha1_extract_2)
print d + " - SHA1 mismatch (extractcd - " + type + " - toc)" print(d + " - SHA1 mismatch (extractcd - " + type + " - toc)")
failure = True failure = True
if not sha1_extract_bin == sha1_extract_bin_2: if not sha1_extract_bin == sha1_extract_bin_2:
print "expected: " + sha1_extract_bin + " found: " + sha1_extract_bin_2 print("expected: " + sha1_extract_bin + " found: " + sha1_extract_bin_2)
print d + " - SHA1 mismatch (extractcd - " + type + " - bin)" print(d + " - SHA1 mismatch (extractcd - " + type + " - bin)")
failure = True failure = True
def extractAndCompare(command, ext): def extractAndCompare(command, ext):
@ -95,7 +99,7 @@ def extractAndCompare(command, ext):
exitcode, stdout, stderr = runProcess([chdmanBin, command, "-f", "-i", outFile, "-o", extractFile]) exitcode, stdout, stderr = runProcess([chdmanBin, command, "-f", "-i", outFile, "-o", extractFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - " + command + " (" + ext + ") failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - " + command + " (" + ext + ") failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
sha1_extract = sha1sum(extractFile) sha1_extract = sha1sum(extractFile)
@ -108,14 +112,14 @@ def extractAndCompare(command, ext):
exitcode, stdout, stderr = runProcess([chdmanBin, command, "-f", "-i", tempFile, "-o", extractFile]) exitcode, stdout, stderr = runProcess([chdmanBin, command, "-f", "-i", tempFile, "-o", extractFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - " + command + " (" + ext + ") failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - " + command + " (" + ext + ") failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
sha1_extract_2 = sha1sum(extractFile) sha1_extract_2 = sha1sum(extractFile)
if not sha1_extract == sha1_extract_2: if not sha1_extract == sha1_extract_2:
print "expected: " + sha1_extract + " found: " + sha1_extract_2 print("expected: " + sha1_extract + " found: " + sha1_extract_2)
print d + " - SHA1 mismatch (" + command + " - " + ext + ")" print(d + " - SHA1 mismatch (" + command + " - " + ext + ")")
failure = True failure = True
currentDirectory = os.path.dirname(os.path.realpath(__file__)) currentDirectory = os.path.dirname(os.path.realpath(__file__))
@ -176,7 +180,7 @@ for root, dirs, files in os.walk(inputPath):
elif command == "copy": elif command == "copy":
inFile += ".chd" inFile += ".chd"
else: else:
print "unsupported mode '%s'" % command print("unsupported mode '%s'" % command)
continue continue
if os.path.exists(inFile): if os.path.exists(inFile):
cmd = [chdmanBin, command, "-f", "-i", inFile, "-o", tempFile] + params cmd = [chdmanBin, command, "-f", "-i", inFile, "-o", tempFile] + params
@ -185,27 +189,27 @@ for root, dirs, files in os.walk(inputPath):
exitcode, stdout, stderr = runProcess(cmd) exitcode, stdout, stderr = runProcess(cmd)
if not exitcode == 0: if not exitcode == 0:
print d + " - command failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - command failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
# verify # verify
exitcode, stdout, stderr = runProcess([chdmanBin, "verify", "-i", tempFile]) exitcode, stdout, stderr = runProcess([chdmanBin, "verify", "-i", tempFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - verify failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - verify failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
# compare info # compare info
# TODO: store expected output of reference file as well and compare # TODO: store expected output of reference file as well and compare
exitcode, info1, stderr = runProcess([chdmanBin, "info", "-v", "-i", tempFile]) exitcode, info1, stderr = runProcess([chdmanBin, "info", "-v", "-i", tempFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - info (temp) failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - info (temp) failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
exitcode, info2, stderr = runProcess([chdmanBin, "info", "-v", "-i", outFile]) exitcode, info2, stderr = runProcess([chdmanBin, "info", "-v", "-i", outFile])
if not exitcode == 0: if not exitcode == 0:
print d + " - info (output) failed with " + str(exitcode) + " (" + stderr + ")" print(d + " - info (output) failed with " + str(exitcode) + " (" + stderr + ")")
failure = True failure = True
if not compareInfo(info1, info2): if not compareInfo(info1, info2):
print d + " - info output differs" print(d + " - info output differs")
failure = True failure = True
# extract and compare # extract and compare
@ -222,9 +226,9 @@ for root, dirs, files in os.walk(inputPath):
sha1_out = sha1sum(outFile) sha1_out = sha1sum(outFile)
sha1_temp = sha1sum(tempFile) sha1_temp = sha1sum(tempFile)
if not sha1_out == sha1_temp: if not sha1_out == sha1_temp:
print "expected: " + sha1_out + " found: " + sha1_temp print("expected: " + sha1_out + " found: " + sha1_temp)
print d + " - SHA1 mismatch (output file)" print(d + " - SHA1 mismatch (output file)")
failure = True failure = True
if not failure: if not failure:
print "All tests finished successfully" print("All tests finished successfully")