Skip to content

Commit b9ff400

Browse files
committed
SCMSourceRetrieverTest: add checkDefaultVersion_BRANCH_NAME_MBP() [JENKINS-69731]
1 parent b8c35fb commit b9ff400

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

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

Lines changed: 56 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")
@@ -269,12 +272,61 @@ public class SCMSourceRetrieverTest {
269272
r.assertLogContains("ERROR: No version bogus found for library branchylib", b4);
270273
r.assertLogContains("org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:", b4);
271274
r.assertLogContains("WorkflowScript: Loading libraries failed", b4);
275+
}
272276

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

280332
@Issue("JENKINS-43802")

0 commit comments

Comments
 (0)