From 603cbc27e99b1a2cc48bdfa2da4393daca7e3436 Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 11 Mar 2020 12:53:11 +0100 Subject: [PATCH 1/6] Add pytest.ini in the build directory --- ros2_batch_job/__main__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index d0514364c..72d5791f0 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -381,6 +381,9 @@ def build_and_test(args, job): with open('pytest.ini', 'w') as ini_file: ini_file.write('[pytest]\njunit_family=xunit2') + with open('build/pytest.ini', 'w') as ini_file: + ini_file.write('[pytest]\njunit_family=xunit2') + test_cmd = [ args.colcon_script, 'test', '--base-paths', '"%s"' % args.sourcespace, From 4902aa76a474e814ca7de8bd2f602f2335e079ba Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 11 Mar 2020 13:13:48 +0100 Subject: [PATCH 2/6] Try to patch all pytest.ini files --- ros2_batch_job/__main__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 72d5791f0..50fdba647 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -381,8 +381,11 @@ def build_and_test(args, job): with open('pytest.ini', 'w') as ini_file: ini_file.write('[pytest]\njunit_family=xunit2') - with open('build/pytest.ini', 'w') as ini_file: - ini_file.write('[pytest]\njunit_family=xunit2') + from pathlib import Path + for path in Path('.').rglob('pytest.ini'): + print(" !! Found pytest.ini at: " + path.name) + with open(path, "a") as pytest: + pytest.write('\njunit_family=xunit2') test_cmd = [ args.colcon_script, 'test', From add260e56c2ab4470259f0d29e78f3e5bc2cf4f0 Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 11 Mar 2020 13:45:08 +0100 Subject: [PATCH 3/6] Patch pytest.ini files in packages --- ros2_batch_job/__main__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 50fdba647..08fef5dff 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -380,12 +380,14 @@ def build_and_test(args, job): # 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 and add the xunit2 + # format2 if not present from pathlib import Path for path in Path('.').rglob('pytest.ini'): - print(" !! Found pytest.ini at: " + path.name) - with open(path, "a") as pytest: - pytest.write('\njunit_family=xunit2') + with open(path, "r+") as pytest: + if 'xunit2' not in pytest.read(): + print("xunit2 patch applied to " + str(path.resolve())) + pytest.write('\njunit_family=xunit2') test_cmd = [ args.colcon_script, 'test', From 1d09f232d55d41f9dd7b3142aafe3bbc4caab9ff Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 11 Mar 2020 14:20:02 +0100 Subject: [PATCH 4/6] Fix grammar --- ros2_batch_job/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 08fef5dff..470a2edc0 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -381,7 +381,7 @@ def build_and_test(args, job): with open('pytest.ini', 'w') as ini_file: ini_file.write('[pytest]\njunit_family=xunit2') # check if packages have a pytest.ini file and add the xunit2 - # format2 if not present + # format if it is not present from pathlib import Path for path in Path('.').rglob('pytest.ini'): with open(path, "r+") as pytest: From a6ce403a933713fc97427eac4a8d8bf320b7a40b Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 11 Mar 2020 18:05:40 +0100 Subject: [PATCH 5/6] Use ConfigParser to manipulate ini file --- ros2_batch_job/__main__.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 470a2edc0..313638960 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -383,11 +383,20 @@ def build_and_test(args, job): # check if packages have a pytest.ini file and add the xunit2 # format if it is not present from pathlib import Path + from configparser import ConfigParser, NoOptionError for path in Path('.').rglob('pytest.ini'): - with open(path, "r+") as pytest: - if 'xunit2' not in pytest.read(): - print("xunit2 patch applied to " + str(path.resolve())) - pytest.write('\njunit_family=xunit2') + config = ConfigParser() + config.read(str(path.resolve())) + try: + # only if xunit2 is set continue the loop with the file unpatched + if config.get('pytest', 'junit_family') == 'xunit2': + continue + except NoOptionError: + pass + print("xunit2 patch applied to " + str(path.resolve())) + config.set('pytest', 'junit_family', 'xunit2') + with open(path, 'w+') as configfile: + config.write(configfile) test_cmd = [ args.colcon_script, 'test', From 5e1884c221b0677ee608bd007d3988977b98db55 Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Wed, 11 Mar 2020 19:13:04 +0100 Subject: [PATCH 6/6] Reorder imports and use relative paths in ConfigParser --- ros2_batch_job/__main__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 313638960..33a3f5a43 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -13,7 +13,9 @@ # limitations under the License. import argparse +import configparser import os +from pathlib import Path import platform from shutil import which import subprocess @@ -382,18 +384,16 @@ def build_and_test(args, job): ini_file.write('[pytest]\njunit_family=xunit2') # check if packages have a pytest.ini file and add the xunit2 # format if it is not present - from pathlib import Path - from configparser import ConfigParser, NoOptionError for path in Path('.').rglob('pytest.ini'): - config = ConfigParser() - config.read(str(path.resolve())) + config = configparser.ConfigParser() + config.read(str(path)) try: # only if xunit2 is set continue the loop with the file unpatched if config.get('pytest', 'junit_family') == 'xunit2': continue - except NoOptionError: + except configparser.NoOptionError: pass - print("xunit2 patch applied to " + str(path.resolve())) + print('xunit2 patch applied to ' + str(path)) config.set('pytest', 'junit_family', 'xunit2') with open(path, 'w+') as configfile: config.write(configfile)