View Javadoc
1   /*
2    * Copyright (C) 2022, Thomas Wolf <thomas.wolf@paranor.ch> 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  
11  package org.eclipse.jgit.lib;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotEquals;
16  import static org.junit.Assert.assertThrows;
17  import static org.junit.Assert.assertTrue;
18  
19  import org.eclipse.jgit.errors.ConfigInvalidException;
20  import org.eclipse.jgit.lib.CommitConfig.CleanupMode;
21  import org.junit.Test;
22  
23  public class CommitConfigTest {
24  
25  	@Test
26  	public void testDefaults() throws Exception {
27  		CommitConfig cfg = parse("");
28  		assertEquals("Unexpected clean-up mode", CleanupMode.DEFAULT,
29  				cfg.getCleanupMode());
30  	}
31  
32  	@Test
33  	public void testCommitCleanup() throws Exception {
34  		String[] values = { "strip", "whitespace", "verbatim", "scissors",
35  				"default" };
36  		CleanupMode[] expected = { CleanupMode.STRIP, CleanupMode.WHITESPACE,
37  				CleanupMode.VERBATIM, CleanupMode.SCISSORS,
38  				CleanupMode.DEFAULT };
39  		for (int i = 0; i < values.length; i++) {
40  			CommitConfig cfg = parse("[commit]\n\tcleanup = " + values[i]);
41  			assertEquals("Unexpected clean-up mode", expected[i],
42  					cfg.getCleanupMode());
43  		}
44  	}
45  
46  	@Test
47  	public void testResolve() throws Exception {
48  		String[] values = { "strip", "whitespace", "verbatim", "scissors",
49  				"default" };
50  		CleanupMode[] expected = { CleanupMode.STRIP, CleanupMode.WHITESPACE,
51  				CleanupMode.VERBATIM, CleanupMode.SCISSORS,
52  				CleanupMode.DEFAULT };
53  		for (int i = 0; i < values.length; i++) {
54  			CommitConfig cfg = parse("[commit]\n\tcleanup = " + values[i]);
55  			for (CleanupMode mode : CleanupMode.values()) {
56  				for (int j = 0; j < 2; j++) {
57  					CleanupMode resolved = cfg.resolve(mode, j == 0);
58  					if (mode != CleanupMode.DEFAULT) {
59  						assertEquals("Clean-up mode should be unchanged", mode,
60  								resolved);
61  					} else if (i + 1 < values.length) {
62  						assertEquals("Unexpected clean-up mode", expected[i],
63  								resolved);
64  					} else {
65  						assertEquals("Unexpected clean-up mode",
66  								j == 0 ? CleanupMode.STRIP
67  										: CleanupMode.WHITESPACE,
68  								resolved);
69  					}
70  				}
71  			}
72  		}
73  	}
74  
75  	@Test
76  	public void testCleanDefaultThrows() throws Exception {
77  		assertThrows(IllegalArgumentException.class, () -> CommitConfig
78  				.cleanText("Whatever", CleanupMode.DEFAULT, '#'));
79  	}
80  
81  	@Test
82  	public void testCleanVerbatim() throws Exception {
83  		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
84  		assertEquals("Unexpected message change", message,
85  				CommitConfig.cleanText(message, CleanupMode.VERBATIM, '#'));
86  	}
87  
88  	@Test
89  	public void testCleanWhitespace() throws Exception {
90  		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
91  		assertEquals("Unexpected message change",
92  				"Whatever\n\n# A comment\n\nMore\n",
93  				CommitConfig.cleanText(message, CleanupMode.WHITESPACE, '#'));
94  	}
95  
96  	@Test
97  	public void testCleanStrip() throws Exception {
98  		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
99  		assertEquals("Unexpected message change", "Whatever\n\nMore\n",
100 				CommitConfig.cleanText(message, CleanupMode.STRIP, '#'));
101 	}
102 
103 	@Test
104 	public void testCleanStripCustomChar() throws Exception {
105 		String message = "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n";
106 		assertEquals("Unexpected message change",
107 				"Whatever\n\n# Not a comment\n\nMore\n",
108 				CommitConfig.cleanText(message, CleanupMode.STRIP, '<'));
109 	}
110 
111 	@Test
112 	public void testCleanScissors() throws Exception {
113 		String message = "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n"
114 				+ "# ------------------------ >8 ------------------------\n"
115 				+ "More\nMore\n";
116 		assertEquals("Unexpected message change",
117 				"Whatever\n\n# Not a comment\n\n   <A comment\nMore\n",
118 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
119 	}
120 
121 	@Test
122 	public void testCleanScissorsCustomChar() throws Exception {
123 		String message = "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n"
124 				+ "< ------------------------ >8 ------------------------\n"
125 				+ "More\nMore\n";
126 		assertEquals("Unexpected message change",
127 				"Whatever\n\n# Not a comment\n\n   <A comment\nMore\n",
128 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '<'));
129 	}
130 
131 	@Test
132 	public void testCleanScissorsAtTop() throws Exception {
133 		String message = "# ------------------------ >8 ------------------------\n"
134 				+ "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n"
135 				+ "More\nMore\n";
136 		assertEquals("Unexpected message change", "",
137 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
138 	}
139 
140 	@Test
141 	public void testCleanScissorsNoScissor() throws Exception {
142 		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
143 		assertEquals("Unexpected message change",
144 				"Whatever\n\n# A comment\n\nMore\n",
145 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
146 	}
147 
148 	@Test
149 	public void testCleanScissorsNoScissor2() throws Exception {
150 		String message = "Text\n"
151 				+ "## ------------------------ >8 ------------------------\n"
152 				+ "More\nMore\n";
153 		assertEquals("Unexpected message change", message,
154 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
155 	}
156 
157 	@Test
158 	public void testCleanScissorsNoScissor3() throws Exception {
159 		String message = "Text\n"
160 				// Wrong number of dashes
161 				+ "# ----------------------- >8 ------------------------\n"
162 				+ "More\nMore\n";
163 		assertEquals("Unexpected message change", message,
164 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
165 	}
166 
167 	@Test
168 	public void testCleanScissorsAtEnd() throws Exception {
169 		String message = "Text\n"
170 				+ "# ------------------------ >8 ------------------------\n";
171 		assertEquals("Unexpected message change", "Text\n",
172 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
173 	}
174 
175 	@Test
176 	public void testCommentCharDefault() throws Exception {
177 		CommitConfig cfg = parse("");
178 		assertEquals('#', cfg.getCommentChar());
179 		assertFalse(cfg.isAutoCommentChar());
180 	}
181 
182 	@Test
183 	public void testCommentCharAuto() throws Exception {
184 		CommitConfig cfg = parse("[core]\n\tcommentChar = auto\n");
185 		assertEquals('#', cfg.getCommentChar());
186 		assertTrue(cfg.isAutoCommentChar());
187 	}
188 
189 	@Test
190 	public void testCommentCharEmpty() throws Exception {
191 		CommitConfig cfg = parse("[core]\n\tcommentChar =\n");
192 		assertEquals('#', cfg.getCommentChar());
193 	}
194 
195 	@Test
196 	public void testCommentCharInvalid() throws Exception {
197 		CommitConfig cfg = parse("[core]\n\tcommentChar = \" \"\n");
198 		assertEquals('#', cfg.getCommentChar());
199 	}
200 
201 	@Test
202 	public void testCommentCharNonAscii() throws Exception {
203 		CommitConfig cfg = parse("[core]\n\tcommentChar = รถ\n");
204 		assertEquals('#', cfg.getCommentChar());
205 	}
206 
207 	@Test
208 	public void testCommentChar() throws Exception {
209 		CommitConfig cfg = parse("[core]\n\tcommentChar = _\n");
210 		assertEquals('_', cfg.getCommentChar());
211 	}
212 
213 	@Test
214 	public void testDetermineCommentChar() throws Exception {
215 		String text = "A commit message\n\nBody\n";
216 		assertEquals('#', CommitConfig.determineCommentChar(text));
217 	}
218 
219 	@Test
220 	public void testDetermineCommentChar2() throws Exception {
221 		String text = "A commit message\n\nBody\n\n# Conflicts:\n#\tfoo.txt\n";
222 		char ch = CommitConfig.determineCommentChar(text);
223 		assertNotEquals('#', ch);
224 		assertTrue(ch > ' ' && ch < 127);
225 	}
226 
227 	@Test
228 	public void testDetermineCommentChar3() throws Exception {
229 		String text = "A commit message\n\n;Body\n\n# Conflicts:\n#\tfoo.txt\n";
230 		char ch = CommitConfig.determineCommentChar(text);
231 		assertNotEquals('#', ch);
232 		assertNotEquals(';', ch);
233 		assertTrue(ch > ' ' && ch < 127);
234 	}
235 
236 	@Test
237 	public void testDetermineCommentChar4() throws Exception {
238 		String text = "A commit message\n\nBody\n\n  # Conflicts:\n\t #\tfoo.txt\n";
239 		char ch = CommitConfig.determineCommentChar(text);
240 		assertNotEquals('#', ch);
241 		assertTrue(ch > ' ' && ch < 127);
242 	}
243 
244 	@Test
245 	public void testDetermineCommentChar5() throws Exception {
246 		String text = "A commit message\n\nBody\n\n#a\n;b\n@c\n!d\n$\n%\n^\n&\n|\n:";
247 		char ch = CommitConfig.determineCommentChar(text);
248 		assertEquals(0, ch);
249 	}
250 
251 	private static CommitConfig parse(String content)
252 			throws ConfigInvalidException {
253 		Config c = new Config();
254 		c.fromText(content);
255 		return c.get(CommitConfig.KEY);
256 	}
257 
258 }