[英]Scoping of a groovy variable inside a closure in try() to access from catch()
[英]How to deal with closure scoping in groovy code in the pipeline
我有一个名为“runTest”的方法,它负责在管道中运行某些测试,我有另一个名为“chooseTest”的方法,它负责解析参数并将这些测试附加到映射中。 在“chooseTest”方法中,我还有 switch-case 来检查哪些测试需要附加到地图中。 我将如何使用闭包在该 switch-case 中调用“runTest”方法,以便我可以在退出循环后调用它(用于检查每个测试)。
我试过让“runTest”本身成为一个闭包,但我遇到了“方法太长错误”,这是一个令人头疼的调试问题,所以我尝试在每个 if 语句案例中创建一个闭包变量。 我遇到了范围问题,这不允许我在 for 循环之外运行这些闭包调用。 我在下面附上了我的方法的例子
// INSIDE of chooseTest method-->
switch (testName){
case 'aTest':
def runATest = {runTest('aTest')}
break
case 'bTest':
def runBTest = {runTest('bTest')}
break
}
runAtest.call()
//This is where I ran into scoping issue
//Another approach I tried (saw this approach elsewhere, not sure if this is even the correct approach syntactically)
def runTests = []
switch (testName){
case 'aTest':
runTests << {-> runTest('aTest')}
break
case 'bTest':
runTests << {-> runTest('bTest')}
break
}
runTests.each
我希望闭包没有范围错误,当我在 case 语句之外调用它时,它会调用chooseTest 方法。
好吧,我是个菜鸟,我可以这样定义。
def runAtest
switch(test){
case 'atest':
runAtest = {print('you a noob')}
break
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.