Skip to content

Commit c81ee58

Browse files
committed
LibraryConfigurationTest.java: add tests for LibraryConfiguration.defaultedVersion() for JENKINS-69731 related behaviors
1 parent 52e0599 commit c81ee58

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
import java.util.Collections;
2828

29+
import hudson.AbortException;
2930
import hudson.plugins.git.GitSCM;
3031
import org.hamcrest.Matchers;
3132
import org.junit.Test;
33+
import org.junit.Assert;
3234
import static org.junit.Assert.*;
3335
import org.junit.Rule;
3436
import org.jvnet.hudson.test.Issue;
@@ -92,6 +94,105 @@ public class LibraryConfigurationTest {
9294
assertNull(cfg.getDefaultVersion());
9395
}
9496

97+
@Issue("JENKINS-69731")
98+
@Test public void nullPresentDefaultedVersion() {
99+
String libraryName = "valid-name";
100+
String defaultVersion = "master";
101+
102+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
103+
cfg.setDefaultVersion(defaultVersion);
104+
105+
assertEquals("master", cfg.getDefaultVersion());
106+
try {
107+
assertEquals("master", cfg.defaultedVersion(null));
108+
} catch(AbortException ae) {
109+
Assert.fail("LibraryConfiguration.defaultedVersion() threw an AbortException when it was not expected: " + ae.getMessage());
110+
}
111+
}
112+
113+
@Issue("JENKINS-69731")
114+
@Test public void nullAbsentDefaultedVersion() {
115+
String libraryName = "valid-name";
116+
117+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
118+
119+
assertEquals(null, cfg.getDefaultVersion());
120+
assertThrows(AbortException.class, () -> cfg.defaultedVersion(null));
121+
}
122+
123+
@Issue("JENKINS-69731")
124+
@Test public void forbiddenOverrideDefaultedVersion() {
125+
String libraryName = "valid-name";
126+
127+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
128+
cfg.setAllowVersionOverride(false);
129+
130+
assertEquals(false, cfg.isAllowVersionOverride());
131+
assertThrows(AbortException.class, () -> cfg.defaultedVersion("branchname"));
132+
}
133+
134+
@Issue("JENKINS-69731")
135+
@Test public void allowedOverrideDefaultedVersion() {
136+
String libraryName = "valid-name";
137+
138+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
139+
cfg.setAllowVersionOverride(true);
140+
141+
assertEquals(true, cfg.isAllowVersionOverride());
142+
try {
143+
assertEquals("branchname", cfg.defaultedVersion("branchname"));
144+
} catch(AbortException ae) {
145+
Assert.fail("LibraryConfiguration.defaultedVersion() threw an AbortException when it was not expected: " + ae.getMessage());
146+
}
147+
}
148+
149+
@Issue("JENKINS-69731")
150+
@Test public void notAllowedOverrideDefaultedVersionWhenBRANCH_NAME() {
151+
String libraryName = "valid-name";
152+
153+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
154+
cfg.setAllowVersionOverride(true);
155+
cfg.setAllowBRANCH_NAME(false);
156+
157+
assertEquals(true, cfg.isAllowVersionOverride());
158+
assertEquals(false, cfg.isAllowBRANCH_NAME());
159+
assertThrows(AbortException.class, () -> cfg.defaultedVersion("${BRANCH_NAME}"));
160+
/* This SHOULD NOT return a version string that literally remains '${BRANCH_NAME}'! */
161+
}
162+
163+
@Issue("JENKINS-69731")
164+
@Test public void allowedBRANCH_NAMEnoRunPresentDefaultedVersion() {
165+
String libraryName = "valid-name";
166+
String defaultVersion = "master";
167+
168+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
169+
cfg.setDefaultVersion(defaultVersion);
170+
cfg.setAllowBRANCH_NAME(true);
171+
172+
assertEquals(true, cfg.isAllowBRANCH_NAME());
173+
try {
174+
assertEquals("master", cfg.defaultedVersion("${BRANCH_NAME}", null, null));
175+
} catch(AbortException ae) {
176+
Assert.fail("LibraryConfiguration.defaultedVersion() threw an AbortException when it was not expected: " + ae.getMessage());
177+
}
178+
}
179+
180+
@Issue("JENKINS-69731")
181+
@Test public void allowedBRANCH_NAMEnoRunAbsentDefaultedVersion() {
182+
String libraryName = "valid-name";
183+
184+
LibraryConfiguration cfg = new LibraryConfiguration(libraryName, new SCMRetriever(new GitSCM("https://phony.jenkins.io/bar.git")));
185+
cfg.setAllowBRANCH_NAME(true);
186+
187+
assertEquals(true, cfg.isAllowBRANCH_NAME());
188+
assertThrows(AbortException.class, () -> cfg.defaultedVersion("${BRANCH_NAME}", null, null));
189+
}
95190

191+
/* Note: further tests for JENKINS-69731 behaviors with allowBRANCH_NAME
192+
* would rely on having a Run with or without a BRANCH_NAME envvar, and
193+
* a TaskListener, and a (mock?) LibraryRetriever that would confirm or
194+
* deny existence of a requested "version" (e.g. Git branch) of the lib.
195+
* For examples, see e.g. SCMSourceRetrieverTest codebase.
196+
*/
96197

97198
}

0 commit comments

Comments
 (0)