@@ -218,6 +218,54 @@ public class SCMSourceRetrieverTest {
218218 assertThat ("foo\\ ..\\ bar" , matchesPattern (PROHIBITED_DOUBLE_DOT ));
219219 }
220220
221+ @ Issue ("JENKINS-69731" )
222+ @ Test public void checkDefaultVersion () throws Exception {
223+ sampleRepo .init ();
224+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something special'}" );
225+ sampleRepo .git ("add" , "vars" );
226+ sampleRepo .git ("commit" , "--message=init" );
227+ sampleRepo .git ("checkout" , "-b" , "feature" );
228+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something very special'}" );
229+ sampleRepo .git ("add" , "vars" );
230+ sampleRepo .git ("commit" , "--message=init" );
231+ SCMSourceRetriever scm = new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true ));
232+ LibraryConfiguration lc = new LibraryConfiguration ("branchylib" , scm );
233+ lc .setDefaultVersion ("master" );
234+ lc .setIncludeInChangesets (false );
235+ lc .setAllowBRANCH_NAME (true );
236+ GlobalLibraries .get ().setLibraries (Collections .singletonList (lc ));
237+
238+ // Basename "libname" notation => use specified default branch
239+ WorkflowJob p0 = r .jenkins .createProject (WorkflowJob .class , "p0" );
240+ p0 .setDefinition (new CpsFlowDefinition ("@Library('branchylib') import myecho; myecho()" , true ));
241+ WorkflowRun b0 = r .buildAndAssertSuccess (p0 );
242+ r .assertLogContains ("something special" , b0 );
243+
244+ // Use specified branch
245+ WorkflowJob p1 = r .jenkins .createProject (WorkflowJob .class , "p1" );
246+ p1 .setDefinition (new CpsFlowDefinition ("@Library('branchylib@master') import myecho; myecho()" , true ));
247+ WorkflowRun b1 = r .buildAndAssertSuccess (p1 );
248+ r .assertLogContains ("something special" , b1 );
249+
250+ // Use another specified branch
251+ WorkflowJob p2 = r .jenkins .createProject (WorkflowJob .class , "p2" );
252+ p2 .setDefinition (new CpsFlowDefinition ("@Library('branchylib@feature') import myecho; myecho()" , true ));
253+ WorkflowRun b2 = r .buildAndAssertSuccess (p2 );
254+ r .assertLogContains ("something very special" , b2 );
255+
256+ // Branch context for job not set - fall back to default
257+ WorkflowJob p3 = r .jenkins .createProject (WorkflowJob .class , "p3" );
258+ p3 .setDefinition (new CpsFlowDefinition ("@Library('branchylib@${BRANCH_NAME}') import myecho; myecho()" , true ));
259+ WorkflowRun b3 = r .buildAndAssertSuccess (p3 );
260+ r .assertLogContains ("something special" , b3 );
261+
262+ // TODO: create a job instantiated from Git (or fooled into
263+ // thinking it is - injecting BRANCH_NAME envvar via Java)
264+ // and check behaviors with BRANCH_NAME="master",
265+ // BRANCH_NAME="feature", BRANCH_NAME="bogus" and
266+ // BRANCH_NAME=""
267+ }
268+
221269 @ Issue ("JENKINS-43802" )
222270 @ Test public void owner () throws Exception {
223271 GlobalLibraries .get ().setLibraries (Collections .singletonList (
0 commit comments