Skip to content

Commit 3ef66ae

Browse files
committed
SCMSourceRetrieverTest: add checkDefaultVersion_BRANCH_NAME_MBP() [JENKINS-69731]
1 parent eb73ce4 commit 3ef66ae

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

src/test/java/org/jenkinsci/plugins/workflow/libs/SCMSourceRetrieverTest.java

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Collections;
4444
import java.util.Iterator;
4545
import java.util.List;
46+
import jenkins.branch.BranchSource;
4647
import jenkins.plugins.git.GitSCMSource;
4748
import jenkins.plugins.git.GitSampleRepoRule;
4849
import jenkins.scm.api.SCMHead;
@@ -58,6 +59,7 @@
5859
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
5960
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
6061
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
62+
import org.jenkinsci.plugins.workflow.multibranch.*;
6163

6264
import static hudson.ExtensionList.lookupSingleton;
6365
import static org.hamcrest.Matchers.contains;
@@ -87,6 +89,7 @@ public class SCMSourceRetrieverTest {
8789
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
8890
@Rule public JenkinsRule r = new JenkinsRule();
8991
@Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
92+
@Rule public GitSampleRepoRule sampleRepo2 = new GitSampleRepoRule();
9093
@Rule public SubversionSampleRepoRule sampleRepoSvn = new SubversionSampleRepoRule();
9194

9295
@Issue("JENKINS-40408")
@@ -276,12 +279,64 @@ public class SCMSourceRetrieverTest {
276279
r.assertLogContains("ERROR: No version bogus found for library branchylib", b4);
277280
r.assertLogContains("org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:", b4);
278281
r.assertLogContains("WorkflowScript: Loading libraries failed", b4);
282+
}
279283

280-
// TODO: create a job instantiated from Git (or fooled into
281-
// thinking it is - injecting BRANCH_NAME envvar via Java)
284+
@Issue("JENKINS-69731")
285+
@Test public void checkDefaultVersion_BRANCH_NAME_MBP() throws Exception {
286+
// Create a MultiBranch Pipeline job instantiated from Git
282287
// and check behaviors with BRANCH_NAME="master",
283-
// BRANCH_NAME="feature", BRANCH_NAME="bogus" and
284-
// BRANCH_NAME=""
288+
// BRANCH_NAME="feature", and BRANCH_NAME="bogus"
289+
// TODO? BRANCH_NAME=""
290+
291+
sampleRepo.init();
292+
sampleRepo.write("vars/myecho.groovy", "def call() {echo 'something special'}");
293+
sampleRepo.git("add", "vars");
294+
sampleRepo.git("commit", "--message=init");
295+
sampleRepo.git("checkout", "-b", "feature");
296+
sampleRepo.write("vars/myecho.groovy", "def call() {echo 'something very special'}");
297+
sampleRepo.git("add", "vars");
298+
sampleRepo.git("commit", "--message=init");
299+
SCMSourceRetriever scm = new SCMSourceRetriever(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true));
300+
LibraryConfiguration lc = new LibraryConfiguration("branchylib", scm);
301+
lc.setDefaultVersion("master");
302+
lc.setIncludeInChangesets(false);
303+
lc.setAllowBRANCH_NAME(true);
304+
GlobalLibraries.get().setLibraries(Collections.singletonList(lc));
305+
306+
// Inspired in part by tests like
307+
// https://github.com/jenkinsci/workflow-multibranch-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/multibranch/NoTriggerBranchPropertyWorkflowTest.java#L132
308+
sampleRepo2.init();
309+
sampleRepo2.write("Jenkinsfile", "@Library('branchylib@${BRANCH_NAME}') import myecho; myecho()");
310+
sampleRepo2.git("add", "Jenkinsfile");
311+
sampleRepo2.git("commit", "--message=init");
312+
sampleRepo2.git("branch", "feature");
313+
sampleRepo2.git("branch", "bogus");
314+
315+
WorkflowMultiBranchProject mbp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "mbp");
316+
BranchSource branchSource = new BranchSource(new GitSCMSource("source-id", sampleRepo2.toString(), "", "*", "", false));
317+
mbp.getSourcesList().add(branchSource);
318+
sampleRepo2.notifyCommit(r);
319+
320+
WorkflowJob p1 = mbp.getItem("master");
321+
WorkflowRun b1 = r.buildAndAssertSuccess(p1);
322+
r.assertLogContains("Loading library branchylib@master", b1);
323+
r.assertLogContains("something special", b1);
324+
325+
WorkflowJob p2 = mbp.getItem("feature");
326+
WorkflowRun b2 = r.buildAndAssertSuccess(p2);
327+
r.assertLogContains("Loading library branchylib@feature", b2);
328+
r.assertLogContains("something very special", b2);
329+
330+
// library branch "bogus" does not exist => fall back to default (master)
331+
WorkflowJob p3 = mbp.getItem("bogus");
332+
WorkflowRun b3 = r.buildAndAssertSuccess(p3);
333+
r.assertLogContains("Loading library branchylib@master", b3);
334+
r.assertLogContains("something special", b3);
335+
336+
// TODO: test that lc.setAllowBRANCH_NAME(false) causes
337+
// fallbacks always
338+
339+
// TODO: test lc.setAllowBRANCH_NAME_PR(true) for PR builds
285340
}
286341

287342
@Issue("JENKINS-43802")

0 commit comments

Comments
 (0)