Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java')
-rw-r--r--org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java b/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java
index 73086ab4af..96550889c8 100644
--- a/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java
+++ b/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java
@@ -44,12 +44,15 @@ package org.eclipse.jgit.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.RejectCommitException;
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.junit.Assume;
@@ -91,6 +94,33 @@ public class HookTest extends RepositoryTestCase {
res.getStatus());
}
+ @Test
+ public void testPreCommitHook() throws Exception {
+ assumeSupportedPlatform();
+
+ Hook h = Hook.PRE_COMMIT;
+ writeHookFile(h.getName(),
+ "#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
+ Git git = Git.wrap(db);
+ String path = "a.txt";
+ writeTrashFile(path, "content");
+ git.add().addFilepattern(path).call();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ git.commit().setMessage("commit")
+ .setHookOutputStream(new PrintStream(out)).call();
+ fail("expected pre-commit hook to abort commit");
+ } catch (RejectCommitException e) {
+ assertEquals("unexpected error message from pre-commit hook",
+ "Commit rejected by \"pre-commit\" hook.\nstderr\n",
+ e.getMessage());
+ assertEquals("unexpected output from pre-commit hook", "test\n",
+ out.toString());
+ } catch (Throwable e) {
+ fail("unexpected exception thrown by pre-commit hook: " + e);
+ }
+ }
+
private File writeHookFile(final String name, final String data)
throws IOException {
File path = new File(db.getWorkTree() + "/.git/hooks/", name);

Back to the top