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

Mock method of shared with defaut parameter #570

Open
Bilgetz opened this issue Dec 7, 2022 · 0 comments
Open

Mock method of shared with defaut parameter #570

Bilgetz opened this issue Dec 7, 2022 · 0 comments

Comments

@Bilgetz
Copy link
Contributor

Bilgetz commented Dec 7, 2022

Jenkins and plugins versions report

jenkins-pipeline-unit 1.16
jenkins-core 2.303.3

What Operating System are you using (both controller, and any agents involved in the problem)?

tried test with maven

  • linux on CI build.
  • windows in Intelli

Reproduction steps

in shared:

vars/myMethod.groovy
def call(Map config = [:]) {
  String testParam= config.getOrDefault('test', "my defaut param")
 // code here
}

my test ( fake shared url here )

src/test/groovy/jenkinsfileTest.groovy
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class JenkinsfileTest extends DeclarativePipelineTest  {

  @Override
  @BeforeAll
  void setUp() throws Exception {
      super.setUp()
  }

  @Test
  void reproducer_1() {
      Object library = library()
          .name('shared-libraries')
          .retriever(gitSource('ssh://git@ssh_url/shared-libraries.git'))
          .targetPath('/')
          .defaultVersion("master")
          .allowOverride(true)
          .implicit(false)
          .build()
      helper.registerSharedLibrary(library)

      helper.registerAllowedMethod("myMethod",[Object], {Map args -> println "${args}"})
      runInlineScript("""
      @Library('shared-libraries') _
              pipeline {
                  stages {
                      stage("my stage") {
                          steps {
                              myMethod test: 'wololo'
                          }
                      }
                  }
              }
      """)
      assertJobStatusSuccess()
  }

  @Test
  void reproducer_2() {
      Object library = library()
          .name('shared-libraries')
          .retriever(gitSource('ssh://git@ssh_url/shared-libraries.git'))
          .targetPath('/')
          .defaultVersion("master")
          .allowOverride(true)
          .implicit(false)
          .build()
      helper.registerSharedLibrary(library)

      helper.registerAllowedMethod("myMethod",[Object], {Map args -> println "${args}"})
      runInlineScript("""
      @Library('shared-libraries') _
              pipeline {
                  stages {
                      stage("my stage") {
                          steps {
                              myMethod()
                          }
                      }
                  }
              }
      """)
      assertJobStatusSuccess()
  }
}

  @Test
  void reproducer_3() {
      Object library = library()
          .name('shared-libraries')
          .retriever(gitSource('ssh://git@ssh_url/shared-libraries.git'))
          .targetPath('/')
          .defaultVersion("master")
          .allowOverride(true)
          .implicit(false)
          .build()
      helper.registerSharedLibrary(library)

      helper.registerAllowedMethod("myMethod",[], {}"})
      runInlineScript("""
      @Library('shared-libraries') _
              pipeline {
                  stages {
                      stage("my stage") {
                          steps {
                              myMethod()
                          }
                      }
                  }
              }
      """)
      assertJobStatusSuccess()
  }
}

Expected Results

call the mock if no parameter in the method of shared.

Actual Results

if no parameter the real method is called.

Anything else?

No response

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

1 participant