Skip to content

Commit

Permalink
add scripted test for DirectoryAndPackageName. support Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Dec 28, 2023
1 parent 5481434 commit 7788eca
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 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
61 changes: 39 additions & 22 deletions rules/src/main/scala/fix/DirectoryAndPackageName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import scalafix.v1.Configuration
import scalafix.v1.Rule
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import java.io.File
import java.util.Locale
import scala.meta.Pkg
import scala.meta.Term
import scala.meta.Tree
import scala.meta.inputs.Input
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 @@ -118,18 +133,25 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn
x
}.headOption

val scalaSourceOpt = PartialFunction.condOpt(doc.input) {
case f: Input.VirtualFile =>
f.path
case f: Input.File =>
f.path.toString
}
val scalaSourceOpt = PartialFunction
.condOpt(doc.input) {
case f: Input.VirtualFile =>
f.path
case f: Input.File =>
f.path.toString
}
.map { path =>
File.separatorChar match {
case '/' =>
path
case c =>
path.replace(c, '/')
}
}

{
for {
path <- scalaSourceOpt
// TODO Windows
if !scala.util.Properties.isWin
dirOpt = config.baseDirectory.find { dir =>
path.contains(dir)
}.map { dir =>
Expand All @@ -155,19 +177,14 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn
if packageName != dir
} yield {
Patch.lint(
DirectoryPackageWarn(
path = path,
packageName = packageName,
position = packages.last.pos
Diagnostic(
id = "",
message = s"inconsistent package and directory\n${path}\n${packageName}",
position = packages.last.pos,
severity = config.severity
)
)
}
}.getOrElse(Patch.empty)
}
}

case class DirectoryPackageWarn(path: String, packageName: String, override val position: Position) 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 7788eca

Please sign in to comment.