From 9658f3b4803e3406e6a3a050871310cbae85f0db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Mon, 10 Feb 2020 12:25:33 +0100 Subject: [PATCH] writeFile() cannot be passed a Map (#1163) * writeFile() cannot be passed a Map I've changed the return type of DebugReport.generateReport() from String to Map in order to get the generated file name as part of the return value instead of getting it from a field of DebugReport. The UnitTest checks whether writeFile() creates the debug_report file successfully and whether it has the expected contents. The effect of passing the Map instead of map.contents to writeFile() should have been an unnecessary wrapping via Map.toString() as in the test, but in the execution context of Jenkins, this throws an IllegalArgumentException: Could not instantiate {... and then the results of map.toString(). * Improve JenkinsWriteFileRule compatibility Calling m.text.toString() is wrong, since the type stored at m.text already needs to be a String (or GString). Expecting valid parameters here makes sure problems are detected by tests already. (All tests pass as before.) --- test/groovy/util/JenkinsWriteFileRule.groovy | 15 ++++++++++++++- vars/debugReportArchive.groovy | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/test/groovy/util/JenkinsWriteFileRule.groovy b/test/groovy/util/JenkinsWriteFileRule.groovy index 6a59dcf32..5c48b2748 100644 --- a/test/groovy/util/JenkinsWriteFileRule.groovy +++ b/test/groovy/util/JenkinsWriteFileRule.groovy @@ -1,6 +1,9 @@ package util import com.lesfurets.jenkins.unit.BasePipelineTest + +import static org.junit.Assert.assertNotNull +import static org.junit.Assert.assertTrue import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement @@ -25,7 +28,17 @@ class JenkinsWriteFileRule implements TestRule { @Override void evaluate() throws Throwable { - testInstance.helper.registerAllowedMethod( 'writeFile', [Map.class], {m -> files[m.file] = m.text.toString()}) + testInstance.helper.registerAllowedMethod( 'writeFile', [Map.class], { m -> + assertNotNull(m.file) + assertTrue(m.file instanceof CharSequence) + assertNotNull(m.text) + assertTrue(m.text instanceof CharSequence) + if (m.encoding) { + assertTrue(m.encoding instanceof CharSequence) + // Would be nice to actually handle encoding + } + files[m.file] = m.text + }) base.evaluate() } diff --git a/vars/debugReportArchive.groovy b/vars/debugReportArchive.groovy index ebb0d153b..68de7d39c 100644 --- a/vars/debugReportArchive.groovy +++ b/vars/debugReportArchive.groovy @@ -63,7 +63,7 @@ void call(Map parameters = [:]) { echo result.contents } - script.writeFile file: result.fileName, text: result + script.writeFile file: result.fileName, text: result.contents script.archiveArtifacts artifacts: result.fileName echo "Successfully archived debug report as '${result.fileName}'" } catch (Exception e) {