Fix newlines and indentation.

This commit is contained in:
Eugene Vigdorchik 2012-04-04 09:30:46 +04:00
parent 291db63af3
commit b5766e4057
2 changed files with 253 additions and 253 deletions

View File

@ -1,102 +1,102 @@
/* sbt -- Simple Build Tool
* Copyright 2012 Eugene Vigdorchik
*/
package sbt
import org.scalatools.testing._
import java.net.ServerSocket
import java.io._
import Tests._
import ForkMain._
private[sbt] object ForkTests {
def apply(frameworks: Seq[TestFramework], tests: List[TestDefinition], config: Execution, classpath: Seq[File], javaHome: Option[File], javaOpts: Seq[String], log: Logger): Task[Output] = {
val opts = config.options.toList
val listeners = opts flatMap {
case Listeners(ls) => ls
case _ => Nil
}
val testListeners = listeners flatMap {
case tl: TestsListener => Some(tl)
case _ => None
}
val filters = opts flatMap {
case Filter(f) => Some(f)
case _ => None
}
val argMap = frameworks.map {
f => f.implClassName -> opts.flatMap {
case Argument(None, args) => args
case Argument(Some(`f`), args) => args
case _ => Nil
}
}.toMap
std.TaskExtra.task {
val server = new ServerSocket(0)
object Acceptor extends Runnable {
val results = collection.mutable.Map.empty[String, TestResult.Value]
def output = (overall(results.values), results.toMap)
def run = {
val socket = server.accept()
val os = new ObjectOutputStream(socket.getOutputStream)
val is = new ObjectInputStream(socket.getInputStream)
import Tags._
@annotation.tailrec def react: Unit = is.readObject match {
case `Done` => os.writeObject(Done);
case Array(`Error`, s: String) => log.error(s); react
case Array(`Warn`, s: String) => log.warn(s); react
case Array(`Info`, s: String) => log.info(s); react
case Array(`Debug`, s: String) => log.debug(s); react
case t: Throwable => log.trace(t); react
case tEvents: Array[Event] =>
for (first <- tEvents.headOption) listeners.foreach(_ startGroup first.testName)
val event = TestEvent(tEvents)
listeners.foreach(_ testEvent event)
for (first <- tEvents.headOption) {
val result = event.result getOrElse TestResult.Passed
results += first.testName -> result
listeners.foreach(_ endGroup (first.testName, result))
}
react
}
try {
os.writeBoolean(log.ansiCodesSupported)
val testsFiltered = tests.filter(test => filters.forall(_(test.name))).map{
t => new ForkTestDefinition(t.name, t.fingerprint)
}.toArray
os.writeObject(testsFiltered)
os.writeInt(frameworks.size)
for ((clazz, args) <- argMap) {
os.writeObject(clazz)
os.writeObject(args.toArray)
}
react
} finally {
is.close(); os.close(); socket.close()
}
}
}
try {
testListeners.foreach(_.doInit())
new Thread(Acceptor).start()
val fullCp = classpath ++: Seq(IO.classLocationFile[ForkMain], IO.classLocationFile[Framework])
val options = javaOpts ++: Seq("-classpath", fullCp mkString File.pathSeparator, classOf[ForkMain].getCanonicalName, server.getLocalPort.toString)
val ec = Fork.java(javaHome, options, LoggedOutput(log))
if (ec != 0) log.error("Running java with options " + options.mkString(" ") + " failed with exit code " + ec)
} finally {
server.close()
}
val result = Acceptor.output
testListeners.foreach(_.doComplete(result._1))
result
} tagw (config.tags: _*)
}
}
/* sbt -- Simple Build Tool
* Copyright 2012 Eugene Vigdorchik
*/
package sbt
import org.scalatools.testing._
import java.net.ServerSocket
import java.io._
import Tests._
import ForkMain._
private[sbt] object ForkTests {
def apply(frameworks: Seq[TestFramework], tests: List[TestDefinition], config: Execution, classpath: Seq[File], javaHome: Option[File], javaOpts: Seq[String], log: Logger): Task[Output] = {
val opts = config.options.toList
val listeners = opts flatMap {
case Listeners(ls) => ls
case _ => Nil
}
val testListeners = listeners flatMap {
case tl: TestsListener => Some(tl)
case _ => None
}
val filters = opts flatMap {
case Filter(f) => Some(f)
case _ => None
}
val argMap = frameworks.map {
f => f.implClassName -> opts.flatMap {
case Argument(None, args) => args
case Argument(Some(`f`), args) => args
case _ => Nil
}
}.toMap
std.TaskExtra.task {
val server = new ServerSocket(0)
object Acceptor extends Runnable {
val results = collection.mutable.Map.empty[String, TestResult.Value]
def output = (overall(results.values), results.toMap)
def run = {
val socket = server.accept()
val os = new ObjectOutputStream(socket.getOutputStream)
val is = new ObjectInputStream(socket.getInputStream)
import Tags._
@annotation.tailrec def react: Unit = is.readObject match {
case `Done` => os.writeObject(Done);
case Array(`Error`, s: String) => log.error(s); react
case Array(`Warn`, s: String) => log.warn(s); react
case Array(`Info`, s: String) => log.info(s); react
case Array(`Debug`, s: String) => log.debug(s); react
case t: Throwable => log.trace(t); react
case tEvents: Array[Event] =>
for (first <- tEvents.headOption) listeners.foreach(_ startGroup first.testName)
val event = TestEvent(tEvents)
listeners.foreach(_ testEvent event)
for (first <- tEvents.headOption) {
val result = event.result getOrElse TestResult.Passed
results += first.testName -> result
listeners.foreach(_ endGroup (first.testName, result))
}
react
}
try {
os.writeBoolean(log.ansiCodesSupported)
val testsFiltered = tests.filter(test => filters.forall(_(test.name))).map{
t => new ForkTestDefinition(t.name, t.fingerprint)
}.toArray
os.writeObject(testsFiltered)
os.writeInt(frameworks.size)
for ((clazz, args) <- argMap) {
os.writeObject(clazz)
os.writeObject(args.toArray)
}
react
} finally {
is.close(); os.close(); socket.close()
}
}
}
try {
testListeners.foreach(_.doInit())
new Thread(Acceptor).start()
val fullCp = classpath ++: Seq(IO.classLocationFile[ForkMain], IO.classLocationFile[Framework])
val options = javaOpts ++: Seq("-classpath", fullCp mkString File.pathSeparator, classOf[ForkMain].getCanonicalName, server.getLocalPort.toString)
val ec = Fork.java(javaHome, options, LoggedOutput(log))
if (ec != 0) log.error("Running java with options " + options.mkString(" ") + " failed with exit code " + ec)
} finally {
server.close()
}
val result = Acceptor.output
testListeners.foreach(_.doComplete(result._1))
result
} tagw (config.tags: _*)
}
}

