From c729ae78c11d3fce9377542f3afba75d53422a61 Mon Sep 17 00:00:00 2001 From: Jerome Liu Date: Thu, 8 Jun 2017 22:32:24 -0700 Subject: [PATCH 1/3] Job to aggregate can contain variable, expand it before use --- .../test/AggregatedTestResultPublisher.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java b/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java index 89a682a76..8771fcf09 100644 --- a/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java +++ b/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java @@ -27,12 +27,14 @@ import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.AutoCompletionCandidates; +import hudson.EnvVars; import hudson.Extension; import hudson.Launcher; import hudson.Util; import static hudson.Util.fixNull; import hudson.model.Action; import hudson.model.BuildListener; +import hudson.model.DependencyGraph; import hudson.model.Fingerprint.RangeSet; import hudson.model.InvisibleAction; import hudson.model.ItemGroup; @@ -51,6 +53,7 @@ import hudson.tasks.Recorder; import hudson.util.FormValidation; import net.sf.json.JSONObject; + import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; @@ -60,7 +63,9 @@ import java.util.Collection; import java.util.Collections; import java.util.List; + import javax.annotation.CheckForNull; + import org.acegisecurity.AccessDeniedException; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -95,9 +100,20 @@ public AggregatedTestResultPublisher(String jobs, boolean includeFailedBuilds) { public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { // add a TestResult just so that it can show up later. - build.addAction(new TestResultAction(jobs, includeFailedBuilds, build)); + // Expand the jobs String as it can contain variable + String expandedJobs = this.getJobs(build.getEnvironment(listener)); + build.addAction(new TestResultAction(expandedJobs, includeFailedBuilds, build)); return true; } + + /** + * Return the expanded job list + * @param env + * @return + */ + public String getJobs(EnvVars env) { + return (env != null ? env.expand(this.jobs) : this.jobs); + } public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; From 9c3e2bed9b50400b345f1d1ca30822127aba0cd3 Mon Sep 17 00:00:00 2001 From: Jerome Liu Date: Fri, 9 Jun 2017 15:27:03 -0700 Subject: [PATCH 2/3] Remove unnecessary import and new lines --- .../java/hudson/tasks/test/AggregatedTestResultPublisher.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java b/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java index 8771fcf09..20ff0862a 100644 --- a/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java +++ b/src/main/java/hudson/tasks/test/AggregatedTestResultPublisher.java @@ -34,7 +34,6 @@ import static hudson.Util.fixNull; import hudson.model.Action; import hudson.model.BuildListener; -import hudson.model.DependencyGraph; import hudson.model.Fingerprint.RangeSet; import hudson.model.InvisibleAction; import hudson.model.ItemGroup; @@ -53,7 +52,6 @@ import hudson.tasks.Recorder; import hudson.util.FormValidation; import net.sf.json.JSONObject; - import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; @@ -63,9 +61,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; - import javax.annotation.CheckForNull; - import org.acegisecurity.AccessDeniedException; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; From 2e850175cca9244e7115f009c6319531c7b7661f Mon Sep 17 00:00:00 2001 From: ahahxof Date: Fri, 20 Dec 2019 14:28:34 -0800 Subject: [PATCH 3/3] Added JUnit test --- .../AggregatedTestResultPublisherTest.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/test/java/hudson/tasks/test/AggregatedTestResultPublisherTest.java b/src/test/java/hudson/tasks/test/AggregatedTestResultPublisherTest.java index fe03ca4c7..9ccd6e738 100644 --- a/src/test/java/hudson/tasks/test/AggregatedTestResultPublisherTest.java +++ b/src/test/java/hudson/tasks/test/AggregatedTestResultPublisherTest.java @@ -1,10 +1,15 @@ package hudson.tasks.test; import com.google.common.collect.ImmutableList; + +import hudson.Functions; import hudson.model.AbstractBuild; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; +import hudson.model.ParametersDefinitionProperty; import hudson.model.Result; +import hudson.model.StringParameterDefinition; +import hudson.tasks.BatchFile; import hudson.tasks.BuildTrigger; import hudson.tasks.Fingerprinter; import hudson.tasks.Shell; @@ -24,6 +29,8 @@ import static org.junit.Assert.assertThat; public class AggregatedTestResultPublisherTest { + private static final String VAR = "var"; + private static final String JOBS = "$" + VAR; public static final String TEST_PROJECT_NAME = "junit"; public static final String AGGREGATION_PROJECT_NAME = "aggregated"; @Rule @@ -123,6 +130,35 @@ public void testResultsAndAggregatedTestResults() throws Exception { .follow() .hasLinkToTestResultOfBuild(TEST_PROJECT_NAME, 1); } + + + @LocalData + @Test + public void testResultsAndAggregatedTestResultsParam() throws Exception { + createUpstreamParamProjectWithTests(); + createDownstreamParamProjectWithTests(); + + buildAndSetupPageObjects(); + + projectPage.getLatestTestReportLink() + .assertHasLatestTestResultText() + .assertHasTests() + .follow(); + projectPage.getLatestAggregatedTestReportLink() + .assertHasLatestAggregatedTestResultText() + .assertHasTests() + .follow(); + + buildPage.getTestReportLink() + .assertHasTestResultText() + .assertHasTests() + .follow(); + buildPage.getAggregatedTestReportLink() + .assertHasAggregatedTestResultText() + .assertHasTests() + .follow() + .hasLinkToTestResultOfBuild(TEST_PROJECT_NAME, 1); + } private void buildAndSetupPageObjects() throws Exception { buildOnce(); @@ -153,6 +189,15 @@ private void createUpstreamProjectWithNoTests() throws Exception { addFingerprinterToProject(upstreamProject, singleContents, singleFiles); upstreamProject.setQuietPeriod(0); } + + private void createUpstreamParamProjectWithTests() throws Exception { + upstreamProject = j.createFreeStyleProject(AGGREGATION_PROJECT_NAME); + StringParameterDefinition params = new StringParameterDefinition(VAR, TEST_PROJECT_NAME); + upstreamProject.addProperty(new ParametersDefinitionProperty(params)); + addFingerprinterToProject(upstreamProject, singleContents, singleFiles); + upstreamProject.setQuietPeriod(0); + addJUnitResultArchiver(upstreamProject); + } private void createDownstreamProjectWithTests() throws Exception { createDownstreamProjectWithNoTests(); @@ -160,6 +205,18 @@ private void createDownstreamProjectWithTests() throws Exception { addJUnitResultArchiver(downstreamProject); j.jenkins.rebuildDependencyGraph(); } + + private void createDownstreamParamProjectWithTests() throws Exception { + downstreamProject = j.createFreeStyleProject(TEST_PROJECT_NAME); + downstreamProject.setQuietPeriod(0); + addFingerprinterToProject(downstreamProject, singleContents, singleFiles); + upstreamProject.getPublishersList().add(new BuildTrigger(ImmutableList.of(downstreamProject), Result.SUCCESS)); + upstreamProject.getPublishersList().add(new AggregatedTestResultPublisher(JOBS)); + + addJUnitResultArchiver(downstreamProject); + + j.jenkins.rebuildDependencyGraph(); + } private void createDownstreamProjectWithNoTests() throws Exception { downstreamProject = j.createFreeStyleProject(TEST_PROJECT_NAME);