From 6b938fbd9d69042a12f7424dcabce45a0118eb6e Mon Sep 17 00:00:00 2001 From: Kien Ha Date: Tue, 24 May 2016 23:15:06 -0400 Subject: [PATCH] Update JaCoCo plugin - update JaCoCo plugin to use convert_mapping_to_xml - fix status-update to update-build-status - add minimal and full test Change-Id: Iff936c93544416ab7759ccfabb7e284fc99e8e7c Signed-off-by: Kien Ha Signed-off-by: Ryo Tagami --- jenkins_jobs/modules/publishers.py | 80 +++++++++++-------- tests/publishers/fixtures/jacoco-complete.xml | 25 ++++++ .../publishers/fixtures/jacoco-complete.yaml | 27 +++++++ tests/publishers/fixtures/jacoco-minimal.xml | 13 +++ tests/publishers/fixtures/jacoco-minimal.yaml | 2 + tests/publishers/fixtures/jacoco001.xml | 4 +- tests/publishers/fixtures/jacoco001.yaml | 2 +- 7 files changed, 115 insertions(+), 38 deletions(-) create mode 100644 tests/publishers/fixtures/jacoco-complete.xml create mode 100644 tests/publishers/fixtures/jacoco-complete.yaml create mode 100644 tests/publishers/fixtures/jacoco-minimal.xml create mode 100644 tests/publishers/fixtures/jacoco-minimal.yaml diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index 34f0325bd..e77497398 100644 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -960,18 +960,17 @@ def jacoco(registry, xml_parent, data): Requires the Jenkins :jenkins-wiki:`JaCoCo Plugin `. :arg str exec-pattern: This is a file name pattern that can be used to - locate the jacoco report files (default - ``**/**.exec``) + locate the jacoco report files (default '**/**.exec') :arg str class-pattern: This is a file name pattern that can be used - to locate class files (default ``**/classes``) + to locate class files (default '**/classes') :arg str source-pattern: This is a file name pattern that can be used - to locate source files (default ``**/src/main/java``) + to locate source files (default '**/src/main/java') :arg bool update-build-status: Update the build according to the results - (default false) + (default false) :arg str inclusion-pattern: This is a file name pattern that can be used - to include certain class files (optional) + to include certain class files (default '') :arg str exclusion-pattern: This is a file name pattern that can be used - to exclude certain class files (optional) + to exclude certain class files (default '') :arg dict targets: :targets: (instruction, branch, complexity, line, method, class) @@ -979,26 +978,30 @@ def jacoco(registry, xml_parent, data): * **healthy** (`int`): Healthy threshold (default 0) * **unhealthy** (`int`): Unhealthy threshold (default 0) - Example: + Minimal Example: - .. literalinclude:: /../../tests/publishers/fixtures/jacoco001.yaml + .. literalinclude:: /../../tests/publishers/fixtures/jacoco-minimal.yaml + :language: yaml + + Full Example: + + .. literalinclude:: /../../tests/publishers/fixtures/jacoco-complete.yaml :language: yaml """ jacoco = XML.SubElement(xml_parent, 'hudson.plugins.jacoco.JacocoPublisher') - XML.SubElement(jacoco, 'execPattern').text = data.get( - 'exec-pattern', '**/**.exec') - XML.SubElement(jacoco, 'classPattern').text = data.get( - 'class-pattern', '**/classes') - XML.SubElement(jacoco, 'sourcePattern').text = data.get( - 'source-pattern', '**/src/main/java') - XML.SubElement(jacoco, 'changeBuildStatus').text = data.get( - 'update-build-status', False) - XML.SubElement(jacoco, 'inclusionPattern').text = data.get( - 'inclusion-pattern', '') - XML.SubElement(jacoco, 'exclusionPattern').text = data.get( - 'exclusion-pattern', '') + jacoco.set('plugin', 'jacoco') + + mappings = [ + ('exec-pattern', 'execPattern', '**/**.exec'), + ('class-pattern', 'classPattern', '**/classes'), + ('source-pattern', 'sourcePattern', '**/src/main/java'), + ('update-build-status', 'changeBuildStatus', False), + ('inclusion-pattern', 'inclusionPattern', ''), + ('exclusion-pattern', 'exclusionPattern', ''), + ] + helpers.convert_mapping_to_xml(jacoco, data, mappings, fail_required=True) itemsList = ['instruction', 'branch', @@ -1007,21 +1010,28 @@ def jacoco(registry, xml_parent, data): 'method', 'class'] - for item in data['targets']: - item_name = next(iter(item.keys())) - if item_name not in itemsList: - raise JenkinsJobsException("item entered is not valid must be " - "one of: %s" % ",".join(itemsList)) - item_values = item.get(item_name, 0) + if 'targets' in data: + for item in data['targets']: + item_name = next(iter(item.keys())) + if item_name not in itemsList: + raise InvalidAttributeError('targets', item_name, itemsList) - XML.SubElement(jacoco, - 'maximum' + - item_name.capitalize() + - 'Coverage').text = str(item_values.get('healthy', 0)) - XML.SubElement(jacoco, - 'minimum' + - item_name.capitalize() + - 'Coverage').text = str(item_values.get('unhealthy', 0)) + item_values = item[item_name] + if item_values: + XML.SubElement(jacoco, + 'maximum' + + item_name.capitalize() + + 'Coverage').text = str( + item_values.get('healthy', 0)) + XML.SubElement(jacoco, + 'minimum' + + item_name.capitalize() + + 'Coverage').text = str( + item_values.get('unhealthy', 0)) + else: + raise MissingAttributeError( + ['healthy', 'unhealthy'], + 'publishers.jacoco.targets.' + item_name) def ftp(registry, xml_parent, data): diff --git a/tests/publishers/fixtures/jacoco-complete.xml b/tests/publishers/fixtures/jacoco-complete.xml new file mode 100644 index 000000000..1c11bb977 --- /dev/null +++ b/tests/publishers/fixtures/jacoco-complete.xml @@ -0,0 +1,25 @@ + + + + + **/**.exec + **/classes + **/src/main/java + true + **/*.class + **/*Test*.class + 7 + 1 + 8 + 2 + 9 + 3 + 10 + 4 + 11 + 5 + 12 + 6 + + + diff --git a/tests/publishers/fixtures/jacoco-complete.yaml b/tests/publishers/fixtures/jacoco-complete.yaml new file mode 100644 index 000000000..f1ea1d8b5 --- /dev/null +++ b/tests/publishers/fixtures/jacoco-complete.yaml @@ -0,0 +1,27 @@ +publishers: + - jacoco: + exec-pattern: '**/**.exec' + class-pattern: '**/classes' + source-pattern: '**/src/main/java' + update-build-status: true + inclusion-pattern: '**/*.class' + exclusion-pattern: '**/*Test*.class' + targets: + - instruction: + healthy: 7 + unhealthy: 1 + - branch: + healthy: 8 + unhealthy: 2 + - complexity: + healthy: 9 + unhealthy: 3 + - line: + healthy: 10 + unhealthy: 4 + - method: + healthy: 11 + unhealthy: 5 + - class: + healthy: 12 + unhealthy: 6 diff --git a/tests/publishers/fixtures/jacoco-minimal.xml b/tests/publishers/fixtures/jacoco-minimal.xml new file mode 100644 index 000000000..309e36dd9 --- /dev/null +++ b/tests/publishers/fixtures/jacoco-minimal.xml @@ -0,0 +1,13 @@ + + + + + **/**.exec + **/classes + **/src/main/java + false + + + + + diff --git a/tests/publishers/fixtures/jacoco-minimal.yaml b/tests/publishers/fixtures/jacoco-minimal.yaml new file mode 100644 index 000000000..12d2d74cc --- /dev/null +++ b/tests/publishers/fixtures/jacoco-minimal.yaml @@ -0,0 +1,2 @@ +publishers: + - jacoco diff --git a/tests/publishers/fixtures/jacoco001.xml b/tests/publishers/fixtures/jacoco001.xml index 7077d8302..ab66dab41 100644 --- a/tests/publishers/fixtures/jacoco001.xml +++ b/tests/publishers/fixtures/jacoco001.xml @@ -1,11 +1,11 @@ - + **/**.exec **/classes **/src/main/java - + true 10 diff --git a/tests/publishers/fixtures/jacoco001.yaml b/tests/publishers/fixtures/jacoco001.yaml index 604196104..fc45fd1c7 100644 --- a/tests/publishers/fixtures/jacoco001.yaml +++ b/tests/publishers/fixtures/jacoco001.yaml @@ -3,7 +3,7 @@ publishers: exec-pattern: "**/**.exec" class-pattern: "**/classes" source-pattern: "**/src/main/java" - status-update: true + update-build-status: true targets: - branch: healthy: 10