Skip to content

Commit

Permalink
rafactoring code from run_tests.py into Python module
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Aug 21, 2014
1 parent 2599eb9 commit c96214a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
52 changes: 4 additions & 48 deletions cmake/test/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
from __future__ import print_function

import argparse
import errno
import os
import sys
import subprocess
from xml.etree.ElementTree import ElementTree, ParseError

from catkin.test_results import read_junit
from catkin.tidy_xml import tidy_xml
from catkin.test_results import ensure_junit_result_exist, remove_junit_result


def main(argv=sys.argv[1:]):
Expand All @@ -21,13 +18,7 @@ def main(argv=sys.argv[1:]):
parser.add_argument('--return-code', action='store_true', default=False, help='Set the return code based on the success of the test command')
args = parser.parse_args(argv)

# if result file exists remove it before test execution
if os.path.exists(args.results):
os.remove(args.results)
# if placeholder (indicating previous failure) exists remove it before test execution
placeholder = os.path.join(os.path.dirname(args.results), 'MISSING-%s' % os.path.basename(args.results))
if os.path.exists(placeholder):
os.remove(placeholder)
remove_junit_result(args.results)

work_dir_msg = ' with working directory "%s"' % args.working_dir if args.working_dir is not None else ''
cmds_msg = ''.join(['\n %s' % cmd for cmd in args.command])
Expand All @@ -40,44 +31,9 @@ def main(argv=sys.argv[1:]):
break

print('-- run_tests.py: verify result "%s"' % args.results)

if os.path.exists(args.results):
# if result file exists ensure that it contains valid xml
tree = None
try:
tree = ElementTree(None, args.results)
except ParseError:
#print('Invalid XML in result file "%s"' % args.results)
tidy_xml(args.results)
try:
tree = ElementTree(None, args.results)
except ParseError as e:
print('Invalid XML in result file "%s" (even after trying to tidy it): %s ' % (args.results, str(e)), file=sys.stderr)
rc = 1
if tree:
_, num_errors, num_failures = read_junit(args.results)
if num_errors or num_failures:
rc = 1
else:
no_errors = ensure_junit_result_exist(args.results)
if not no_errors:
rc = 1
# if result file does not exist create placeholder which indicates failure
print('Cannot find results, writing failure results to "%s"' % placeholder, file=sys.stderr)
# create folder if necessary
if not os.path.exists(os.path.dirname(args.results)):
try:
os.makedirs(os.path.dirname(args.results))
except OSError as e:
# catch case where folder has been created in the mean time
if e.errno != errno.EEXIST:
raise
with open(placeholder, 'w') as f:
data = {'test': os.path.basename(args.results), 'test_file': args.results}
f.write('''<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="1" failures="1" time="1" errors="0" name="%(test)s">
<testcase name="test_ran" status="run" time="1" classname="Results">
<failure message="Unable to find test results for %(test)s, test did not run.\nExpected results in %(test_file)s" type=""/>
</testcase>
</testsuite>''' % data)

if args.return_code:
return rc
Expand Down
62 changes: 61 additions & 1 deletion python/catkin/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,68 @@
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function
import errno
import os
from xml.etree.ElementTree import ElementTree
import sys
from xml.etree.ElementTree import ElementTree, ParseError

from catkin.tidy_xml import tidy_xml


def remove_junit_result(filename):
# if result file exists remove it before test execution
if os.path.exists(filename):
os.remove(filename)
# if placeholder (indicating previous failure) exists remove it before test execution
missing_filename = _get_missing_junit_result_filename(filename)
if os.path.exists(missing_filename):
os.remove(missing_filename)


def ensure_junit_result_exist(filename):
if os.path.exists(filename):
# if result file exists ensure that it contains valid xml
tree = None
try:
tree = ElementTree(None, filename)
except ParseError:
# print('Invalid XML in result file "%s"' % filename)
tidy_xml(filename)
try:
tree = ElementTree(None, filename)
except ParseError as e:
print("Invalid XML in result file '%s' (even after trying to tidy it): %s " % (filename, str(e)), file=sys.stderr)
return False
if tree:
_, num_errors, num_failures = read_junit(filename)
if num_errors or num_failures:
return False
else:
# if result file does not exist create placeholder which indicates failure
missing_filename = _get_missing_junit_result_filename(filename)
print("Cannot find results, writing failure results to '%s'" % missing_filename, file=sys.stderr)
# create folder if necessary
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as e:
# catch case where folder has been created in the mean time
if e.errno != errno.EEXIST:
raise
with open(missing_filename, 'w') as f:
data = {'test': os.path.basename(filename), 'test_file': filename}
f.write('''<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="1" failures="1" time="1" errors="0" name="%(test)s">
<testcase name="test_ran" status="run" time="1" classname="Results">
<failure message="Unable to find test results for %(test)s, test did not run.\nExpected results in %(test_file)s" type=""/>
</testcase>
</testsuite>''' % data)
return False
return


def _get_missing_junit_result_filename(filename):
return os.path.join(os.path.dirname(filename), 'MISSING-%s' % os.path.basename(filename))


def read_junit(filename):
Expand Down

0 comments on commit c96214a

Please sign in to comment.