1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-20 05:19:40 +02:00

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.)
This commit is contained in:
Stephan Aßmus 2020-02-10 12:25:33 +01:00 committed by GitHub
parent 71766be2f5
commit 9658f3b480
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -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()
}

View File

@ -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) {