View Javadoc
1   /*
2    * Copyright (C) 2014, Obeo.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Tests attributes node behavior on the local filesystem.
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 	 * Checks that if there is no .gitattributes file in the repository
127 	 * everything still work fine.
128 	 *
129 	 * @throws Exception
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 	 * Checks that empty .gitattribute files do not return incorrect value.
152 	 *
153 	 * @throws Exception
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 }