Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IgnoreMatcherParametrizedTest.java 12KB

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