diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy index e1d43420..c82b9b7c 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/global/lib/LibraryLoader.groovy @@ -95,6 +95,16 @@ class LibraryLoader { groovyClassLoader.addURL(varsPath.toUri().toURL()) groovyClassLoader.addURL(resourcesPath.toUri().toURL()) + //FIXME here! hail Mary attempt to avoid a feature flag + // pre-load library classes using JPU groovy class loader + if (srcPath.toFile().exists()) { + srcPath.toFile().eachFileRecurse (FILES) { File srcFile -> + if (srcFile.name.endsWith(".groovy")) { + Class clazz = groovyClassLoader.parseClass(srcFile) + groovyClassLoader.loadClass(clazz.name) + } + } + } if (varsPath.toFile().exists()) { def ds = Files.list(varsPath) ds.map { it.toFile() } @@ -110,15 +120,6 @@ class LibraryLoader { // prevent fd leak on the DirectoryStream from Files.list() ds.close() } - // pre-load library classes using JPU groovy class loader - if (srcPath.toFile().exists()) { - srcPath.toFile().eachFileRecurse (FILES) { File srcFile -> - if (srcFile.name.endsWith(".groovy")) { - Class clazz = groovyClassLoader.parseClass(srcFile) - groovyClassLoader.loadClass(clazz.name) - } - } - } } record.definedGlobalVars = globalVars } catch (Exception e) { diff --git a/src/test/groovy/com/lesfurets/jenkins/TestInterceptingGCL.groovy b/src/test/groovy/com/lesfurets/jenkins/TestInterceptingGCL.groovy index 3b8738e7..b168a8d5 100644 --- a/src/test/groovy/com/lesfurets/jenkins/TestInterceptingGCL.groovy +++ b/src/test/groovy/com/lesfurets/jenkins/TestInterceptingGCL.groovy @@ -378,5 +378,35 @@ class TestInterceptingGCL extends BasePipelineTest { assertCallStack().contains("""test_annotation.sh(echo 'ClassB: I'm field of B')""") } + /** + * 1. Load library by annotation, with implicity, load lib classes lazily + * 2. Create instance of library class + * 3. Call a vars step that accepts that type as an argument, passing that instance + * 4. Make sure interception of pipeline methods works propertly + */ + @Test + void test_cross_class_as_var_arg_implicit_annotation_lazy_load() throws Exception { + //This does not factor in the current test but does replicate the use + //case in which the lazy load feature originated. + helper.cloneArgsOnMethodCallRegistration = false + + //In contrast to the previous flag, this is exactly what we are tesing. + //FIXME here! restore if we really go this route + //helper.preloadLibraryClasses = false + + final library = library().name("test_cross_class_usage") + .defaultVersion("master") + .allowOverride(false) + .implicit(true) + .targetPath(sharedLibCls) + .retriever(projectSource(sharedLibCls)) + .build() + helper.registerSharedLibrary(library) + final pipeline = "test_var_with_lib_class_arg_annotation_lazy_load" + runScript("job/library/cross_class_pre_loaded/${pipeline}.jenkins") + printCallStack() + assertCallStack().contains("""${pipeline}.monster1(org.test.Monster1""") + assertCallStack().contains("""monster1.echo(Dracula makes quite a scary monster)""") + } } diff --git a/src/test/jenkins/job/library/cross_class_pre_loaded/test_var_with_lib_class_arg_annotation_lazy_load.jenkins b/src/test/jenkins/job/library/cross_class_pre_loaded/test_var_with_lib_class_arg_annotation_lazy_load.jenkins new file mode 100644 index 00000000..b1f0760b --- /dev/null +++ b/src/test/jenkins/job/library/cross_class_pre_loaded/test_var_with_lib_class_arg_annotation_lazy_load.jenkins @@ -0,0 +1,6 @@ +//Commenting the annotation appears to make no difference when load is implicit +@Library("test_cross_class_usage") +import org.test.Monster1 + +vampire = new Monster1("Dracula") +monster1(vampire) diff --git a/src/test/resources/libs/test_cross_class_usage/src/org/test/Monster1.groovy b/src/test/resources/libs/test_cross_class_usage/src/org/test/Monster1.groovy new file mode 100644 index 00000000..909180da --- /dev/null +++ b/src/test/resources/libs/test_cross_class_usage/src/org/test/Monster1.groovy @@ -0,0 +1,9 @@ +package org.test + +class Monster1 { + String moniker + + Monster1(String m) { + moniker = m + } +} diff --git a/src/test/resources/libs/test_cross_class_usage/vars/monster1.groovy b/src/test/resources/libs/test_cross_class_usage/vars/monster1.groovy new file mode 100644 index 00000000..f4330dcb --- /dev/null +++ b/src/test/resources/libs/test_cross_class_usage/vars/monster1.groovy @@ -0,0 +1,5 @@ +import org.test.Monster1 + +void call(Monster1 m1) { + echo "$m1.moniker makes quite a scary monster" +}