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.File;
54 import java.io.IOException;
55 import java.util.Collections;
56 import java.util.List;
57
58 import org.eclipse.jgit.attributes.Attribute.State;
59 import org.eclipse.jgit.junit.JGitTestUtil;
60 import org.eclipse.jgit.junit.RepositoryTestCase;
61 import org.eclipse.jgit.lib.FileMode;
62 import org.eclipse.jgit.treewalk.FileTreeIterator;
63 import org.eclipse.jgit.treewalk.TreeWalk;
64 import org.eclipse.jgit.treewalk.WorkingTreeIterator;
65 import org.junit.Test;
66
67
68
69
70 public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase {
71
72 private static final FileMode D = FileMode.TREE;
73
74 private static final FileMode F = FileMode.REGULAR_FILE;
75
76 private static Attribute EOL_LF = new Attribute("eol", "lf");
77
78 private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET);
79
80 private TreeWalk walk;
81
82 @Test
83 public void testRules() throws Exception {
84
85 File customAttributeFile = File.createTempFile("tmp_",
86 "customAttributeFile", null);
87 customAttributeFile.deleteOnExit();
88
89 JGitTestUtil.write(customAttributeFile, "*.txt custom=value");
90 db.getConfig().setString("core", null, "attributesfile",
91 customAttributeFile.getAbsolutePath());
92 writeAttributesFile(".git/info/attributes", "windows* eol=crlf");
93
94 writeAttributesFile(".gitattributes", "*.txt eol=lf");
95 writeTrashFile("windows.file", "");
96 writeTrashFile("windows.txt", "");
97 writeTrashFile("global.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 walk = beginWalk();
106
107 assertIteration(F, ".gitattributes");
108 assertIteration(F, "global.txt", asList(EOL_LF));
109 assertIteration(F, "readme.txt", asList(EOL_LF));
110
111 assertIteration(D, "src");
112
113 assertIteration(D, "src/config");
114 assertIteration(F, "src/config/.gitattributes");
115 assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET));
116 assertIteration(F, "src/config/windows.file", null);
117 assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET));
118
119 assertIteration(F, "windows.file", null);
120 assertIteration(F, "windows.txt", asList(EOL_LF));
121
122 endWalk();
123 }
124
125
126
127
128
129
130
131 @Test
132 public void testNoAttributes() throws Exception {
133 writeTrashFile("l0.txt", "");
134 writeTrashFile("level1/l1.txt", "");
135 writeTrashFile("level1/level2/l2.txt", "");
136
137 walk = beginWalk();
138
139 assertIteration(F, "l0.txt");
140
141 assertIteration(D, "level1");
142 assertIteration(F, "level1/l1.txt");
143
144 assertIteration(D, "level1/level2");
145 assertIteration(F, "level1/level2/l2.txt");
146
147 endWalk();
148 }
149
150
151
152
153
154
155 @Test
156 public void testEmptyGitAttributeFile() throws Exception {
157 writeAttributesFile(".git/info/attributes", "");
158 writeTrashFile("l0.txt", "");
159 writeAttributesFile(".gitattributes", "");
160 writeTrashFile("level1/l1.txt", "");
161 writeTrashFile("level1/level2/l2.txt", "");
162
163 walk = beginWalk();
164
165 assertIteration(F, ".gitattributes");
166 assertIteration(F, "l0.txt");
167
168 assertIteration(D, "level1");
169 assertIteration(F, "level1/l1.txt");
170
171 assertIteration(D, "level1/level2");
172 assertIteration(F, "level1/level2/l2.txt");
173
174 endWalk();
175 }
176
177 @Test
178 public void testNoMatchingAttributes() throws Exception {
179 writeAttributesFile(".git/info/attributes", "*.java delta");
180 writeAttributesFile(".gitattributes", "*.java -delta");
181 writeAttributesFile("levelA/.gitattributes", "*.java eol=lf");
182 writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf");
183
184 writeTrashFile("levelA/lA.txt", "");
185
186 walk = beginWalk();
187
188 assertIteration(F, ".gitattributes");
189
190 assertIteration(D, "levelA");
191 assertIteration(F, "levelA/.gitattributes");
192 assertIteration(F, "levelA/lA.txt");
193
194 assertIteration(D, "levelB");
195 assertIteration(F, "levelB/.gitattributes");
196
197 endWalk();
198 }
199
200 private void assertIteration(FileMode type, String pathName)
201 throws IOException {
202 assertIteration(type, pathName, Collections.<Attribute> emptyList());
203 }
204
205 private void assertIteration(FileMode type, String pathName,
206 List<Attribute> nodeAttrs)
207 throws IOException {
208 assertTrue("walk has entry", walk.next());
209 assertEquals(pathName, walk.getPathString());
210 assertEquals(type, walk.getFileMode(0));
211 WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
212 assertNotNull("has tree", itr);
213
214 AttributesNode attributesNode = itr.getEntryAttributesNode();
215 assertAttributesNode(pathName, attributesNode, nodeAttrs);
216 if (D.equals(type))
217 walk.enterSubtree();
218
219 }
220
221 private void assertAttributesNode(String pathName,
222 AttributesNode attributesNode, List<Attribute> nodeAttrs)
223 throws IOException {
224 if (attributesNode == null)
225 assertTrue(nodeAttrs == null || nodeAttrs.isEmpty());
226 else {
227
228 Attributes entryAttributes = new Attributes();
229 new AttributesHandler(walk).mergeAttributes(attributesNode,
230 pathName, false,
231 entryAttributes);
232
233 if (nodeAttrs != null && !nodeAttrs.isEmpty()) {
234 for (Attribute attribute : nodeAttrs) {
235 assertThat(entryAttributes.getAll(),
236 hasItem(attribute));
237 }
238 } else {
239 assertTrue(
240 "The entry "
241 + pathName
242 + " should not have any attributes. Instead, the following attributes are applied to this file "
243 + entryAttributes.toString(),
244 entryAttributes.isEmpty());
245 }
246 }
247 }
248
249 private void writeAttributesFile(String name, String... rules)
250 throws IOException {
251 StringBuilder data = new StringBuilder();
252 for (String line : rules)
253 data.append(line + "\n");
254 writeTrashFile(name, data.toString());
255 }
256
257 private TreeWalk beginWalk() {
258 TreeWalk newWalk = new TreeWalk(db);
259 newWalk.addTree(new FileTreeIterator(db));
260 return newWalk;
261 }
262
263 private void endWalk() throws IOException {
264 assertFalse("Not all files tested", walk.next());
265 }
266 }