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

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