Skip to content

Commit

Permalink
add scripted test for DirectoryAndPackageName
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 28, 2023
1 parent 5481434 commit 8cc8ea7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
17 changes: 17 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ lazy val rules = projectMatrix
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(rulesCrossVersions)

lazy val rules212 = rules
.jvm(V.scala212)
.enablePlugins(ScriptedPlugin)
.settings(
Test / test := (Test / test).dependsOn(scripted.toTask("")).value,
scriptedBufferLog := false,
scriptedLaunchOpts += ("-Dscalafix-rules.version=" + version.value),
scriptedLaunchOpts += ("-Dscalafix.version=" + _root_.scalafix.sbt.BuildInfo.scalafixVersion),
sbtTestDirectory := (LocalRootProject / baseDirectory).value / "sbt-test",
scriptedLaunchOpts ++= {
import scala.collection.JavaConverters.*
val javaVmArgs: List[String] =
java.lang.management.ManagementFactory.getRuntimeMXBean.getInputArguments.asScala.toList
javaVmArgs.filter(a => Seq("-Xmx", "-Xms", "-XX", "-Dsbt.log.noformat").exists(a.startsWith))
}
)

lazy val inputOutputCommon = Def.settings(
scalacOptions ++= {
scalaBinaryVersion.value match {
Expand Down
32 changes: 26 additions & 6 deletions rules/src/main/scala/fix/DirectoryAndPackageName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scalafix.v1.Configuration
import scalafix.v1.Rule
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import java.util.Locale
import scala.meta.Pkg
import scala.meta.Term
import scala.meta.Tree
Expand All @@ -18,7 +19,8 @@ import scala.meta.inputs.Position

case class DirectoryAndPackageNameConfig(
baseDirectory: Seq[String],
keyword: Seq[String]
keyword: Seq[String],
severity: LintSeverity
)

object DirectoryAndPackageNameConfig {
Expand Down Expand Up @@ -82,14 +84,27 @@ object DirectoryAndPackageNameConfig {
"while",
"with",
"yield",
)
),
severity = LintSeverity.Warning
)

implicit val surface: Surface[DirectoryAndPackageNameConfig] =
metaconfig.generic.deriveSurface[DirectoryAndPackageNameConfig]

implicit val decoder: ConfDecoder[DirectoryAndPackageNameConfig] =
implicit val decoder: ConfDecoder[DirectoryAndPackageNameConfig] = {
implicit val instance: metaconfig.ConfDecoder[scalafix.lint.LintSeverity] = { conf =>
conf.as[String].map(_.toUpperCase(Locale.ROOT)).map {
case "ERROR" =>
LintSeverity.Error
case "INFO" =>
LintSeverity.Info
case _ =>
LintSeverity.Warning
}
}

metaconfig.generic.deriveDecoder(default)
}
}

class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends SyntacticRule("DirectoryAndPackageName") {
Expand Down Expand Up @@ -158,16 +173,21 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn
DirectoryPackageWarn(
path = path,
packageName = packageName,
position = packages.last.pos
position = packages.last.pos,
severity = config.severity
)
)
}
}.getOrElse(Patch.empty)
}
}

case class DirectoryPackageWarn(path: String, packageName: String, override val position: Position) extends Diagnostic {
case class DirectoryPackageWarn(
path: String,
packageName: String,
override val position: Position,
override val severity: LintSeverity
) extends Diagnostic {
override def message = s"inconsistent package and directory\n${path}\n${packageName}"

override def severity: LintSeverity = LintSeverity.Warning
}
5 changes: 5 additions & 0 deletions sbt-test/test/DirectoryAndPackageName/.scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rules = [
DirectoryAndPackageName
]

DirectoryAndPackageName.severity = error
1 change: 1 addition & 0 deletions sbt-test/test/DirectoryAndPackageName/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ThisBuild / scalafixDependencies += "com.github.xuwei-k" %% "scalafix-rules" % sys.props("scalafix-rules.version")
1 change: 1 addition & 0 deletions sbt-test/test/DirectoryAndPackageName/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("scalafix.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package b

class X
2 changes: 2 additions & 0 deletions sbt-test/test/DirectoryAndPackageName/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> Test/compile
-> scalafixAll

0 comments on commit 8cc8ea7

Please sign in to comment.