Skip to content

Commit

Permalink
Issue jenkinsci#224 - Load lib classes before lib vars
Browse files Browse the repository at this point in the history
FILL IN THE BLANKS. This is a preliminary commit just to ship WIP from
the abandoned `support-lazy-lib-class-load` branch.
  • Loading branch information
brianeray committed Jul 1, 2020
1 parent 3d0c6c2 commit 26e96c5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand All @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions src/test/groovy/com/lesfurets/jenkins/TestInterceptingGCL.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)""")
}
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.test

class Monster1 {
String moniker

Monster1(String m) {
moniker = m
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import org.test.Monster1

void call(Monster1 m1) {
echo "$m1.moniker makes quite a scary monster"
}

0 comments on commit 26e96c5

Please sign in to comment.