|
26 | 26 |
|
27 | 27 | import java.util.Collections;
|
28 | 28 |
|
| 29 | +import hudson.AbortException; |
29 | 30 | import hudson.plugins.git.GitSCM;
|
30 | 31 | import org.hamcrest.Matchers;
|
31 | 32 | import org.junit.Test;
|
| 33 | +import org.junit.Assert; |
32 | 34 | import static org.junit.Assert.*;
|
33 | 35 | import org.junit.Rule;
|
34 | 36 | import org.jvnet.hudson.test.Issue;
|
@@ -92,6 +94,105 @@ public class LibraryConfigurationTest {
|
92 | 94 | assertNull(cfg.getDefaultVersion());
|
93 | 95 | }
|
94 | 96 |
|
| 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 | + } |
95 | 190 |
|
| 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 | + */ |
96 | 197 |
|
97 | 198 | }
|
0 commit comments