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.

CGitVsJGitRandomIgnorePatternTest.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2015, Sebastien Arod <sebastien.arod@gmail.com>
  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 java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.io.InputStreamReader;
  49. import java.io.OutputStream;
  50. import java.io.UnsupportedEncodingException;
  51. import java.nio.file.Files;
  52. import java.nio.file.StandardOpenOption;
  53. import java.util.Arrays;
  54. import java.util.List;
  55. import java.util.Random;
  56. import org.eclipse.jgit.api.Git;
  57. import org.junit.Assert;
  58. import org.junit.Test;
  59. /**
  60. * This test generates random ignore patterns and random path and compares the
  61. * output of Cgit check-ignore to the output of {@link FastIgnoreRule}.
  62. */
  63. public class CGitVsJGitRandomIgnorePatternTest {
  64. private static class PseudoRandomPatternGenerator {
  65. private static final int DEFAULT_MAX_FRAGMENTS_PER_PATTERN = 15;
  66. /**
  67. * Generates 75% Special fragments and 25% "standard" characters
  68. */
  69. private static final double DEFAULT_SPECIAL_FRAGMENTS_FREQUENCY = 0.75d;
  70. private static final List<String> SPECIAL_FRAGMENTS = Arrays.asList(
  71. "\\", "!", "#", "[", "]", "|", "/", "*", "?", "{", "}", "(",
  72. ")", "\\d", "(", "**", "[a\\]]", "\\ ", "+", "-", "^", "$", ".",
  73. ":", "=", "[[:", ":]]"
  74. );
  75. private static final String STANDARD_CHARACTERS = new String(
  76. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
  77. private final Random random = new Random();
  78. private final int maxFragmentsPerPattern;
  79. private final double specialFragmentsFrequency;
  80. public PseudoRandomPatternGenerator() {
  81. this(DEFAULT_MAX_FRAGMENTS_PER_PATTERN,
  82. DEFAULT_SPECIAL_FRAGMENTS_FREQUENCY);
  83. }
  84. public PseudoRandomPatternGenerator(int maxFragmentsPerPattern,
  85. double specialFragmentsFrequency) {
  86. this.maxFragmentsPerPattern = maxFragmentsPerPattern;
  87. this.specialFragmentsFrequency = specialFragmentsFrequency;
  88. }
  89. public String nextRandomString() {
  90. StringBuilder builder = new StringBuilder();
  91. int length = randomFragmentCount();
  92. for (int i = 0; i < length; i++) {
  93. if (useSpecialFragment()) {
  94. builder.append(randomSpecialFragment());
  95. } else {
  96. builder.append(randomStandardCharacters());
  97. }
  98. }
  99. return builder.toString();
  100. }
  101. private int randomFragmentCount() {
  102. // We want at least one fragment
  103. return 1 + random.nextInt(maxFragmentsPerPattern - 1);
  104. }
  105. private char randomStandardCharacters() {
  106. return STANDARD_CHARACTERS
  107. .charAt(random.nextInt(STANDARD_CHARACTERS.length()));
  108. }
  109. private boolean useSpecialFragment() {
  110. return random.nextDouble() < specialFragmentsFrequency;
  111. }
  112. private String randomSpecialFragment() {
  113. return SPECIAL_FRAGMENTS
  114. .get(random.nextInt(SPECIAL_FRAGMENTS.size()));
  115. }
  116. }
  117. @SuppressWarnings("serial")
  118. public static class CgitFatalException extends Exception {
  119. public CgitFatalException(int cgitExitCode, String pattern, String path,
  120. String cgitStdError) {
  121. super("CgitFatalException (" + cgitExitCode + ") for pattern:["
  122. + pattern + "] and path:[" + path + "]\n" + cgitStdError);
  123. }
  124. }
  125. public static class CGitIgnoreRule {
  126. private File gitDir;
  127. private String pattern;
  128. public CGitIgnoreRule(File gitDir, String pattern)
  129. throws UnsupportedEncodingException, IOException {
  130. this.gitDir = gitDir;
  131. this.pattern = pattern;
  132. Files.write(new File(gitDir, ".gitignore").toPath(),
  133. (pattern + "\n").getBytes("UTF-8"),
  134. StandardOpenOption.CREATE,
  135. StandardOpenOption.TRUNCATE_EXISTING,
  136. StandardOpenOption.WRITE);
  137. }
  138. public boolean isMatch(String path)
  139. throws IOException, InterruptedException, CgitFatalException {
  140. Process proc = startCgitCheckIgnore(path);
  141. String cgitStdOutput = readProcessStream(proc.getInputStream());
  142. String cgitStdError = readProcessStream(proc.getErrorStream());
  143. int cgitExitCode = proc.waitFor();
  144. if (cgitExitCode == 128) {
  145. throw new CgitFatalException(cgitExitCode, pattern, path,
  146. cgitStdError);
  147. }
  148. return !cgitStdOutput.startsWith("::");
  149. }
  150. private Process startCgitCheckIgnore(String path) throws IOException {
  151. // Use --stdin instead of using argument otherwise paths starting
  152. // with "-" were interpreted as
  153. // options by git check-ignore
  154. String[] command = new String[] { "git", "check-ignore",
  155. "--no-index", "-v", "-n", "--stdin" };
  156. Process proc = Runtime.getRuntime().exec(command, new String[0],
  157. gitDir);
  158. OutputStream out = proc.getOutputStream();
  159. out.write((path + "\n").getBytes("UTF-8"));
  160. out.flush();
  161. out.close();
  162. return proc;
  163. }
  164. private String readProcessStream(InputStream processStream)
  165. throws IOException {
  166. try (BufferedReader stdOut = new BufferedReader(
  167. new InputStreamReader(processStream))) {
  168. StringBuilder out = new StringBuilder();
  169. String s;
  170. while ((s = stdOut.readLine()) != null) {
  171. out.append(s);
  172. }
  173. return out.toString();
  174. }
  175. }
  176. }
  177. private static final int NB_PATTERN = 1000;
  178. private static final int PATH_PER_PATTERN = 1000;
  179. @Test
  180. public void testRandomPatterns() throws Exception {
  181. // Initialize new git repo
  182. File gitDir = Files.createTempDirectory("jgit").toFile();
  183. Git.init().setDirectory(gitDir).call();
  184. PseudoRandomPatternGenerator generator = new PseudoRandomPatternGenerator();
  185. // Generate random patterns and paths
  186. for (int i = 0; i < NB_PATTERN; i++) {
  187. String pattern = generator.nextRandomString();
  188. FastIgnoreRule jgitIgnoreRule = new FastIgnoreRule(pattern);
  189. CGitIgnoreRule cgitIgnoreRule = new CGitIgnoreRule(gitDir, pattern);
  190. // Test path with pattern as path
  191. assertCgitAndJgitMatch(pattern, jgitIgnoreRule, cgitIgnoreRule,
  192. pattern);
  193. for (int p = 0; p < PATH_PER_PATTERN; p++) {
  194. String path = generator.nextRandomString();
  195. assertCgitAndJgitMatch(pattern, jgitIgnoreRule, cgitIgnoreRule,
  196. path);
  197. }
  198. }
  199. }
  200. @SuppressWarnings({ "boxing" })
  201. private void assertCgitAndJgitMatch(String pattern,
  202. FastIgnoreRule jgitIgnoreRule, CGitIgnoreRule cgitIgnoreRule,
  203. String pathToTest) throws IOException, InterruptedException {
  204. try {
  205. boolean cgitMatch = cgitIgnoreRule.isMatch(pathToTest);
  206. boolean jgitMatch = jgitIgnoreRule.isMatch(pathToTest,
  207. pathToTest.endsWith("/"));
  208. if (cgitMatch != jgitMatch) {
  209. System.err.println(
  210. buildAssertionToAdd(pattern, pathToTest, cgitMatch));
  211. }
  212. Assert.assertEquals("jgit:" + jgitMatch + " <> cgit:" + cgitMatch
  213. + " for pattern:[" + pattern + "] and path:[" + pathToTest
  214. + "]", cgitMatch, jgitMatch);
  215. } catch (CgitFatalException e) {
  216. // Lots of generated patterns or path are rejected by Cgit with a
  217. // fatal error. We want to ignore them.
  218. }
  219. }
  220. private String buildAssertionToAdd(String pattern, String pathToTest,
  221. boolean cgitMatch) {
  222. return "assertMatch(" + toJavaString(pattern) + ", "
  223. + toJavaString(pathToTest) + ", " + cgitMatch
  224. + " /*cgit result*/);";
  225. }
  226. private String toJavaString(String pattern2) {
  227. return "\"" + pattern2.replace("\\", "\\\\").replace("\"", "\\\"")
  228. + "\"";
  229. }
  230. }