You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AttributesNodeDirCacheIteratorTest.java 8.8KB

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