diff --git a/modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/gradleParser.scala b/modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/gradleParser.scala index 82fef6a61..9567390ca 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/gradleParser.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/buildtool/gradle/gradleParser.scala @@ -24,9 +24,9 @@ import scala.jdk.CollectionConverters.* object gradleParser { def parseDependenciesAndPlugins(input: String): (List[Dependency], List[Dependency]) = { val parsed = Toml.parse(input) - val versionsTable = parsed.getTableOrEmpty("versions") - val librariesTable = parsed.getTableOrEmpty("libraries") - val pluginsTable = parsed.getTableOrEmpty("plugins") + val versionsTable = getTableSafe(parsed, "versions") + val librariesTable = getTableSafe(parsed, "libraries") + val pluginsTable = getTableSafe(parsed, "plugins") val dependencies = collectEntries(librariesTable, parseDependency(_, versionsTable)) val plugins = collectEntries(pluginsTable, parsePlugin(_, versionsTable)) @@ -76,6 +76,13 @@ object gradleParser { versionString.orElse(versionRef).map(Version.apply) } + private def getTableSafe(table: TomlTable, key: String): TomlTable = + Option + .when(table.contains(key) && table.isTable(key))(table.getTableOrEmpty(key)) + .getOrElse(emptyTable) + + private val emptyTable: TomlTable = Toml.parse("") + private def getStringSafe(table: TomlTable, key: String): Option[String] = Option.when(table.contains(key) && table.isString(key))(table.getString(key)) } diff --git a/modules/core/src/test/scala/org/scalasteward/core/buildtool/gradle/gradleParserTest.scala b/modules/core/src/test/scala/org/scalasteward/core/buildtool/gradle/gradleParserTest.scala index a3643d82c..8b10095b9 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/buildtool/gradle/gradleParserTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/buildtool/gradle/gradleParserTest.scala @@ -49,6 +49,9 @@ class gradleParserTest extends FunSuite { |groovy = "3.0.5" |[libraries] |groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" + |foo = { module = "bar:qux:foo", version = "1" } + |[plugins] + |foo = "" |""".stripMargin val obtained = gradleParser.parseDependenciesAndPlugins(input) assertEquals(obtained, (List.empty, List.empty))