2018-02-07 14:17:33 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
import org.junit.rules.TestRule
|
|
|
|
import org.junit.runner.Description
|
|
|
|
import org.junit.runners.model.Statement
|
|
|
|
|
|
|
|
class JenkinsReadFileRule implements TestRule {
|
|
|
|
|
|
|
|
final BasePipelineTest testInstance
|
|
|
|
final String testRoot
|
2020-01-28 14:22:37 +02:00
|
|
|
final Map files = [:]
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
JenkinsReadFileRule(BasePipelineTest testInstance, String testRoot) {
|
|
|
|
this.testInstance = testInstance
|
|
|
|
this.testRoot = testRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
Statement apply(Statement base, Description description) {
|
|
|
|
return statement(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
private Statement statement(final Statement base) {
|
|
|
|
return new Statement() {
|
|
|
|
@Override
|
|
|
|
void evaluate() throws Throwable {
|
|
|
|
|
2020-01-28 14:22:37 +02:00
|
|
|
testInstance.helper.registerAllowedMethod( 'readFile', [String.class], {s -> return load(s, 'UTF-8')} )
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2020-01-28 14:22:37 +02:00
|
|
|
testInstance.helper.registerAllowedMethod( 'readFile', [Map.class], {m -> return load(m.file, m.encoding?m.encoding:'UTF-8')} )
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
base.evaluate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:37 +02:00
|
|
|
String load(String path, String encoding) {
|
|
|
|
|
|
|
|
if(files[path]) {
|
|
|
|
return files[path]
|
|
|
|
}
|
|
|
|
|
|
|
|
if(testRoot == null) {
|
|
|
|
throw new IllegalStateException("Test root not set. Resolving: \"${path}\".")
|
|
|
|
}
|
|
|
|
loadFile(testRoot + '/' + path).getText(encoding)
|
|
|
|
}
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
File loadFile(String path){
|
|
|
|
return new File(path)
|
|
|
|
}
|
|
|
|
}
|