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/JenkinsFileExistsRule.groovy
Oliver Nocon a46b57e6b4
Persisted pipeline environment for golang library (#1091)
* Use commonPipelineEnvironment in go binary

* Update groovy part incl. tests

* Rework structure and naming

* Support influx resources in steps

* Update tests and some cleanups

* Add correct defer handling

* Address PR feedback

* Fix test

* Update resources.go

Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2020-01-15 12:16:25 +01:00

58 lines
1.6 KiB
Groovy

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 JenkinsFileExistsRule implements TestRule {
final BasePipelineTest testInstance
final List existingFiles
/**
* The List of files that have been queried via `fileExists`
*/
final List queriedFiles = []
JenkinsFileExistsRule(BasePipelineTest testInstance) {
this(testInstance,[])
}
JenkinsFileExistsRule(BasePipelineTest testInstance, List existingFiles) {
this.testInstance = testInstance
this.existingFiles = existingFiles
}
JenkinsFileExistsRule registerExistingFile(String file) {
existingFiles.add(file)
return this
}
@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('fileExists', [String.class], {s ->
queriedFiles.add(s.toString())
return s.toString() in existingFiles
})
testInstance.helper.registerAllowedMethod('fileExists', [Map.class], {m ->
queriedFiles.add(m.file.toString())
return m.file.toString() in existingFiles}
)
base.evaluate()
}
}
}
}