View Javadoc
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  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.fail;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.File;
51  import java.io.IOException;
52  import java.io.PrintStream;
53  
54  import org.eclipse.jgit.api.Git;
55  import org.eclipse.jgit.api.errors.AbortedByHookException;
56  import org.eclipse.jgit.hooks.CommitMsgHook;
57  import org.eclipse.jgit.hooks.PreCommitHook;
58  import org.eclipse.jgit.junit.JGitTestUtil;
59  import org.eclipse.jgit.junit.RepositoryTestCase;
60  import org.eclipse.jgit.revwalk.RevCommit;
61  import org.junit.Assume;
62  import org.junit.Test;
63  
64  public class HookTest extends RepositoryTestCase {
65  
66  	@Test
67  	public void testFindHook() throws Exception {
68  		assumeSupportedPlatform();
69  
70  		assertNull("no hook should be installed",
71  				FS.DETECTED.findHook(db, PreCommitHook.NAME));
72  		File hookFile = writeHookFile(PreCommitHook.NAME,
73  				"#!/bin/bash\necho \"test $1 $2\"");
74  		assertEquals("expected to find pre-commit hook", hookFile,
75  				FS.DETECTED.findHook(db, PreCommitHook.NAME));
76  	}
77  
78  	@Test
79  	public void testFailedCommitMsgHookBlocksCommit() throws Exception {
80  		assumeSupportedPlatform();
81  
82  		writeHookFile(CommitMsgHook.NAME,
83  				"#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
84  		Git git = Git.wrap(db);
85  		String path = "a.txt";
86  		writeTrashFile(path, "content");
87  		git.add().addFilepattern(path).call();
88  		ByteArrayOutputStream out = new ByteArrayOutputStream();
89  		try {
90  			git.commit().setMessage("commit")
91  					.setHookOutputStream(new PrintStream(out)).call();
92  			fail("expected commit-msg hook to abort commit");
93  		} catch (AbortedByHookException e) {
94  			assertEquals("unexpected error message from commit-msg hook",
95  					"Rejected by \"commit-msg\" hook.\nstderr\n",
96  					e.getMessage());
97  			assertEquals("unexpected output from commit-msg hook", "test\n",
98  					out.toString());
99  		}
100 	}
101 
102 	@Test
103 	public void testCommitMsgHookReceivesCorrectParameter() throws Exception {
104 		assumeSupportedPlatform();
105 
106 		writeHookFile(CommitMsgHook.NAME,
107 				"#!/bin/sh\necho $1\n\necho 1>&2 \"stderr\"\nexit 0");
108 		Git git = Git.wrap(db);
109 		String path = "a.txt";
110 		writeTrashFile(path, "content");
111 		git.add().addFilepattern(path).call();
112 		ByteArrayOutputStream out = new ByteArrayOutputStream();
113 		git.commit().setMessage("commit")
114 				.setHookOutputStream(new PrintStream(out)).call();
115 		assertEquals(".git/COMMIT_EDITMSG\n", out.toString("UTF-8"));
116 	}
117 
118 	@Test
119 	public void testCommitMsgHookCanModifyCommitMessage() throws Exception {
120 		assumeSupportedPlatform();
121 
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 
134 	@Test
135 	public void testRunHook() throws Exception {
136 		assumeSupportedPlatform();
137 
138 		writeHookFile(PreCommitHook.NAME,
139 				"#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\necho 1>&2 \"stderr\"");
140 		ByteArrayOutputStream out = new ByteArrayOutputStream();
141 		ByteArrayOutputStream err = new ByteArrayOutputStream();
142 		ProcessResult res = FS.DETECTED.runHookIfPresent(db,
143 				PreCommitHook.NAME,
144 				new String[] {
145 				"arg1", "arg2" },
146 				new PrintStream(out), new PrintStream(err), "stdin");
147 		assertEquals("unexpected hook output", "test arg1 arg2\nstdin\n",
148 				out.toString("UTF-8"));
149 		assertEquals("unexpected output on stderr stream", "stderr\n",
150 				err.toString("UTF-8"));
151 		assertEquals("unexpected exit code", 0, res.getExitCode());
152 		assertEquals("unexpected process status", ProcessResult.Status.OK,
153 				res.getStatus());
154 	}
155 
156 	@Test
157 	public void testFailedPreCommitHookBlockCommit() throws Exception {
158 		assumeSupportedPlatform();
159 
160 		writeHookFile(PreCommitHook.NAME,
161 				"#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
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 			fail("expected pre-commit hook to abort commit");
171 		} catch (AbortedByHookException e) {
172 			assertEquals("unexpected error message from pre-commit hook",
173 					"Rejected by \"pre-commit\" hook.\nstderr\n",
174 					e.getMessage());
175 			assertEquals("unexpected output from pre-commit hook", "test\n",
176 					out.toString());
177 		}
178 	}
179 
180 	private File writeHookFile(final String name, final String data)
181 			throws IOException {
182 		File path = new File(db.getWorkTree() + "/.git/hooks/", name);
183 		JGitTestUtil.write(path, data);
184 		FS.DETECTED.setExecute(path, true);
185 		return path;
186 	}
187 
188 	private void assumeSupportedPlatform() {
189 		Assume.assumeTrue(FS.DETECTED instanceof FS_POSIX
190 				|| FS.DETECTED instanceof FS_Win32_Cygwin);
191 	}
192 }