1
2
3
4
5
6
7
8
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 testDescribeCommitMatchAbbrev() throws Exception {
93 initialCommitAndTag();
94 secondCommit();
95 assertArrayEquals(new String[] { "v1.0-1-g56f6cebdf3f5", "" },
96 execute("git describe --abbrev 12 --match v1.*"));
97 }
98
99 @Test
100 public void testDescribeCommitMatchAbbrevMin() throws Exception {
101 initialCommitAndTag();
102 secondCommit();
103 assertArrayEquals(new String[] { "v1.0-1-g56f6", "" },
104 execute("git describe --abbrev -5 --match v1.*"));
105 }
106
107 @Test
108 public void testDescribeCommitMatchAbbrevMax() throws Exception {
109 initialCommitAndTag();
110 secondCommit();
111 assertArrayEquals(new String[] {
112 "v1.0-1-g56f6cebdf3f5ceeecd803365abf0996fb1fa006d", "" },
113 execute("git describe --abbrev 50 --match v1.*"));
114 }
115
116 @Test
117 public void testDescribeCommitMatch2() throws Exception {
118 initialCommitAndTag();
119 secondCommit();
120 git.tag().setName("v2.0").call();
121 assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
122 execute("git describe --match v1.*"));
123 }
124
125 @Test
126 public void testDescribeCommitMultiMatch() throws Exception {
127 initialCommitAndTag();
128 secondCommit();
129 git.tag().setName("v2.0.0").call();
130 git.tag().setName("v2.1.1").call();
131 assertArrayEquals("git yields v2.0.0", new String[] { "v2.0.0", "" },
132 execute("git describe --match v2.0* --match v2.1.*"));
133 }
134
135 @Test
136 public void testDescribeCommitNoMatch() throws Exception {
137 initialCommitAndTag();
138 writeTrashFile("greeting", "Hello, world!");
139 secondCommit();
140 try {
141 execute("git describe --match 1.*");
142 fail("git describe should not find any tag matching 1.*");
143 } catch (Die e) {
144 assertEquals("No names found, cannot describe anything.",
145 e.getMessage());
146 }
147 }
148
149 @Test
150 public void testHelpArgumentBeforeUnknown() throws Exception {
151 String[] output = execute("git describe -h -XYZ");
152 String all = Arrays.toString(output);
153 assertTrue("Unexpected help output: " + all,
154 all.contains("jgit describe"));
155 assertFalse("Unexpected help output: " + all, all.contains("fatal"));
156 }
157
158 @Test
159 public void testHelpArgumentAfterUnknown() throws Exception {
160 String[] output = executeUnchecked("git describe -XYZ -h");
161 String all = Arrays.toString(output);
162 assertTrue("Unexpected help output: " + all,
163 all.contains("jgit describe"));
164 assertTrue("Unexpected help output: " + all, all.contains("fatal"));
165 }
166 }