From 4c5dbb264356e5cecaebd20d532c7aa3764e5727 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 --- build.sbt | 17 ++++++++++ .../scala/fix/DirectoryAndPackageName.scala | 33 +++++++++++++++---- .../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, 56 insertions(+), 6 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..7ac4f09 100644 --- a/rules/src/main/scala/fix/DirectoryAndPackageName.scala +++ b/rules/src/main/scala/fix/DirectoryAndPackageName.scala @@ -1,6 +1,7 @@ package fix import metaconfig.generic.Surface +import metaconfig.Conf import metaconfig.ConfDecoder import metaconfig.Configured import scalafix.Diagnostic @@ -10,6 +11,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 @@ -18,7 +20,8 @@ import scala.meta.inputs.Position case class DirectoryAndPackageNameConfig( baseDirectory: Seq[String], - keyword: Seq[String] + keyword: Seq[String], + severity: LintSeverity ) object DirectoryAndPackageNameConfig { @@ -82,14 +85,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") { @@ -158,7 +174,8 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn DirectoryPackageWarn( path = path, packageName = packageName, - position = packages.last.pos + position = packages.last.pos, + severity = config.severity ) ) } @@ -166,8 +183,12 @@ class DirectoryAndPackageName(config: DirectoryAndPackageNameConfig) extends Syn } } -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 } diff --git a/sbt-test/test/DirectoryAndPackageName/.scalafix.conf b/sbt-test/test/DirectoryAndPackageName/.scalafix.conf new file mode 100644 index 0000000..296f890 --- /dev/null +++ b/sbt-test/test/DirectoryAndPackageName/.scalafix.conf @@ -0,0 +1,5 @@ +rules = [ + DirectoryAndPackageName +] + +DirectoryAndPackageName.lintSeverity = 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