Skip to content

Commit

Permalink
Always set junit_family to xunit2 for dashing, eloquent, and foxy (#503
Browse files Browse the repository at this point in the history
)

* Always set junit_family to xunit2 for older distros

The pytest arg has precedence over a value set in an existing pytest.ini file.

Signed-off-by: Jacob Perron <[email protected]>

* Handle --pytest-args option coming from test args

Signed-off-by: Jacob Perron <[email protected]>
  • Loading branch information
jacobperron authored Aug 18, 2020
1 parent ecbb92a commit 20e0343
Showing 1 changed file with 13 additions and 39 deletions.
52 changes: 13 additions & 39 deletions ros2_batch_job/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
# limitations under the License.

import argparse
import configparser
import os
from pathlib import Path
import platform
from shutil import which
import subprocess
Expand Down Expand Up @@ -356,39 +354,6 @@ def build_and_test(args, job):

print('# BEGIN SUBSECTION: test')

# In Foxy and prior, xunit2 format is needed to make Jenkins xunit plugin 2.x happy
# After Foxy, we introduced per-package changes to make local builds and CI
# builds act the same.
if args.ros_distro in ('dashing', 'eloquent', 'foxy'):
# xunit2 format is needed to make Jenkins xunit plugin 2.x happy
with open('pytest.ini', 'w') as ini_file:
ini_file.write('[pytest]\njunit_family=xunit2')
# check if packages have a pytest.ini file that doesn't choose junit_family=xunit2
# and patch configuration if needed to force the xunit2 value
pytest_6_or_greater = job.run([
'"%s"' % job.python, '-c', "'"
'from distutils.version import StrictVersion;'
'import pytest;'
'import sys;'
'sys.exit(StrictVersion(pytest.__version__) >= StrictVersion("6.0.0"))'
"'"],
exit_on_error=False)
for path in Path('.').rglob('pytest.ini'):
config = configparser.ConfigParser()
config.read(str(path))
if pytest_6_or_greater:
# only need to correct explicit legacy option if exists
if not check_xunit2_junit_family_value(config, 'legacy'):
continue
else:
# in pytest < 6 need to enforce xunit2 if not set
if check_xunit2_junit_family_value(config, 'xunit2'):
continue
print("Patch '%s' to override 'pytest.junit_family=xunit2'" % path)
config.set('pytest', 'junit_family', 'xunit2')
with open(path, 'w+') as configfile:
config.write(configfile)

test_cmd = [
args.colcon_script, 'test',
'--base-paths', '"%s"' % args.sourcespace,
Expand All @@ -401,6 +366,19 @@ def build_and_test(args, job):
test_cmd.append('--pytest-with-coverage')
test_cmd.extend(args.test_args)

# In Foxy and prior, xunit2 format is needed to make Jenkins xunit plugin 2.x happy
# After Foxy, we introduced per-package changes to make local builds and CI
# builds act the same.
if args.ros_distro in ('dashing', 'eloquent', 'foxy'):
pytest_args = ['-o', 'junit_family=xunit2']
# We should only have one --pytest-args option, or some options might get ignored
if '--pytest-args' in test_cmd:
pytest_opts_index = test_cmd.index('--pytest-args') + 1
test_cmd = test_cmd[:pytest_opts_index] + pytest_args + test_cmd[pytest_opts_index:]
else:
test_cmd.append('--pytest-args')
test_cmd.extend(pytest_args)

ret_test = job.run(test_cmd, exit_on_error=False, shell=True)
info("colcon test returned: '{0}'".format(ret_test))
print('# END SUBSECTION')
Expand Down Expand Up @@ -432,10 +410,6 @@ def build_and_test(args, job):
return 0


def check_xunit2_junit_family_value(config, value):
return config.get('pytest', 'junit_family', fallback='') == value


def run(args, build_function, blacklisted_package_names=None):
if blacklisted_package_names is None:
blacklisted_package_names = []
Expand Down

0 comments on commit 20e0343

Please sign in to comment.