Support testing commit-msg hook from JAR
Some build systems may choose to pass off a JAR containing all resources under test rather than a directory of tests (e.g. Buck). Support testing under those environments by copying the script to a temporary file and removing said file after the test is done. Change-Id: Idf1f07546b0a5b523b314f1085b7e0123e0cb502
This commit is contained in:
@@ -52,16 +52,22 @@ package com.google.gerrit.server.tools.hooks;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
public abstract class HookTestCase extends LocalDiskRepositoryTestCase {
|
||||
protected Repository repository;
|
||||
private File hooksh;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
@@ -70,22 +76,48 @@ public abstract class HookTestCase extends LocalDiskRepositoryTestCase {
|
||||
repository = createWorkRepository();
|
||||
}
|
||||
|
||||
protected File getHook(final String name) {
|
||||
@Override
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (hooksh != null) {
|
||||
if (!hooksh.delete()) {
|
||||
hooksh.deleteOnExit();
|
||||
}
|
||||
hooksh = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected File getHook(final String name) throws IOException {
|
||||
final String scproot = "com/google/gerrit/server/tools/root";
|
||||
final String path = scproot + "/hooks/" + name;
|
||||
final URL url = cl().getResource(path);
|
||||
URL url = cl().getResource(path);
|
||||
if (url == null) {
|
||||
fail("Cannot locate " + path + " in CLASSPATH");
|
||||
}
|
||||
|
||||
File hook;
|
||||
try {
|
||||
hook = new File(url.toURI());
|
||||
} catch (URISyntaxException e) {
|
||||
if ("file".equals(url.getProtocol())) {
|
||||
hook = new File(url.getPath());
|
||||
}
|
||||
if (!hook.isFile()) {
|
||||
fail("Cannot locate " + path + " in CLASSPATH");
|
||||
if (!hook.isFile()) {
|
||||
fail("Cannot locate " + path + " in CLASSPATH");
|
||||
}
|
||||
} else if ("jar".equals(url.getProtocol())) {
|
||||
hooksh = File.createTempFile("hook_", ".sh");
|
||||
InputStream in = url.openStream();
|
||||
try {
|
||||
FileOutputStream out = new FileOutputStream(hooksh);
|
||||
try {
|
||||
ByteStreams.copy(in, out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
hook = hooksh;
|
||||
} else {
|
||||
fail("Cannot invoke " + url);
|
||||
hook = null;
|
||||
}
|
||||
|
||||
// The hook was copied out of our source control system into the
|
||||
|
||||
Reference in New Issue
Block a user