1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.attributes;
44
45 import static java.util.Arrays.asList;
46 import static org.hamcrest.CoreMatchers.hasItem;
47 import static org.hamcrest.MatcherAssert.assertThat;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertFalse;
50 import static org.junit.Assert.assertNotNull;
51 import static org.junit.Assert.assertTrue;
52
53 import java.io.IOException;
54 import java.util.Collections;
55 import java.util.List;
56
57 import org.eclipse.jgit.api.Git;
58 import org.eclipse.jgit.attributes.Attribute.State;
59 import org.eclipse.jgit.dircache.DirCacheIterator;
60 import org.eclipse.jgit.junit.RepositoryTestCase;
61 import org.eclipse.jgit.lib.FileMode;
62 import org.eclipse.jgit.treewalk.TreeWalk;
63 import org.junit.Before;
64 import org.junit.Test;
65
66
67
68
69 public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase {
70
71 private static final FileMode D = FileMode.TREE;
72
73 private static final FileMode F = FileMode.REGULAR_FILE;
74
75 private static Attribute EOL_LF = new Attribute("eol", "lf");
76
77 private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET);
78
79 private Git git;
80
81 private TreeWalk walk;
82
83 @Override
84 @Before
85 public void setUp() throws Exception {
86 super.setUp();
87 git = new Git(db);
88
89 }
90
91 @Test
92 public void testRules() throws Exception {
93 writeAttributesFile(".git/info/attributes", "windows* eol=crlf");
94
95 writeAttributesFile(".gitattributes", "*.txt eol=lf");
96 writeTrashFile("windows.file", "");
97 writeTrashFile("windows.txt", "");
98 writeTrashFile("readme.txt", "");
99
100 writeAttributesFile("src/config/.gitattributes", "*.txt -delta");
101 writeTrashFile("src/config/readme.txt", "");
102 writeTrashFile("src/config/windows.file", "");
103 writeTrashFile("src/config/windows.txt", "");
104
105
106 git.add().addFilepattern(".").call();
107
108 walk = beginWalk();
109
110 assertIteration(F, ".gitattributes");
111 assertIteration(F, "readme.txt", asList(EOL_LF));
112
113 assertIteration(D, "src");
114
115 assertIteration(D, "src/config");
116 assertIteration(F, "src/config/.gitattributes");
117 assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET));
118 assertIteration(F, "src/config/windows.file", null);
119 assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET));
120
121 assertIteration(F, "windows.file", null);
122 assertIteration(F, "windows.txt", asList(EOL_LF));
123
124 endWalk();
125 }
126
127
128
129
130
131
132
133 @Test
134 public void testNoAttributes() throws Exception {
135 writeTrashFile("l0.txt", "");
136 writeTrashFile("level1/l1.txt", "");
137 writeTrashFile("level1/level2/l2.txt", "");
138
139
140 git.add().addFilepattern(".").call();
141 walk = beginWalk();
142
143 assertIteration(F, "l0.txt");
144
145 assertIteration(D, "level1");
146 assertIteration(F, "level1/l1.txt");
147
148 assertIteration(D, "level1/level2");
149 assertIteration(F, "level1/level2/l2.txt");
150
151 endWalk();
152 }
153
154
155
156
157
158
159 @Test
160 public void testEmptyGitAttributeFile() throws Exception {
161 writeAttributesFile(".git/info/attributes", "");
162 writeTrashFile("l0.txt", "");
163 writeAttributesFile(".gitattributes", "");
164 writeTrashFile("level1/l1.txt", "");
165 writeTrashFile("level1/level2/l2.txt", "");
166
167
168 git.add().addFilepattern(".").call();
169 walk = beginWalk();
170
171 assertIteration(F, ".gitattributes");
172 assertIteration(F, "l0.txt");
173
174 assertIteration(D, "level1");
175 assertIteration(F, "level1/l1.txt");
176
177 assertIteration(D, "level1/level2");
178 assertIteration(F, "level1/level2/l2.txt");
179
180 endWalk();
181 }
182
183 @Test
184 public void testNoMatchingAttributes() throws Exception {
185 writeAttributesFile(".git/info/attributes", "*.java delta");
186 writeAttributesFile(".gitattributes", "*.java -delta");
187 writeAttributesFile("levelA/.gitattributes", "*.java eol=lf");
188 writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf");
189
190 writeTrashFile("levelA/lA.txt", "");
191
192
193 git.add().addFilepattern(".").call();
194 walk = beginWalk();
195
196 assertIteration(F, ".gitattributes");
197
198 assertIteration(D, "levelA");
199 assertIteration(F, "levelA/.gitattributes");
200 assertIteration(F, "levelA/lA.txt");
201
202 assertIteration(D, "levelB");
203 assertIteration(F, "levelB/.gitattributes");
204
205 endWalk();
206 }
207
208 @Test
209 public void testIncorrectAttributeFileName() throws Exception {
210 writeAttributesFile("levelA/file.gitattributes", "*.txt -delta");
211 writeAttributesFile("gitattributes", "*.txt eol=lf");
212
213 writeTrashFile("l0.txt", "");
214 writeTrashFile("levelA/lA.txt", "");
215
216
217 git.add().addFilepattern(".").call();
218 walk = beginWalk();
219
220 assertIteration(F, "gitattributes");
221
222 assertIteration(F, "l0.txt");
223
224 assertIteration(D, "levelA");
225 assertIteration(F, "levelA/file.gitattributes");
226 assertIteration(F, "levelA/lA.txt");
227
228 endWalk();
229 }
230
231 private void assertIteration(FileMode type, String pathName)
232 throws IOException {
233 assertIteration(type, pathName, Collections.<Attribute> emptyList());
234 }
235
236 private void assertIteration(FileMode type, String pathName,
237 List<Attribute> nodeAttrs) throws IOException {
238 assertTrue("walk has entry", walk.next());
239 assertEquals(pathName, walk.getPathString());
240 assertEquals(type, walk.getFileMode(0));
241 DirCacheIterator itr = walk.getTree(0, DirCacheIterator.class);
242 assertNotNull("has tree", itr);
243
244 AttributesNode attributesNode = itr.getEntryAttributesNode(db
245 .newObjectReader());
246 assertAttributesNode(pathName, attributesNode, nodeAttrs);
247
248 if (D.equals(type))
249 walk.enterSubtree();
250
251 }
252
253 private void assertAttributesNode(String pathName,
254 AttributesNode attributesNode, List<Attribute> nodeAttrs)
255 throws IOException {
256 if (attributesNode == null)
257 assertTrue(nodeAttrs == null || nodeAttrs.isEmpty());
258 else {
259
260 Attributes entryAttributes = new Attributes();
261 new AttributesHandler(walk).mergeAttributes(attributesNode,
262 pathName,
263 false,
264 entryAttributes);
265
266 if (nodeAttrs != null && !nodeAttrs.isEmpty()) {
267 for (Attribute attribute : nodeAttrs) {
268 assertThat(entryAttributes.getAll(),
269 hasItem(attribute));
270 }
271 } else {
272 assertTrue(
273 "The entry "
274 + pathName
275 + " should not have any attributes. Instead, the following attributes are applied to this file "
276 + entryAttributes.toString(),
277 entryAttributes.isEmpty());
278 }
279 }
280 }
281
282 private void writeAttributesFile(String name, String... rules)
283 throws IOException {
284 StringBuilder data = new StringBuilder();
285 for (String line : rules)
286 data.append(line + "\n");
287 writeTrashFile(name, data.toString());
288 }
289
290 private TreeWalk beginWalk() throws Exception {
291 TreeWalk newWalk = new TreeWalk(db);
292 newWalk.addTree(new DirCacheIterator(db.readDirCache()));
293 return newWalk;
294 }
295
296 private void endWalk() throws IOException {
297 assertFalse("Not all files tested", walk.next());
298 }
299 }