-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #741 from olafurpg/sbt-testkit
Add ScalafixTestkitPlugin to simplify g8 template.
- Loading branch information
Showing
8 changed files
with
265 additions
and
126 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
47 changes: 47 additions & 0 deletions
47
scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixTestkitPlugin.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,47 @@ | ||
package scalafix.sbt | ||
|
||
import sbt.Def | ||
import sbt._ | ||
import sbt.Keys._ | ||
import java.io.File.pathSeparator | ||
import sbt.plugins.JvmPlugin | ||
|
||
object ScalafixTestkitPlugin extends AutoPlugin { | ||
override def trigger: PluginTrigger = noTrigger | ||
override def requires: Plugins = JvmPlugin | ||
object autoImport { | ||
val scalafixTestkitInputClasspath = | ||
taskKey[Classpath]("Classpath of input project") | ||
val scalafixTestkitInputSourceDirectories = | ||
taskKey[Seq[File]]("Source directory of output projects") | ||
val scalafixTestkitOutputSourceDirectories = | ||
taskKey[Seq[File]]("Source directories of output projects") | ||
} | ||
import autoImport._ | ||
|
||
override def projectSettings: Seq[Def.Setting[_]] = List( | ||
resourceGenerators.in(Test) += Def.task { | ||
val props = new java.util.Properties() | ||
val values = Map[String, Seq[File]]( | ||
"inputClasspath" -> | ||
scalafixTestkitInputClasspath.value.map(_.data), | ||
"inputSourceDirectories" -> | ||
scalafixTestkitInputSourceDirectories.value, | ||
"outputSourceDirectories" -> | ||
scalafixTestkitOutputSourceDirectories.value | ||
) | ||
values.foreach { | ||
case (key, files) => | ||
props.put( | ||
key, | ||
files.iterator.filter(_.exists()).mkString(pathSeparator) | ||
) | ||
} | ||
val out = | ||
managedResourceDirectories.in(Test).value.head / | ||
"scalafix-testkit.properties" | ||
IO.write(props, "Input data for scalafix testkit", out) | ||
List(out) | ||
} | ||
) | ||
} |
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
62 changes: 62 additions & 0 deletions
62
scalafix-testkit/src/main/scala/scalafix/testkit/TestkitProperties.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,62 @@ | ||
package scalafix.testkit | ||
|
||
import scala.meta.AbsolutePath | ||
import scala.meta.Classpath | ||
|
||
/** | ||
* Input arguments to run scalafix testkit rules. | ||
* | ||
* @param inputClasspath | ||
* The class directory of the input sources. This directory should contain a | ||
* META-INF/semanticd sub-directory with SemanticDB files. | ||
* @param inputSourceDirectories | ||
* The source directory of the input sources. This directory should contain Scala code to | ||
* be fixed by Scalafix. | ||
* @param outputSourceDirectories | ||
* The source directories of the expected output sources. These directories should contain | ||
* Scala source files with the expected output after running Scalafix. When multiple directories | ||
* are provided, the first directory that contains a source files with a matching relative path | ||
* in inputSourceroot is used. | ||
*/ | ||
final class TestkitProperties( | ||
val inputClasspath: Classpath, | ||
val inputSourceDirectories: List[AbsolutePath], | ||
val outputSourceDirectories: List[AbsolutePath] | ||
) { | ||
def inputSourceDirectory: AbsolutePath = | ||
inputSourceDirectories.head | ||
def outputSourceDirectory: AbsolutePath = | ||
outputSourceDirectories.head | ||
override def toString: String = { | ||
val map = Map( | ||
"inputSourceDirectories" -> inputSourceDirectories, | ||
"outputSourceDirectories" -> outputSourceDirectories, | ||
"inputClasspath" -> inputClasspath.syntax | ||
) | ||
pprint.PPrinter.BlackWhite.tokenize(map).mkString | ||
} | ||
} | ||
|
||
object TestkitProperties { | ||
|
||
/** Loads TestkitProperties from resource "scalafix-testkit.properties" of this classloader. */ | ||
def loadFromResources(): TestkitProperties = { | ||
import scala.collection.JavaConverters._ | ||
val props = new java.util.Properties() | ||
val path = "scalafix-testkit.properties" | ||
val in = this.getClass.getClassLoader.getResourceAsStream(path) | ||
if (in == null) { | ||
sys.error(s"Failed to load resource $path") | ||
} else { | ||
val sprops = props.asScala | ||
try props.load(in) | ||
finally in.close() | ||
new TestkitProperties( | ||
Classpath(sprops("inputClasspath")), | ||
Classpath(sprops("inputSourceDirectories")).entries, | ||
Classpath(sprops("outputSourceDirectories")).entries | ||
) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.