mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-02-07 13:42:23 +02:00
76 lines
2.0 KiB
Groovy
76 lines
2.0 KiB
Groovy
|
import static java.util.stream.Collectors.toList
|
||
|
import static org.hamcrest.Matchers.empty
|
||
|
import static org.hamcrest.Matchers.is
|
||
|
import static org.junit.Assert.assertThat
|
||
|
|
||
|
import java.lang.reflect.Field
|
||
|
|
||
|
import org.junit.Assert
|
||
|
import org.junit.Rule
|
||
|
import org.junit.Test
|
||
|
import org.junit.rules.RuleChain
|
||
|
|
||
|
import groovy.io.FileType
|
||
|
import util.BasePiperTest
|
||
|
import util.Rules
|
||
|
|
||
|
/*
|
||
|
* Intended for collecting generic checks applied to all steps.
|
||
|
*/
|
||
|
public class CommonStepsTest extends BasePiperTest{
|
||
|
|
||
|
@Rule
|
||
|
public RuleChain ruleChain = Rules.getCommonRules(this)
|
||
|
|
||
|
@Test
|
||
|
public void stepsWithWrongFieldNameTest() {
|
||
|
|
||
|
def whitelist = ['commonPipelineEnvironment']
|
||
|
|
||
|
def stepsWithWrongStepName = []
|
||
|
|
||
|
for(def step in getSteps()) {
|
||
|
|
||
|
if(whitelist.contains(step)) continue
|
||
|
|
||
|
def script = loadScript("${step}.groovy")
|
||
|
|
||
|
def fields = script.getClass().getDeclaredFields() as Set
|
||
|
Field stepNameField = fields.find { it.getName() == 'STEP_NAME'}
|
||
|
|
||
|
if(! stepNameField) {
|
||
|
stepsWithWrongStepName.add(step)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
boolean notAccessible = false;
|
||
|
def fieldName
|
||
|
|
||
|
if(!stepNameField.isAccessible()) {
|
||
|
stepNameField.setAccessible(true)
|
||
|
notAccessible = true
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
fieldName = stepNameField.get(script)
|
||
|
} finally {
|
||
|
if(notAccessible) stepNameField.setAccessible(false)
|
||
|
}
|
||
|
if(fieldName != step) {
|
||
|
stepsWithWrongStepName.add(step)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
assertThat("Steps with wrong step name or without STEP_NAME field.: ${stepsWithWrongStepName}",
|
||
|
stepsWithWrongStepName, is(empty()))
|
||
|
}
|
||
|
|
||
|
private static getSteps() {
|
||
|
List steps = []
|
||
|
new File('vars').traverse(type: FileType.FILES, maxDepth: 0)
|
||
|
{ if(it.getName().endsWith('.groovy')) steps << (it =~ /vars\/(.*)\.groovy/)[0][1] }
|
||
|
return steps
|
||
|
|
||
|
}
|
||
|
}
|