|
43 | 43 | import java.util.Collections;
|
44 | 44 | import java.util.Iterator;
|
45 | 45 | import java.util.List;
|
| 46 | +import jenkins.branch.BranchSource; |
46 | 47 | import jenkins.plugins.git.GitSCMSource;
|
47 | 48 | import jenkins.plugins.git.GitSampleRepoRule;
|
48 | 49 | import jenkins.scm.api.SCMHead;
|
|
58 | 59 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
|
59 | 60 | import org.jenkinsci.plugins.workflow.job.WorkflowJob;
|
60 | 61 | import org.jenkinsci.plugins.workflow.job.WorkflowRun;
|
| 62 | +import org.jenkinsci.plugins.workflow.multibranch.*; |
61 | 63 |
|
62 | 64 | import static hudson.ExtensionList.lookupSingleton;
|
63 | 65 | import static org.hamcrest.Matchers.contains;
|
@@ -87,6 +89,7 @@ public class SCMSourceRetrieverTest {
|
87 | 89 | @ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
|
88 | 90 | @Rule public JenkinsRule r = new JenkinsRule();
|
89 | 91 | @Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
|
| 92 | + @Rule public GitSampleRepoRule sampleRepo2 = new GitSampleRepoRule(); |
90 | 93 | @Rule public SubversionSampleRepoRule sampleRepoSvn = new SubversionSampleRepoRule();
|
91 | 94 |
|
92 | 95 | @Issue("JENKINS-40408")
|
@@ -273,12 +276,64 @@ public class SCMSourceRetrieverTest {
|
273 | 276 | r.assertLogContains("ERROR: No version bogus found for library branchylib", b4);
|
274 | 277 | r.assertLogContains("org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:", b4);
|
275 | 278 | r.assertLogContains("WorkflowScript: Loading libraries failed", b4);
|
| 279 | + } |
276 | 280 |
|
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 |
279 | 284 | // 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 |
282 | 337 | }
|
283 | 338 |
|
284 | 339 | @Issue("JENKINS-43802")
|
|
0 commit comments