11package hudson .plugins .git ;
22
33import java .io .File ;
4+ import java .io .StringWriter ;
45import java .time .LocalDateTime ;
56import java .time .format .DateTimeFormatter ;
67import java .util .ArrayList ;
1920import hudson .model .FreeStyleProject ;
2021import hudson .model .Run ;
2122import hudson .model .TaskListener ;
23+ import hudson .plugins .git .Branch ;
2224import hudson .plugins .git .extensions .GitSCMExtension ;
2325import hudson .plugins .git .extensions .impl .LocalBranch ;
2426
3032
3133import static org .hamcrest .Matchers .*;
3234import static org .junit .Assert .*;
35+ import static org .junit .Assume .*;
3336
3437import com .gargoylesoftware .htmlunit .html .HtmlForm ;
3538import com .gargoylesoftware .htmlunit .html .HtmlPage ;
4144import org .jvnet .hudson .test .JenkinsRule ;
4245
4346/**
44- * Test git tag action.
47+ * Test git tag action. Low value test that was created as part of
48+ * another investigation.
49+ *
50+ * Unreliable on ci.jenkins.io Windows agents. Results are not worth
51+ * sacrificing other things in order to investigate. Runs reliably on
52+ * Unix-like operating systems. Runs reliably on Mark Waite's windows
53+ * computers.
4554 *
4655 * @author Mark Waite
4756 */
@@ -71,13 +80,19 @@ public GitTagActionTest() {
7180 private static final DateTimeFormatter FORMAT = DateTimeFormatter .ofPattern ("-yyyy-MM-dd-H-m-ss.SS" );
7281 private static final String TAG_PREFIX = "test-tag-" ;
7382 private static final String TAG_SUFFIX = LocalDateTime .now ().format (FORMAT );
83+ private static final String INITIAL_COMMIT_MESSAGE = "init" + TAG_SUFFIX + "-" + random .nextInt (10000 );
84+ private static final String ADDED_COMMIT_MESSAGE_BASE = "added" + TAG_SUFFIX ;
85+ private static String sampleRepoHead = null ;
7486
7587 @ BeforeClass
7688 public static void deleteMatchingTags () throws Exception {
89+ if (isWindows ()) { // Test is unreliable on Windows, too low value to investigate further
90+ return ;
91+ }
7792 /* Remove tags from working repository that start with TAG_PREFIX and don't contain TAG_SUFFIX */
7893 GitClient gitClient = Git .with (TaskListener .NULL , new EnvVars ())
7994 .in (new File ("." ))
80- .using (random . nextBoolean () ? "git" : "jgit" ) // Use random implementation, both should work
95+ .using (chooseGitImplementation () ) // Use random implementation, both should work
8196 .getClient ();
8297 for (GitObject tag : gitClient .getTags ()) {
8398 if (tag .getName ().startsWith (TAG_PREFIX ) && !tag .getName ().contains (TAG_SUFFIX )) {
@@ -88,10 +103,13 @@ public static void deleteMatchingTags() throws Exception {
88103
89104 @ BeforeClass
90105 public static void createThreeGitTagActions () throws Exception {
106+ if (isWindows ()) { // Test is unreliable on Windows, too low value to investigate further
107+ return ;
108+ }
91109 sampleRepo .init ();
92- sampleRepo .write ("file" , "init" );
93- sampleRepo .git ("commit" , "--all" , "--message=init" );
94- String head = sampleRepo .head ();
110+ sampleRepo .write ("file" , INITIAL_COMMIT_MESSAGE );
111+ sampleRepo .git ("commit" , "--all" , "--message=" + INITIAL_COMMIT_MESSAGE );
112+ sampleRepoHead = sampleRepo .head ();
95113 List <UserRemoteConfig > remotes = new ArrayList <>();
96114 String refSpec = "+refs/heads/master:refs/remotes/origin/master" ;
97115 remotes .add (new UserRemoteConfig (sampleRepo .fileUrl (), "origin" , refSpec , "" ));
@@ -100,7 +118,7 @@ public static void createThreeGitTagActions() throws Exception {
100118 Collections .singletonList (new BranchSpec ("origin/master" )),
101119 false , Collections .<SubmoduleConfig >emptyList (),
102120 null ,
103- random . nextBoolean () ? "git" : "jgit" , // Both git implementations should work, choose randomly
121+ chooseGitImplementation () , // Both git implementations should work, choose randomly
104122 Collections .<GitSCMExtension >emptyList ());
105123 scm .getExtensions ().add (new LocalBranch ("master" ));
106124 p = r .createFreeStyleProject ();
@@ -136,6 +154,8 @@ private static String getTagComment(String message) {
136154 return getTagName (message ) + "-comment" ;
137155 }
138156
157+ private static int messageCounter = 1 ;
158+
139159 /**
140160 * Return a GitTagAction which uses 'message' in the tag name, tag value, and tag comment.
141161 * If 'message' is null, the GitTagAction is returned but tag creation is not scheduled.
@@ -146,8 +166,9 @@ private static String getTagComment(String message) {
146166 */
147167 private static GitTagAction createTagAction (String message ) throws Exception {
148168 /* Run with a tag action defined */
169+ String commitMessage = message == null ? ADDED_COMMIT_MESSAGE_BASE + "-" + messageCounter ++ : message ;
149170 sampleRepo .write ("file" , message );
150- sampleRepo .git ("commit" , "--all" , "--message=" + ( message == null ? random . nextInt () : message ) );
171+ sampleRepo .git ("commit" , "--all" , "--message=" + commitMessage );
151172 List <Branch > masterBranchList = new ArrayList <>();
152173 ObjectId tagObjectId = ObjectId .fromString (sampleRepo .head ());
153174 masterBranchList .add (new Branch ("master" , tagObjectId ));
@@ -162,12 +183,38 @@ private static GitTagAction createTagAction(String message) throws Exception {
162183 /* Assumes workspace does not move after first run */
163184 workspaceGitClient = Git .with (TaskListener .NULL , new EnvVars ())
164185 .in (workspace )
165- .using (random . nextBoolean () ? "git" : "jgit" ) // Use random implementation, both should work
186+ .using (chooseGitImplementation () ) // Use random implementation, both should work
166187 .getClient ();
167188 }
168189 /* Fail if the workspace moved */
169190 assertThat (workspace , is (workspaceGitClient .getWorkTree ()));
170191
192+ /* Fail if initial commit and subsequent commit not detected in workspace */
193+ StringWriter stringWriter = new StringWriter ();
194+ workspaceGitClient .changelog (sampleRepoHead + "^" , "HEAD" , stringWriter );
195+ assertThat (stringWriter .toString (), containsString (INITIAL_COMMIT_MESSAGE ));
196+ assertThat (stringWriter .toString (), containsString (commitMessage ));
197+
198+ /* Fail if master branch is not defined in the workspace */
199+ assertThat (workspaceGitClient .getRemoteUrl ("origin" ), is (sampleRepo .fileUrl ().replace ("file:/" , "file:///" )));
200+ Set <Branch > branches = workspaceGitClient .getBranches ();
201+ if (branches .isEmpty ()) {
202+ /* Should not be required since the LocalBranch extension was enabled */
203+ workspaceGitClient .branch ("master" );
204+ branches = workspaceGitClient .getBranches ();
205+ assertThat (branches , is (not (empty ())));
206+ }
207+ boolean foundMasterBranch = false ;
208+ String lastBranchName = null ;
209+ for (Branch branch : branches ) {
210+ lastBranchName = branch .getName ();
211+ assertThat (lastBranchName , endsWith ("master" ));
212+ if (lastBranchName .equals ("master" )) {
213+ foundMasterBranch = true ;
214+ }
215+ }
216+ assertTrue ("master branch not found, last branch name was " + lastBranchName , foundMasterBranch );
217+
171218 /* Create the GitTagAction */
172219 GitTagAction tagAction = new GitTagAction (tagRun , workspace , tagRevision );
173220
@@ -200,12 +247,13 @@ private static void waitForTagCreation(GitTagAction tagAction, String message) t
200247 backoffDelay = backoffDelay * 2 ;
201248 Thread .sleep (backoffDelay ); // Allow some time for tag creation
202249 }
203- assertThat (tagAction .getLastTagName (), is (getTagValue (message )));
204250 assertThat (tagAction .getLastTagException (), is (nullValue ()));
251+ assertThat (tagAction .getLastTagName (), is (getTagValue (message )));
205252 }
206253
207254 @ Test
208255 public void testDoPost () throws Exception {
256+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
209257 JenkinsRule .WebClient browser = r .createWebClient ();
210258
211259 // Don't need all cases until at least one case works fully
@@ -241,44 +289,52 @@ public void testDoPost() throws Exception {
241289
242290 @ Test
243291 public void testGetDescriptor () {
292+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
244293 Descriptor <GitTagAction > descriptor = noTagAction .getDescriptor ();
245294 assertThat (descriptor .getDisplayName (), is ("Tag" ));
246295 }
247296
248297 // @Test
249298 public void testIsTagged () {
299+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
250300 assertTrue (tagTwoAction .isTagged ());
251301 }
252302
253303 @ Test
254304 public void testIsNotTagged () {
305+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
255306 assertFalse (noTagAction .isTagged ());
256307 }
257308
258309 @ Test
259310 public void testGetDisplayNameNoTagAction () {
311+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
260312 assertThat (noTagAction .getDisplayName (), is ("No Tags" ));
261313 }
262314
263315 // Not working yet
264316 // @Test
265317 public void testGetDisplayNameOneTagAction () {
318+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
266319 assertThat (tagOneAction .getDisplayName (), is ("One Tag" ));
267320 }
268321
269322 // Not working yet
270323 // @Test
271324 public void testGetDisplayNameTwoTagAction () {
325+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
272326 assertThat (tagTwoAction .getDisplayName (), is ("Multiple Tags" ));
273327 }
274328
275329 @ Test
276330 public void testGetIconFileName () {
331+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
277332 assertThat (noTagAction .getIconFileName (), is ("save.gif" ));
278333 }
279334
280335 @ Test
281336 public void testGetTagsNoTagAction () {
337+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
282338 Collection <List <String >> valueList = noTagAction .getTags ().values ();
283339 for (List <String > value : valueList ) {
284340 assertThat (value , is (empty ()));
@@ -287,6 +343,7 @@ public void testGetTagsNoTagAction() {
287343
288344 @ Test
289345 public void testGetTagsOneTagAction () {
346+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
290347 Collection <List <String >> valueList = tagOneAction .getTags ().values ();
291348 for (List <String > value : valueList ) {
292349 assertThat (value , is (empty ()));
@@ -295,6 +352,7 @@ public void testGetTagsOneTagAction() {
295352
296353 @ Test
297354 public void testGetTagsTwoTagAction () {
355+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
298356 Collection <List <String >> valueList = tagTwoAction .getTags ().values ();
299357 for (List <String > value : valueList ) {
300358 assertThat (value , is (empty ()));
@@ -303,17 +361,32 @@ public void testGetTagsTwoTagAction() {
303361
304362 @ Test
305363 public void testGetTagInfo () {
364+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
306365 assertThat (noTagAction .getTagInfo (), is (empty ()));
307366 }
308367
309368 @ Test
310369 public void testGetTooltipNoTagAction () {
370+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
311371 assertThat (noTagAction .getTooltip (), is (nullValue ()));
312372 }
313373
314374 @ Test
315375 public void testGetPermission () {
376+ assumeTrue (!isWindows ()); // Test is unreliable on Windows, too low value to investigate further
316377 assertThat (noTagAction .getPermission (), is (GitSCM .TAG ));
317378 assertThat (tagOneAction .getPermission (), is (GitSCM .TAG ));
318379 }
380+
381+ private static String chooseGitImplementation () {
382+ return random .nextBoolean () ? "git" : "jgit" ;
383+ }
384+
385+ /**
386+ * inline ${@link hudson.Functions#isWindows()} to prevent a transient
387+ * remote classloader issue
388+ */
389+ private static boolean isWindows () {
390+ return File .pathSeparatorChar == ';' ;
391+ }
319392}
0 commit comments