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.

AttributesNodeWorkingTreeIteratorTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 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.File;
  19. import java.io.IOException;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import org.eclipse.jgit.attributes.Attribute.State;
  23. import org.eclipse.jgit.junit.JGitTestUtil;
  24. import org.eclipse.jgit.junit.RepositoryTestCase;
  25. import org.eclipse.jgit.lib.FileMode;
  26. import org.eclipse.jgit.treewalk.FileTreeIterator;
  27. import org.eclipse.jgit.treewalk.TreeWalk;
  28. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  29. import org.junit.Test;
  30. /**
  31. * Tests attributes node behavior on the local filesystem.
  32. */
  33. public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase {
  34. private static final FileMode D = FileMode.TREE;
  35. private static final FileMode F = FileMode.REGULAR_FILE;
  36. private static Attribute EOL_LF = new Attribute("eol", "lf");
  37. private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET);
  38. @Test
  39. public void testRules() throws Exception {
  40. File customAttributeFile = File.createTempFile("tmp_",
  41. "customAttributeFile", null);
  42. customAttributeFile.deleteOnExit();
  43. JGitTestUtil.write(customAttributeFile, "*.txt custom=value");
  44. db.getConfig().setString("core", null, "attributesfile",
  45. customAttributeFile.getAbsolutePath());
  46. writeAttributesFile(".git/info/attributes", "windows* eol=crlf");
  47. writeAttributesFile(".gitattributes", "*.txt eol=lf");
  48. writeTrashFile("windows.file", "");
  49. writeTrashFile("windows.txt", "");
  50. writeTrashFile("global.txt", "");
  51. writeTrashFile("readme.txt", "");
  52. writeAttributesFile("src/config/.gitattributes", "*.txt -delta");
  53. writeTrashFile("src/config/readme.txt", "");
  54. writeTrashFile("src/config/windows.file", "");
  55. writeTrashFile("src/config/windows.txt", "");
  56. try (TreeWalk walk = beginWalk()) {
  57. assertIteration(walk, F, ".gitattributes");
  58. assertIteration(walk, F, "global.txt", asList(EOL_LF));
  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. try (TreeWalk walk = beginWalk()) {
  85. assertIteration(walk, F, "l0.txt");
  86. assertIteration(walk, D, "level1");
  87. assertIteration(walk, F, "level1/l1.txt");
  88. assertIteration(walk, D, "level1/level2");
  89. assertIteration(walk, F, "level1/level2/l2.txt");
  90. assertFalse("Not all files tested", walk.next());
  91. }
  92. }
  93. /**
  94. * Checks that empty .gitattribute files do not return incorrect value.
  95. *
  96. * @throws Exception
  97. */
  98. @Test
  99. public void testEmptyGitAttributeFile() throws Exception {
  100. writeAttributesFile(".git/info/attributes", "");
  101. writeTrashFile("l0.txt", "");
  102. writeAttributesFile(".gitattributes", "");
  103. writeTrashFile("level1/l1.txt", "");
  104. writeTrashFile("level1/level2/l2.txt", "");
  105. try (TreeWalk walk = beginWalk()) {
  106. assertIteration(walk, F, ".gitattributes");
  107. assertIteration(walk, F, "l0.txt");
  108. assertIteration(walk, D, "level1");
  109. assertIteration(walk, F, "level1/l1.txt");
  110. assertIteration(walk, D, "level1/level2");
  111. assertIteration(walk, F, "level1/level2/l2.txt");
  112. assertFalse("Not all files tested", walk.next());
  113. }
  114. }
  115. @Test
  116. public void testNoMatchingAttributes() throws Exception {
  117. writeAttributesFile(".git/info/attributes", "*.java delta");
  118. writeAttributesFile(".gitattributes", "*.java -delta");
  119. writeAttributesFile("levelA/.gitattributes", "*.java eol=lf");
  120. writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf");
  121. writeTrashFile("levelA/lA.txt", "");
  122. try (TreeWalk walk = beginWalk()) {
  123. assertIteration(walk, F, ".gitattributes");
  124. assertIteration(walk, D, "levelA");
  125. assertIteration(walk, F, "levelA/.gitattributes");
  126. assertIteration(walk, F, "levelA/lA.txt");
  127. assertIteration(walk, D, "levelB");
  128. assertIteration(walk, F, "levelB/.gitattributes");
  129. assertFalse("Not all files tested", walk.next());
  130. }
  131. }
  132. private void assertIteration(TreeWalk walk, FileMode type, String pathName)
  133. throws IOException {
  134. assertIteration(walk, type, pathName,
  135. Collections.<Attribute> emptyList());
  136. }
  137. private void assertIteration(TreeWalk walk, FileMode type, String pathName,
  138. List<Attribute> nodeAttrs)
  139. throws IOException {
  140. assertTrue("walk has entry", walk.next());
  141. assertEquals(pathName, walk.getPathString());
  142. assertEquals(type, walk.getFileMode(0));
  143. WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
  144. assertNotNull("has tree", itr);
  145. AttributesNode attributesNode = itr.getEntryAttributesNode();
  146. assertAttributesNode(walk, pathName, attributesNode, nodeAttrs);
  147. if (D.equals(type))
  148. walk.enterSubtree();
  149. }
  150. private void assertAttributesNode(TreeWalk walk, String pathName,
  151. AttributesNode attributesNode, List<Attribute> nodeAttrs)
  152. throws IOException {
  153. if (attributesNode == null)
  154. assertTrue(nodeAttrs == null || nodeAttrs.isEmpty());
  155. else {
  156. Attributes entryAttributes = new Attributes();
  157. new AttributesHandler(walk).mergeAttributes(attributesNode,
  158. pathName, false,
  159. entryAttributes);
  160. if (nodeAttrs != null && !nodeAttrs.isEmpty()) {
  161. for (Attribute attribute : nodeAttrs) {
  162. assertThat(entryAttributes.getAll(),
  163. hasItem(attribute));
  164. }
  165. } else {
  166. assertTrue(
  167. "The entry "
  168. + pathName
  169. + " should not have any attributes. Instead, the following attributes are applied to this file "
  170. + entryAttributes.toString(),
  171. entryAttributes.isEmpty());
  172. }
  173. }
  174. }
  175. private void writeAttributesFile(String name, String... rules)
  176. throws IOException {
  177. StringBuilder data = new StringBuilder();
  178. for (String line : rules)
  179. data.append(line + "\n");
  180. writeTrashFile(name, data.toString());
  181. }
  182. private TreeWalk beginWalk() {
  183. TreeWalk newWalk = new TreeWalk(db);
  184. newWalk.addTree(new FileTreeIterator(db));
  185. return newWalk;
  186. }
  187. }