From 7788ecacd4f9c292f4416b16c6eecaae663a15a8 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Thu, 28 Dec 2023 10:33:33 +0900 Subject: [PATCH] add scripted test for DirectoryAndPackageName. support Windows --- build.sbt | 17 ++++++ .../scala/fix/DirectoryAndPackageName.scala | 61 ++++++++++++------- .../DirectoryAndPackageName/.scalafix.conf | 5 ++ .../test/DirectoryAndPackageName/build.sbt | 1 + .../project/plugins.sbt | 1 + .../src/main/scala/a/X.scala | 3 + sbt-test/test/DirectoryAndPackageName/test | 2 + 7 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 sbt-test/test/DirectoryAndPackageName/.scalafix.conf create mode 100644 sbt-test/test/DirectoryAndPackageName/build.sbt create mode 100644 sbt-test/test/DirectoryAndPackageName/project/plugins.sbt create mode 100644 sbt-test/test/DirectoryAndPackageName/src/main/scala/a/X.scala create mode 100644 sbt-test/test/DirectoryAndPackageName/test diff --git a/build.sbt b/build.sbt index 259e5b7..b1828ee 100644 --- a/build.sbt +++ b/build.sbt @@ -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 { diff --git a/rules/src/main/scala/fix/DirectoryAndPackageName.scala b/rules/src/main/scala/fix/DirectoryAndPackageName.scala index 602110f..6a2a76a 100644 --- a/rules/src/main/scala/fix/DirectoryAndPackageName.scala +++ b/rules/src/main/scala/fix/DirectoryAndPackageName.scala @@ -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 { @@ -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") { @@ -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 => @@ -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 -} diff --git a/sbt-test/test/DirectoryAndPackageName/.scalafix.conf b/sbt-test/test/DirectoryAndPackageName/.scalafix.conf new file mode 100644 index 0000000..5a2fe83 --- /dev/null +++ b/sbt-test/test/DirectoryAndPackageName/.scalafix.conf @@ -0,0 +1,5 @@ +rules = [ + DirectoryAndPackageName +] + +DirectoryAndPackageName.severity = error diff --git a/sbt-test/test/DirectoryAndPackageName/build.sbt b/sbt-test/test/DirectoryAndPackageName/build.sbt new file mode 100644 index 0000000..856724a --- /dev/null +++ b/sbt-test/test/DirectoryAndPackageName/build.sbt @@ -0,0 +1 @@ +ThisBuild / scalafixDependencies += "com.github.xuwei-k" %% "scalafix-rules" % sys.props("scalafix-rules.version") diff --git a/sbt-test/test/DirectoryAndPackageName/project/plugins.sbt b/sbt-test/test/DirectoryAndPackageName/project/plugins.sbt new file mode 100644 index 0000000..45b3184 --- /dev/null +++ b/sbt-test/test/DirectoryAndPackageName/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("scalafix.version")) diff --git a/sbt-test/test/DirectoryAndPackageName/src/main/scala/a/X.scala b/sbt-test/test/DirectoryAndPackageName/src/main/scala/a/X.scala new file mode 100644 index 0000000..6193582 --- /dev/null +++ b/sbt-test/test/DirectoryAndPackageName/src/main/scala/a/X.scala @@ -0,0 +1,3 @@ +package b + +class X diff --git a/sbt-test/test/DirectoryAndPackageName/test b/sbt-test/test/DirectoryAndPackageName/test new file mode 100644 index 0000000..e9f1cce --- /dev/null +++ b/sbt-test/test/DirectoryAndPackageName/test @@ -0,0 +1,2 @@ +> Test/compile +-> scalafixAll