View File

@ -1,151 +1,151 @@
/* sbt -- Simple Build Tool
* Copyright 2012 Eugene Vigdorchik
*/
package sbt;
import org.scalatools.testing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
public class ForkMain {
public static enum Tags {
Error, Warn, Info, Debug, Done;
}
static class SubclassFingerscan implements TestFingerprint, Serializable {
private boolean isModule;
private String superClassName;
SubclassFingerscan(SubclassFingerprint print) {
isModule = print.isModule();
superClassName = print.superClassName();
}
public boolean isModule() { return isModule; }
public String superClassName() { return superClassName; }
}
static class AnnotatedFingerscan implements AnnotatedFingerprint, Serializable {
private boolean isModule;
private String annotationName;
AnnotatedFingerscan(AnnotatedFingerprint print) {
isModule = print.isModule();
annotationName = print.annotationName();
}
public boolean isModule() { return isModule; }
public String annotationName() { return annotationName; }
}
public static class ForkTestDefinition implements Serializable {
public String name;
public Fingerprint fingerprint;
public ForkTestDefinition(String name, Fingerprint fingerprint) {
this.name = name;
if (fingerprint instanceof SubclassFingerprint) {
this.fingerprint = new SubclassFingerscan((SubclassFingerprint) fingerprint);
} else {
this.fingerprint = new AnnotatedFingerscan((AnnotatedFingerprint) fingerprint);
}
}
}
static class ForkEvent implements Event, Serializable {
private String testName;
private String description;
private Result result;
ForkEvent(Event e) {
testName = e.testName();
description = e.description();
result = e.result();
}
public String testName() { return testName; }
public String description() { return description; }
public Result result() { return result;}
public Throwable error() { return null; }
}
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName(null), Integer.valueOf(args[0]));
final ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
final ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
try {
new Run().run(is, os);
} finally {
is.close();
os.close();
}
}
private static class Run {
boolean matches(Fingerprint f1, Fingerprint f2) {
if (f1 instanceof SubclassFingerprint && f2 instanceof SubclassFingerprint) {
final SubclassFingerprint sf1 = (SubclassFingerprint) f1;
final SubclassFingerprint sf2 = (SubclassFingerprint) f2;
return sf1.isModule() == sf2.isModule() && sf1.superClassName().equals(sf2.superClassName());
} else if (f1 instanceof AnnotatedFingerprint && f2 instanceof AnnotatedFingerprint) {
AnnotatedFingerprint af1 = (AnnotatedFingerprint) f1;
AnnotatedFingerprint af2 = (AnnotatedFingerprint) f2;
return af1.isModule() == af2.isModule() && af1.annotationName().equals(af2.annotationName());
}
return false;
}
void write(ObjectOutputStream os, Object obj) {
try {
os.writeObject(obj);
} catch (IOException e) {
System.err.println("Cannot write to socket");
}
}
void run(ObjectInputStream is, final ObjectOutputStream os) throws Exception {
final boolean ansiCodesSupported = is.readBoolean();
Logger[] loggers = {
new Logger() {
public boolean ansiCodesSupported() { return ansiCodesSupported; }
public void error(String s) { write(os, new Object[]{Tags.Error, s}); }
public void warn(String s) { write(os, new Object[]{Tags.Warn, s}); }
public void info(String s) { write(os, new Object[]{Tags.Info, s}); }
public void debug(String s) { write(os, new Object[]{Tags.Debug, s}); }
public void trace(Throwable t) { write(os, t); }
}
};
final ForkTestDefinition[] tests = (ForkTestDefinition[]) is.readObject();
int nFrameworks = is.readInt();
for (int i = 0; i < nFrameworks; i++) {
final Framework framework;
final String implClassName = (String) is.readObject();
try {
framework = (Framework) Class.forName(implClassName).newInstance();
} catch (ClassNotFoundException e) {
write(os, new Object[]{Tags.Error, "Framework implementation '" + implClassName + "' not present."});
continue;
}
final String[] frameworkArgs = (String[]) is.readObject();
ArrayList<ForkTestDefinition> filteredTests = new ArrayList<ForkTestDefinition>();
for (Fingerprint testFingerprint : framework.tests()) {
for (ForkTestDefinition test : tests) {
if (matches(testFingerprint, test.fingerprint)) filteredTests.add(test);
}
}
final org.scalatools.testing.Runner runner = framework.testRunner(getClass().getClassLoader(), loggers);
for (ForkTestDefinition test : filteredTests) {
final List<ForkEvent> events = new ArrayList<ForkEvent>();
EventHandler handler = new EventHandler() { public void handle(Event e){ events.add(new ForkEvent(e)); } };
if (runner instanceof Runner2) {
((Runner2) runner).run(test.name, test.fingerprint, handler, frameworkArgs);
} else if (test.fingerprint instanceof TestFingerprint) {
runner.run(test.name, (TestFingerprint) test.fingerprint, handler, frameworkArgs);
} else {
write(os, new Object[]{Tags.Error, "Framework '" + framework + "' does not support test '" + test.name + "'"});
}
write(os, events.toArray(new ForkEvent[events.size()]));
}
}
write(os, Tags.Done);
is.readObject();
}
}
}
/* sbt -- Simple Build Tool
* Copyright 2012 Eugene Vigdorchik
*/
package sbt;
import org.scalatools.testing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
public class ForkMain {
public static enum Tags {
Error, Warn, Info, Debug, Done;
}
static class SubclassFingerscan implements TestFingerprint, Serializable {
private boolean isModule;
private String superClassName;
SubclassFingerscan(SubclassFingerprint print) {
isModule = print.isModule();
superClassName = print.superClassName();
}
public boolean isModule() { return isModule; }
public String superClassName() { return superClassName; }
}
static class AnnotatedFingerscan implements AnnotatedFingerprint, Serializable {
private boolean isModule;
private String annotationName;
AnnotatedFingerscan(AnnotatedFingerprint print) {
isModule = print.isModule();
annotationName = print.annotationName();
}
public boolean isModule() { return isModule; }
public String annotationName() { return annotationName; }
}
public static class ForkTestDefinition implements Serializable {
public String name;
public Fingerprint fingerprint;
public ForkTestDefinition(String name, Fingerprint fingerprint) {
this.name = name;
if (fingerprint instanceof SubclassFingerprint) {
this.fingerprint = new SubclassFingerscan((SubclassFingerprint) fingerprint);
} else {
this.fingerprint = new AnnotatedFingerscan((AnnotatedFingerprint) fingerprint);
}
}
}
static class ForkEvent implements Event, Serializable {
private String testName;
private String description;
private Result result;
ForkEvent(Event e) {
testName = e.testName();
description = e.description();
result = e.result();
}
public String testName() { return testName; }
public String description() { return description; }
public Result result() { return result;}
public Throwable error() { return null; }
}
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName(null), Integer.valueOf(args[0]));
final ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
final ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
try {
new Run().run(is, os);
} finally {
is.close();
os.close();
}
}
private static class Run {
boolean matches(Fingerprint f1, Fingerprint f2) {
if (f1 instanceof SubclassFingerprint && f2 instanceof SubclassFingerprint) {
final SubclassFingerprint sf1 = (SubclassFingerprint) f1;
final SubclassFingerprint sf2 = (SubclassFingerprint) f2;
return sf1.isModule() == sf2.isModule() && sf1.superClassName().equals(sf2.superClassName());
} else if (f1 instanceof AnnotatedFingerprint && f2 instanceof AnnotatedFingerprint) {
AnnotatedFingerprint af1 = (AnnotatedFingerprint) f1;
AnnotatedFingerprint af2 = (AnnotatedFingerprint) f2;
return af1.isModule() == af2.isModule() && af1.annotationName().equals(af2.annotationName());
}
return false;
}
void write(ObjectOutputStream os, Object obj) {
try {
os.writeObject(obj);
} catch (IOException e) {
System.err.println("Cannot write to socket");
}
}
void run(ObjectInputStream is, final ObjectOutputStream os) throws Exception {
final boolean ansiCodesSupported = is.readBoolean();
Logger[] loggers = {
new Logger() {
public boolean ansiCodesSupported() { return ansiCodesSupported; }
public void error(String s) { write(os, new Object[]{Tags.Error, s}); }
public void warn(String s) { write(os, new Object[]{Tags.Warn, s}); }
public void info(String s) { write(os, new Object[]{Tags.Info, s}); }
public void debug(String s) { write(os, new Object[]{Tags.Debug, s}); }
public void trace(Throwable t) { write(os, t); }
}
};
final ForkTestDefinition[] tests = (ForkTestDefinition[]) is.readObject();
int nFrameworks = is.readInt();
for (int i = 0; i < nFrameworks; i++) {
final Framework framework;
final String implClassName = (String) is.readObject();
try {
framework = (Framework) Class.forName(implClassName).newInstance();
} catch (ClassNotFoundException e) {
write(os, new Object[]{Tags.Error, "Framework implementation '" + implClassName + "' not present."});
continue;
}
final String[] frameworkArgs = (String[]) is.readObject();
ArrayList<ForkTestDefinition> filteredTests = new ArrayList<ForkTestDefinition>();
for (Fingerprint testFingerprint : framework.tests()) {
for (ForkTestDefinition test : tests) {
if (matches(testFingerprint, test.fingerprint)) filteredTests.add(test);
}
}
final org.scalatools.testing.Runner runner = framework.testRunner(getClass().getClassLoader(), loggers);
for (ForkTestDefinition test : filteredTests) {
final List<ForkEvent> events = new ArrayList<ForkEvent>();
EventHandler handler = new EventHandler() { public void handle(Event e){ events.add(new ForkEvent(e)); } };
if (runner instanceof Runner2) {
((Runner2) runner).run(test.name, test.fingerprint, handler, frameworkArgs);
} else if (test.fingerprint instanceof TestFingerprint) {
runner.run(test.name, (TestFingerprint) test.fingerprint, handler, frameworkArgs);
} else {
write(os, new Object[]{Tags.Error, "Framework '" + framework + "' does not support test '" + test.name + "'"});
}
write(os, events.toArray(new ForkEvent[events.size()]));
}
}
write(os, Tags.Done);
is.readObject();
}
}
}