forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change to make scalatest test work using the JUnitRunner builtin to scala test. Basically I check if the test extends org.scalatest.Suite, if it does I include it in the specs. Later on, we do the same thing when converting the test class to a junit Runner. We check to see if it is a Suite, and if so then fake like we included the @RunWith annotation. A few notes: 1) I didn't support scala test "methods" 2) I used reflection instead of modifying junit.py and friends to make scalatest be a non-shaded jar (in fact we don't need to include it at all) 3) I made sure I used the test class's class loader to load Suite and JUnitRunner 4) I bumped the runner_jar version to 1.0.16, I hope this is correct. I would love to see this as a 1.2.1 release. NOTE: This is a recreate of pantsbuild#4344 Bugs closed: 4013 Reviewed at https://rbcommons.com/s/twitter/r/4361/
- Loading branch information
Showing
13 changed files
with
174 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/java/org/pantsbuild/tools/junit/impl/CustomAnnotationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package org.pantsbuild.tools.junit.impl; | ||
|
||
import java.io.PrintStream; | ||
|
||
import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; | ||
import org.junit.internal.builders.AnnotatedBuilder; | ||
import org.junit.internal.builders.JUnit4Builder; | ||
import org.junit.runner.Runner; | ||
import org.junit.runners.model.RunnerBuilder; | ||
import org.pantsbuild.tools.junit.withretry.JUnit4BuilderWithRetry; | ||
|
||
/** | ||
* Needed to support retrying flaky tests as well as add support for running scala tests. | ||
* Using method overriding, gives us access to code in JUnit4 that cannot be customized | ||
* in a simpler way. | ||
*/ | ||
public class CustomAnnotationBuilder extends AllDefaultPossibilitiesBuilder { | ||
|
||
private final int numRetries; | ||
private final PrintStream err; | ||
|
||
public CustomAnnotationBuilder(int numRetries, PrintStream err) { | ||
super(true); | ||
this.numRetries = numRetries; | ||
this.err = err; | ||
} | ||
|
||
@Override | ||
public JUnit4Builder junit4Builder() { | ||
return new JUnit4BuilderWithRetry(numRetries, err); | ||
} | ||
|
||
// override annotated builder to "fake" the scala test junit runner for scala tests | ||
@Override | ||
protected AnnotatedBuilder annotatedBuilder() { | ||
return new ScalaTestAnnotatedBuilder(this); | ||
} | ||
|
||
private static class ScalaTestAnnotatedBuilder extends AnnotatedBuilder { | ||
ScalaTestAnnotatedBuilder(RunnerBuilder suiteBuilder) { | ||
super(suiteBuilder); | ||
} | ||
|
||
@Override | ||
public Runner runnerForClass(Class<?> testClass) throws Exception { | ||
Runner runner = super.runnerForClass(testClass); | ||
if (runner == null) { | ||
if (ScalaTestUtil.isScalaTestTest(testClass)) { | ||
return ScalaTestUtil.getJUnitRunner(testClass); | ||
} | ||
} | ||
return runner; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/java/org/pantsbuild/tools/junit/impl/ScalaTestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.pantsbuild.tools.junit.impl; | ||
|
||
import org.junit.runner.Runner; | ||
|
||
public final class ScalaTestUtil { | ||
private ScalaTestUtil() {} | ||
|
||
/** | ||
* Returns a scalatest junit runner using reflection in the classloader of the test. | ||
* @param clazz the test class | ||
* | ||
* @return a new scala test junit runner | ||
*/ | ||
public static Runner getJUnitRunner(Class<?> clazz) { | ||
try { | ||
Class<?> junitRunnerClass = Class.forName("org.scalatest.junit.JUnitRunner", | ||
true, clazz.getClassLoader()); | ||
return (Runner)junitRunnerClass.getConstructor(Class.class).newInstance(clazz); | ||
} catch (Exception e) { | ||
// isScalaTest should fail if scala test isn't available so this is probably ok. | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the passed in test clazz has an ancestor that is the scala test suite | ||
* object (looked up in the test classes class loader). | ||
* @param clazz the test class | ||
* | ||
* @return true if the test class is a scalatest test, false if not. | ||
*/ | ||
public static boolean isScalaTestTest(Class<?> clazz) { | ||
try { | ||
Class suiteClass = Class.forName("org.scalatest.Suite", true, clazz.getClassLoader()); | ||
return suiteClass.isAssignableFrom(clazz); | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
src/java/org/pantsbuild/tools/junit/withretry/AllDefaultPossibilitiesBuilderWithRetry.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
scala_library( | ||
name='scala-test-dep', | ||
dependencies=[ | ||
'3rdparty:scalatest', | ||
'tests/java/org/pantsbuild/tools/junit/lib:test-dep', | ||
], | ||
sources=globs('*.scala') | ||
) |
11 changes: 11 additions & 0 deletions
11
tests/scala/org/pantsbuild/tools/junit/lib/MockScalaTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.pantsbuild.tools.junit.lib | ||
|
||
import org.scalatest.FreeSpec | ||
|
||
class MockScalaTest extends FreeSpec { | ||
"test" - { | ||
"should pass" in { | ||
TestRegistry.registerTestCall("MockScalaTest-1") | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/scala/org/pantsbuild/tools/junit/lib/NotAScalaTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.pantsbuild.tools.junit.lib | ||
|
||
class NotAScalaTest {} |