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

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