Moved tests to internal package

This commit is contained in:
andrzej.jozwik@gmail.com 2014-09-30 14:54:33 +02:00 committed by Eugene Yokota
parent 9294351e24
commit 2a603da0a8
26 changed files with 103 additions and 76 deletions

View File

@ -10,6 +10,7 @@ import Types.Endo
import compiler.Eval
import SessionSettings._
import sbt.internals.parser.SbtRefactorings
/**
* Represents (potentially) transient settings added into a build via commands/user.
@ -192,7 +193,7 @@ object SessionSettings {
}
val newSettings = settings diff replace
val oldContent = IO.readLines(writeTo)
val exist: List[String] = SessionSettingsNoBlankies.oldLinesToNew(oldContent, statements)
val exist: List[String] = SbtRefactorings.applyStatements(oldContent, statements)
val adjusted = if (!newSettings.isEmpty && needsTrailingBlank(exist)) exist :+ "" else exist
val lines = adjusted ++ newSettings.flatMap(_._2 ::: "" :: Nil)
IO.writeLines(writeTo, lines)

View File

@ -1,15 +1,14 @@
package sbt
package sbt.internals.parser
import scala.reflect.runtime.universe._
import sbt.internals.parser.{ XmlContent, SplitExpressionsNoBlankies }
object SessionSettingsNoBlankies {
import SplitExpressionsNoBlankies.FAKE_FILE
private[sbt] object SbtRefactorings {
import sbt.internals.parser.SplitExpressionsNoBlankies.{ END_OF_LINE, FAKE_FILE }
val EMPTY_STRING = ""
val REVERSE_ORDERING_INT = Ordering[Int].reverse
def oldLinesToNew(lines: List[String], setCommands: List[List[String]]): List[String] = {
def applyStatements(lines: List[String], setCommands: List[List[String]]): List[String] = {
val split = SplitExpressionsNoBlankies(FAKE_FILE, lines)
val recordedCommand = setCommands.flatMap {
command =>
@ -21,9 +20,9 @@ object SessionSettingsNoBlankies {
val treeName = extractSettingName(tree)
if (name == treeName) {
val replacement = if (acc.isEmpty) {
command.mkString("\n")
command.mkString(END_OF_LINE)
} else {
""
EMPTY_STRING
}
(tree.pos.start, statement, replacement) +: acc
} else {
@ -39,7 +38,6 @@ object SessionSettingsNoBlankies {
val before = acc.substring(0, from)
val after = acc.substring(from + old.length, acc.length)
before + replacement + after
// acc.replace(old, replacement)
}
newContent.lines.toList
}

View File

@ -22,7 +22,7 @@ private[sbt] case class SplitExpressionsNoBlankies(file: File, lines: Seq[String
import scala.reflect.runtime._
import scala.compat.Platform.EOL
import BugInParser._
import MissingBracketHandler._
import XmlContent._
import scala.tools.reflect.{ ToolBox, ToolBoxError }
@ -97,7 +97,7 @@ private[sbt] case class SplitExpressionsNoBlankies(file: File, lines: Seq[String
* Scala parser cuts last bracket -
* @see http://stackoverflow.com/questions/25547149/scala-parser-cuts-last-bracket
*/
private[sbt] object BugInParser {
private[sbt] object MissingBracketHandler {
/**
*
* @param content - parsed file

View File

@ -0,0 +1,17 @@
k1 := {}
k2 := {}
k3 := {
val x = "hi"
()
}
k4 := { }; k5 := ()
k1 <<= k1 map {_ => error("k1")}
k4 := { val x = k4.value; () }

View File

@ -0,0 +1 @@
k4 := ()

View File

@ -0,0 +1,15 @@
k1 := {}
k2 := {}
k3 := {
val x = "hi"
()
}
k4 := (); k5 := ()
k1 <<= k1 map {_ => error("k1")}

View File

@ -0,0 +1 @@
name := "alaMaKota"

View File

@ -1,7 +0,0 @@
<settings>
<setting>
<start>1</start>
<end>2</end>
<set><![CDATA[name := "alaMaKota"]]></set>
</setting>
</settings>

View File

@ -0,0 +1,2 @@
name := "alaMaKota"
organization := "scalania"

View File

@ -1,12 +0,0 @@
<settings>
<setting>
<start>1</start>
<end>2</end>
<set><![CDATA[name := "alaMaKota"]]></set>
</setting>
<setting>
<start>25</start>
<end>26</end>
<set><![CDATA[organization := "scalania"]]></set>
</setting>
</settings>

View File

@ -0,0 +1 @@
checkPom := "OK"

View File

@ -1,7 +0,0 @@
<settings>
<setting>
<start>4</start>
<end>11</end>
<set><![CDATA[checkPom := "OK"]]></set>
</setting>
</settings>

View File

@ -0,0 +1 @@
name := "alaMaKota"

View File

@ -1,7 +0,0 @@
<settings>
<setting>
<start>1</start>
<end>2</end>
<set><![CDATA[name := "alaMaKota"]]></set>
</setting>
</settings>

View File

@ -0,0 +1 @@
scmpom := <a/><b a="rt">OK</b>

View File

@ -1,7 +0,0 @@
<settings>
<setting>
<start>5</start>
<end>18</end>
<set><![CDATA[scmpom := <a/><b a="rt">OK</b>]]></set>
</setting>
</settings>

View File

@ -0,0 +1,17 @@
k1 := {}
k2 := {}
k3 := {
val x = "hi"
()
}
k4 := { }; k5 := ()
k1 <<= k1 map {_ => error("k1")}
k4 := { val x = k4.value; () }

View File

@ -0,0 +1 @@
k4 := ()

View File

@ -0,0 +1,15 @@
k1 := {}
k2 := {}
k3 := {
val x = "hi"
()
}
k4 := (); k5 := ()
k1 <<= k1 map {_ => error("k1")}

View File

@ -37,7 +37,7 @@ class ErrorSpec extends AbstractSpec with ScalaCheck {
| } /* */ //
|}
""".stripMargin
BugInParser.findMissingText(buildSbt, buildSbt.length, 2, "fake.txt", new MessageOnlyException("fake")) must throwA[MessageOnlyException]
MissingBracketHandler.findMissingText(buildSbt, buildSbt.length, 2, "fake.txt", new MessageOnlyException("fake")) must throwA[MessageOnlyException]
}
}

View File

@ -1,16 +1,14 @@
package sbt
package sbt.internals.parser
import java.io.{ File, FilenameFilter }
import org.specs2.matcher.MatchResult
import sbt.internals.parser.{ AbstractSpec, SplitExpressionsNoBlankies }
import scala.collection.GenTraversableOnce
import scala.io.Source
import scala.xml.XML
abstract class AbstractSessionSettingsSpec(folder: String, printDetails: Boolean = false) extends AbstractSpec {
protected val rootPath = getClass.getResource("").getPath + folder
abstract class AbstractSessionSettingsSpec(folder: String, deepCompare: Boolean = false) extends AbstractSpec {
protected val rootPath = getClass.getClassLoader.getResource("").getPath + folder
println(s"Reading files from: $rootPath")
protected val rootDir = new File(rootPath)
@ -25,7 +23,7 @@ abstract class AbstractSessionSettingsSpec(folder: String, printDetails: Boolean
}
}
private def runTestOnFiles(expectedResultAndMap: File => Seq[(List[String], List[List[String]])]): MatchResult[GenTraversableOnce[File]] = {
private def runTestOnFiles(expectedResultAndMap: File => Seq[(List[String], List[String])]): MatchResult[GenTraversableOnce[File]] = {
val allFiles = rootDir.listFiles(new FilenameFilter() {
def accept(dir: File, name: String) = name.endsWith(".sbt.txt")
@ -34,11 +32,15 @@ abstract class AbstractSessionSettingsSpec(folder: String, printDetails: Boolean
file =>
val originalLines = Source.fromFile(file).getLines().toList
foreach(expectedResultAndMap(file)) {
case (expectedResultList, map) =>
val resultList = SessionSettingsNoBlankies.oldLinesToNew(originalLines, map)
case (expectedResultList, commands) =>
val resultList = SbtRefactorings.applyStatements(originalLines, commands.map(List(_)))
val expected = SplitExpressionsNoBlankies(file, expectedResultList)
val result = SplitExpressionsNoBlankies(file, resultList)
if (deepCompare) {
expectedResultList must_== resultList
}
result.settings must_== expected.settings
}
}
}
@ -49,28 +51,22 @@ abstract class AbstractSessionSettingsSpec(folder: String, printDetails: Boolean
val startsWith = f.getName + "_"
name.startsWith(startsWith)
}
}).toList
}).toSeq
dirs.flatMap {
dir =>
val files = dir.listFiles(new FilenameFilter {
override def accept(dir: File, name: String) = name.endsWith(".xml")
override def accept(dir: File, name: String) = name.endsWith(".set")
})
files.map { xmlFile =>
val xml = XML.loadFile(xmlFile)
val result = Source.fromFile(xmlFile.getAbsolutePath + ".result").getLines().toList
val tupleCollection = (xml \\ "settings" \\ "setting").map {
node =>
val set = (node \\ "set").text
val start = (node \\ "start").text.toInt
val end = (node \\ "end").text.toInt
List(set)
}.toList
(result, tupleCollection)
files.map { file =>
val list = Source.fromFile(file).getLines().toList
val result = Source.fromFile(file.getAbsolutePath + ".result").getLines().toList
(result, list)
}
}
}
}
class SessionSettingsSpec extends AbstractSessionSettingsSpec("../session-settings")
class SessionSettingsSpec extends AbstractSessionSettingsSpec("session-settings")
//class SessionSettingsQuickSpec extends AbstractSessionSettingsSpec("session-settings-quick", true)