1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/util/JenkinsWriteFileRule.groovy
Stephan Aßmus 9658f3b480
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.)
2020-02-10 12:25:33 +01:00

48 lines
1.4 KiB
Groovy

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
class JenkinsWriteFileRule implements TestRule {
final BasePipelineTest testInstance
Map files = [:]
JenkinsWriteFileRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
}
@Override
Statement apply(Statement base, Description description) {
return statement(base)
}
private Statement statement(final Statement base) {
return new Statement() {
@Override
void evaluate() throws Throwable {
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()
}
}
}
}