Skip to content

Commit

Permalink
lightbend-labs#171 Exclude glob problem filter
Browse files Browse the repository at this point in the history
This alse makes excludePackage filter obsolete
and removes it.
  • Loading branch information
2m committed Apr 29, 2017
1 parent 14e2157 commit ece8e07
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
13 changes: 3 additions & 10 deletions core/src/main/scala/com/typesafe/tools/mima/core/Filters.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.typesafe.tools.mima.core
import scala.reflect.ClassTag
import sbt.io.GlobFilter

object ProblemFilters {

private case class ExcludeByName[P <: ProblemRef: ClassTag](name: String) extends ProblemFilter {
private[this] val glob = GlobFilter(name)
override def apply(problem: Problem): Boolean = {
!(implicitly[ClassTag[P]].runtimeClass.isAssignableFrom(problem.getClass) &&
Some(name) == problem.matchName)
glob.accept(problem.matchName.getOrElse("")))
}

override def toString(): String = """ExcludeByName[%s]("%s")""".format(implicitly[ClassTag[P]].runtimeClass.getSimpleName, name)
Expand All @@ -27,13 +29,4 @@ object ProblemFilters {
val problemClass: Class[_ <: ProblemRef] = Class.forName("com.typesafe.tools.mima.core." + problemName).asInstanceOf[Class[_ <: ProblemRef]]
exclude(name)(ClassTag(problemClass))
}

private case class ExcludeByPackage(excludedPackageName: String) extends ProblemFilter {
def apply(problem: Problem): Boolean = {
// careful to avoid excluding "com.foobar" with an exclusion "com.foo"
!problem.matchName.getOrElse("").startsWith(excludedPackageName + ".")
}
}

def excludePackage(packageName: String): ProblemFilter = new ExcludeByPackage(packageName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.typesafe.tools.mima.core

import org.scalatest._
import org.scalatest.prop.TableDrivenPropertyChecks

class ProblemFiltersSpec extends WordSpec with TableDrivenPropertyChecks with Matchers {
import ProblemFiltersSpec._

val filters = Table(
("filter", "problem", "realProblem"),
(ProblemFilters.exclude[Problem]("impl.Http"), problem("impl.Http"), false),
(ProblemFilters.exclude[Problem]("impl.Http"), problem("impl.Http2"), true),
(ProblemFilters.exclude[Problem]("impl.*"), problem("impl.Http"), false),
(ProblemFilters.exclude[Problem]("impl.*"), problem("impl2.Http"), true),
(ProblemFilters.exclude[Problem]("a$Impl*"), problem("a$Impl$B"), false),
(ProblemFilters.exclude[Problem]("a$Impl*"), problem("a2$Impl$B"), true)
)

"problem filters" should {
"filter problems" in {
forAll (filters) { (filter, problem, realProblem) =>
filter(problem) shouldBe realProblem
}
}
}

}

object ProblemFiltersSpec {
def problem(name: String) = new TemplateProblem(NoClass) {
override def description: (String) => String = ???
override def matchName: Some[String] = Some(name)
}
}
8 changes: 7 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ object Dependencies {
import BuildSettings._

val typesafeConfig = "com.typesafe" % "config" % "1.0.0"
val sbtIo = "org.scala-sbt" %% "io" % "1.0.0-M11"
val scalatest = "org.scalatest" %% "scalatest" % "3.0.1" % Test

}

Expand Down Expand Up @@ -104,7 +106,11 @@ object MimaBuild {
buildInfoObject := "BuildInfo"
)
)
settings(libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value,
settings(libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
sbtIo,
scalatest
),
name := buildName + "-core")
settings(sonatypePublishSettings:_*)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CollectProblemsTest {
val allProblems = mima.collectProblems(oldJarPath, newJarPath)

val problems = (if(filterPath ne null) {
val fallback = ConfigFactory.parseString("filter { problems = []\npackages=[] }")
val fallback = ConfigFactory.parseString("filter { problems = [] }")
val config = ConfigFactory.parseFile(new File(filterPath)).withFallback(fallback).resolve()
val filters = ProblemFiltersConfig.parseProblemFilters(config)
allProblems.filter(p => filters.forall(_.apply(p)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.collection.JavaConverters._

object ProblemFiltersConfig {
private val filterProblemsPath = "filter.problems"
private val filterPackagesPath = "filter.packages"
private val problemNameKey = "problemName"
private val matchNameKey = "matchName"

Expand All @@ -20,16 +19,11 @@ object ProblemFiltersConfig {
*/
def parseProblemFilters(config: Config): Seq[ProblemFilter] = {
val filters = config.getConfigList(filterProblemsPath).asScala.toSeq
val individualFilter = for (problemConfig <- filters) yield {
for (problemConfig <- filters) yield {
val problemClassName = problemConfig.getString(problemNameKey)
val matchName = problemConfig.getString(matchNameKey)
ProblemFilters.exclude(problemClassName, matchName)
}
val packages = config.getStringList(filterPackagesPath).asScala.toSeq
val packageFilter = for (pack <- packages) yield {
ProblemFilters.excludePackage(pack)
}
individualFilter ++ packageFilter
}

/**
Expand Down

0 comments on commit ece8e07

Please sign in to comment.