From acf1a39ef4fb17d980b5dbc8edb558ef855ade9a Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Wed, 20 Mar 2024 03:01:32 -0400 Subject: [PATCH] Add scripted tests for Scala 3 --- build.sbt | 2 +- src/main/scala/sbtbuildinfo/BuildInfo.scala | 4 +- src/sbt-test/sbt-buildinfo/scala3/build.sbt | 77 +++++++++++++++++++ .../sbt-buildinfo/scala3/project/plugins.sbt | 7 ++ src/sbt-test/sbt-buildinfo/scala3/test | 4 + 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/sbt-test/sbt-buildinfo/scala3/build.sbt create mode 100644 src/sbt-test/sbt-buildinfo/scala3/project/plugins.sbt create mode 100644 src/sbt-test/sbt-buildinfo/scala3/test diff --git a/build.sbt b/build.sbt index 9458495..1d852b3 100644 --- a/build.sbt +++ b/build.sbt @@ -17,7 +17,7 @@ lazy val root = (project in file(".")) scriptedBufferLog := false, (pluginCrossBuild / sbtVersion) := { scalaBinaryVersion.value match { - case "2.12" => "1.2.8" + case "2.12" => "1.5.8" } } ) diff --git a/src/main/scala/sbtbuildinfo/BuildInfo.scala b/src/main/scala/sbtbuildinfo/BuildInfo.scala index 0db64a7..ee3e02f 100644 --- a/src/main/scala/sbtbuildinfo/BuildInfo.scala +++ b/src/main/scala/sbtbuildinfo/BuildInfo.scala @@ -1,5 +1,6 @@ package sbtbuildinfo +import scala.annotation.nowarn import sbt._, Keys._ case class BuildInfoResult(identifier: String, value: Any, typeExpr: TypeExpression) @@ -44,9 +45,10 @@ object BuildInfo { distinctKeys.flatMap(entry(_)).join } + @nowarn private def scope(scoped: Scoped, project: ProjectReference) = { val scope0 = scoped.scope - if (scope0.project == This) scope0 in project + if (scope0.project == This) scope0.in(project) else scope0 } diff --git a/src/sbt-test/sbt-buildinfo/scala3/build.sbt b/src/sbt-test/sbt-buildinfo/scala3/build.sbt new file mode 100644 index 0000000..3531074 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/scala3/build.sbt @@ -0,0 +1,77 @@ +lazy val check = taskKey[Unit]("checks this plugin") + +ThisBuild / version := "0.1" +ThisBuild / scalaVersion := "3.4.0" + +lazy val root = (project in file(".")) + .enablePlugins(BuildInfoPlugin) + .settings( + name := "helloworld", + buildInfoKeys := Seq( + name, + scalaVersion, + ), + buildInfoPackage := "hello", + buildInfoOptions ++= Seq( + BuildInfoOption.ToJson, + ), + homepage := Some(url("http://example.com")), + licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")), + scalacOptions ++= Seq("-Xfatal-warnings"), + check := { + val f = (Compile / sourceManaged).value / "sbt-buildinfo" / ("%s.scala" format "BuildInfo") + val lines = scala.io.Source.fromFile(f).getLines.toList + val Expected = """// $COVERAGE-OFF$""" :: + """package hello""" :: + """""" :: + """/** This object was generated by sbt-buildinfo. */""" :: + """case object BuildInfo {""" :: + """ /** The value is "helloworld". */""" :: + """ val name: String = "helloworld"""" :: + """ /** The value is "3.4.0". */""" :: + """ val scalaVersion: String = "3.4.0"""" :: + """ override val toString: String = {""" :: + """ "name: %s, scalaVersion: %s".format(""" :: + """ name, scalaVersion""" :: + """ )""" :: + """ }""" :: + """ val toMap: Map[String, scala.Any] = Map[String, scala.Any](""" :: + """ "name" -> name,""" :: + """ "scalaVersion" -> scalaVersion)""" :: + """""" :: + """ private def quote(x: scala.Any): String = "\"" + x + "\""""" :: + """ private def toJsonValue[T <: Matchable](value: T): String = {""" :: + """ value match {""" :: + """ case elem: scala.collection.Seq[? <: Matchable] => elem.map(toJsonValue).mkString("[", ",", "]")""" :: + """ case elem: scala.Option[? <: Matchable] => elem.map(toJsonValue).getOrElse("null")""" :: + """ case elem: scala.collection.Map[?, ? <: Matchable] => elem.map {""" :: + """ case (k, v) => toJsonValue(k.toString) + ":" + toJsonValue(v)""" :: + """ }.mkString("{", ", ", "}")""" :: + """ case d: scala.Double => d.toString""" :: + """ case f: scala.Float => f.toString""" :: + """ case l: scala.Long => l.toString""" :: + """ case i: scala.Int => i.toString""" :: + """ case s: scala.Short => s.toString""" :: + """ case bool: scala.Boolean => bool.toString""" :: + """ case str: String => quote(str)""" :: + """ case other => quote(other.toString)""" :: + """ }""" :: + """ }""" :: + """""" :: + """ val toJson: String = toJsonValue(toMap)""" :: + """}""" :: + """// $COVERAGE-ON$""" :: + Nil + lines match { + case Expected => () + case _ => + (lines, Expected).zipped.toList.foreach { case (a, b) => + if (a != b) { + println("- " + a) + } + } + sys.error("unexpected output: \n" + lines.mkString("\n")) + } + () + } + ) diff --git a/src/sbt-test/sbt-buildinfo/scala3/project/plugins.sbt b/src/sbt-test/sbt-buildinfo/scala3/project/plugins.sbt new file mode 100644 index 0000000..e392da8 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/scala3/project/plugins.sbt @@ -0,0 +1,7 @@ +{ + val pluginVersion = System.getProperty("plugin.version") + if(pluginVersion == null) + throw new RuntimeException("""|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) + else addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % pluginVersion) +} diff --git a/src/sbt-test/sbt-buildinfo/scala3/test b/src/sbt-test/sbt-buildinfo/scala3/test new file mode 100644 index 0000000..f5588c2 --- /dev/null +++ b/src/sbt-test/sbt-buildinfo/scala3/test @@ -0,0 +1,4 @@ +> compile +> doc +$ exists target/scala-3.4.0/src_managed/main/sbt-buildinfo/BuildInfo.scala +> check