From a42e727da20e617b357de4e209cf5076a86b1762 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 19 Mar 2019 12:40:58 +0100 Subject: [PATCH] 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. --- test/groovy/util/StepTracker.groovy | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 test/groovy/util/StepTracker.groovy diff --git a/test/groovy/util/StepTracker.groovy b/test/groovy/util/StepTracker.groovy new file mode 100644 index 000000000..748ad6532 --- /dev/null +++ b/test/groovy/util/StepTracker.groovy @@ -0,0 +1,69 @@ +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()) + } +}