View Javadoc
1   /*
2    * Copyright (C) 2014, Obeo. 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  package org.eclipse.jgit.attributes;
11  
12  import static java.util.Arrays.asList;
13  import static org.hamcrest.CoreMatchers.hasItem;
14  import static org.hamcrest.MatcherAssert.assertThat;
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.eclipse.jgit.attributes.Attribute.State;
26  import org.eclipse.jgit.junit.JGitTestUtil;
27  import org.eclipse.jgit.junit.RepositoryTestCase;
28  import org.eclipse.jgit.lib.FileMode;
29  import org.eclipse.jgit.treewalk.FileTreeIterator;
30  import org.eclipse.jgit.treewalk.TreeWalk;
31  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
32  import org.junit.Test;
33  
34  /**
35   * Tests attributes node behavior on the local filesystem.
36   */
37  public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase {
38  
39  	private static final FileMode D = FileMode.TREE;
40  
41  	private static final FileMode F = FileMode.REGULAR_FILE;
42  
43  	private static Attribute EOL_LF = new Attribute("eol", "lf");
44  
45  	private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET);
46  
47  	@Test
48  	public void testRules() throws Exception {
49  
50  		File customAttributeFile = File.createTempFile("tmp_",
51  				"customAttributeFile", null);
52  		customAttributeFile.deleteOnExit();
53  
54  		JGitTestUtil.write(customAttributeFile, "*.txt custom=value");
55  		db.getConfig().setString("core", null, "attributesfile",
56  				customAttributeFile.getAbsolutePath());
57  		writeAttributesFile(".git/info/attributes", "windows* eol=crlf");
58  
59  		writeAttributesFile(".gitattributes", "*.txt eol=lf");
60  		writeTrashFile("windows.file", "");
61  		writeTrashFile("windows.txt", "");
62  		writeTrashFile("global.txt", "");
63  		writeTrashFile("readme.txt", "");
64  
65  		writeAttributesFile("src/config/.gitattributes", "*.txt -delta");
66  		writeTrashFile("src/config/readme.txt", "");
67  		writeTrashFile("src/config/windows.file", "");
68  		writeTrashFile("src/config/windows.txt", "");
69  
70  		try (TreeWalk walk = beginWalk()) {
71  			assertIteration(walk, F, ".gitattributes");
72  			assertIteration(walk, F, "global.txt", asList(EOL_LF));
73  			assertIteration(walk, F, "readme.txt", asList(EOL_LF));
74  
75  			assertIteration(walk, D, "src");
76  
77  			assertIteration(walk, D, "src/config");
78  			assertIteration(walk, F, "src/config/.gitattributes");
79  			assertIteration(walk, F, "src/config/readme.txt",
80  					asList(DELTA_UNSET));
81  			assertIteration(walk, F, "src/config/windows.file", null);
82  			assertIteration(walk, F, "src/config/windows.txt",
83  					asList(DELTA_UNSET));
84  
85  			assertIteration(walk, F, "windows.file", null);
86  			assertIteration(walk, F, "windows.txt", asList(EOL_LF));
87  
88  			assertFalse("Not all files tested", walk.next());
89  		}
90  	}
91  
92  	/**
93  	 * Checks that if there is no .gitattributes file in the repository
94  	 * everything still work fine.
95  	 *
96  	 * @throws Exception
97  	 */
98  	@Test
99  	public void testNoAttributes() throws Exception {
100 		writeTrashFile("l0.txt", "");
101 		writeTrashFile("level1/l1.txt", "");
102 		writeTrashFile("level1/level2/l2.txt", "");
103 
104 		try (TreeWalk walk = beginWalk()) {
105 			assertIteration(walk, F, "l0.txt");
106 
107 			assertIteration(walk, D, "level1");
108 			assertIteration(walk, F, "level1/l1.txt");
109 
110 			assertIteration(walk, D, "level1/level2");
111 			assertIteration(walk, F, "level1/level2/l2.txt");
112 
113 			assertFalse("Not all files tested", walk.next());
114 		}
115 	}
116 
117 	/**
118 	 * Checks that empty .gitattribute files do not return incorrect value.
119 	 *
120 	 * @throws Exception
121 	 */
122 	@Test
123 	public void testEmptyGitAttributeFile() throws Exception {
124 		writeAttributesFile(".git/info/attributes", "");
125 		writeTrashFile("l0.txt", "");
126 		writeAttributesFile(".gitattributes", "");
127 		writeTrashFile("level1/l1.txt", "");
128 		writeTrashFile("level1/level2/l2.txt", "");
129 
130 		try (TreeWalk walk = beginWalk()) {
131 			assertIteration(walk, F, ".gitattributes");
132 			assertIteration(walk, F, "l0.txt");
133 
134 			assertIteration(walk, D, "level1");
135 			assertIteration(walk, F, "level1/l1.txt");
136 
137 			assertIteration(walk, D, "level1/level2");
138 			assertIteration(walk, F, "level1/level2/l2.txt");
139 
140 			assertFalse("Not all files tested", walk.next());
141 		}
142 	}
143 
144 	@Test
145 	public void testNoMatchingAttributes() throws Exception {
146 		writeAttributesFile(".git/info/attributes", "*.java delta");
147 		writeAttributesFile(".gitattributes", "*.java -delta");
148 		writeAttributesFile("levelA/.gitattributes", "*.java eol=lf");
149 		writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf");
150 
151 		writeTrashFile("levelA/lA.txt", "");
152 
153 		try (TreeWalk walk = beginWalk()) {
154 			assertIteration(walk, F, ".gitattributes");
155 
156 			assertIteration(walk, D, "levelA");
157 			assertIteration(walk, F, "levelA/.gitattributes");
158 			assertIteration(walk, F, "levelA/lA.txt");
159 
160 			assertIteration(walk, D, "levelB");
161 			assertIteration(walk, F, "levelB/.gitattributes");
162 
163 			assertFalse("Not all files tested", walk.next());
164 		}
165 	}
166 
167 	private void assertIteration(TreeWalk walk, FileMode type, String pathName)
168 			throws IOException {
169 		assertIteration(walk, type, pathName,
170 				Collections.<Attribute> emptyList());
171 	}
172 
173 	private void assertIteration(TreeWalk walk, FileMode type, String pathName,
174 			List<Attribute> nodeAttrs)
175 			throws IOException {
176 		assertTrue("walk has entry", walk.next());
177 		assertEquals(pathName, walk.getPathString());
178 		assertEquals(type, walk.getFileMode(0));
179 		WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
180 		assertNotNull("has tree", itr);
181 
182 		AttributesNode attributesNode = itr.getEntryAttributesNode();
183 		assertAttributesNode(walk, pathName, attributesNode, nodeAttrs);
184 		if (D.equals(type))
185 			walk.enterSubtree();
186 
187 	}
188 
189 	private void assertAttributesNode(TreeWalk walk, String pathName,
190 			AttributesNode attributesNode, List<Attribute> nodeAttrs)
191 					throws IOException {
192 		if (attributesNode == null)
193 			assertTrue(nodeAttrs == null || nodeAttrs.isEmpty());
194 		else {
195 
196 			Attributes entryAttributes = new Attributes();
197 			new AttributesHandler(walk).mergeAttributes(attributesNode,
198 					pathName, false,
199 					entryAttributes);
200 
201 			if (nodeAttrs != null && !nodeAttrs.isEmpty()) {
202 				for (Attribute attribute : nodeAttrs) {
203 					assertThat(entryAttributes.getAll(),
204 							hasItem(attribute));
205 				}
206 			} else {
207 				assertTrue(
208 						"The entry "
209 								+ pathName
210 								+ " should not have any attributes. Instead, the following attributes are applied to this file "
211 								+ entryAttributes.toString(),
212 						entryAttributes.isEmpty());
213 			}
214 		}
215 	}
216 
217 	private void writeAttributesFile(String name, String... rules)
218 			throws IOException {
219 		StringBuilder data = new StringBuilder();
220 		for (String line : rules)
221 			data.append(line + "\n");
222 		writeTrashFile(name, data.toString());
223 	}
224 
225 	private TreeWalk beginWalk() {
226 		TreeWalk newWalk = new TreeWalk(db);
227 		newWalk.addTree(new FileTreeIterator(db));
228 		return newWalk;
229 	}
230 }