Skip to content

Commit

Permalink
Ramp up tests, towards #38.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Jan 2, 2017
1 parent 14dc2bc commit 3bd5a8a
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 40 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import sbt.ScriptedPlugin
import sbt.ScriptedPlugin._

organization in ThisBuild := "ch.epfl.scala"
version in ThisBuild := "0.2.1"

lazy val crossVersions = Seq(
"2.11.8",
Expand Down Expand Up @@ -68,7 +67,7 @@ lazy val publishSettings = Seq(
"Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
homepage := Some(url("https://github.com/scalacenter/scalafix")),
autoAPIMappings := true,
apiURL := Some(url("https://scalacenter.github.io/scalafix/docs/")),
apiURL := Some(url("https://scalacenter.github.io/scalafix/")),
scmInfo := Some(
ScmInfo(
url("https://github.com/scalacenter/scalafix"),
Expand All @@ -86,6 +85,7 @@ lazy val publishSettings = Seq(
)

lazy val noPublish = Seq(
publishArtifact := false,
publish := {},
publishLocal := {}
)
Expand All @@ -94,6 +94,7 @@ lazy val buildInfoSettings: Seq[Def.Setting[_]] = Seq(
buildInfoKeys := Seq[BuildInfoKey](
name,
version,
"stableVersion" -> version.value, // TODO(olafur) customize.
scalaVersion,
sbtVersion
),
Expand Down
41 changes: 41 additions & 0 deletions scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixTestUtility.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package scalafix.sbt

import sbt.Keys.TaskStreams

/**
* Created by ollie on 02/01/17.
*/
object ScalafixTestUtility {
// returns true if test succeeded, else false.
def assertContentMatches(streams: TaskStreams)(
file: String,
expectedUntrimmed: String): Boolean = {
val expected = expectedUntrimmed.trim
val obtained = new String(
java.nio.file.Files.readAllBytes(
java.nio.file.Paths.get(
new java.io.File(file).toURI
))
).trim

if (obtained.diff(expected).nonEmpty ||
expected.diff(obtained).nonEmpty) {
val msg =
s"""File: $file
|Obtained output:
|$obtained
|Expected:
|$expected
|Diff:
|${obtained.diff(expected)}
|${expected.diff(obtained)}
|""".stripMargin
streams.log.error(file)
streams.log.error(msg)
false
} else {
streams.log.success(file)
true
}
}
}
34 changes: 2 additions & 32 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
scalafixConfig in ThisBuild := Some(baseDirectory.value / ".scalafix.conf")

lazy val root = project
.in(file("."))
.aggregate(
Expand All @@ -13,36 +11,8 @@ lazy val p2 = project.settings(scalaVersion := "2.10.5")
lazy val p3 = project.settings(scalaVersion := "2.12.1")

TaskKey[Unit]("check") := {
// returns true if test succeeded, else false.
def assertContentMatches(file: String, expectedUntrimmed: String): Boolean = {
val expected = expectedUntrimmed.trim
val obtained = new String(
java.nio.file.Files.readAllBytes(
java.nio.file.Paths.get(
new java.io.File(file).toURI
))
).trim

if (obtained.diff(expected).nonEmpty ||
expected.diff(obtained).nonEmpty) {
val msg =
s"""File: $file
|Obtained output:
|$obtained
|Expected:
|$expected
|Diff:
|${obtained.diff(expected)}
|${expected.diff(obtained)}
|""".stripMargin
streams.value.log.error(file)
streams.value.log.error(msg)
false
} else {
streams.value.log.success(file)
true
}
}
val assertContentMatches: ((String, String) => Boolean) =
scalafix.sbt.ScalafixTestUtility.assertContentMatches(streams.value) _
val expected =
"""object Main {
| implicit val x: Int = 2
Expand Down
22 changes: 22 additions & 0 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/config/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
scalaVersion := "2.11.8"

scalafixConfig in ThisBuild := Some(file("myscalafix.conf"))

TaskKey[Unit]("check") := {
val assertContentMatches: ((String, String) => Boolean) =
scalafix.sbt.ScalafixTestUtility.assertContentMatches(streams.value) _
val expected =
"""object Main {
| implicit val x = 2
| @volatile lazy val y = 2
| def main(args: Array[String]) {
| println("hello")
| }
|}""".stripMargin
assert(
assertContentMatches(
"src/main/scala/Test.scala",
expected
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rewrites = [VolatileLazyVal]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sbt.version=0.13.13

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Main {
implicit val x = 2
lazy val y = 2
def main(args: Array[String]) {
println("hello")
}
}
5 changes: 5 additions & 0 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/config/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# compile should not affect scalafix
> compile
> scalafix
> check

20 changes: 20 additions & 0 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/noconfig/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
scalaVersion := "2.11.8"

TaskKey[Unit]("check") := {
val assertContentMatches: ((String, String) => Boolean) =
scalafix.sbt.ScalafixTestUtility.assertContentMatches(streams.value) _
val expected =
"""object Main {
| implicit val x: Int = 2
| lazy val y = 2
| def main(args: Array[String]): Unit = {
| println("hello")
| }
|}""".stripMargin
assert(
assertContentMatches(
"src/main/scala/Test.scala",
expected
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sbt.version=0.13.13

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Main {
implicit val x = 2
lazy val y = 2
def main(args: Array[String]) {
println("hello")
}
}
5 changes: 5 additions & 0 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/noconfig/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# compile should not affect scalafix
> compile
> scalafix
> check

Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ abstract class IntegrationPropertyTest(t: ItTest, skip: Boolean = false)

case class FailOk(cmd: String, msg: String) extends Exception(msg)
def check(): Unit = {

def assertDiffIsNonEmpty(): Unit = {
%%("git", "diff", "--name-only")(t.workingPath).out.lines
.exists(_.endsWith(".scala"))
}
def sbt(cmd: String): Unit = {
val id = s"${t.name}/$cmd"
logger.info(s"Running $id")
Expand All @@ -111,12 +116,8 @@ abstract class IntegrationPropertyTest(t: ItTest, skip: Boolean = false)
}
val testFun: () => Any = { () =>
setup(t)
try {
t.cmds.map(_.cmd).foreach(sbt)
} catch {
case FailOk(cmd, msg) =>
logger.warn(s"Failed to run $cmd, error $msg")
}
t.cmds.map(_.cmd).foreach(sbt)
assertDiffIsNonEmpty()
}

if (skip) ignore(t.name)(testFun())
Expand Down
1 change: 1 addition & 0 deletions version.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version in ThisBuild := "0.2.2-SNAPSHOT"

0 comments on commit 3bd5a8a

Please sign in to comment.