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.

FastIgnoreRuleTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de>
  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.eclipse.jgit.ignore.internal.Strings.split;
  45. import static org.junit.Assert.assertArrayEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import org.junit.Test;
  50. public class FastIgnoreRuleTest {
  51. @Test
  52. public void testSimpleCharClass() {
  53. assertMatched("][a]", "]a");
  54. assertMatched("[a]", "a");
  55. assertMatched("][a]", "]a");
  56. assertMatched("[a]", "a/");
  57. assertMatched("[a]", "a/b");
  58. assertMatched("[a]", "b/a");
  59. assertMatched("[a]", "b/a/");
  60. assertMatched("[a]", "b/a/b");
  61. assertMatched("[a]", "/a/");
  62. assertMatched("[a]", "/a/b");
  63. assertMatched("[a]", "c/a/b");
  64. assertMatched("[a]", "c/b/a");
  65. assertMatched("/[a]", "a");
  66. assertMatched("/[a]", "a/");
  67. assertMatched("/[a]", "a/b");
  68. assertMatched("/[a]", "/a");
  69. assertMatched("/[a]", "/a/");
  70. assertMatched("/[a]", "/a/b");
  71. assertMatched("[a]/", "a/");
  72. assertMatched("[a]/", "a/b");
  73. assertMatched("[a]/", "/a/");
  74. assertMatched("[a]/", "/a/b");
  75. assertMatched("/[a]/", "a/");
  76. assertMatched("/[a]/", "a/b");
  77. assertMatched("/[a]/", "/a/");
  78. assertMatched("/[a]/", "/a/b");
  79. }
  80. @Test
  81. public void testCharClass() {
  82. assertMatched("[v-z]", "x");
  83. assertMatched("[v-z]", "x/");
  84. assertMatched("[v-z]", "x/b");
  85. assertMatched("[v-z]", "b/x");
  86. assertMatched("[v-z]", "b/x/");
  87. assertMatched("[v-z]", "b/x/b");
  88. assertMatched("[v-z]", "/x/");
  89. assertMatched("[v-z]", "/x/b");
  90. assertMatched("[v-z]", "c/x/b");
  91. assertMatched("[v-z]", "c/b/x");
  92. assertMatched("/[v-z]", "x");
  93. assertMatched("/[v-z]", "x/");
  94. assertMatched("/[v-z]", "x/b");
  95. assertMatched("/[v-z]", "/x");
  96. assertMatched("/[v-z]", "/x/");
  97. assertMatched("/[v-z]", "/x/b");
  98. assertMatched("[v-z]/", "x/");
  99. assertMatched("[v-z]/", "x/b");
  100. assertMatched("[v-z]/", "/x/");
  101. assertMatched("[v-z]/", "/x/b");
  102. assertMatched("/[v-z]/", "x/");
  103. assertMatched("/[v-z]/", "x/b");
  104. assertMatched("/[v-z]/", "/x/");
  105. assertMatched("/[v-z]/", "/x/b");
  106. }
  107. @Test
  108. public void testTrailingSpaces() {
  109. assertMatched("a ", "a");
  110. assertMatched("a/ ", "a/");
  111. assertMatched("a/ ", "a/b");
  112. assertMatched("a/\\ ", "a/ ");
  113. assertNotMatched("a/\\ ", "a/");
  114. assertNotMatched("a/\\ ", "a/b");
  115. assertNotMatched("/ ", "a");
  116. }
  117. @Test
  118. public void testAsteriskDot() {
  119. assertMatched("*.a", ".a");
  120. assertMatched("*.a", "/.a");
  121. assertMatched("*.a", "a.a");
  122. assertMatched("*.a", "/b.a");
  123. assertMatched("*.a", "b.a");
  124. assertMatched("*.a", "/a/b.a");
  125. assertMatched("*.a", "/b/.a");
  126. }
  127. @Test
  128. public void testAsteriskDotDoNotMatch() {
  129. assertNotMatched("*.a", ".ab");
  130. assertNotMatched("*.a", "/.ab");
  131. assertNotMatched("*.a", "/b.ba");
  132. assertNotMatched("*.a", "a.ab");
  133. assertNotMatched("*.a", "/b.ab");
  134. assertNotMatched("*.a", "b.ab");
  135. assertNotMatched("*.a", "/a/b.ab");
  136. assertNotMatched("*.a", "/b/.ab");
  137. }
  138. @Test
  139. public void testDotAsteriskMatch() {
  140. assertMatched("a.*", "a.");
  141. assertMatched("a.*", "a./");
  142. assertMatched("a.*", "a.b");
  143. assertMatched("a.*", "b/a.b");
  144. assertMatched("a.*", "b/a.b/");
  145. assertMatched("a.*", "b/a.b/b");
  146. assertMatched("a.*", "/a.b/");
  147. assertMatched("a.*", "/a.b/b");
  148. assertMatched("a.*", "c/a.b/b");
  149. assertMatched("a.*", "c/b/a.b");
  150. assertMatched("/a.*", "a.b");
  151. assertMatched("/a.*", "a.b/");
  152. assertMatched("/a.*", "a.b/b");
  153. assertMatched("/a.*", "/a.b");
  154. assertMatched("/a.*", "/a.b/");
  155. assertMatched("/a.*", "/a.b/b");
  156. assertMatched("/a.*/b", "a.b/b");
  157. assertMatched("/a.*/b", "/a.b/b");
  158. assertMatched("/a.*/b", "/a.bc/b");
  159. assertMatched("/a.*/b", "/a./b");
  160. }
  161. @Test
  162. public void testAsterisk() {
  163. assertMatched("a*", "a");
  164. assertMatched("a*", "a/");
  165. assertMatched("a*", "ab");
  166. assertMatched("a*", "b/ab");
  167. assertMatched("a*", "b/ab/");
  168. assertMatched("a*", "b/ab/b");
  169. assertMatched("a*", "b/abc");
  170. assertMatched("a*", "b/abc/");
  171. assertMatched("a*", "b/abc/b");
  172. assertMatched("a*", "/abc/");
  173. assertMatched("a*", "/abc/b");
  174. assertMatched("a*", "c/abc/b");
  175. assertMatched("a*", "c/b/abc");
  176. assertMatched("/a*", "abc");
  177. assertMatched("/a*", "abc/");
  178. assertMatched("/a*", "abc/b");
  179. assertMatched("/a*", "/abc");
  180. assertMatched("/a*", "/abc/");
  181. assertMatched("/a*", "/abc/b");
  182. assertMatched("/a*/b", "abc/b");
  183. assertMatched("/a*/b", "/abc/b");
  184. assertMatched("/a*/b", "/abcd/b");
  185. assertMatched("/a*/b", "/a/b");
  186. }
  187. @Test
  188. public void testQuestionmark() {
  189. assertMatched("a?", "ab");
  190. assertMatched("a?", "ab/");
  191. assertMatched("a?", "b/ab");
  192. assertMatched("a?", "b/ab/");
  193. assertMatched("a?", "b/ab/b");
  194. assertMatched("a?", "/ab/");
  195. assertMatched("a?", "/ab/b");
  196. assertMatched("a?", "c/ab/b");
  197. assertMatched("a?", "c/b/ab");
  198. assertMatched("/a?", "ab");
  199. assertMatched("/a?", "ab/");
  200. assertMatched("/a?", "ab/b");
  201. assertMatched("/a?", "/ab");
  202. assertMatched("/a?", "/ab/");
  203. assertMatched("/a?", "/ab/b");
  204. assertMatched("/a?/b", "ab/b");
  205. assertMatched("/a?/b", "/ab/b");
  206. }
  207. @Test
  208. public void testQuestionmarkDoNotMatch() {
  209. assertNotMatched("a?", "a/");
  210. assertNotMatched("a?", "abc");
  211. assertNotMatched("a?", "abc/");
  212. assertNotMatched("a?", "b/abc");
  213. assertNotMatched("a?", "b/abc/");
  214. assertNotMatched("a?", "/abc/");
  215. assertNotMatched("a?", "/abc/b");
  216. assertNotMatched("a?", "c/abc/b");
  217. assertNotMatched("a?", "c/b/abc");
  218. assertNotMatched("/a?", "abc");
  219. assertNotMatched("/a?", "abc/");
  220. assertNotMatched("/a?", "abc/b");
  221. assertNotMatched("/a?", "/abc");
  222. assertNotMatched("/a?", "/abc/");
  223. assertNotMatched("/a?", "/abc/b");
  224. assertNotMatched("/a?/b", "abc/b");
  225. assertNotMatched("/a?/b", "/abc/b");
  226. assertNotMatched("/a?/b", "/a/b");
  227. }
  228. @Test
  229. public void testSimplePatterns() {
  230. assertMatched("a", "a");
  231. assertMatched("a", "a/");
  232. assertMatched("a", "a/b");
  233. assertMatched("a", "b/a");
  234. assertMatched("a", "b/a/");
  235. assertMatched("a", "b/a/b");
  236. assertMatched("a", "/a/");
  237. assertMatched("a", "/a/b");
  238. assertMatched("a", "c/a/b");
  239. assertMatched("a", "c/b/a");
  240. assertMatched("/a", "a");
  241. assertMatched("/a", "a/");
  242. assertMatched("/a", "a/b");
  243. assertMatched("/a", "/a");
  244. assertMatched("/a", "/a/");
  245. assertMatched("/a", "/a/b");
  246. assertMatched("a/", "a/");
  247. assertMatched("a/", "a/b");
  248. assertMatched("a/", "/a/");
  249. assertMatched("a/", "/a/b");
  250. assertMatched("/a/", "a/");
  251. assertMatched("/a/", "a/b");
  252. assertMatched("/a/", "/a/");
  253. assertMatched("/a/", "/a/b");
  254. }
  255. @Test
  256. public void testSimplePatternsDoNotMatch() {
  257. assertNotMatched("ab", "a");
  258. assertNotMatched("abc", "a/");
  259. assertNotMatched("abc", "a/b");
  260. assertNotMatched("a", "ab");
  261. assertNotMatched("a", "ba");
  262. assertNotMatched("a", "aa");
  263. assertNotMatched("a", "b/ab");
  264. assertNotMatched("a", "b/ba");
  265. assertNotMatched("a", "b/ba");
  266. assertNotMatched("a", "b/ab");
  267. assertNotMatched("a", "b/ba/");
  268. assertNotMatched("a", "b/ba/b");
  269. assertNotMatched("a", "/aa");
  270. assertNotMatched("a", "aa/");
  271. assertNotMatched("a", "/aa/");
  272. assertNotMatched("/a", "b/a");
  273. assertNotMatched("/a", "/b/a/");
  274. assertNotMatched("a/", "a");
  275. assertNotMatched("a/", "b/a");
  276. assertNotMatched("/a/", "a");
  277. assertNotMatched("/a/", "/a");
  278. assertNotMatched("/a/", "b/a");
  279. }
  280. @Test
  281. public void testSegments() {
  282. assertMatched("/a/b", "a/b");
  283. assertMatched("/a/b", "/a/b");
  284. assertMatched("/a/b", "/a/b/");
  285. assertMatched("/a/b", "/a/b/c");
  286. assertMatched("a/b", "a/b");
  287. assertMatched("a/b", "/a/b");
  288. assertMatched("a/b", "/a/b/");
  289. assertMatched("a/b", "/a/b/c");
  290. assertMatched("a/b/", "a/b/");
  291. assertMatched("a/b/", "/a/b/");
  292. assertMatched("a/b/", "/a/b/c");
  293. }
  294. @Test
  295. public void testSegmentsDoNotMatch() {
  296. assertNotMatched("a/b", "/a/bb");
  297. assertNotMatched("a/b", "/aa/b");
  298. assertNotMatched("a/b", "a/bb");
  299. assertNotMatched("a/b", "aa/b");
  300. assertNotMatched("a/b", "c/aa/b");
  301. assertNotMatched("a/b", "c/a/bb");
  302. assertNotMatched("a/b/", "/a/b");
  303. assertNotMatched("/a/b/", "/a/b");
  304. assertNotMatched("/a/b", "c/a/b");
  305. assertNotMatched("/a/b/", "c/a/b");
  306. assertNotMatched("/a/b/", "c/a/b/");
  307. // XXX why is it like this????
  308. assertNotMatched("a/b", "c/a/b");
  309. assertNotMatched("a/b", "c/a/b/");
  310. assertNotMatched("a/b", "c/a/b/c");
  311. assertNotMatched("a/b/", "c/a/b/");
  312. assertNotMatched("a/b/", "c/a/b/c");
  313. }
  314. @Test
  315. public void testWildmatch() {
  316. assertMatched("**/a/b", "a/b");
  317. assertMatched("**/a/b", "c/a/b");
  318. assertMatched("**/a/b", "c/d/a/b");
  319. assertMatched("**/**/a/b", "c/d/a/b");
  320. assertMatched("/**/a/b", "a/b");
  321. assertMatched("/**/a/b", "c/a/b");
  322. assertMatched("/**/a/b", "c/d/a/b");
  323. assertMatched("/**/**/a/b", "c/d/a/b");
  324. assertMatched("a/b/**", "a/b");
  325. assertMatched("a/b/**", "a/b/c");
  326. assertMatched("a/b/**", "a/b/c/d/");
  327. assertMatched("a/b/**/**", "a/b/c/d");
  328. assertMatched("**/a/**/b", "c/d/a/b");
  329. assertMatched("**/a/**/b", "c/d/a/e/b");
  330. assertMatched("**/**/a/**/**/b", "c/d/a/e/b");
  331. assertMatched("/**/a/**/b", "c/d/a/b");
  332. assertMatched("/**/a/**/b", "c/d/a/e/b");
  333. assertMatched("/**/**/a/**/**/b", "c/d/a/e/b");
  334. assertMatched("a/**/b", "a/b");
  335. assertMatched("a/**/b", "a/c/b");
  336. assertMatched("a/**/b", "a/c/d/b");
  337. assertMatched("a/**/**/b", "a/c/d/b");
  338. assertMatched("a/**/b/**/c", "a/c/b/d/c");
  339. assertMatched("a/**/**/b/**/**/c", "a/c/b/d/c");
  340. }
  341. @Test
  342. public void testWildmatchDoNotMatch() {
  343. assertNotMatched("**/a/b", "a/c/b");
  344. assertNotMatched("!/**/*.zip", "c/a/b.zip");
  345. assertNotMatched("!**/*.zip", "c/a/b.zip");
  346. assertNotMatched("a/**/b", "a/c/bb");
  347. }
  348. @SuppressWarnings("unused")
  349. @Test
  350. public void testSimpleRules() {
  351. try {
  352. new FastIgnoreRule(null);
  353. fail("Illegal input allowed!");
  354. } catch (IllegalArgumentException e) {
  355. // expected
  356. }
  357. assertFalse(new FastIgnoreRule("/").isMatch("/", false));
  358. assertFalse(new FastIgnoreRule("//").isMatch("//", false));
  359. assertFalse(new FastIgnoreRule("#").isMatch("#", false));
  360. assertFalse(new FastIgnoreRule("").isMatch("", false));
  361. assertFalse(new FastIgnoreRule(" ").isMatch(" ", false));
  362. }
  363. @Test
  364. public void testSplit() {
  365. try {
  366. split("/", '/').toArray();
  367. fail("should not allow single slash");
  368. } catch (IllegalStateException e) {
  369. // expected
  370. }
  371. assertArrayEquals(new String[] { "a", "b" }, split("a/b", '/')
  372. .toArray());
  373. assertArrayEquals(new String[] { "a", "b/" }, split("a/b/", '/')
  374. .toArray());
  375. assertArrayEquals(new String[] { "/a", "b" }, split("/a/b", '/')
  376. .toArray());
  377. assertArrayEquals(new String[] { "/a", "b/" }, split("/a/b/", '/')
  378. .toArray());
  379. assertArrayEquals(new String[] { "/a", "b", "c" }, split("/a/b/c", '/')
  380. .toArray());
  381. assertArrayEquals(new String[] { "/a", "b", "c/" },
  382. split("/a/b/c/", '/').toArray());
  383. }
  384. public void assertMatched(String pattern, String path) {
  385. boolean match = match(pattern, path);
  386. String result = path + " is " + (match ? "ignored" : "not ignored")
  387. + " via '" + pattern + "' rule";
  388. if (!match) {
  389. System.err.println(result);
  390. }
  391. assertTrue("Expected a match for: " + pattern + " with: " + path,
  392. match);
  393. if (pattern.startsWith("!")) {
  394. pattern = pattern.substring(1);
  395. } else {
  396. pattern = "!" + pattern;
  397. }
  398. match = match(pattern, path);
  399. assertFalse("Expected no match for: " + pattern + " with: " + path,
  400. match);
  401. }
  402. public void assertNotMatched(String pattern, String path) {
  403. boolean match = match(pattern, path);
  404. String result = path + " is " + (match ? "ignored" : "not ignored")
  405. + " via '" + pattern + "' rule";
  406. if (match) {
  407. System.err.println(result);
  408. }
  409. assertFalse("Expected no match for: " + pattern + " with: " + path,
  410. match);
  411. if (pattern.startsWith("!")) {
  412. pattern = pattern.substring(1);
  413. } else {
  414. pattern = "!" + pattern;
  415. }
  416. match = match(pattern, path);
  417. assertTrue("Expected a match for: " + pattern + " with: " + path,
  418. match);
  419. }
  420. /**
  421. * Check for a match. If target ends with "/", match will assume that the
  422. * target is meant to be a directory.
  423. *
  424. * @param pattern
  425. * Pattern as it would appear in a .gitignore file
  426. * @param target
  427. * Target file path relative to repository's GIT_DIR
  428. * @return Result of {@link FastIgnoreRule#isMatch(String, boolean)}
  429. */
  430. private boolean match(String pattern, String target) {
  431. boolean isDirectory = target.endsWith("/");
  432. FastIgnoreRule r = new FastIgnoreRule(pattern);
  433. // If speed of this test is ever an issue, we can use a presetRule field
  434. // to avoid recompiling a pattern each time.
  435. boolean match = r.isMatch(target, isDirectory);
  436. if (r.getNegation())
  437. match = !match;
  438. return match;
  439. }
  440. }