Skip to content

Commit 7a92b40

Browse files
committed
SCMSourceRetrieverTest: add checkDefaultVersion_BRANCH_NAME_MBP() [JENKINS-69731]
1 parent 7d0a00d commit 7a92b40

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")
@@ -273,12 +276,64 @@ public class SCMSourceRetrieverTest {
273276
r.assertLogContains("ERROR: No version bogus found for library branchylib", b4);
274277
r.assertLogContains("org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:", b4);
275278
r.assertLogContains("WorkflowScript: Loading libraries failed", b4);
279+
}
276280

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

284339
@Issue("JENKINS-43802")

0 commit comments

Comments
 (0)