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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. 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.File;
  52. import java.io.IOException;
  53. import java.util.Collections;
  54. import java.util.List;
  55. import org.eclipse.jgit.attributes.Attribute.State;
  56. import org.eclipse.jgit.junit.JGitTestUtil;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.treewalk.FileTreeIterator;
  60. import org.eclipse.jgit.treewalk.TreeWalk;
  61. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  62. import org.junit.Test;
  63. /**
  64. * Tests attributes node behavior on the local filesystem.
  65. */
  66. public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase {
  67. private static final FileMode D = FileMode.TREE;
  68. private static final FileMode F = FileMode.REGULAR_FILE;
  69. private static Attribute EOL_LF = new Attribute("eol", "lf");
  70. private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET);
  71. private TreeWalk walk;
  72. @Test
  73. public void testRules() throws Exception {
  74. File customAttributeFile = File.createTempFile("tmp_",
  75. "customAttributeFile", null);
  76. customAttributeFile.deleteOnExit();
  77. JGitTestUtil.write(customAttributeFile, "*.txt custom=value");
  78. db.getConfig().setString("core", null, "attributesfile",
  79. customAttributeFile.getAbsolutePath());
  80. writeAttributesFile(".git/info/attributes", "windows* eol=crlf");
  81. writeAttributesFile(".gitattributes", "*.txt eol=lf");
  82. writeTrashFile("windows.file", "");
  83. writeTrashFile("windows.txt", "");
  84. writeTrashFile("global.txt", "");
  85. writeTrashFile("readme.txt", "");
  86. writeAttributesFile("src/config/.gitattributes", "*.txt -delta");
  87. writeTrashFile("src/config/readme.txt", "");
  88. writeTrashFile("src/config/windows.file", "");
  89. writeTrashFile("src/config/windows.txt", "");
  90. walk = beginWalk();
  91. assertIteration(F, ".gitattributes");
  92. assertIteration(F, "global.txt", asList(EOL_LF));
  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. walk = beginWalk();
  116. assertIteration(F, "l0.txt");
  117. assertIteration(D, "level1");
  118. assertIteration(F, "level1/l1.txt");
  119. assertIteration(D, "level1/level2");
  120. assertIteration(F, "level1/level2/l2.txt");
  121. endWalk();
  122. }
  123. /**
  124. * Checks that empty .gitattribute files do not return incorrect value.
  125. *
  126. * @throws Exception
  127. */
  128. @Test
  129. public void testEmptyGitAttributeFile() throws Exception {
  130. writeAttributesFile(".git/info/attributes", "");
  131. writeTrashFile("l0.txt", "");
  132. writeAttributesFile(".gitattributes", "");
  133. writeTrashFile("level1/l1.txt", "");
  134. writeTrashFile("level1/level2/l2.txt", "");
  135. walk = beginWalk();
  136. assertIteration(F, ".gitattributes");
  137. assertIteration(F, "l0.txt");
  138. assertIteration(D, "level1");
  139. assertIteration(F, "level1/l1.txt");
  140. assertIteration(D, "level1/level2");
  141. assertIteration(F, "level1/level2/l2.txt");
  142. endWalk();
  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. writeTrashFile("levelA/lA.txt", "");
  151. walk = beginWalk();
  152. assertIteration(F, ".gitattributes");
  153. assertIteration(D, "levelA");
  154. assertIteration(F, "levelA/.gitattributes");
  155. assertIteration(F, "levelA/lA.txt");
  156. assertIteration(D, "levelB");
  157. assertIteration(F, "levelB/.gitattributes");
  158. endWalk();
  159. }
  160. private void assertIteration(FileMode type, String pathName)
  161. throws IOException {
  162. assertIteration(type, pathName, Collections.<Attribute> emptyList());
  163. }
  164. private void assertIteration(FileMode type, String pathName,
  165. List<Attribute> nodeAttrs)
  166. throws IOException {
  167. assertTrue("walk has entry", walk.next());
  168. assertEquals(pathName, walk.getPathString());
  169. assertEquals(type, walk.getFileMode(0));
  170. WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
  171. assertNotNull("has tree", itr);
  172. AttributesNode attributesNode = itr.getEntryAttributesNode();
  173. assertAttributesNode(pathName, attributesNode, nodeAttrs);
  174. if (D.equals(type))
  175. walk.enterSubtree();
  176. }
  177. private void assertAttributesNode(String pathName,
  178. AttributesNode attributesNode, List<Attribute> nodeAttrs)
  179. throws IOException {
  180. if (attributesNode == null)
  181. assertTrue(nodeAttrs == null || nodeAttrs.isEmpty());
  182. else {
  183. Attributes entryAttributes = new Attributes();
  184. new AttributesHandler(walk).mergeAttributes(attributesNode,
  185. pathName, false,
  186. entryAttributes);
  187. if (nodeAttrs != null && !nodeAttrs.isEmpty()) {
  188. for (Attribute attribute : nodeAttrs) {
  189. assertThat(entryAttributes.getAll(),
  190. hasItem(attribute));
  191. }
  192. } else {
  193. assertTrue(
  194. "The entry "
  195. + pathName
  196. + " should not have any attributes. Instead, the following attributes are applied to this file "
  197. + entryAttributes.toString(),
  198. entryAttributes.isEmpty());
  199. }
  200. }
  201. }
  202. private void writeAttributesFile(String name, String... rules)
  203. throws IOException {
  204. StringBuilder data = new StringBuilder();
  205. for (String line : rules)
  206. data.append(line + "\n");
  207. writeTrashFile(name, data.toString());
  208. }
  209. private TreeWalk beginWalk() {
  210. TreeWalk newWalk = new TreeWalk(db);
  211. newWalk.addTree(new FileTreeIterator(db));
  212. return newWalk;
  213. }
  214. private void endWalk() throws IOException {
  215. assertFalse("Not all files tested", walk.next());
  216. }
  217. }