Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AttributesNodeDirCacheIteratorTest.java 7.6KB

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