View Javadoc
1   /*
2    * Copyright (C) 2013, Matthias Sohn <matthias.sohn@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.pgm;
11  
12  import static org.junit.Assert.assertArrayEquals;
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import java.util.Arrays;
19  
20  import org.eclipse.jgit.api.Git;
21  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
22  import org.eclipse.jgit.pgm.internal.CLIText;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class DescribeTest extends CLIRepositoryTestCase {
27  
28  	private Git git;
29  
30  	@Override
31  	@Before
32  	public void setUp() throws Exception {
33  		super.setUp();
34  		git = new Git(db);
35  	}
36  
37  	private void initialCommitAndTag() throws Exception {
38  		git.commit().setMessage("initial commit").call();
39  		git.tag().setName("v1.0").call();
40  	}
41  
42  	private void secondCommit() throws Exception {
43  		writeTrashFile("greeting", "Hello, world!");
44  		git.add().addFilepattern("greeting").call();
45  		git.commit().setMessage("2nd commit").call();
46  	}
47  
48  	@Test
49  	public void testNoHead() throws Exception {
50  		assertEquals(CLIText.fatalError(CLIText.get().noNamesFound),
51  				toString(executeUnchecked("git describe")));
52  	}
53  
54  	@Test
55  	public void testHeadNoTag() throws Exception {
56  		git.commit().setMessage("initial commit").call();
57  		assertEquals(CLIText.fatalError(CLIText.get().noNamesFound),
58  				toString(executeUnchecked("git describe")));
59  	}
60  
61  	@Test
62  	public void testDescribeTag() throws Exception {
63  		initialCommitAndTag();
64  		assertArrayEquals(new String[] { "v1.0", "" },
65  				execute("git describe HEAD"));
66  	}
67  
68  	@Test
69  	public void testDescribeCommit() throws Exception {
70  		initialCommitAndTag();
71  		secondCommit();
72  		assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
73  				execute("git describe"));
74  	}
75  
76  	@Test
77  	public void testDescribeTagLong() throws Exception {
78  		initialCommitAndTag();
79  		assertArrayEquals(new String[] { "v1.0-0-g6fd41be", "" },
80  				execute("git describe --long HEAD"));
81  	}
82  
83  	@Test
84  	public void testDescribeCommitMatch() throws Exception {
85  		initialCommitAndTag();
86  		secondCommit();
87  		assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
88  				execute("git describe --match v1.*"));
89  	}
90  
91  	@Test
92  	public void testDescribeCommitMatch2() throws Exception {
93  		initialCommitAndTag();
94  		secondCommit();
95  		git.tag().setName("v2.0").call();
96  		assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
97  				execute("git describe --match v1.*"));
98  	}
99  
100 	@Test
101 	public void testDescribeCommitMultiMatch() throws Exception {
102 		initialCommitAndTag();
103 		secondCommit();
104 		git.tag().setName("v2.0.0").call();
105 		git.tag().setName("v2.1.1").call();
106 		assertArrayEquals("git yields v2.0.0", new String[] { "v2.0.0", "" },
107 				execute("git describe --match v2.0* --match v2.1.*"));
108 	}
109 
110 	@Test
111 	public void testDescribeCommitNoMatch() throws Exception {
112 		initialCommitAndTag();
113 		writeTrashFile("greeting", "Hello, world!");
114 		secondCommit();
115 		try {
116 			execute("git describe --match 1.*");
117 			fail("git describe should not find any tag matching 1.*");
118 		} catch (Die e) {
119 			assertEquals("No names found, cannot describe anything.",
120 					e.getMessage());
121 		}
122 	}
123 
124 	@Test
125 	public void testHelpArgumentBeforeUnknown() throws Exception {
126 		String[] output = execute("git describe -h -XYZ");
127 		String all = Arrays.toString(output);
128 		assertTrue("Unexpected help output: " + all,
129 				all.contains("jgit describe"));
130 		assertFalse("Unexpected help output: " + all, all.contains("fatal"));
131 	}
132 
133 	@Test
134 	public void testHelpArgumentAfterUnknown() throws Exception {
135 		String[] output = executeUnchecked("git describe -XYZ -h");
136 		String all = Arrays.toString(output);
137 		assertTrue("Unexpected help output: " + all,
138 				all.contains("jgit describe"));
139 		assertTrue("Unexpected help output: " + all, all.contains("fatal"));
140 	}
141 }