def main(argv=sys.argv[1:]): parser = argparse.ArgumentParser(description='Runs the test command passed as an argument and verifies that the expected result file has been generated.') parser.add_argument('command', nargs='+', help='The test command to execute') parser.add_argument('results', help='The path to the xunit result file') parser.add_argument('--working-dir', nargs='?', help='The working directory for the executed 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) 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]) print('-- run_tests.py: execute commands%s%s' % (work_dir_msg, cmds_msg)) for cmd in args.command: subprocess.call(cmd, cwd=args.working_dir, shell=True) 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 try: root = ElementTree(None, args.results) except ParseError as e1: #print('Invalid XML in result file "%s"' % args.results) tidy_xml(args.results) try: root = ElementTree(None, args.results) except ParseError as pe2: print('Invalid XML in result file "%s" (even after ' 'trying to tidy it): %s ' % (args.results, str(pe2))) else: # if result file does not exist create placeholder which indicates failure print('Cannot find results, writing failure results to "%s"' % placeholder) # create folder if necessary if not os.path.exists(os.path.dirname(args.results)): os.makedirs(os.path.dirname(args.results)) 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)
def ensure_junit_result_exist(filename, errors): if os.path.exists(filename): # if result file exists ensure that it contains valid xml try: ET.parse(filename) except ParseError: from catkin.tidy_xml import tidy_xml tidy_xml(filename) try: ET.parse(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 True # 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 cdata = "<![CDATA[Executable crashed with:\n{}]]>".format( errors) if errors else "" cdata = cdata.replace("\033", "") with open(missing_filename, 'w') as f: data = { 'test': os.path.basename(filename), 'test_file': filename, 'cdata': cdata } f.write(( '<?xml version="1.1" encoding="UTF-8"?>\n' '<testsuite tests="1" failures="0" time="1" errors="1" name="%(test)s">\n' ' <testcase name="%(test)s" status="run" time="1" classname="Results">\n' ' <failure message="Unable to find test results for %(test)s, test most probably crashed.\nExpected results in %(test_file)s" type="">' '%(cdata)s' ' </failure>\n' ' </testcase>\n' '</testsuite>\n') % data) return False
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 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 True
def test_tiny_xml(self): try: rootdir = tempfile.mkdtemp() not_exist_file = os.path.join(rootdir, 'not_exist') self.assertRaises(ValueError, tidy_xml, not_exist_file) utf8_file = os.path.join(rootdir, 'utf8.xml') with open(utf8_file, 'ab') as fhand: fhand.write(u'\u0000') tidy_xml(utf8_file) with open(utf8_file, 'r') as fhand: contents = fhand.read() self.assertEqual('?', contents) iso_file = os.path.join(rootdir, 'iso.xml') with open(iso_file, 'ab') as fhand: fhand.write(u'\u0000') tidy_xml(iso_file) with open(iso_file, 'r') as fhand: contents = fhand.read() self.assertEqual('?', contents) finally: shutil.rmtree(rootdir)
print('-- run_tests.py: remove old result "%s"' % args.results) os.remove(args.results) print('-- run_tests.py: execute commands%s%s' % (' with working directory "%s"' % args.working_dir if args.working_dir is not None else '', (''.join(['\n %s' % cmd for cmd in args.command])))) for cmd in args.command: subprocess.call(cmd, cwd=args.working_dir, shell=True) 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 try: root = ElementTree(None, args.results) except ParseError as e: #print('Invalid XML in result file "%s"' % args.results) tidy_xml(args.results) try: root = ElementTree(None, args.results) except ParseError as e: print('Invalid XML in result file "%s" (even after trying to tidy it) ' % args.results) else: # if result file does not exist create placeholder which indicates failure placeholder = os.path.join(os.path.dirname(args.results), 'MISSING-%s' % os.path.basename(args.results)) print('Cannot find results, writing failure results to "%s"' % placeholder) # create folder if necessary if not os.path.exists(os.path.dirname(args.results)): os.makedirs(os.path.dirname(args.results)) 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">
print('-- run_tests.py: execute commands%s%s' % (' with working directory "%s"' % args.working_dir if args.working_dir is not None else '', (''.join(['\n %s' % cmd for cmd in args.command])))) for cmd in args.command: subprocess.call(cmd, cwd=args.working_dir, shell=True) 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 try: root = ElementTree(None, args.results) except ParseError as e: #print('Invalid XML in result file "%s"' % args.results) tidy_xml(args.results) try: root = ElementTree(None, args.results) except ParseError as e: print( 'Invalid XML in result file "%s" (even after trying to tidy it) ' % args.results) else: # if result file does not exist create placeholder which indicates failure print('Cannot find results, writing failure results to "%s"' % placeholder) # create folder if necessary if not os.path.exists(os.path.dirname(args.results)): os.makedirs(os.path.dirname(args.results)) with open(placeholder, 'w') as f: data = { 'test': os.path.basename(args.results),
def main(argv=sys.argv[1:]): parser = argparse.ArgumentParser( description= 'Runs the test command passed as an argument and verifies that the expected result file has been generated.' ) parser.add_argument('results', help='The path to the xunit result file') parser.add_argument('command', nargs='+', help='The test command to execute') parser.add_argument('--working-dir', nargs='?', help='The working directory for the executed command') 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) 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]) print('-- run_tests.py: execute commands%s%s' % (work_dir_msg, cmds_msg)) rc = 0 for cmd in args.command: rc = subprocess.call(cmd, cwd=args.working_dir, shell=True) if rc: 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: 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)): os.makedirs(os.path.dirname(args.results)) 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 return 0