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.

IgnoreMatcherParametrizedTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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.assertFalse;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assume.assumeFalse;
  47. import static org.junit.Assume.assumeTrue;
  48. import java.util.Arrays;
  49. import org.eclipse.jgit.junit.Assert;
  50. import org.junit.Test;
  51. import org.junit.runner.RunWith;
  52. import org.junit.runners.Parameterized;
  53. import org.junit.runners.Parameterized.Parameter;
  54. import org.junit.runners.Parameterized.Parameters;
  55. /**
  56. * Tests ignore pattern matches
  57. */
  58. @SuppressWarnings("deprecation")
  59. @RunWith(Parameterized.class)
  60. public class IgnoreMatcherParametrizedTest {
  61. @Parameters(name = "OldRule? {0}")
  62. public static Iterable<Boolean[]> data() {
  63. return Arrays.asList(new Boolean[][] { { Boolean.FALSE },
  64. { Boolean.TRUE } });
  65. }
  66. @Parameter
  67. public Boolean useOldRule;
  68. @Test
  69. public void testBasic() {
  70. String pattern = "/test.stp";
  71. assertMatched(pattern, "/test.stp");
  72. pattern = "#/test.stp";
  73. assertNotMatched(pattern, "/test.stp");
  74. }
  75. @Test
  76. public void testFileNameWildcards() {
  77. // Test basic * and ? for any pattern + any character
  78. String pattern = "*.st?";
  79. assertMatched(pattern, "/test.stp");
  80. assertMatched(pattern, "/anothertest.stg");
  81. assertMatched(pattern, "/anothertest.st0");
  82. assertNotMatched(pattern, "/anothertest.sta1");
  83. // Check that asterisk does not expand to "/"
  84. assertNotMatched(pattern, "/another/test.sta1");
  85. // Same as above, with a leading slash to ensure that doesn't cause
  86. // problems
  87. pattern = "/*.st?";
  88. assertMatched(pattern, "/test.stp");
  89. assertMatched(pattern, "/anothertest.stg");
  90. assertMatched(pattern, "/anothertest.st0");
  91. assertNotMatched(pattern, "/anothertest.sta1");
  92. // Check that asterisk does not expand to "/"
  93. assertNotMatched(pattern, "/another/test.sta1");
  94. // Test for numbers
  95. pattern = "*.sta[0-5]";
  96. assertMatched(pattern, "/test.sta5");
  97. assertMatched(pattern, "/test.sta4");
  98. assertMatched(pattern, "/test.sta3");
  99. assertMatched(pattern, "/test.sta2");
  100. assertMatched(pattern, "/test.sta1");
  101. assertMatched(pattern, "/test.sta0");
  102. assertMatched(pattern, "/anothertest.sta2");
  103. assertNotMatched(pattern, "test.stag");
  104. assertNotMatched(pattern, "test.sta6");
  105. // Test for letters
  106. pattern = "/[tv]est.sta[a-d]";
  107. assertMatched(pattern, "/test.staa");
  108. assertMatched(pattern, "/test.stab");
  109. assertMatched(pattern, "/test.stac");
  110. assertMatched(pattern, "/test.stad");
  111. assertMatched(pattern, "/vest.stac");
  112. assertNotMatched(pattern, "test.stae");
  113. assertNotMatched(pattern, "test.sta9");
  114. // Test child directory/file is matched
  115. pattern = "/src/ne?";
  116. assertMatched(pattern, "/src/new/");
  117. assertMatched(pattern, "/src/new");
  118. assertMatched(pattern, "/src/new/a.c");
  119. assertMatched(pattern, "/src/new/a/a.c");
  120. assertNotMatched(pattern, "/src/new.c");
  121. // Test name-only fnmatcher matches
  122. pattern = "ne?";
  123. assertMatched(pattern, "/src/new/");
  124. assertMatched(pattern, "/src/new");
  125. assertMatched(pattern, "/src/new/a.c");
  126. assertMatched(pattern, "/src/new/a/a.c");
  127. assertMatched(pattern, "/neb");
  128. assertNotMatched(pattern, "/src/new.c");
  129. }
  130. @Test
  131. public void testTargetWithoutLeadingSlash() {
  132. // Test basic * and ? for any pattern + any character
  133. String pattern = "/*.st?";
  134. assertMatched(pattern, "test.stp");
  135. assertMatched(pattern, "anothertest.stg");
  136. assertMatched(pattern, "anothertest.st0");
  137. assertNotMatched(pattern, "anothertest.sta1");
  138. // Check that asterisk does not expand to ""
  139. assertNotMatched(pattern, "another/test.sta1");
  140. // Same as above, with a leading slash to ensure that doesn't cause
  141. // problems
  142. pattern = "/*.st?";
  143. assertMatched(pattern, "test.stp");
  144. assertMatched(pattern, "anothertest.stg");
  145. assertMatched(pattern, "anothertest.st0");
  146. assertNotMatched(pattern, "anothertest.sta1");
  147. // Check that asterisk does not expand to ""
  148. assertNotMatched(pattern, "another/test.sta1");
  149. // Test for numbers
  150. pattern = "/*.sta[0-5]";
  151. assertMatched(pattern, "test.sta5");
  152. assertMatched(pattern, "test.sta4");
  153. assertMatched(pattern, "test.sta3");
  154. assertMatched(pattern, "test.sta2");
  155. assertMatched(pattern, "test.sta1");
  156. assertMatched(pattern, "test.sta0");
  157. assertMatched(pattern, "anothertest.sta2");
  158. assertNotMatched(pattern, "test.stag");
  159. assertNotMatched(pattern, "test.sta6");
  160. // Test for letters
  161. pattern = "/[tv]est.sta[a-d]";
  162. assertMatched(pattern, "test.staa");
  163. assertMatched(pattern, "test.stab");
  164. assertMatched(pattern, "test.stac");
  165. assertMatched(pattern, "test.stad");
  166. assertMatched(pattern, "vest.stac");
  167. assertNotMatched(pattern, "test.stae");
  168. assertNotMatched(pattern, "test.sta9");
  169. // Test child directory/file is matched
  170. pattern = "/src/ne?";
  171. assertMatched(pattern, "src/new/");
  172. assertMatched(pattern, "src/new");
  173. assertMatched(pattern, "src/new/a.c");
  174. assertMatched(pattern, "src/new/a/a.c");
  175. assertNotMatched(pattern, "src/new.c");
  176. // Test name-only fnmatcher matches
  177. pattern = "ne?";
  178. assertMatched(pattern, "src/new/");
  179. assertMatched(pattern, "src/new");
  180. assertMatched(pattern, "src/new/a.c");
  181. assertMatched(pattern, "src/new/a/a.c");
  182. assertMatched(pattern, "neb");
  183. assertNotMatched(pattern, "src/new.c");
  184. }
  185. @Test
  186. public void testParentDirectoryGitIgnores() {
  187. // Contains git ignore patterns such as might be seen in a parent
  188. // directory
  189. // Test for wildcards
  190. String pattern = "/*/*.c";
  191. assertMatched(pattern, "/file/a.c");
  192. assertMatched(pattern, "/src/a.c");
  193. assertNotMatched(pattern, "/src/new/a.c");
  194. // Test child directory/file is matched
  195. pattern = "/src/new";
  196. assertMatched(pattern, "/src/new/");
  197. assertMatched(pattern, "/src/new");
  198. assertMatched(pattern, "/src/new/a.c");
  199. assertMatched(pattern, "/src/new/a/a.c");
  200. assertNotMatched(pattern, "/src/new.c");
  201. // Test child directory is matched, slash after name
  202. pattern = "/src/new/";
  203. assertMatched(pattern, "/src/new/");
  204. assertMatched(pattern, "/src/new/a.c");
  205. assertMatched(pattern, "/src/new/a/a.c");
  206. assertNotMatched(pattern, "/src/new");
  207. assertNotMatched(pattern, "/src/new.c");
  208. // Test directory is matched by name only
  209. pattern = "b1";
  210. assertMatched(pattern, "/src/new/a/b1/a.c");
  211. assertNotMatched(pattern, "/src/new/a/b2/file.c");
  212. assertNotMatched(pattern, "/src/new/a/bb1/file.c");
  213. assertNotMatched(pattern, "/src/new/a/file.c");
  214. }
  215. @Test
  216. public void testDirModeAndNoRegex() {
  217. String pattern = "/src/";
  218. assertMatched(pattern, "/src/");
  219. assertMatched(pattern, "/src/new");
  220. assertMatched(pattern, "/src/new/a.c");
  221. assertMatched(pattern, "/src/a.c");
  222. // no match as a "file" pattern, because rule is for directories only
  223. assertNotMatched(pattern, "/src");
  224. assertNotMatched(pattern, "/srcA/");
  225. }
  226. @Test
  227. public void testDirModeAndRegex1() {
  228. // IgnoreRule was buggy for some cases below, therefore using "Assume"
  229. Boolean assume = useOldRule;
  230. String pattern = "a/*/src/";
  231. assertMatched(pattern, "a/b/src/");
  232. assertMatched(pattern, "a/b/src/new");
  233. assertMatched(pattern, "a/b/src/new/a.c");
  234. assertMatched(pattern, "a/b/src/a.c");
  235. // no match as a "file" pattern, because rule is for directories only
  236. assertNotMatched(pattern, "a/b/src", assume);
  237. assertNotMatched(pattern, "a/b/srcA/");
  238. }
  239. @Test
  240. public void testDirModeAndRegex2() {
  241. // IgnoreRule was buggy for some cases below, therefore using "Assume"
  242. Boolean assume = useOldRule;
  243. String pattern = "a/[a-b]/src/";
  244. assertMatched(pattern, "a/b/src/");
  245. assertMatched(pattern, "a/b/src/new");
  246. assertMatched(pattern, "a/b/src/new/a.c");
  247. assertMatched(pattern, "a/b/src/a.c");
  248. // no match as a "file" pattern, because rule is for directories only
  249. assertNotMatched(pattern, "a/b/src", assume);
  250. assertNotMatched(pattern, "a/b/srcA/");
  251. }
  252. @Test
  253. public void testDirModeAndRegex3() {
  254. // IgnoreRule was buggy for some cases below, therefore using "Assume"
  255. Boolean assume = useOldRule;
  256. String pattern = "**/src/";
  257. assertMatched(pattern, "a/b/src/", assume);
  258. assertMatched(pattern, "a/b/src/new", assume);
  259. assertMatched(pattern, "a/b/src/new/a.c", assume);
  260. assertMatched(pattern, "a/b/src/a.c", assume);
  261. // no match as a "file" pattern, because rule is for directories only
  262. assertNotMatched(pattern, "a/b/src", assume);
  263. assertNotMatched(pattern, "a/b/srcA/", assume);
  264. }
  265. @Test
  266. public void testNameOnlyMatches() {
  267. /*
  268. * Name-only matches do not contain any path separators
  269. */
  270. // Test matches for file extension
  271. String pattern = "*.stp";
  272. assertMatched(pattern, "/test.stp");
  273. assertMatched(pattern, "/src/test.stp");
  274. assertNotMatched(pattern, "/test.stp1");
  275. assertNotMatched(pattern, "/test.astp");
  276. // Test matches for name-only, applies to file name or folder name
  277. pattern = "src";
  278. assertMatched(pattern, "/src");
  279. assertMatched(pattern, "/src/");
  280. assertMatched(pattern, "/src/a.c");
  281. assertMatched(pattern, "/src/new/a.c");
  282. assertMatched(pattern, "/new/src/a.c");
  283. assertMatched(pattern, "/file/src");
  284. // Test matches for name-only, applies only to folder names
  285. pattern = "src/";
  286. assertMatched(pattern, "/src/");
  287. assertMatched(pattern, "/src/a.c");
  288. assertMatched(pattern, "/src/new/a.c");
  289. assertMatched(pattern, "/new/src/a.c");
  290. assertNotMatched(pattern, "/src");
  291. assertNotMatched(pattern, "/file/src");
  292. // Test matches for name-only, applies to file name or folder name
  293. // With a small wildcard
  294. pattern = "?rc";
  295. assertMatched(pattern, "/src/a.c");
  296. assertMatched(pattern, "/src/new/a.c");
  297. assertMatched(pattern, "/new/src/a.c");
  298. assertMatched(pattern, "/file/src");
  299. assertMatched(pattern, "/src/");
  300. // Test matches for name-only, applies to file name or folder name
  301. // With a small wildcard
  302. pattern = "?r[a-c]";
  303. assertMatched(pattern, "/src/a.c");
  304. assertMatched(pattern, "/src/new/a.c");
  305. assertMatched(pattern, "/new/src/a.c");
  306. assertMatched(pattern, "/file/src");
  307. assertMatched(pattern, "/src/");
  308. assertMatched(pattern, "/srb/a.c");
  309. assertMatched(pattern, "/grb/new/a.c");
  310. assertMatched(pattern, "/new/crb/a.c");
  311. assertMatched(pattern, "/file/3rb");
  312. assertMatched(pattern, "/xrb/");
  313. assertMatched(pattern, "/3ra/a.c");
  314. assertMatched(pattern, "/5ra/new/a.c");
  315. assertMatched(pattern, "/new/1ra/a.c");
  316. assertMatched(pattern, "/file/dra");
  317. assertMatched(pattern, "/era/");
  318. assertNotMatched(pattern, "/crg");
  319. assertNotMatched(pattern, "/cr3");
  320. }
  321. @Test
  322. public void testNegation() {
  323. String pattern = "!/test.stp";
  324. assertMatched(pattern, "/test.stp");
  325. }
  326. /**
  327. * Check for a match. If target ends with "/", match will assume that the
  328. * target is meant to be a directory.
  329. *
  330. * @param pattern
  331. * Pattern as it would appear in a .gitignore file
  332. * @param target
  333. * Target file path relative to repository's GIT_DIR
  334. * @param assume
  335. */
  336. public void assertMatched(String pattern, String target, Boolean... assume) {
  337. boolean value = match(pattern, target);
  338. if (assume.length == 0 || !assume[0].booleanValue())
  339. assertTrue("Expected a match for: " + pattern + " with: " + target,
  340. value);
  341. else
  342. assumeTrue("Expected a match for: " + pattern + " with: " + target,
  343. value);
  344. }
  345. /**
  346. * Check for a match. If target ends with "/", match will assume that the
  347. * target is meant to be a directory.
  348. *
  349. * @param pattern
  350. * Pattern as it would appear in a .gitignore file
  351. * @param target
  352. * Target file path relative to repository's GIT_DIR
  353. * @param assume
  354. */
  355. public void assertNotMatched(String pattern, String target,
  356. Boolean... assume) {
  357. boolean value = match(pattern, target);
  358. if (assume.length == 0 || !assume[0].booleanValue())
  359. assertFalse("Expected no match for: " + pattern + " with: "
  360. + target, value);
  361. else
  362. assumeFalse("Expected no match for: " + pattern + " with: "
  363. + target, value);
  364. }
  365. /**
  366. * Check for a match. If target ends with "/", match will assume that the
  367. * target is meant to be a directory.
  368. *
  369. * @param pattern
  370. * Pattern as it would appear in a .gitignore file
  371. * @param target
  372. * Target file path relative to repository's GIT_DIR
  373. * @return Result of {@link FastIgnoreRule#isMatch(String, boolean)}
  374. */
  375. private boolean match(String pattern, String target) {
  376. boolean isDirectory = target.endsWith("/");
  377. boolean match;
  378. if (useOldRule.booleanValue()) {
  379. IgnoreRule r = new IgnoreRule(pattern);
  380. match = r.isMatch(target, isDirectory);
  381. } else {
  382. FastIgnoreRule r = new FastIgnoreRule(pattern);
  383. match = r.isMatch(target, isDirectory);
  384. }
  385. if (isDirectory) {
  386. boolean noTrailingSlash = matchAsDir(pattern,
  387. target.substring(0, target.length() - 1));
  388. if (match != noTrailingSlash) {
  389. String message = "Difference in result for directory pattern: "
  390. + pattern + " with: " + target
  391. + " if target is given without trailing slash";
  392. Assert.assertEquals(message, match, noTrailingSlash);
  393. }
  394. }
  395. return match;
  396. }
  397. /**
  398. *
  399. * @param target
  400. * must not ends with a slash!
  401. * @param pattern
  402. * same as {@link #match(String, String)}
  403. * @return same as {@link #match(String, String)}
  404. */
  405. private boolean matchAsDir(String pattern, String target) {
  406. assertFalse(target.endsWith("/"));
  407. if (useOldRule.booleanValue()) {
  408. IgnoreRule r = new IgnoreRule(pattern);
  409. return r.isMatch(target, true);
  410. }
  411. FastIgnoreRule r = new FastIgnoreRule(pattern);
  412. return r.isMatch(target, true);
  413. }
  414. }