Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call methods in a script with an invalid name from other methods in that same script #80

Open
awittha opened this issue Jul 10, 2020 · 0 comments
Labels
bug Something isn't working

Comments

@awittha
Copy link
Contributor

awittha commented Jul 10, 2020

Expected Behavior

Given a script with an invalid JVM name, like some-script.groovy, and contents with multiple methods:

def one() {}

def two() {
  one()
}

return this

I would expect to be able to call method two() during a test, like this:

setup:
  def script = loadPipelineScriptForTest( "some-script.groovy ")
when:
  script.two()
then:
  // some expectation

Actual Behavior

The fact that some-script.groovy is compiled to a class file whose name is not valid to a JVM is a problem. Branch https://github.com/ExpediaGroup/jenkins-spock/tree/invalid-name-internal adds a unit test; the output is

[ERROR] can run methods in helper scripts whose names create invalid JVM class names from other methods in those scripts(com.homeaway.devtools.jenkins.testing.scripts.InvalidClassNameSpec)  Time elapsed: 0.043 s  <<< ERROR!
java.lang.ClassFormatError: Illegal class name "some-helper-script$internal_method" in class file some-helper-script$internal_method
	at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
	at some-helper-script.composed_method(some-helper-script.groovy:27)
	at com.homeaway.devtools.jenkins.testing.InvalidlyNamedScriptWrapper.invokeMethod(InvalidlyNamedScriptWrapper.groovy:68)
	at com.homeaway.devtools.jenkins.testing.scripts.InvalidClassNameSpec.can run methods in helper scripts whose names create invalid JVM class names from other methods in those scripts(InvalidClassNameSpec.groovy:96)

Steps to Reproduce

See the unit tests added in branch https://github.com/ExpediaGroup/jenkins-spock/tree/invalid-name-internal

Additional Information

This may just mean that the "invalid class name" handling needs to be improved.

This may be symptomatic of the script-loading mechanism being used incorrectly and may warrant looking into alternate implementations for this snippet:

protected Script loadPipelineScriptForTest(String _path) {
String[] path_parts = _path.split( "/" )
String filename = path_parts[path_parts.length-1]
String resource_path = "/"
if( path_parts.length >= 2 ) {
resource_path = String.join( "/", path_parts[0..path_parts.length-2] )
resource_path = "/${resource_path}/"
}
GroovyScriptEngine script_engine = new GroovyScriptEngine(generateScriptClasspath(resource_path))
Class<Script> script_class = script_engine.loadScriptByName( "${filename}" )
Script script = script_class.newInstance()
addPipelineMocksToObjects( script )
if( SourceVersion.isName( script_class.getSimpleName() ) ) {
return script
} else {
return new InvalidlyNamedScriptWrapper( script )
}
}

We should investigate and if it can be solved with the Wrapper, make sure that N & N+1 layers of nesting are solved, too.

@awittha awittha added the bug Something isn't working label Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant