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.

AttributesHandlerTest.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * This program and the accompanying materials are made available
  3. * under the terms of the Eclipse Distribution License v1.0 which
  4. * accompanies this distribution, is reproduced below, and is
  5. * available at http://www.eclipse.org/org/documents/edl-v10.php
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or
  10. * without modification, are permitted provided that the following
  11. * conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer in the documentation and/or other materials provided
  19. * with the distribution.
  20. *
  21. * - Neither the name of the Eclipse Foundation, Inc. nor the
  22. * names of its contributors may be used to endorse or promote
  23. * products derived from this software without specific prior
  24. * written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  27. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  28. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  38. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. package org.eclipse.jgit.attributes;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertFalse;
  43. import static org.junit.Assert.assertTrue;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.Collections;
  49. import org.eclipse.jgit.junit.RepositoryTestCase;
  50. import org.eclipse.jgit.lib.ConfigConstants;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.storage.file.FileBasedConfig;
  54. import org.eclipse.jgit.treewalk.FileTreeIterator;
  55. import org.eclipse.jgit.treewalk.TreeWalk;
  56. import org.junit.Test;
  57. /**
  58. * Tests {@link AttributesHandler}
  59. */
  60. public class AttributesHandlerTest extends RepositoryTestCase {
  61. private static final FileMode D = FileMode.TREE;
  62. private static final FileMode F = FileMode.REGULAR_FILE;
  63. private TreeWalk walk;
  64. @Test
  65. public void testExpandNonMacro1() throws Exception {
  66. setupRepo(null, null, null, "*.txt text");
  67. walk = beginWalk();
  68. assertIteration(D, "sub");
  69. assertIteration(F, "sub/.gitattributes");
  70. assertIteration(F, "sub/a.txt", attrs("text"));
  71. endWalk();
  72. }
  73. @Test
  74. public void testExpandNonMacro2() throws Exception {
  75. setupRepo(null, null, null, "*.txt -text");
  76. walk = beginWalk();
  77. assertIteration(D, "sub");
  78. assertIteration(F, "sub/.gitattributes");
  79. assertIteration(F, "sub/a.txt", attrs("-text"));
  80. endWalk();
  81. }
  82. @Test
  83. public void testExpandNonMacro3() throws Exception {
  84. setupRepo(null, null, null, "*.txt !text");
  85. walk = beginWalk();
  86. assertIteration(D, "sub");
  87. assertIteration(F, "sub/.gitattributes");
  88. assertIteration(F, "sub/a.txt", attrs(""));
  89. endWalk();
  90. }
  91. @Test
  92. public void testExpandNonMacro4() throws Exception {
  93. setupRepo(null, null, null, "*.txt text=auto");
  94. walk = beginWalk();
  95. assertIteration(D, "sub");
  96. assertIteration(F, "sub/.gitattributes");
  97. assertIteration(F, "sub/a.txt", attrs("text=auto"));
  98. endWalk();
  99. }
  100. @Test
  101. public void testExpandBuiltInMacro1() throws Exception {
  102. setupRepo(null, null, null, "*.txt binary");
  103. walk = beginWalk();
  104. assertIteration(D, "sub");
  105. assertIteration(F, "sub/.gitattributes");
  106. assertIteration(F, "sub/a.txt", attrs("binary -diff -merge -text"));
  107. endWalk();
  108. }
  109. @Test
  110. public void testExpandBuiltInMacro2() throws Exception {
  111. setupRepo(null, null, null, "*.txt -binary");
  112. walk = beginWalk();
  113. assertIteration(D, "sub");
  114. assertIteration(F, "sub/.gitattributes");
  115. assertIteration(F, "sub/a.txt", attrs("-binary diff merge text"));
  116. endWalk();
  117. }
  118. @Test
  119. public void testExpandBuiltInMacro3() throws Exception {
  120. setupRepo(null, null, null, "*.txt !binary");
  121. walk = beginWalk();
  122. assertIteration(D, "sub");
  123. assertIteration(F, "sub/.gitattributes");
  124. assertIteration(F, "sub/a.txt", attrs(""));
  125. endWalk();
  126. }
  127. @Test
  128. public void testCustomGlobalMacro1() throws Exception {
  129. setupRepo(
  130. "[attr]foo a -b !c d=e", null, null, "*.txt foo");
  131. walk = beginWalk();
  132. assertIteration(D, "sub");
  133. assertIteration(F, "sub/.gitattributes");
  134. assertIteration(F, "sub/a.txt", attrs("foo a -b d=e"));
  135. endWalk();
  136. }
  137. @Test
  138. public void testCustomGlobalMacro2() throws Exception {
  139. setupRepo("[attr]foo a -b !c d=e", null, null, "*.txt -foo");
  140. walk = beginWalk();
  141. assertIteration(D, "sub");
  142. assertIteration(F, "sub/.gitattributes");
  143. assertIteration(F, "sub/a.txt", attrs("-foo -a b d=e"));
  144. endWalk();
  145. }
  146. @Test
  147. public void testCustomGlobalMacro3() throws Exception {
  148. setupRepo("[attr]foo a -b !c d=e", null, null, "*.txt !foo");
  149. walk = beginWalk();
  150. assertIteration(D, "sub");
  151. assertIteration(F, "sub/.gitattributes");
  152. assertIteration(F, "sub/a.txt", attrs(""));
  153. endWalk();
  154. }
  155. @Test
  156. public void testCustomGlobalMacro4() throws Exception {
  157. setupRepo("[attr]foo a -b !c d=e", null, null, "*.txt foo=bar");
  158. walk = beginWalk();
  159. assertIteration(D, "sub");
  160. assertIteration(F, "sub/.gitattributes");
  161. assertIteration(F, "sub/a.txt", attrs("foo=bar a -b d=bar"));
  162. endWalk();
  163. }
  164. @Test
  165. public void testInfoOverridesGlobal() throws Exception {
  166. setupRepo("[attr]foo bar1",
  167. "[attr]foo bar2", null, "*.txt foo");
  168. walk = beginWalk();
  169. assertIteration(D, "sub");
  170. assertIteration(F, "sub/.gitattributes");
  171. assertIteration(F, "sub/a.txt", attrs("foo bar2"));
  172. endWalk();
  173. }
  174. @Test
  175. public void testWorkDirRootOverridesGlobal() throws Exception {
  176. setupRepo("[attr]foo bar1",
  177. null,
  178. "[attr]foo bar3", "*.txt foo");
  179. walk = beginWalk();
  180. assertIteration(F, ".gitattributes");
  181. assertIteration(D, "sub");
  182. assertIteration(F, "sub/.gitattributes");
  183. assertIteration(F, "sub/a.txt", attrs("foo bar3"));
  184. endWalk();
  185. }
  186. @Test
  187. public void testInfoOverridesWorkDirRoot() throws Exception {
  188. setupRepo("[attr]foo bar1",
  189. "[attr]foo bar2", "[attr]foo bar3", "*.txt foo");
  190. walk = beginWalk();
  191. assertIteration(F, ".gitattributes");
  192. assertIteration(D, "sub");
  193. assertIteration(F, "sub/.gitattributes");
  194. assertIteration(F, "sub/a.txt", attrs("foo bar2"));
  195. endWalk();
  196. }
  197. @Test
  198. public void testRecursiveMacro() throws Exception {
  199. setupRepo(
  200. "[attr]foo x bar -foo",
  201. null, null, "*.txt foo");
  202. walk = beginWalk();
  203. assertIteration(D, "sub");
  204. assertIteration(F, "sub/.gitattributes");
  205. assertIteration(F, "sub/a.txt", attrs("foo x bar"));
  206. endWalk();
  207. }
  208. @Test
  209. public void testCyclicMacros() throws Exception {
  210. setupRepo(
  211. "[attr]foo x -bar\n[attr]bar y -foo", null, null, "*.txt foo");
  212. walk = beginWalk();
  213. assertIteration(D, "sub");
  214. assertIteration(F, "sub/.gitattributes");
  215. assertIteration(F, "sub/a.txt", attrs("foo x -bar -y"));
  216. endWalk();
  217. }
  218. private static Collection<Attribute> attrs(String s) {
  219. return new AttributesRule("*", s).getAttributes();
  220. }
  221. private void assertIteration(FileMode type, String pathName)
  222. throws IOException {
  223. assertIteration(type, pathName, Collections.<Attribute> emptyList());
  224. }
  225. private void assertIteration(FileMode type, String pathName,
  226. Collection<Attribute> expectedAttrs) throws IOException {
  227. assertTrue("walk has entry", walk.next());
  228. assertEquals(pathName, walk.getPathString());
  229. assertEquals(type, walk.getFileMode(0));
  230. if (expectedAttrs != null) {
  231. assertEquals(new ArrayList<>(expectedAttrs),
  232. new ArrayList<>(walk.getAttributes().getAll()));
  233. }
  234. if (D.equals(type))
  235. walk.enterSubtree();
  236. }
  237. /**
  238. * @param globalAttributesContent
  239. * @param infoAttributesContent
  240. * @param rootAttributesContent
  241. * @param subDirAttributesContent
  242. * @throws Exception
  243. * Setup a repo with .gitattributes files and a test file
  244. * sub/a.txt
  245. */
  246. private void setupRepo(
  247. String globalAttributesContent,
  248. String infoAttributesContent, String rootAttributesContent, String subDirAttributesContent)
  249. throws Exception {
  250. FileBasedConfig config = db.getConfig();
  251. if (globalAttributesContent != null) {
  252. File f = new File(db.getDirectory(), "global/attributes");
  253. write(f, globalAttributesContent);
  254. config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  255. ConfigConstants.CONFIG_KEY_ATTRIBUTESFILE,
  256. f.getAbsolutePath());
  257. }
  258. if (infoAttributesContent != null) {
  259. File f = new File(db.getDirectory(), Constants.INFO_ATTRIBUTES);
  260. write(f, infoAttributesContent);
  261. }
  262. config.save();
  263. if (rootAttributesContent != null) {
  264. writeAttributesFile(Constants.DOT_GIT_ATTRIBUTES,
  265. rootAttributesContent);
  266. }
  267. if (subDirAttributesContent != null) {
  268. writeAttributesFile("sub/" + Constants.DOT_GIT_ATTRIBUTES,
  269. subDirAttributesContent);
  270. }
  271. writeTrashFile("sub/a.txt", "a");
  272. }
  273. private void writeAttributesFile(String name, String... rules)
  274. throws IOException {
  275. StringBuilder data = new StringBuilder();
  276. for (String line : rules)
  277. data.append(line + "\n");
  278. writeTrashFile(name, data.toString());
  279. }
  280. private TreeWalk beginWalk() {
  281. TreeWalk newWalk = new TreeWalk(db);
  282. newWalk.addTree(new FileTreeIterator(db));
  283. return newWalk;
  284. }
  285. private void endWalk() throws IOException {
  286. assertFalse("Not all files tested", walk.next());
  287. }
  288. }