1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/test/groovy/util/StepTracker.groovy
Marcus Holl a42e727da2 Create step specific plugin lists: Helper class for tracking step calls
Before the test we remmber which test is currently running.
During the test we collect all the calls to steps.

Beside that we persist the names of all steps within this shared
lib itself.

After the test(s) we write a corresponding json file. In fact we
write the file after each test, which is too often. But since we
don't know which test is the last test we can't do better.

The resulting file can be used later on for resolving the plugins
contributing the particular steps.

With that we are able to create a list of required plugins for each
step.
2019-03-27 16:02:04 +01:00

70 lines
1.9 KiB
Groovy

package util
import static com.lesfurets.jenkins.unit.MethodSignature.method
import static util.StepHelper.getSteps
import org.codehaus.groovy.runtime.MetaClassHelper
import com.lesfurets.jenkins.unit.MethodSignature
import com.lesfurets.jenkins.unit.PipelineTestHelper
import groovy.json.JsonBuilder
class StepTracker {
/*
* Contains the piper steps as key (derived from the test name, so this is blurry since it might
* contains also other cases than only piper step name) and the observed calls in a collection.
*/
static Map piperStepCallMapping = [:]
static Set piperSteps = StepHelper.getSteps()
static Set calls
static {
initialize()
}
final static void initialize() {
PipelineTestHelper.metaClass.getAllowedMethodEntry = {
// We need to be careful here, in case we switch to another
// version of the Les Furets framework we have to check if
// this here still works.
String name, Object[] args ->
Class[] paramTypes = MetaClassHelper.castArgumentsToClassArray(args)
MethodSignature signature = method(name, paramTypes)
def intercepted = allowedMethodCallbacks.find { k, v -> k == signature }
if(intercepted != null)
StepTracker.add(name)
return intercepted
}
}
static void before(String stepName) {
if(piperStepCallMapping[stepName] == null)
piperStepCallMapping[stepName] = (Set)[]
calls = piperStepCallMapping[stepName]
}
static void after() {
calls = null
write()
}
static void add (String call) {
calls.add(call)
}
static private void write() {
Map root = [
piperSteps: piperSteps,
calls: piperStepCallMapping.sort()
]
new File('target/trackedCalls.json').write(new JsonBuilder(root).toPrettyString())
}
}