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.

IgnoreMatcherTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.ignore;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import org.junit.Test;
  48. /**
  49. * Tests ignore pattern matches
  50. */
  51. public class IgnoreMatcherTest {
  52. @Test
  53. public void testBasic() {
  54. String pattern = "/test.stp";
  55. assertMatched(pattern, "/test.stp");
  56. pattern = "#/test.stp";
  57. assertNotMatched(pattern, "/test.stp");
  58. }
  59. @Test
  60. public void testFileNameWildcards() {
  61. //Test basic * and ? for any pattern + any character
  62. String pattern = "*.st?";
  63. assertMatched(pattern, "/test.stp");
  64. assertMatched(pattern, "/anothertest.stg");
  65. assertMatched(pattern, "/anothertest.st0");
  66. assertNotMatched(pattern, "/anothertest.sta1");
  67. //Check that asterisk does not expand to "/"
  68. assertNotMatched(pattern, "/another/test.sta1");
  69. //Same as above, with a leading slash to ensure that doesn't cause problems
  70. pattern = "/*.st?";
  71. assertMatched(pattern, "/test.stp");
  72. assertMatched(pattern, "/anothertest.stg");
  73. assertMatched(pattern, "/anothertest.st0");
  74. assertNotMatched(pattern, "/anothertest.sta1");
  75. //Check that asterisk does not expand to "/"
  76. assertNotMatched(pattern, "/another/test.sta1");
  77. //Test for numbers
  78. pattern = "*.sta[0-5]";
  79. assertMatched(pattern, "/test.sta5");
  80. assertMatched(pattern, "/test.sta4");
  81. assertMatched(pattern, "/test.sta3");
  82. assertMatched(pattern, "/test.sta2");
  83. assertMatched(pattern, "/test.sta1");
  84. assertMatched(pattern, "/test.sta0");
  85. assertMatched(pattern, "/anothertest.sta2");
  86. assertNotMatched(pattern, "test.stag");
  87. assertNotMatched(pattern, "test.sta6");
  88. //Test for letters
  89. pattern = "/[tv]est.sta[a-d]";
  90. assertMatched(pattern, "/test.staa");
  91. assertMatched(pattern, "/test.stab");
  92. assertMatched(pattern, "/test.stac");
  93. assertMatched(pattern, "/test.stad");
  94. assertMatched(pattern, "/vest.stac");
  95. assertNotMatched(pattern, "test.stae");
  96. assertNotMatched(pattern, "test.sta9");
  97. //Test child directory/file is matched
  98. pattern = "/src/ne?";
  99. assertMatched(pattern, "/src/new/");
  100. assertMatched(pattern, "/src/new");
  101. assertMatched(pattern, "/src/new/a.c");
  102. assertMatched(pattern, "/src/new/a/a.c");
  103. assertNotMatched(pattern, "/src/new.c");
  104. //Test name-only fnmatcher matches
  105. pattern = "ne?";
  106. assertMatched(pattern, "/src/new/");
  107. assertMatched(pattern, "/src/new");
  108. assertMatched(pattern, "/src/new/a.c");
  109. assertMatched(pattern, "/src/new/a/a.c");
  110. assertMatched(pattern, "/neb");
  111. assertNotMatched(pattern, "/src/new.c");
  112. }
  113. @Test
  114. public void testTargetWithoutLeadingSlash() {
  115. //Test basic * and ? for any pattern + any character
  116. String pattern = "/*.st?";
  117. assertMatched(pattern, "test.stp");
  118. assertMatched(pattern, "anothertest.stg");
  119. assertMatched(pattern, "anothertest.st0");
  120. assertNotMatched(pattern, "anothertest.sta1");
  121. //Check that asterisk does not expand to ""
  122. assertNotMatched(pattern, "another/test.sta1");
  123. //Same as above, with a leading slash to ensure that doesn't cause problems
  124. pattern = "/*.st?";
  125. assertMatched(pattern, "test.stp");
  126. assertMatched(pattern, "anothertest.stg");
  127. assertMatched(pattern, "anothertest.st0");
  128. assertNotMatched(pattern, "anothertest.sta1");
  129. //Check that asterisk does not expand to ""
  130. assertNotMatched(pattern, "another/test.sta1");
  131. //Test for numbers
  132. pattern = "/*.sta[0-5]";
  133. assertMatched(pattern, "test.sta5");
  134. assertMatched(pattern, "test.sta4");
  135. assertMatched(pattern, "test.sta3");
  136. assertMatched(pattern, "test.sta2");
  137. assertMatched(pattern, "test.sta1");
  138. assertMatched(pattern, "test.sta0");
  139. assertMatched(pattern, "anothertest.sta2");
  140. assertNotMatched(pattern, "test.stag");
  141. assertNotMatched(pattern, "test.sta6");
  142. //Test for letters
  143. pattern = "/[tv]est.sta[a-d]";
  144. assertMatched(pattern, "test.staa");
  145. assertMatched(pattern, "test.stab");
  146. assertMatched(pattern, "test.stac");
  147. assertMatched(pattern, "test.stad");
  148. assertMatched(pattern, "vest.stac");
  149. assertNotMatched(pattern, "test.stae");
  150. assertNotMatched(pattern, "test.sta9");
  151. //Test child directory/file is matched
  152. pattern = "/src/ne?";
  153. assertMatched(pattern, "src/new/");
  154. assertMatched(pattern, "src/new");
  155. assertMatched(pattern, "src/new/a.c");
  156. assertMatched(pattern, "src/new/a/a.c");
  157. assertNotMatched(pattern, "src/new.c");
  158. //Test name-only fnmatcher matches
  159. pattern = "ne?";
  160. assertMatched(pattern, "src/new/");
  161. assertMatched(pattern, "src/new");
  162. assertMatched(pattern, "src/new/a.c");
  163. assertMatched(pattern, "src/new/a/a.c");
  164. assertMatched(pattern, "neb");
  165. assertNotMatched(pattern, "src/new.c");
  166. }
  167. @Test
  168. public void testParentDirectoryGitIgnores() {
  169. //Contains git ignore patterns such as might be seen in a parent directory
  170. //Test for wildcards
  171. String pattern = "/*/*.c";
  172. assertMatched(pattern, "/file/a.c");
  173. assertMatched(pattern, "/src/a.c");
  174. assertNotMatched(pattern, "/src/new/a.c");
  175. //Test child directory/file is matched
  176. pattern = "/src/new";
  177. assertMatched(pattern, "/src/new/");
  178. assertMatched(pattern, "/src/new");
  179. assertMatched(pattern, "/src/new/a.c");
  180. assertMatched(pattern, "/src/new/a/a.c");
  181. assertNotMatched(pattern, "/src/new.c");
  182. //Test child directory is matched, slash after name
  183. pattern = "/src/new/";
  184. assertMatched(pattern, "/src/new/");
  185. assertMatched(pattern, "/src/new/a.c");
  186. assertMatched(pattern, "/src/new/a/a.c");
  187. assertNotMatched(pattern, "/src/new");
  188. assertNotMatched(pattern, "/src/new.c");
  189. //Test directory is matched by name only
  190. pattern = "b1";
  191. assertMatched(pattern, "/src/new/a/b1/a.c");
  192. assertNotMatched(pattern, "/src/new/a/b2/file.c");
  193. assertNotMatched(pattern, "/src/new/a/bb1/file.c");
  194. assertNotMatched(pattern, "/src/new/a/file.c");
  195. }
  196. @Test
  197. public void testTrailingSlash() {
  198. String pattern = "/src/";
  199. assertMatched(pattern, "/src/");
  200. assertMatched(pattern, "/src/new");
  201. assertMatched(pattern, "/src/new/a.c");
  202. assertMatched(pattern, "/src/a.c");
  203. assertNotMatched(pattern, "/src");
  204. assertNotMatched(pattern, "/srcA/");
  205. }
  206. @Test
  207. public void testNameOnlyMatches() {
  208. /*
  209. * Name-only matches do not contain any path separators
  210. */
  211. //Test matches for file extension
  212. String pattern = "*.stp";
  213. assertMatched(pattern, "/test.stp");
  214. assertMatched(pattern, "/src/test.stp");
  215. assertNotMatched(pattern, "/test.stp1");
  216. assertNotMatched(pattern, "/test.astp");
  217. //Test matches for name-only, applies to file name or folder name
  218. pattern = "src";
  219. assertMatched(pattern, "/src/a.c");
  220. assertMatched(pattern, "/src/new/a.c");
  221. assertMatched(pattern, "/new/src/a.c");
  222. assertMatched(pattern, "/file/src");
  223. assertMatched(pattern, "/src/");
  224. //Test matches for name-only, applies to file name or folder name
  225. //With a small wildcard
  226. pattern = "?rc";
  227. assertMatched(pattern, "/src/a.c");
  228. assertMatched(pattern, "/src/new/a.c");
  229. assertMatched(pattern, "/new/src/a.c");
  230. assertMatched(pattern, "/file/src");
  231. assertMatched(pattern, "/src/");
  232. //Test matches for name-only, applies to file name or folder name
  233. //With a small wildcard
  234. pattern = "?r[a-c]";
  235. assertMatched(pattern, "/src/a.c");
  236. assertMatched(pattern, "/src/new/a.c");
  237. assertMatched(pattern, "/new/src/a.c");
  238. assertMatched(pattern, "/file/src");
  239. assertMatched(pattern, "/src/");
  240. assertMatched(pattern, "/srb/a.c");
  241. assertMatched(pattern, "/grb/new/a.c");
  242. assertMatched(pattern, "/new/crb/a.c");
  243. assertMatched(pattern, "/file/3rb");
  244. assertMatched(pattern, "/xrb/");
  245. assertMatched(pattern, "/3ra/a.c");
  246. assertMatched(pattern, "/5ra/new/a.c");
  247. assertMatched(pattern, "/new/1ra/a.c");
  248. assertMatched(pattern, "/file/dra");
  249. assertMatched(pattern, "/era/");
  250. assertNotMatched(pattern, "/crg");
  251. assertNotMatched(pattern, "/cr3");
  252. }
  253. @Test
  254. public void testNegation() {
  255. String pattern = "!/test.stp";
  256. assertMatched(pattern, "/test.stp");
  257. }
  258. @Test
  259. public void testGetters() {
  260. IgnoreRule r = new IgnoreRule("/pattern/");
  261. assertFalse(r.getNameOnly());
  262. assertTrue(r.dirOnly());
  263. assertFalse(r.getNegation());
  264. assertEquals(r.getPattern(), "/pattern");
  265. r = new IgnoreRule("/patter?/");
  266. assertFalse(r.getNameOnly());
  267. assertTrue(r.dirOnly());
  268. assertFalse(r.getNegation());
  269. assertEquals(r.getPattern(), "/patter?");
  270. r = new IgnoreRule("patt*");
  271. assertTrue(r.getNameOnly());
  272. assertFalse(r.dirOnly());
  273. assertFalse(r.getNegation());
  274. assertEquals(r.getPattern(), "patt*");
  275. r = new IgnoreRule("pattern");
  276. assertTrue(r.getNameOnly());
  277. assertFalse(r.dirOnly());
  278. assertFalse(r.getNegation());
  279. assertEquals(r.getPattern(), "pattern");
  280. r = new IgnoreRule("!pattern");
  281. assertTrue(r.getNameOnly());
  282. assertFalse(r.dirOnly());
  283. assertTrue(r.getNegation());
  284. assertEquals(r.getPattern(), "pattern");
  285. r = new IgnoreRule("!/pattern");
  286. assertFalse(r.getNameOnly());
  287. assertFalse(r.dirOnly());
  288. assertTrue(r.getNegation());
  289. assertEquals(r.getPattern(), "/pattern");
  290. r = new IgnoreRule("!/patter?");
  291. assertFalse(r.getNameOnly());
  292. assertFalse(r.dirOnly());
  293. assertTrue(r.getNegation());
  294. assertEquals(r.getPattern(), "/patter?");
  295. }
  296. /**
  297. * Check for a match. If target ends with "/", match will assume that the
  298. * target is meant to be a directory.
  299. * @param pattern
  300. * Pattern as it would appear in a .gitignore file
  301. * @param target
  302. * Target file path relative to repository's GIT_DIR
  303. */
  304. public void assertMatched(String pattern, String target) {
  305. boolean value = match(pattern, target);
  306. assertTrue("Expected a match for: " + pattern + " with: " + target,
  307. value);
  308. }
  309. /**
  310. * Check for a match. If target ends with "/", match will assume that the
  311. * target is meant to be a directory.
  312. * @param pattern
  313. * Pattern as it would appear in a .gitignore file
  314. * @param target
  315. * Target file path relative to repository's GIT_DIR
  316. */
  317. public void assertNotMatched(String pattern, String target) {
  318. boolean value = match(pattern, target);
  319. assertFalse("Expected no match for: " + pattern + " with: " + target,
  320. value);
  321. }
  322. /**
  323. * Check for a match. If target ends with "/", match will assume that the
  324. * target is meant to be a directory.
  325. * @param pattern
  326. * Pattern as it would appear in a .gitignore file
  327. * @param target
  328. * Target file path relative to repository's GIT_DIR
  329. * @return
  330. * Result of {@link IgnoreRule#isMatch(String, boolean)}
  331. */
  332. private boolean match(String pattern, String target) {
  333. IgnoreRule r = new IgnoreRule(pattern);
  334. //If speed of this test is ever an issue, we can use a presetRule field
  335. //to avoid recompiling a pattern each time.
  336. return r.isMatch(target, target.endsWith("/"));
  337. }
  338. }