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.

HookTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2014 Matthias Sohn <matthias.sohn@sap.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.util;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import static org.junit.Assert.fail;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.io.PrintStream;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.api.errors.AbortedByHookException;
  53. import org.eclipse.jgit.hooks.CommitMsgHook;
  54. import org.eclipse.jgit.hooks.PostCommitHook;
  55. import org.eclipse.jgit.hooks.PreCommitHook;
  56. import org.eclipse.jgit.junit.JGitTestUtil;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.junit.Assume;
  60. import org.junit.Test;
  61. public class HookTest extends RepositoryTestCase {
  62. @Test
  63. public void testFindHook() throws Exception {
  64. assumeSupportedPlatform();
  65. assertNull("no hook should be installed",
  66. FS.DETECTED.findHook(db, PreCommitHook.NAME));
  67. File hookFile = writeHookFile(PreCommitHook.NAME,
  68. "#!/bin/bash\necho \"test $1 $2\"");
  69. assertEquals("expected to find pre-commit hook", hookFile,
  70. FS.DETECTED.findHook(db, PreCommitHook.NAME));
  71. }
  72. @Test
  73. public void testFindPostCommitHook() throws Exception {
  74. assumeSupportedPlatform();
  75. assertNull("no hook should be installed",
  76. FS.DETECTED.findHook(db, PostCommitHook.NAME));
  77. File hookFile = writeHookFile(PostCommitHook.NAME,
  78. "#!/bin/bash\necho \"test $1 $2\"");
  79. assertEquals("expected to find post-commit hook", hookFile,
  80. FS.DETECTED.findHook(db, PostCommitHook.NAME));
  81. }
  82. @Test
  83. public void testFailedCommitMsgHookBlocksCommit() throws Exception {
  84. assumeSupportedPlatform();
  85. writeHookFile(CommitMsgHook.NAME,
  86. "#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
  87. Git git = Git.wrap(db);
  88. String path = "a.txt";
  89. writeTrashFile(path, "content");
  90. git.add().addFilepattern(path).call();
  91. ByteArrayOutputStream out = new ByteArrayOutputStream();
  92. try {
  93. git.commit().setMessage("commit")
  94. .setHookOutputStream(new PrintStream(out)).call();
  95. fail("expected commit-msg hook to abort commit");
  96. } catch (AbortedByHookException e) {
  97. assertEquals("unexpected error message from commit-msg hook",
  98. "Rejected by \"commit-msg\" hook.\nstderr\n",
  99. e.getMessage());
  100. assertEquals("unexpected output from commit-msg hook", "test\n",
  101. out.toString());
  102. }
  103. }
  104. @Test
  105. public void testCommitMsgHookReceivesCorrectParameter() throws Exception {
  106. assumeSupportedPlatform();
  107. writeHookFile(CommitMsgHook.NAME,
  108. "#!/bin/sh\necho $1\n\necho 1>&2 \"stderr\"\nexit 0");
  109. Git git = Git.wrap(db);
  110. String path = "a.txt";
  111. writeTrashFile(path, "content");
  112. git.add().addFilepattern(path).call();
  113. ByteArrayOutputStream out = new ByteArrayOutputStream();
  114. git.commit().setMessage("commit")
  115. .setHookOutputStream(new PrintStream(out)).call();
  116. assertEquals(".git/COMMIT_EDITMSG\n",
  117. out.toString("UTF-8"));
  118. }
  119. @Test
  120. public void testCommitMsgHookCanModifyCommitMessage() throws Exception {
  121. assumeSupportedPlatform();
  122. writeHookFile(CommitMsgHook.NAME,
  123. "#!/bin/sh\necho \"new message\" > $1\nexit 0");
  124. Git git = Git.wrap(db);
  125. String path = "a.txt";
  126. writeTrashFile(path, "content");
  127. git.add().addFilepattern(path).call();
  128. ByteArrayOutputStream out = new ByteArrayOutputStream();
  129. RevCommit revCommit = git.commit().setMessage("commit")
  130. .setHookOutputStream(new PrintStream(out)).call();
  131. assertEquals("new message\n", revCommit.getFullMessage());
  132. }
  133. @Test
  134. public void testPostCommitRunHook() throws Exception {
  135. assumeSupportedPlatform();
  136. writeHookFile(PostCommitHook.NAME,
  137. "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\necho 1>&2 \"stderr\"");
  138. ByteArrayOutputStream out = new ByteArrayOutputStream();
  139. ByteArrayOutputStream err = new ByteArrayOutputStream();
  140. ProcessResult res = FS.DETECTED.runHookIfPresent(db,
  141. PostCommitHook.NAME,
  142. new String[] {
  143. "arg1", "arg2" },
  144. new PrintStream(out), new PrintStream(err), "stdin");
  145. assertEquals("unexpected hook output", "test arg1 arg2\nstdin\n",
  146. out.toString("UTF-8"));
  147. assertEquals("unexpected output on stderr stream", "stderr\n",
  148. err.toString("UTF-8"));
  149. assertEquals("unexpected exit code", 0, res.getExitCode());
  150. assertEquals("unexpected process status", ProcessResult.Status.OK,
  151. res.getStatus());
  152. }
  153. @Test
  154. public void testAllCommitHooks() throws Exception {
  155. assumeSupportedPlatform();
  156. writeHookFile(PreCommitHook.NAME,
  157. "#!/bin/sh\necho \"test pre-commit\"\n\necho 1>&2 \"stderr pre-commit\"\nexit 0");
  158. writeHookFile(CommitMsgHook.NAME,
  159. "#!/bin/sh\necho \"test commit-msg $1\"\n\necho 1>&2 \"stderr commit-msg\"\nexit 0");
  160. writeHookFile(PostCommitHook.NAME,
  161. "#!/bin/sh\necho \"test post-commit\"\necho 1>&2 \"stderr post-commit\"\nexit 0");
  162. Git git = Git.wrap(db);
  163. String path = "a.txt";
  164. writeTrashFile(path, "content");
  165. git.add().addFilepattern(path).call();
  166. ByteArrayOutputStream out = new ByteArrayOutputStream();
  167. try {
  168. git.commit().setMessage("commit")
  169. .setHookOutputStream(new PrintStream(out)).call();
  170. } catch (AbortedByHookException e) {
  171. fail("unexpected hook failure");
  172. }
  173. assertEquals("unexpected hook output",
  174. "test pre-commit\ntest commit-msg .git/COMMIT_EDITMSG\ntest post-commit\n",
  175. out.toString("UTF-8"));
  176. }
  177. @Test
  178. public void testRunHook() throws Exception {
  179. assumeSupportedPlatform();
  180. writeHookFile(PreCommitHook.NAME,
  181. "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\necho 1>&2 \"stderr\"");
  182. ByteArrayOutputStream out = new ByteArrayOutputStream();
  183. ByteArrayOutputStream err = new ByteArrayOutputStream();
  184. ProcessResult res = FS.DETECTED.runHookIfPresent(db,
  185. PreCommitHook.NAME,
  186. new String[] {
  187. "arg1", "arg2" },
  188. new PrintStream(out), new PrintStream(err), "stdin");
  189. assertEquals("unexpected hook output", "test arg1 arg2\nstdin\n",
  190. out.toString("UTF-8"));
  191. assertEquals("unexpected output on stderr stream", "stderr\n",
  192. err.toString("UTF-8"));
  193. assertEquals("unexpected exit code", 0, res.getExitCode());
  194. assertEquals("unexpected process status", ProcessResult.Status.OK,
  195. res.getStatus());
  196. }
  197. @Test
  198. public void testFailedPreCommitHookBlockCommit() throws Exception {
  199. assumeSupportedPlatform();
  200. writeHookFile(PreCommitHook.NAME,
  201. "#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
  202. Git git = Git.wrap(db);
  203. String path = "a.txt";
  204. writeTrashFile(path, "content");
  205. git.add().addFilepattern(path).call();
  206. ByteArrayOutputStream out = new ByteArrayOutputStream();
  207. try {
  208. git.commit().setMessage("commit")
  209. .setHookOutputStream(new PrintStream(out)).call();
  210. fail("expected pre-commit hook to abort commit");
  211. } catch (AbortedByHookException e) {
  212. assertEquals("unexpected error message from pre-commit hook",
  213. "Rejected by \"pre-commit\" hook.\nstderr\n",
  214. e.getMessage());
  215. assertEquals("unexpected output from pre-commit hook", "test\n",
  216. out.toString());
  217. }
  218. }
  219. private File writeHookFile(final String name, final String data)
  220. throws IOException {
  221. File path = new File(db.getWorkTree() + "/.git/hooks/", name);
  222. JGitTestUtil.write(path, data);
  223. FS.DETECTED.setExecute(path, true);
  224. return path;
  225. }
  226. private void assumeSupportedPlatform() {
  227. Assume.assumeTrue(FS.DETECTED instanceof FS_POSIX
  228. || FS.DETECTED instanceof FS_Win32_Cygwin);
  229. }
  230. }