@@ -61,31 +61,57 @@ public class LibraryCachingConfigurationTest {
6161 private static int NO_REFRESH_TIME_MINUTES = 0 ;
6262
6363 private static String NULL_EXCLUDED_VERSION = null ;
64+ private static String NULL_INCLUDED_VERSION = null ;
65+
6466 private static String ONE_EXCLUDED_VERSION = "branch-1" ;
6567
68+ private static String ONE_INCLUDED_VERSION = "branch-1i" ;
69+
70+
6671 private static String MULTIPLE_EXCLUDED_VERSIONS_1 = "main" ;
72+
73+ private static String MULTIPLE_INCLUDED_VERSIONS_1 = "master" ;
74+
6775 private static String MULTIPLE_EXCLUDED_VERSIONS_2 = "branch-2" ;
76+
77+ private static String MULTIPLE_INCLUDED_VERSIONS_2 = "branch-2i" ;
78+
6879 private static String MULTIPLE_EXCLUDED_VERSIONS_3 = "branch-3" ;
80+ private static String MULTIPLE_INCLUDED_VERSIONS_3 = "branch-3i" ;
81+
6982
7083 private static String SUBSTRING_EXCLUDED_VERSIONS_1 = "feature/test-substring-exclude" ;
84+
85+ private static String SUBSTRING_INCLUDED_VERSIONS_1 = "feature_include/test-substring" ;
86+
7187 private static String SUBSTRING_EXCLUDED_VERSIONS_2 = "test-other-substring-exclude" ;
88+ private static String SUBSTRING_INCLUDED_VERSIONS_2 = "test-other-substring-include" ;
89+
7290
7391 private static String MULTIPLE_EXCLUDED_VERSIONS =
7492 MULTIPLE_EXCLUDED_VERSIONS_1 + " " +
7593 MULTIPLE_EXCLUDED_VERSIONS_2 + " " +
7694 MULTIPLE_EXCLUDED_VERSIONS_3 ;
7795
96+ private static String MULTIPLE_INCLUDED_VERSIONS =
97+ MULTIPLE_INCLUDED_VERSIONS_1 + " " +
98+ MULTIPLE_INCLUDED_VERSIONS_2 + " " +
99+ MULTIPLE_INCLUDED_VERSIONS_3 ;
100+
78101 private static String SUBSTRING_EXCLUDED_VERSIONS =
79102 "feature/ other-substring" ;
80103
104+ private static String SUBSTRING_INCLUDED_VERSIONS =
105+ "feature_include/ other-substring" ;
106+
81107 private static String NEVER_EXCLUDED_VERSION = "never-excluded-version" ;
82108
83109 @ Before
84110 public void createCachingConfiguration () {
85- nullVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , NULL_EXCLUDED_VERSION );
86- oneVersionConfig = new LibraryCachingConfiguration (NO_REFRESH_TIME_MINUTES , ONE_EXCLUDED_VERSION );
87- multiVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , MULTIPLE_EXCLUDED_VERSIONS );
88- substringVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , SUBSTRING_EXCLUDED_VERSIONS );
111+ nullVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , NULL_EXCLUDED_VERSION , NULL_INCLUDED_VERSION );
112+ oneVersionConfig = new LibraryCachingConfiguration (NO_REFRESH_TIME_MINUTES , ONE_EXCLUDED_VERSION , ONE_INCLUDED_VERSION );
113+ multiVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , MULTIPLE_EXCLUDED_VERSIONS , MULTIPLE_INCLUDED_VERSIONS );
114+ substringVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , SUBSTRING_EXCLUDED_VERSIONS , SUBSTRING_INCLUDED_VERSIONS );
89115 }
90116
91117 @ Issue ("JENKINS-66045" ) // NPE getting excluded versions
@@ -125,6 +151,15 @@ public void getExcludedVersionsStr() {
125151 assertThat (substringVersionConfig .getExcludedVersionsStr (), is (SUBSTRING_EXCLUDED_VERSIONS ));
126152 }
127153
154+ @ Test
155+ @ WithoutJenkins
156+ public void getIncludedVersionsStr () {
157+ assertThat (nullVersionConfig .getIncludedVersionsStr (), is (NULL_INCLUDED_VERSION ));
158+ assertThat (oneVersionConfig .getIncludedVersionsStr (), is (ONE_INCLUDED_VERSION ));
159+ assertThat (multiVersionConfig .getIncludedVersionsStr (), is (MULTIPLE_INCLUDED_VERSIONS ));
160+ assertThat (substringVersionConfig .getIncludedVersionsStr (), is (SUBSTRING_INCLUDED_VERSIONS ));
161+ }
162+
128163 @ Test
129164 @ WithoutJenkins
130165 public void isExcluded () {
@@ -155,6 +190,24 @@ public void isExcluded() {
155190 assertFalse (substringVersionConfig .isExcluded (null ));
156191 }
157192
193+ @ Issue ("JENKINS-69135" ) //"Versions to include" feature for caching
194+ @ Test
195+ @ WithoutJenkins
196+ public void isIncluded () {
197+ assertFalse (nullVersionConfig .isIncluded (NULL_INCLUDED_VERSION ));
198+ assertFalse (nullVersionConfig .isIncluded ("" ));
199+
200+ assertTrue (oneVersionConfig .isIncluded (ONE_INCLUDED_VERSION ));
201+
202+ assertTrue (multiVersionConfig .isIncluded (MULTIPLE_INCLUDED_VERSIONS_1 ));
203+ assertTrue (multiVersionConfig .isIncluded (MULTIPLE_INCLUDED_VERSIONS_2 ));
204+ assertTrue (multiVersionConfig .isIncluded (MULTIPLE_INCLUDED_VERSIONS_3 ));
205+
206+ assertTrue (substringVersionConfig .isIncluded (SUBSTRING_INCLUDED_VERSIONS_1 ));
207+ assertTrue (substringVersionConfig .isIncluded (SUBSTRING_INCLUDED_VERSIONS_2 ));
208+
209+ }
210+
158211 @ Test
159212 public void clearCache () throws Exception {
160213 sampleRepo .init ();
@@ -182,4 +235,72 @@ public void clearCache() throws Exception {
182235 assertThat (new File (cache .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
183236 }
184237
238+ //Test similar substrings in "Versions to include" & "Versions to exclude"
239+ //Exclusion takes precedence
240+ @ Issue ("JENKINS-69135" ) //"Versions to include" feature for caching
241+ @ Test
242+ public void clearCacheConflict () throws Exception {
243+ sampleRepo .init ();
244+ sampleRepo .write ("vars/foo.groovy" , "def call() { echo 'foo' }" );
245+ sampleRepo .git ("add" , "vars" );
246+ sampleRepo .git ("commit" , "--message=init" );
247+ LibraryConfiguration config = new LibraryConfiguration ("library" ,
248+ new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true )));
249+ config .setDefaultVersion ("master" );
250+ config .setImplicit (true );
251+ // Same version specified in both include and exclude version
252+ //Exclude takes precedence
253+ config .setCachingConfiguration (new LibraryCachingConfiguration (30 , "master" , "master" ));
254+ GlobalLibraries .get ().getLibraries ().add (config );
255+ // Run build and check that cache gets created.
256+ WorkflowJob p = r .createProject (WorkflowJob .class );
257+ p .setDefinition (new CpsFlowDefinition ("foo()" , true ));
258+ WorkflowRun b = r .buildAndAssertSuccess (p );
259+ LibrariesAction action = b .getAction (LibrariesAction .class );
260+ LibraryRecord record = action .getLibraries ().get (0 );
261+ FilePath cache = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().child (record .getDirectoryName ());
262+ // Cache should not get created since the version is included in "Versions to exclude"
263+ assertThat (new File (cache .getRemote ()), not (anExistingDirectory ()));
264+ assertThat (new File (cache .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
265+ }
266+
267+ @ Issue ("JENKINS-69135" ) //"Versions to include" feature for caching
268+ @ Test
269+ public void clearCacheIncludedVersion () throws Exception {
270+ sampleRepo .init ();
271+ sampleRepo .write ("vars/foo.groovy" , "def call() { echo 'foo' }" );
272+ sampleRepo .git ("add" , "vars" );
273+ sampleRepo .git ("commit" , "--message=init" );
274+ sampleRepo .git ("branch" , "test/include" );
275+ LibraryConfiguration config = new LibraryConfiguration ("library" ,
276+ new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true )));
277+ config .setDefaultVersion ("master" );
278+ config .setAllowVersionOverride (true );
279+ config .setImplicit (false );
280+ config .setCachingConfiguration (new LibraryCachingConfiguration (30 , "" , "test/include" ));
281+ GlobalLibraries .get ().getLibraries ().add (config );
282+ // Run build and check that cache gets created.
283+ WorkflowJob p = r .createProject (WorkflowJob .class );
284+ p .setDefinition (new CpsFlowDefinition ("library identifier: 'library', changelog:false\n \n foo()" , true ));
285+ WorkflowRun b = r .buildAndAssertSuccess (p );
286+ WorkflowJob p2 = r .createProject (WorkflowJob .class );
287+ p2 .setDefinition (new CpsFlowDefinition ("library identifier: 'library@test/include', changelog:false\n \n foo()" , true ));
288+ WorkflowRun b2 = r .buildAndAssertSuccess (p2 );
289+ LibrariesAction action = b .getAction (LibrariesAction .class );
290+ LibraryRecord record = action .getLibraries ().get (0 );
291+ LibrariesAction action2 = b2 .getAction (LibrariesAction .class );
292+ LibraryRecord record2 = action2 .getLibraries ().get (0 );
293+ FilePath cache = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().child (record .getDirectoryName ());
294+ FilePath cache2 = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().child (record2 .getDirectoryName ());
295+ assertThat (new File (cache .getRemote ()), not (anExistingDirectory ()));
296+ assertThat (new File (cache .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
297+ assertThat (new File (cache2 .getRemote ()), anExistingDirectory ());
298+ assertThat (new File (cache2 .withSuffix ("-name.txt" ).getRemote ()), anExistingFile ());
299+ // Clears cache for the entire library, until the "Delete specific cache version" feature in merged
300+ // Clear the cache. TODO: Would be more realistic to set up security and use WebClient.
301+ ExtensionList .lookupSingleton (LibraryCachingConfiguration .DescriptorImpl .class ).doClearCache ("library" , false );
302+ assertThat (new File (cache2 .getRemote ()), not (anExistingDirectory ()));
303+ assertThat (new File (cache2 .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
304+ }
305+
185306}
0 commit comments