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.assertThrows;
15  
16  import org.eclipse.jgit.errors.ConfigInvalidException;
17  import org.eclipse.jgit.lib.CommitConfig.CleanupMode;
18  import org.junit.Test;
19  
20  public class CommitConfigTest {
21  
22  	@Test
23  	public void testDefaults() throws Exception {
24  		CommitConfig cfg = parse("");
25  		assertEquals("Unexpected clean-up mode", CleanupMode.DEFAULT,
26  				cfg.getCleanupMode());
27  	}
28  
29  	@Test
30  	public void testCommitCleanup() throws Exception {
31  		String[] values = { "strip", "whitespace", "verbatim", "scissors",
32  				"default" };
33  		CleanupMode[] expected = { CleanupMode.STRIP, CleanupMode.WHITESPACE,
34  				CleanupMode.VERBATIM, CleanupMode.SCISSORS,
35  				CleanupMode.DEFAULT };
36  		for (int i = 0; i < values.length; i++) {
37  			CommitConfig cfg = parse("[commit]\n\tcleanup = " + values[i]);
38  			assertEquals("Unexpected clean-up mode", expected[i],
39  					cfg.getCleanupMode());
40  		}
41  	}
42  
43  	@Test
44  	public void testResolve() throws Exception {
45  		String[] values = { "strip", "whitespace", "verbatim", "scissors",
46  				"default" };
47  		CleanupMode[] expected = { CleanupMode.STRIP, CleanupMode.WHITESPACE,
48  				CleanupMode.VERBATIM, CleanupMode.SCISSORS,
49  				CleanupMode.DEFAULT };
50  		for (int i = 0; i < values.length; i++) {
51  			CommitConfig cfg = parse("[commit]\n\tcleanup = " + values[i]);
52  			for (CleanupMode mode : CleanupMode.values()) {
53  				for (int j = 0; j < 2; j++) {
54  					CleanupMode resolved = cfg.resolve(mode, j == 0);
55  					if (mode != CleanupMode.DEFAULT) {
56  						assertEquals("Clean-up mode should be unchanged", mode,
57  								resolved);
58  					} else if (i + 1 < values.length) {
59  						assertEquals("Unexpected clean-up mode", expected[i],
60  								resolved);
61  					} else {
62  						assertEquals("Unexpected clean-up mode",
63  								j == 0 ? CleanupMode.STRIP
64  										: CleanupMode.WHITESPACE,
65  								resolved);
66  					}
67  				}
68  			}
69  		}
70  	}
71  
72  	@Test
73  	public void testCleanDefaultThrows() throws Exception {
74  		assertThrows(IllegalArgumentException.class, () -> CommitConfig
75  				.cleanText("Whatever", CleanupMode.DEFAULT, '#'));
76  	}
77  
78  	@Test
79  	public void testCleanVerbatim() throws Exception {
80  		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
81  		assertEquals("Unexpected message change", message,
82  				CommitConfig.cleanText(message, CleanupMode.VERBATIM, '#'));
83  	}
84  
85  	@Test
86  	public void testCleanWhitespace() throws Exception {
87  		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
88  		assertEquals("Unexpected message change",
89  				"Whatever\n\n# A comment\n\nMore\n",
90  				CommitConfig.cleanText(message, CleanupMode.WHITESPACE, '#'));
91  	}
92  
93  	@Test
94  	public void testCleanStrip() throws Exception {
95  		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
96  		assertEquals("Unexpected message change", "Whatever\n\nMore\n",
97  				CommitConfig.cleanText(message, CleanupMode.STRIP, '#'));
98  	}
99  
100 	@Test
101 	public void testCleanStripCustomChar() throws Exception {
102 		String message = "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n";
103 		assertEquals("Unexpected message change",
104 				"Whatever\n\n# Not a comment\n\nMore\n",
105 				CommitConfig.cleanText(message, CleanupMode.STRIP, '<'));
106 	}
107 
108 	@Test
109 	public void testCleanScissors() throws Exception {
110 		String message = "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n"
111 				+ "# ------------------------ >8 ------------------------\n"
112 				+ "More\nMore\n";
113 		assertEquals("Unexpected message change",
114 				"Whatever\n\n# Not a comment\n\n   <A comment\nMore\n",
115 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
116 	}
117 
118 	@Test
119 	public void testCleanScissorsCustomChar() throws Exception {
120 		String message = "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n"
121 				+ "< ------------------------ >8 ------------------------\n"
122 				+ "More\nMore\n";
123 		assertEquals("Unexpected message change",
124 				"Whatever\n\n# Not a comment\n\n   <A comment\nMore\n",
125 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '<'));
126 	}
127 
128 	@Test
129 	public void testCleanScissorsAtTop() throws Exception {
130 		String message = "# ------------------------ >8 ------------------------\n"
131 				+ "\n  \nWhatever  \n\n\n# Not a comment\n\n   <A comment\nMore\t \n\n\n"
132 				+ "More\nMore\n";
133 		assertEquals("Unexpected message change", "",
134 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
135 	}
136 
137 	@Test
138 	public void testCleanScissorsNoScissor() throws Exception {
139 		String message = "\n  \nWhatever  \n\n\n# A comment\n\nMore\t \n\n\n";
140 		assertEquals("Unexpected message change",
141 				"Whatever\n\n# A comment\n\nMore\n",
142 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
143 	}
144 
145 	@Test
146 	public void testCleanScissorsNoScissor2() throws Exception {
147 		String message = "Text\n"
148 				+ "## ------------------------ >8 ------------------------\n"
149 				+ "More\nMore\n";
150 		assertEquals("Unexpected message change", message,
151 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
152 	}
153 
154 	@Test
155 	public void testCleanScissorsNoScissor3() throws Exception {
156 		String message = "Text\n"
157 				// Wrong number of dashes
158 				+ "# ----------------------- >8 ------------------------\n"
159 				+ "More\nMore\n";
160 		assertEquals("Unexpected message change", message,
161 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
162 	}
163 
164 	@Test
165 	public void testCleanScissorsAtEnd() throws Exception {
166 		String message = "Text\n"
167 				+ "# ------------------------ >8 ------------------------\n";
168 		assertEquals("Unexpected message change", "Text\n",
169 				CommitConfig.cleanText(message, CleanupMode.SCISSORS, '#'));
170 	}
171 
172 	private static CommitConfig parse(String content)
173 			throws ConfigInvalidException {
174 		Config c = new Config();
175 		c.fromText(content);
176 		return c.get(CommitConfig.KEY);
177 	}
178 
179 }