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

Error when declaring type of script #499

Open
larakiba opened this issue Mar 11, 2022 · 4 comments
Open

Error when declaring type of script #499

larakiba opened this issue Mar 11, 2022 · 4 comments

Comments

@larakiba
Copy link

larakiba commented Mar 11, 2022

I've got a question related to initiating the type of the script variable.
I want to use the correct types so I can use auto completion and type checking. I've seen someone else asking this question (#413 (comment)) and wanted to ask if there is really no way to achieve that.

This works:

class PipelineUtils { 
    Object script

    PipelineUtils (Object script) {
        this.script = script
    }
}

but as soon as I declare that script is of type CpsScript:

class PipelineUtils {
    CpsScript script

    PipelineUtils (CpsScript script) {
        this.script = script
    }
}

I get the following error:

Could not find matching constructor for: org.company.PipelineUtils(example)
@UlrichEckhardt
Copy link
Contributor

Quoting some code here:

  private Script script

...but that's from a class that extends BasePipelineTest, not sure where your's came from.

  • Can you clarify where you read someone asking about this?
  • Also, where did you get the CpsScript from? That could potentially be a documentation bug, though I'm not really sure of the context.

@nre-ableton
Copy link
Contributor

nre-ableton commented Mar 11, 2022

CpsScript is actually org.jenkinsci.plugins.workflow.cps.CpsScript, so you need to import org.jenkinsci.plugins.workflow.cps.CpsScript to use that type.

FWIW, I don't usually bother with this -- I just declare it as Object and let Groovy deal with it. 😄

This is because to import CpsScript in something outside of Jenkins (ie, a library that you plan on testing with JenkinsPipelineUnit), you'll need to import all of those Jenkins in your Gradle/Maven files. This is really much more trouble than it's worth -- so IMHO it's better to just use Object and treat script as a black-box.

@larakiba
Copy link
Author

@UlrichEckhardt here is where I read about it before #413 (comment)

And to your second question:
Every script executed by Jenkins is of type CpsScript. I.e. if I write in my Jenkinsfile

PipelineUtils utils = new PipelineUtils(this)

then this is of type CpsScript.

@g3n35i5
Copy link

g3n35i5 commented Mar 17, 2022

@nre-ableton welcome back to the topic 😄

I've been using JenkinsPipelineUnit for a while now to test and prototype my shared libraries. Still a wonderful framework!

Anyway, I have to agree with @larakiba , if the mocked objects were actually of type CpsScript, it would be a lot easier to develop, just because Object doesn't include type checking etc.

But of course you are right, maintaining the Jenkins plugins as dependencies is a certain effort, but I still like to do it to develop exactly based on the plugin classes. If you are interested, this is what my build.gradle currently looks like:

plugins {
    id 'groovy'
}

repositories {
    mavenCentral()
    maven {
        url "https://repo.jenkins-ci.org/releases/"
    }
    maven {
        url "https://repo.jenkins-ci.org/public/"
    }
}

dependencies {
    implementation "org.jenkins-ci.main:jenkins-war:2.303.2"
    implementation 'com.cloudbees:groovy-cps:1.24'
    implementation 'org.jenkins-ci.main:jenkins-core:2.317@jar'
    implementation "org.jenkins-ci.plugins:branch-api:2.7.0@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-api:2.47@jar"
    implementation "org.jenkins-ci.plugins:scm-api:2.6.5@jar"
    implementation "org.jenkins-ci.plugins:cloudbees-folder:6.16@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-api:2.47@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-cps-global-lib:2.21@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-support:3.8@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-multibranch:2.26@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-cps-global-lib:2.21@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-cps:2.94@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-basic-steps:2.24@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-durable-task-step:2.40@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-cps:2.94@jar"
    implementation "org.jenkins-ci.plugins.workflow:workflow-job:2.42@jar"
    implementation "org.jenkins-ci.plugins:sidebar-link:1.12.1@jar"
    implementation 'org.codehaus.groovy:groovy-all:3.0.5'
    implementation 'org.jenkins-ci.plugins:git:4.8.2@jar'
    testImplementation "com.lesfurets:jenkins-pipeline-unit:1.10"
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

sourceSets {
    main {
        groovy {
            srcDirs = ['src']
        }
    }

    test {
        groovy {
            srcDirs = ['test/groovy']
        }
    }
}

test {
    useJUnitPlatform()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants