From 294fc40e860ee810130f3679bdf5a4eb64df649d Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 27 Oct 2024 13:11:13 -0600 Subject: [PATCH] Increase coverage by testing special parameters with a build Uses a JUnit 5 WithJenkins annotation to provide a JenkinsRule argument to the test method. The JenkinsRule argument provides access to a running Jenkins from within the test. The test creates a freestyle job and runs it twice. It changes the description of each of those builds and confirms that the change is resolved by the parameter resolver. Plugin documentation describes the parameter resolver https://plugins.jenkins.io/embeddable-build-status/#plugin-content-parameter-resolver that can be applied to builds and provides replacement text in the embeddable build status result based on the data from the build. --- .../plugins/badge/ParameterResolverTest.java | 76 ++++++++----------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/badge/ParameterResolverTest.java b/src/test/java/org/jenkinsci/plugins/badge/ParameterResolverTest.java index 80200988..e03ba03d 100644 --- a/src/test/java/org/jenkinsci/plugins/badge/ParameterResolverTest.java +++ b/src/test/java/org/jenkinsci/plugins/badge/ParameterResolverTest.java @@ -5,12 +5,24 @@ import static org.hamcrest.MatcherAssert.assertThat; import hudson.model.Actionable; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.mockito.Mockito; class ParameterResolverTest { + private ParameterResolver resolver; + + @BeforeEach + void defineResolver() { + resolver = new ParameterResolver(); + } + @ParameterizedTest @CsvSource({ "Build ${params.BUILD_BRANCH},Build params.BUILD_BRANCH", @@ -18,66 +30,40 @@ class ParameterResolverTest { "Build ${params.BUILD_BRANCH|master} (${displayName}),Build params.BUILD_BRANCH|master (displayName)" }) void shouldResolveSubjectWithVariables(String queryParameter, String expectedParameter) { - String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), queryParameter); + String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), queryParameter); assertThat(resolvedParameter, is(expectedParameter)); } @Test public void shouldResolveNullParameter() { - String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), null); + String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), null); assertThat(resolvedParameter, is(nullValue())); } @Test public void shouldResolveEmptyParameter() { - String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), ""); + String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), ""); assertThat(resolvedParameter, is("")); } @Test - public void shouldResolveParameterWithoutVariables() { - String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), "Build"); - assertThat(resolvedParameter, is("Build")); - } - - @Test - public void shouldResolveParameterWithVariables() { - String resolvedParameter = - new ParameterResolver().resolve(Mockito.mock(Actionable.class), "Build ${params.BUILD_BRANCH}"); - assertThat(resolvedParameter, is("Build params.BUILD_BRANCH")); - } + @WithJenkins + public void shouldResolveSpecialParameters(JenkinsRule r) throws Exception { + FreeStyleProject p = r.createFreeStyleProject(); + p.setDescription("Project description is ignored"); - @Test - public void shouldResolveParameterWithVariablesAndText() { - String resolvedParameter = new ParameterResolver() - .resolve(Mockito.mock(Actionable.class), "Build ${params.BUILD_BRANCH} (${displayName})"); - assertThat(resolvedParameter, is("Build params.BUILD_BRANCH (displayName)")); - } + FreeStyleBuild build1 = r.buildAndAssertSuccess(p); + String myDescription1 = "Build 1 description is used"; + build1.setDescription(myDescription1); + String resolved1 = resolver.resolve( + build1, "build-${buildNumber} ${displayName} id-${buildId} ${description} ${duration}"); + assertThat(resolved1, is("build-1 #1 id-1 " + myDescription1 + " " + build1.getDurationString())); - @Test - public void shouldResolveParameterWithVariablesAndTextAndDefault() { - String resolvedParameter = new ParameterResolver() - .resolve(Mockito.mock(Actionable.class), "Build ${params.BUILD_BRANCH|master} (${displayName})"); - assertThat(resolvedParameter, is("Build params.BUILD_BRANCH|master (displayName)")); - } - - @Test - public void shouldNotResolveParameterOfVaryingLengthInRegex() { - ParameterResolver resolver = new ParameterResolver(); - String resolvedParameter = resolver.resolve(Mockito.mock(Actionable.class), "(?<=\\w*)"); - assertThat(resolvedParameter, is("(?<=\\w*)")); - } - - @Test - public void shouldNotResolveParameterWithFullRangeUnicode() { - String resolvedParameter = new ParameterResolver().resolve(Mockito.mock(Actionable.class), "\\p{InGreek}"); - assertThat(resolvedParameter, is("\\p{InGreek}")); - } - - @Test - public void shouldNotResolveParameterWithRegexInlineFlags() { - String resolvedParameter = - new ParameterResolver().resolve(Mockito.mock(Actionable.class), "(${\\Build(?i) params\\b})"); - assertThat(resolvedParameter, is("(${\\Build(?i) params\\b})")); + FreeStyleBuild build2 = r.buildAndAssertSuccess(p); + String myDescription2 = "Build 2 description is used"; + build2.setDescription(myDescription2); + String resolved2 = resolver.resolve( + build2, "${displayName} build-${buildNumber} id-${buildId} ${duration} ${description}"); + assertThat(resolved2, is("#2 build-2 id-2 " + build2.getDurationString() + " " + myDescription2)); } }