#2917 better error behavior if file does not exist

This commit is contained in:
Ken Conley 2011-02-14 06:14:35 +00:00
parent 1bddcb5d12
commit c6e970bc95
1 changed files with 11 additions and 5 deletions

View File

@ -275,14 +275,18 @@ def _read_file_safe_xml(test_file):
"""
read in file, screen out unsafe unicode characters
"""
f = None
try:
# this is ugly, but the files in question that are problematic
# do not declare unicode type.
if not os.path.isfile(test_file):
raise Exception("test file does not exist")
try:
f = codecs.open(test_file, "r", "utf-8" )
x = f.read()
except:
f.close()
if f is not None:
f.close()
f = codecs.open(test_file, "r", "iso8859-1" )
x = f.read()
@ -290,7 +294,8 @@ def _read_file_safe_xml(test_file):
x = x[:match.start()] + "?" + x[match.end():]
return x.encode("utf-8")
finally:
f.close()
if f is not None:
f.close()
def read(test_file, test_name):
"""
@ -304,10 +309,11 @@ def read(test_file, test_name):
"""
try:
xml_str = _read_file_safe_xml(test_file)
if not xml_str.strip():
print "WARN: test result file is empty [%s]"%(test_file)
return Result(test_name, 0, 0, 0)
test_suite = parseString(xml_str).getElementsByTagName('testsuite')
except Exception, e:
import traceback
traceback.print_exc()
except Exception as e:
print "WARN: cannot read test result file [%s]: %s"%(test_file, str(e))
return Result(test_name, 0, 0, 0)
if not test_suite: