@@ -333,12 +333,121 @@ public class SCMSourceRetrieverTest {
333333 r .assertLogContains ("Loading library branchylib@master" , b3 );
334334 r .assertLogContains ("something special" , b3 );
335335
336- // TODO: test that lc.setAllowBRANCH_NAME(false) causes
337- // fallbacks always
338-
339336 // TODO: test lc.setAllowBRANCH_NAME_PR(true) for PR builds
340337 }
341338
339+ @ Issue ("JENKINS-69731" )
340+ @ Test public void checkDefaultVersion_MBP () throws Exception {
341+ // Test that lc.setAllowBRANCH_NAME(false) does not
342+ // preclude fixed branch names (they should work),
343+ // like @Library('branchylib@master')
344+
345+ sampleRepo .init ();
346+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something special'}" );
347+ sampleRepo .git ("add" , "vars" );
348+ sampleRepo .git ("commit" , "--message=init" );
349+ sampleRepo .git ("checkout" , "-b" , "feature" );
350+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something very special'}" );
351+ sampleRepo .git ("add" , "vars" );
352+ sampleRepo .git ("commit" , "--message=init" );
353+ SCMSourceRetriever scm = new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true ));
354+ LibraryConfiguration lc = new LibraryConfiguration ("branchylib" , scm );
355+ lc .setDefaultVersion ("master" );
356+ lc .setIncludeInChangesets (false );
357+ lc .setAllowVersionOverride (true );
358+ lc .setAllowBRANCH_NAME (false );
359+ GlobalLibraries .get ().setLibraries (Collections .singletonList (lc ));
360+
361+ // Inspired in part by tests like
362+ // https://github.com/jenkinsci/workflow-multibranch-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/multibranch/NoTriggerBranchPropertyWorkflowTest.java#L132
363+ sampleRepo2 .init ();
364+ sampleRepo2 .write ("Jenkinsfile" , "@Library('branchylib@master') import myecho; myecho()" );
365+ sampleRepo2 .git ("add" , "Jenkinsfile" );
366+ sampleRepo2 .git ("commit" , "--message=master" );
367+ sampleRepo2 .git ("checkout" , "-b" , "feature" );
368+ sampleRepo2 .write ("Jenkinsfile" , "@Library('branchylib@feature') import myecho; myecho()" );
369+ sampleRepo2 .git ("add" , "Jenkinsfile" );
370+ sampleRepo2 .git ("commit" , "--message=feature" );
371+ sampleRepo2 .git ("checkout" , "-b" , "bogus" );
372+ sampleRepo2 .write ("Jenkinsfile" , "@Library('branchylib@bogus') import myecho; myecho()" );
373+ sampleRepo2 .git ("add" , "Jenkinsfile" );
374+ sampleRepo2 .git ("commit" , "--message=bogus" );
375+
376+ WorkflowMultiBranchProject mbp = r .jenkins .createProject (WorkflowMultiBranchProject .class , "mbp" );
377+ BranchSource branchSource = new BranchSource (new GitSCMSource ("source-id" , sampleRepo2 .toString (), "" , "*" , "" , false ));
378+ mbp .getSourcesList ().add (branchSource );
379+ sampleRepo2 .notifyCommit (r );
380+
381+ WorkflowJob p1 = mbp .getItem ("master" );
382+ WorkflowRun b1 = r .buildAndAssertSuccess (p1 );
383+ r .assertLogContains ("Loading library branchylib@master" , b1 );
384+ r .assertLogContains ("something special" , b1 );
385+
386+ WorkflowJob p2 = mbp .getItem ("feature" );
387+ WorkflowRun b2 = r .buildAndAssertSuccess (p2 );
388+ r .assertLogContains ("Loading library branchylib@feature" , b2 );
389+ r .assertLogContains ("something very special" , b2 );
390+
391+ WorkflowJob p3 = mbp .getItem ("bogus" );
392+ WorkflowRun b3 = r .buildAndAssertStatus (Result .FAILURE , p3 );
393+ r .assertLogContains ("ERROR: Could not resolve bogus" , b3 );
394+ r .assertLogContains ("ambiguous argument 'bogus^{commit}': unknown revision or path not in the working tree" , b3 );
395+ r .assertLogContains ("ERROR: No version bogus found for library branchylib" , b3 );
396+ r .assertLogContains ("WorkflowScript: Loading libraries failed" , b3 );
397+ }
398+
399+ @ Issue ("JENKINS-69731" )
400+ @ Test public void checkDefaultVersion_BRANCH_NAME_notAllowed () throws Exception {
401+ // Test that lc.setAllowBRANCH_NAME(false) causes
402+ // @Library('libname@${BRANCH_NAME}') to always
403+ // fail, while fixed branch names should work.
404+
405+ sampleRepo .init ();
406+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something special'}" );
407+ sampleRepo .git ("add" , "vars" );
408+ sampleRepo .git ("commit" , "--message=init" );
409+ sampleRepo .git ("checkout" , "-b" , "feature" );
410+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something very special'}" );
411+ sampleRepo .git ("add" , "vars" );
412+ sampleRepo .git ("commit" , "--message=init" );
413+ SCMSourceRetriever scm = new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true ));
414+ LibraryConfiguration lc = new LibraryConfiguration ("branchylib" , scm );
415+ lc .setDefaultVersion ("master" );
416+ lc .setIncludeInChangesets (false );
417+ lc .setAllowVersionOverride (true );
418+ lc .setAllowBRANCH_NAME (false );
419+ GlobalLibraries .get ().setLibraries (Collections .singletonList (lc ));
420+
421+ // Inspired in part by tests like
422+ // https://github.com/jenkinsci/workflow-multibranch-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/multibranch/NoTriggerBranchPropertyWorkflowTest.java#L132
423+ sampleRepo2 .init ();
424+ sampleRepo2 .write ("Jenkinsfile" , "@Library('branchylib@${BRANCH_NAME}') import myecho; myecho()" );
425+ sampleRepo2 .git ("add" , "Jenkinsfile" );
426+ sampleRepo2 .git ("commit" , "--message=init" );
427+ sampleRepo2 .git ("branch" , "feature" );
428+ sampleRepo2 .git ("branch" , "bogus" );
429+
430+ WorkflowMultiBranchProject mbp = r .jenkins .createProject (WorkflowMultiBranchProject .class , "mbp" );
431+ BranchSource branchSource = new BranchSource (new GitSCMSource ("source-id" , sampleRepo2 .toString (), "" , "*" , "" , false ));
432+ mbp .getSourcesList ().add (branchSource );
433+ sampleRepo2 .notifyCommit (r );
434+
435+ WorkflowJob p1 = mbp .getItem ("master" );
436+ WorkflowRun b1 = r .buildAndAssertStatus (Result .FAILURE , p1 );
437+ r .assertLogContains ("ERROR: Version override not permitted for library branchylib" , b1 );
438+ r .assertLogContains ("WorkflowScript: Loading libraries failed" , b1 );
439+
440+ WorkflowJob p2 = mbp .getItem ("feature" );
441+ WorkflowRun b2 = r .buildAndAssertStatus (Result .FAILURE , p2 );
442+ r .assertLogContains ("ERROR: Version override not permitted for library branchylib" , b2 );
443+ r .assertLogContains ("WorkflowScript: Loading libraries failed" , b2 );
444+
445+ WorkflowJob p3 = mbp .getItem ("bogus" );
446+ WorkflowRun b3 = r .buildAndAssertStatus (Result .FAILURE , p3 );
447+ r .assertLogContains ("ERROR: Version override not permitted for library branchylib" , b3 );
448+ r .assertLogContains ("WorkflowScript: Loading libraries failed" , b3 );
449+ }
450+
342451 @ Issue ("JENKINS-43802" )
343452 @ Test public void owner () throws Exception {
344453 GlobalLibraries .get ().setLibraries (Collections .singletonList (
0 commit comments