From d1f225705104244c6b1392e0c07135a82d147145 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Thu, 26 Dec 2024 16:33:21 -0500 Subject: [PATCH] Fix tests --- src/main/scala/sbtbuildinfo/JavaRenderer.scala | 13 ++++++++++++- .../sbt-buildinfo/caseclassrenderer/build.sbt | 6 +++--- .../sbt-buildinfo/constantvalue/build.sbt | 10 +++++----- .../javasingletonrenderer/build.sbt | 18 +++++++++++++----- .../sbt-buildinfo/javasingletonrenderer/test | 3 ++- .../javastaticfieldsrenderer/build.sbt | 16 ++++++++++++---- src/sbt-test/sbt-buildinfo/multi/build.sbt | 2 +- src/sbt-test/sbt-buildinfo/simple/build.sbt | 8 ++++---- .../skipimportscaseclass/build.sbt | 8 ++++---- .../sbt-buildinfo/skipimportscaseclass/test | 2 +- .../sbt-buildinfo/skipimportscaseobject/test | 2 +- .../sbt-buildinfo/usepackageaspath/build.sbt | 10 +++++----- .../sbt-buildinfo/usepackageaspath/test | 2 +- 13 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/main/scala/sbtbuildinfo/JavaRenderer.scala b/src/main/scala/sbtbuildinfo/JavaRenderer.scala index 2bcad39..c0fe484 100644 --- a/src/main/scala/sbtbuildinfo/JavaRenderer.scala +++ b/src/main/scala/sbtbuildinfo/JavaRenderer.scala @@ -40,6 +40,14 @@ abstract class JavaRenderer(pkg: String, cl: String, makeStatic: Boolean) extend | return null; | } | } + | + | private static java.net.URI internalAsUri(String uriString) { + | try { + | return new java.net.URI(uriString); + | } catch (Exception e) { + | return null; + | } + | } |""".stripMargin protected val buildMapLines: String = @@ -153,6 +161,7 @@ abstract class JavaRenderer(pkg: String, cl: String, makeStatic: Boolean) extend case TypeExpression("java.net.URL", Nil) => Some("java.net.URL") case TypeExpression("sbt.URL", Nil) => Some("java.net.URL") case TypeExpression("java.net.URI", Nil) => Some("java.net.URI") + case TypeExpression("sbt.URI", Nil) => Some("java.net.URI") case TypeExpression("java.io.File", Nil) => Some("java.io.File") case TypeExpression("sbt.File", Nil) => Some("java.io.File") case TypeExpression("scala.xml.NodeSeq", Nil) => None @@ -184,9 +193,10 @@ abstract class JavaRenderer(pkg: String, cl: String, makeStatic: Boolean) extend x0 <- tpeToReturnType(arg0) x1 <- tpeToReturnType(arg1) } yield s"java.util.Map.Entry<$x0, $x1>" - + case TypeExpression("java.time.LocalDate", Nil) => Some("java.time.LocalDate") case TypeExpression("java.time.Instant", Nil) => Some("java.time.Instant") + case TypeExpression("sbt.librarymanagement.License", Nil) => Some("String") case _ => // println(s"java other: $tpe") @@ -209,6 +219,7 @@ abstract class JavaRenderer(pkg: String, cl: String, makeStatic: Boolean) extend case op: Option[_] => op map { x => "java.util.Optional.of(" + quote(x) + ")" } getOrElse { "java.util.Optional.empty()" } case url: java.net.URL => "internalAsUrl(%s)" format quote(url.toString) + case uri: java.net.URI => "internalAsUri(%s)" format quote(uri.toString) case file: java.io.File => "new java.io.File(%s)" format quote(file.toString) case attr: sbt.Attributed[_] => quote(attr.data) case date: java.time.LocalDate => "java.time.LocalDate.parse(%s)" format quote(date.toString) diff --git a/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt b/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt index 0b6c52f..20c1410 100644 --- a/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt +++ b/src/sbt-test/sbt-buildinfo/caseclassrenderer/build.sbt @@ -51,8 +51,8 @@ lazy val root = (project in file(".")) projectVer :: """ scalaVersion: String,""" :: """ ivyXML: scala.xml.NodeSeq,""" :: - """ homepage: scala.Option[java.net.URL],""" :: - """ licenses: scala.collection.immutable.Seq[(String, java.net.URL)],""" :: + homepageComment :: + homepageCode :: apiMappingsDecl :: """ isSnapshot: scala.Boolean,""" :: """ year: scala.Int,""" :: @@ -107,7 +107,7 @@ lazy val root = (project in file(".")) """ projectVersion = 0.1,""" :: """ scalaVersion = "2.12.12",""" :: """ ivyXML = scala.xml.NodeSeq.Empty,""" :: - """ homepage = scala.Some(new java.net.URI("http://example.com").toURL),""" :: + homepage :: licenses :: """ apiMappings = Map(),""" :: """ isSnapshot = false,""" :: diff --git a/src/sbt-test/sbt-buildinfo/constantvalue/build.sbt b/src/sbt-test/sbt-buildinfo/constantvalue/build.sbt index d731c7f..d551281 100644 --- a/src/sbt-test/sbt-buildinfo/constantvalue/build.sbt +++ b/src/sbt-test/sbt-buildinfo/constantvalue/build.sbt @@ -57,10 +57,10 @@ lazy val root = (project in file(".")). scalaVersionInfo :: """ /** The value is scala.xml.NodeSeq.Empty. */""" :: """ val ivyXML: scala.xml.NodeSeq = scala.xml.NodeSeq.Empty""" :: - """ /** The value is scala.Some(new java.net.URI("http://example.com").toURL). */""" :: - """ val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URI("http://example.com").toURL)""" :: - """ /** The value is scala.collection.immutable.Seq(("MIT License" -> new java.net.URI("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE").toURL)). */""" :: - """ val licenses: scala.collection.immutable.Seq[(String, java.net.URL)] = scala.collection.immutable.Seq(("MIT License" -> new java.net.URI("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE").toURL))""" :: + homepageComment :: + homepageCode :: + licenseComment :: + licenseCode :: """ /** The value is Map(). */""" :: apiMappingsCode :: """ /** The value is false. */""" :: @@ -85,7 +85,7 @@ lazy val root = (project in file(".")). """ )""" :: """ }""" :: """}""" :: - """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target = new java.io.File(") && + """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target: java.io.File = new java.io.File(") && (scalaVersionInfo.trim == s"""final val scalaVersion = "$sv"""") => () case _ => sys.error("unexpected output: \n" + lines.mkString("\n")) } diff --git a/src/sbt-test/sbt-buildinfo/javasingletonrenderer/build.sbt b/src/sbt-test/sbt-buildinfo/javasingletonrenderer/build.sbt index 2f85185..a87f9b2 100644 --- a/src/sbt-test/sbt-buildinfo/javasingletonrenderer/build.sbt +++ b/src/sbt-test/sbt-buildinfo/javasingletonrenderer/build.sbt @@ -53,10 +53,10 @@ lazy val root = (project in file(".")) """ public final String name = "helloworld";""" :: """ /** The value is "2.12.12". */""" :: """ public final String scalaVersion = "2.12.12";""" :: - """ /** The value is java.util.Optional.of(internalAsUrl("http://example.com")). */""" :: - """ public final java.util.Optional homepage = java.util.Optional.of(internalAsUrl("http://example.com"));""" :: - """ /** The value is java.util.Collections.unmodifiableList(java.util.Arrays.asList(new java.util.AbstractMap.SimpleImmutableEntry<>("MIT License", internalAsUrl("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")))). */""" :: - """ public final java.util.Collection> licenses = java.util.Collections.unmodifiableList(java.util.Arrays.asList(new java.util.AbstractMap.SimpleImmutableEntry<>("MIT License", internalAsUrl("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))));""" :: + homepageComment :: + homepageCode :: + licensesComment :: + licensesCode :: """ /** The value is internalAsMap(). */""" :: apiMappingsCode :: """ /** The value is false. */""" :: @@ -105,6 +105,14 @@ lazy val root = (project in file(".")) """ }""" :: """ }""" :: """""" :: + """ private static java.net.URI internalAsUri(String uriString) {""" :: + """ try {""" :: + """ return new java.net.URI(uriString);""" :: + """ } catch (Exception e) {""" :: + """ return null;""" :: + """ }""" :: + """ }""" :: + """""" :: """ @SuppressWarnings({"unchecked", "rawtypes"})""" :: """ private static java.util.Map internalAsMap(java.util.Map.Entry... entries) {""" :: """ java.util.Map m = new java.util.HashMap<>();""" :: @@ -151,7 +159,7 @@ lazy val root = (project in file(".")) """""" :: """ public final String toJson = toJsonValue(toMap());""" :: """}""" :: - """// $COVERAGE-ON$""" :: Nil if (targetInfo.contains("target = new java.io.File(") && + """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target = new java.io.File(") && targetInfoComment.contains("/** The value is new java.io.File(") => () case _ => sys.error("unexpected output: \n" + lines.mkString("\n")) } diff --git a/src/sbt-test/sbt-buildinfo/javasingletonrenderer/test b/src/sbt-test/sbt-buildinfo/javasingletonrenderer/test index 070eac4..f8d3436 100644 --- a/src/sbt-test/sbt-buildinfo/javasingletonrenderer/test +++ b/src/sbt-test/sbt-buildinfo/javasingletonrenderer/test @@ -1,4 +1,5 @@ > compile -# $ exists target/src_managed/main/sbt-buildinfo/BuildInfo.java +# > show Compile/sources +# $ exists target/**/src_managed/main/sbt-buildinfo/BuildInfo.java > check diff --git a/src/sbt-test/sbt-buildinfo/javastaticfieldsrenderer/build.sbt b/src/sbt-test/sbt-buildinfo/javastaticfieldsrenderer/build.sbt index c8bd32d..4a474f0 100644 --- a/src/sbt-test/sbt-buildinfo/javastaticfieldsrenderer/build.sbt +++ b/src/sbt-test/sbt-buildinfo/javastaticfieldsrenderer/build.sbt @@ -51,10 +51,10 @@ lazy val root = (project in file(".")) """ public static final String name = "helloworld";""" :: """ /** The value is "2.12.12". */""" :: """ public static final String scalaVersion = "2.12.12";""" :: - """ /** The value is java.util.Optional.of(internalAsUrl("http://example.com")). */""" :: - """ public static final java.util.Optional homepage = java.util.Optional.of(internalAsUrl("http://example.com"));""" :: - """ /** The value is java.util.Collections.unmodifiableList(java.util.Arrays.asList(new java.util.AbstractMap.SimpleImmutableEntry<>("MIT License", internalAsUrl("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")))). */""" :: - """ public static final java.util.Collection> licenses = java.util.Collections.unmodifiableList(java.util.Arrays.asList(new java.util.AbstractMap.SimpleImmutableEntry<>("MIT License", internalAsUrl("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE"))));""" :: + homepageComment :: + homepageCode :: + licensesComment :: + licensesCode :: """ /** The value is internalAsMap(). */""" :: apiMappingsCode :: """ /** The value is false. */""" :: @@ -103,6 +103,14 @@ lazy val root = (project in file(".")) """ }""" :: """ }""" :: """""" :: + """ private static java.net.URI internalAsUri(String uriString) {""" :: + """ try {""" :: + """ return new java.net.URI(uriString);""" :: + """ } catch (Exception e) {""" :: + """ return null;""" :: + """ }""" :: + """ }""" :: + """""" :: """ @SuppressWarnings({"unchecked", "rawtypes"})""" :: """ private static java.util.Map internalAsMap(java.util.Map.Entry... entries) {""" :: """ java.util.Map m = new java.util.HashMap<>();""" :: diff --git a/src/sbt-test/sbt-buildinfo/multi/build.sbt b/src/sbt-test/sbt-buildinfo/multi/build.sbt index b4ff74d..0aa8320 100644 --- a/src/sbt-test/sbt-buildinfo/multi/build.sbt +++ b/src/sbt-test/sbt-buildinfo/multi/build.sbt @@ -39,7 +39,7 @@ lazy val app = (project in file("app")) projectIdCode :: // project id changed in sbt 2.x """ /** The value is "0.1". */""" :: """ val version: String = "0.1"""" :: - """ /** The value is new java.net.URI("http://example.com").toURL. */""" :: + homepageComment :: homepageCode :: scalaVersionInfoComment :: scalaVersionInfo :: diff --git a/src/sbt-test/sbt-buildinfo/simple/build.sbt b/src/sbt-test/sbt-buildinfo/simple/build.sbt index 9a0d229..d43a933 100644 --- a/src/sbt-test/sbt-buildinfo/simple/build.sbt +++ b/src/sbt-test/sbt-buildinfo/simple/build.sbt @@ -57,9 +57,9 @@ lazy val root = (project in file(".")) scalaVersionInfo :: """ /** The value is scala.xml.NodeSeq.Empty. */""" :: """ val ivyXML: scala.xml.NodeSeq = scala.xml.NodeSeq.Empty""" :: - """ /** The value is scala.Some(new java.net.URI("http://example.com").toURL). */""" :: - """ val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URI("http://example.com").toURL)""" :: - """ /** The value is scala.collection.immutable.Seq(("MIT License" -> new java.net.URI("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE").toURL)). */""" :: + homepageComment :: + homepageCode :: + licenseComment :: licensesCode :: """ /** The value is Map(). */""" :: apiMappingsCode :: @@ -83,7 +83,7 @@ lazy val root = (project in file(".")) """ )""" :: """ }""" :: """}""" :: - """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target = new java.io.File(") && + """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target: java.io.File = new java.io.File(") && (scalaVersionInfo.trim == s"""val scalaVersion: String = "$sv"""") && (someCpInfo.contains("/tmp/f.txt")) && (projectVer.contains("val projectVersion")) => () diff --git a/src/sbt-test/sbt-buildinfo/skipimportscaseclass/build.sbt b/src/sbt-test/sbt-buildinfo/skipimportscaseclass/build.sbt index 960f92f..04c598d 100644 --- a/src/sbt-test/sbt-buildinfo/skipimportscaseclass/build.sbt +++ b/src/sbt-test/sbt-buildinfo/skipimportscaseclass/build.sbt @@ -45,8 +45,8 @@ lazy val root = (project in file(".")) """ name: String,""" :: """ scalaVersion: String,""" :: """ ivyXML: scala.xml.NodeSeq,""" :: - """ homepage: scala.Option[java.net.URL],""" :: - """ licenses: scala.collection.immutable.Seq[(String, java.net.URL)],""" :: + homagepageCode :: + licensesCode :: apiMappingsCode :: """ isSnapshot: scala.Boolean,""" :: """ year: scala.Int,""" :: @@ -95,8 +95,8 @@ lazy val root = (project in file(".")) """ name = "helloworld",""" :: """ scalaVersion = "2.12.12",""" :: """ ivyXML = scala.xml.NodeSeq.Empty,""" :: - """ homepage = scala.Some(new java.net.URI("http://example.com").toURL),""" :: - """ licenses = scala.collection.immutable.Seq(("MIT License" -> new java.net.URI("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE").toURL)),""" :: + homagepageApply :: + licensesApply :: """ apiMappings = Map(),""" :: """ isSnapshot = false,""" :: """ year = 2012,""" :: diff --git a/src/sbt-test/sbt-buildinfo/skipimportscaseclass/test b/src/sbt-test/sbt-buildinfo/skipimportscaseclass/test index be2f9e2..61f876e 100644 --- a/src/sbt-test/sbt-buildinfo/skipimportscaseclass/test +++ b/src/sbt-test/sbt-buildinfo/skipimportscaseclass/test @@ -1,4 +1,4 @@ > compile -# $ exists target/scala-2.12/src_managed/main/sbt-buildinfo/BuildInfo.scala +$ exists target/**/src_managed/main/sbt-buildinfo/BuildInfo.scala > check diff --git a/src/sbt-test/sbt-buildinfo/skipimportscaseobject/test b/src/sbt-test/sbt-buildinfo/skipimportscaseobject/test index 3eded65..dcff9b7 100644 --- a/src/sbt-test/sbt-buildinfo/skipimportscaseobject/test +++ b/src/sbt-test/sbt-buildinfo/skipimportscaseobject/test @@ -1,5 +1,5 @@ > compile -# $ exists target/scala-2.12/src_managed/main/sbt-buildinfo/BuildInfo.scala +$ exists target/**/src_managed/main/sbt-buildinfo/BuildInfo.scala > check diff --git a/src/sbt-test/sbt-buildinfo/usepackageaspath/build.sbt b/src/sbt-test/sbt-buildinfo/usepackageaspath/build.sbt index a902e63..fe58e3f 100644 --- a/src/sbt-test/sbt-buildinfo/usepackageaspath/build.sbt +++ b/src/sbt-test/sbt-buildinfo/usepackageaspath/build.sbt @@ -47,10 +47,10 @@ lazy val root = (project in file(".")) """ val scalaVersion: String = "2.12.12"""" :: """ /** The value is scala.xml.NodeSeq.Empty. */""" :: """ val ivyXML: scala.xml.NodeSeq = scala.xml.NodeSeq.Empty""" :: - """ /** The value is scala.Some(new java.net.URI("http://example.com").toURL). */""" :: - """ val homepage: scala.Option[java.net.URL] = scala.Some(new java.net.URI("http://example.com").toURL)""" :: - """ /** The value is scala.collection.immutable.Seq(("MIT License" -> new java.net.URI("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE").toURL)). */""" :: - """ val licenses: scala.collection.immutable.Seq[(String, java.net.URL)] = scala.collection.immutable.Seq(("MIT License" -> new java.net.URI("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE").toURL))""" :: + homepageComment :: + homepageCode :: + licensesComment :: + licensesCode :: """ /** The value is Map(). */""" :: apiMappingsCode :: """ /** The value is false. */""" :: @@ -69,7 +69,7 @@ lazy val root = (project in file(".")) """ )""" :: """ }""" :: """}""" :: - """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target = new java.io.File(") => () + """// $COVERAGE-ON$""" :: Nil if targetInfo.contains("target: java.io.File = new java.io.File(") => () case _ => sys.error("unexpected output: \n" + lines.mkString("\n")) } () diff --git a/src/sbt-test/sbt-buildinfo/usepackageaspath/test b/src/sbt-test/sbt-buildinfo/usepackageaspath/test index 19b7e14..33c3621 100644 --- a/src/sbt-test/sbt-buildinfo/usepackageaspath/test +++ b/src/sbt-test/sbt-buildinfo/usepackageaspath/test @@ -1,4 +1,4 @@ > compile -# $ exists target/scala-2.12/src_managed/main/foo/bar/baz/BuildInfo.scala +$ exists target/**/src_managed/main/foo/bar/baz/BuildInfo.scala > check