jenkinsci / JenkinsPipelineUnit

Framework for unit testing Jenkins pipelines
MIT License
1.53k stars 392 forks source link

Code coverage of pipeline library - MissingMethodException #46

Open roderickrandolph opened 7 years ago

roderickrandolph commented 7 years ago

I'm attempting to analyze the unit test code coverage of our pipeline groovy scripts using jacoco. Right now only the groovy code under the src directory is getting analyzed and I believe the scripts under vars aren't getting analyzed because vars isn't part of the source class path.

When adding the vars directory to the sourceSets in build.gradle (see below) some of my tests fail with an error like groovy.lang.MissingMethodException: No signature of method: static hello.world() is applicable for argument types: () values: []. This error seems to only occur when my pipeline scripts make explicit static method calls (e.g. hello.world()). Is this a bug or user error? Any possible workarounds? Thanks in advance!

// build.gradle

sourceSets {
  main {
    groovy {
      srcDirs = ['src', 'vars']
      exclude '**/test/**'
    }
  }
}
// vars/hello.groovy

def world() {
  echo "hello, world!"
}
mioyn commented 7 years ago

could you please provide the build directory structure.

ozangunalp commented 7 years ago

@roderickrandolph Last time I looked I could not include global library vars into the source set of Gradle project. I'll check it out and return back to you.

ghost commented 7 years ago

This would really me as well. @roderickrandolph I was having problems including pipeline library via gradle because the default packaging for all the artifacts was hpi. If I made gradle download the jar using @jar, it would only download one jar and none of the transitive dependencies. Even I added transitive = true it would download the hpi - which is useless.

ozangunalp commented 7 years ago

Hi @roderickrandolph ,

I'd time to check out the test coverage report you were asking. It is definitely possible to configure Maven or Gradle to make debugging work in IDE.

However, because the tested files are scripts and they are not compiled but loaded by GroovyScriptEngine at runtime, the coverage reports won't include them.

BUT when use the internal code coverage of IntelliJ IDEA, it can count the hits to the scripts. So you get the test coverage colors on the code, but not on the report.

Hope that helps.

CCFenner commented 6 years ago

We have the same issue for a library with Maven. Adding the file to the class path results in "undefined method echo" at compile time.