diff --git a/README.md b/README.md index a75e013..920cbfd 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Add the plugin to your Gradle build file. ```kotlin plugins { - id("io.github.pemistahl.version-catalog-linter") version "1.0.2" + id("io.github.pemistahl.version-catalog-linter") version "1.0.3" } ``` @@ -60,7 +60,8 @@ tasks.check { ## Example Below, you find examples for a totally messed up version catalog and how the output -of the plugin's Gradle tasks looks like. +of the plugin's Gradle tasks looks like. Comments are currently filtered out in the output. +It is planned to preserve comments in a later release. ### Version catalog input @@ -77,9 +78,10 @@ jgoodiesDesktop = { group = "com.jgoodies", name = "jgoodies-desktop", version = jgoodiesDialogs = { group = "com.jgoodies", name = "jgoodies-dialogs", version = "1.20.0" } antisamy = { group = "org.owasp.antisamy", name = "antisamy", version = "1.5.2" } antlr = { module = "antlr:antlr", version = "2.7.7" } +# This is a single-line comment. apacheHttpClient = { group = "org.apache.httpcomponents", name = "httpclient", version = "4.5.14" } apacheHttpCore = { group = "org.apache.httpcomponents", name = "httpcore", version = "4.4.16" } -apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" } +apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" } # This comment is for a key-value pair. groovyTemplates = {name = "groovy-templates", group = "org.codehaus.groovy", version.ref = "groovy" } diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 6a2e362..90e6fd6 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,10 @@ +## Version Catalog Linter 1.0.3 (released on Jan 31 2024) + +### Bug Fixes + +- The Gradle tasks failed when a single-line comment was part of a version catalog. + This has been fixed. (#5) + ## Version Catalog Linter 1.0.2 (released on Jan 16 2024) ### Bug Fixes diff --git a/build.gradle.kts b/build.gradle.kts index 5d41141..26a81d6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,7 +24,7 @@ plugins { } group = "io.github.pemistahl" -version = "1.1.0" +version = "1.0.3" kotlin { compilerOptions { diff --git a/src/main/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtils.kt b/src/main/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtils.kt index 3742017..681979d 100644 --- a/src/main/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtils.kt +++ b/src/main/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtils.kt @@ -30,9 +30,7 @@ internal fun readVersionCatalog(versionCatalogFile: File): VersionCatalog { var pluginsRead = false for ((lineNumber, line) in versionCatalogFile.readLines().withIndex()) { - val actualLineNumber = lineNumber + 1 val trimmedLine = line.trim() - val trimmedLineIsNotEmpty = trimmedLine.isNotEmpty() if (trimmedLine == VersionCatalogSection.VERSIONS.label) { versionsRead = true @@ -54,21 +52,26 @@ internal fun readVersionCatalog(versionCatalogFile: File): VersionCatalog { librariesRead = false bundlesRead = false pluginsRead = true - } else if (versionsRead && trimmedLineIsNotEmpty) { - versions.add(Pair(actualLineNumber..actualLineNumber, line)) - } else if (librariesRead && trimmedLineIsNotEmpty) { - libraries.add(Pair(actualLineNumber..actualLineNumber, line)) - } else if (bundlesRead && trimmedLineIsNotEmpty) { - if (line.contains("[")) { - bundles.add(Pair(actualLineNumber..actualLineNumber, line)) - } else { - val (currentRange, currentLine) = bundles.removeAt(bundles.size - 1) - val newRange = currentRange.first..actualLineNumber - val newLine = currentLine + "\n" + line - bundles.add(Pair(newRange, newLine)) + } else if (trimmedLine.isNotEmpty() && !trimmedLine.startsWith('#')) { + val actualLineNumber = lineNumber + 1 + val element = Pair(actualLineNumber..actualLineNumber, line) + + if (versionsRead) { + versions.add(element) + } else if (librariesRead) { + libraries.add(element) + } else if (bundlesRead) { + if (line.contains("[")) { + bundles.add(element) + } else { + val (currentRange, currentLine) = bundles.removeAt(bundles.size - 1) + val newRange = currentRange.first..actualLineNumber + val newLine = currentLine + "\n" + line + bundles.add(Pair(newRange, newLine)) + } + } else if (pluginsRead) { + plugins.add(element) } - } else if (pluginsRead && trimmedLineIsNotEmpty) { - plugins.add(Pair(actualLineNumber..actualLineNumber, line)) } } diff --git a/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtilsTest.kt b/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtilsTest.kt index e306dd1..891ec1d 100644 --- a/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtilsTest.kt +++ b/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/IOUtilsTest.kt @@ -27,16 +27,16 @@ class IOUtilsTest { VersionCatalog( versions = listOf( - 30..30 to "duns = \"V0\"", - 31..31 to "slf4j = { prefer = \"1.7.25\", strictly = \"[1.7, 1.8[\" }", - 32..32 to " exact = \"1.0\"", - 33..33 to "groovy = \"2.5.7\"", - 34..34 to "axis = \"1.3\"", - 35..35 to "ktlint = \"12.0.2\"", - 36..36 to "byteBuddy = \"1.12.9\"", - 37..37 to "springCore = {require=\"4.2.9.RELEASE\",reject=[\"4.3.18.RELEASE\",\"4.3.16.RELEASE\"] }", - 38..38 to "cache2k = \"2.0.0.Final\"", - 39..39 to "dockerJava = \"3.2.12\"", + 31..31 to "duns = \"V0\"", + 32..32 to "slf4j = { prefer = \"1.7.25\", strictly = \"[1.7, 1.8[\" }", + 33..33 to " exact = \"1.0\"", + 34..34 to "groovy = \"2.5.7\"", + 35..35 to "axis = \"1.3\"", + 36..36 to "ktlint = \"12.0.2\"", + 37..37 to "byteBuddy = \"1.12.9\"", + 38..38 to "springCore = {require=\"4.2.9.RELEASE\",reject=[\"4.3.18.RELEASE\",\"4.3.16.RELEASE\"] }", + 39..39 to "cache2k = \"2.0.0.Final\"", + 40..40 to "dockerJava = \"3.2.12\"", ), libraries = listOf( @@ -47,17 +47,18 @@ class IOUtilsTest { 10..10 to "jgoodiesDialogs = { group = \"com.jgoodies\", name = \"jgoodies-dialogs\", version = \"1.20.0\" }", 11..11 to "antisamy = { group = \"org.owasp.antisamy\", name = \"antisamy\", version = \"1.5.2\" }", 12..12 to "antlr = { module = \"antlr:antlr\", version = \"2.7.7\" }", - 13..13 to + 14..14 to "apacheHttpClient = { group = \"org.apache.httpcomponents\", name = \"httpclient\", version = \"4.5.14\" }", - 14..14 to "apacheHttpCore = { group = \"org.apache.httpcomponents\", name = \"httpcore\", version = \"4.4.16\" }", - 15..15 to "apacheHttpMime = {name = \"httpmime\", version = \"4.5.14\", group = \"org.apache.httpcomponents\" }", - 17..17 to + 15..15 to "apacheHttpCore = { group = \"org.apache.httpcomponents\", name = \"httpcore\", version = \"4.4.16\" }", + 16..16 to "apacheHttpMime = {name = \"httpmime\", version = \"4.5.14\", group = \"org.apache.httpcomponents\" } " + + "# This comment is for a key-value pair.", + 18..18 to "groovyTemplates = {name = \"groovy-templates\", group = \"org.codehaus.groovy\", version.ref = \"groovy\" }", ), bundles = listOf( - 20..20 to "groovy = [\"groovyTemplates\", \"groovy\"]", - 21..25 to + 21..21 to "groovy = [\"groovyTemplates\", \"groovy\"]", + 22..26 to """ jgoodies = [ "jgoodiesDesktop", @@ -68,8 +69,8 @@ class IOUtilsTest { ), plugins = listOf( - 44..44 to " shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" }", - 45..45 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" }", + 45..45 to " shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" }", + 46..46 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" }", ), ) diff --git a/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogCheckerTest.kt b/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogCheckerTest.kt index c9fa686..b81b18f 100644 --- a/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogCheckerTest.kt +++ b/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogCheckerTest.kt @@ -36,6 +36,17 @@ class VersionCatalogCheckerTest { ), ) + assertEquals( + emptyList(), + task.checkVersions( + listOf( + 1..1 to "byteBuddy = \"1.12.9\"", + 2..2 to "cache2k = \"2.0.0.Final\" # This is a comment.", + 3..3 to "slf4j = { strictly = \"[1.7, 1.8[\", prefer = \"1.7.25\" }", + ), + ), + ) + assertEquals( listOf("Line 1: Entry with key 'byteBuddy' in section '[versions]' must not have leading whitespace."), task.checkVersions( @@ -162,6 +173,16 @@ class VersionCatalogCheckerTest { ), ) + assertEquals( + emptyList(), + task.checkLibraries( + listOf( + 1..1 to "activation = { group = \"com.sun.activation\", name = \"javax.activation\", version = \"1.2.0\" }", + 2..2 to "antisamy = { group = \"org.owasp.antisamy\", name = \"antisamy\", version = \"1.5.2\" } # This is a comment.", + ), + ), + ) + assertEquals( emptyList(), task.checkLibraries( @@ -332,6 +353,30 @@ class VersionCatalogCheckerTest { ), ) + assertEquals( + emptyList(), + task.checkBundles( + listOf( + 1..5 to + """ + groovy = [ + "groovy", + "groovyTemplates", # This is a comment. + "spock" + ] + """.trimIndent(), + 6..10 to + """ + jgoodies = [ # This a another comment. + "jgoodiesDesktop", + "jgoodiesDialogs", + "jgoodiesFramework" + ] + """.trimIndent(), + ), + ), + ) + assertEquals( listOf( "Lines 1-5: Bundle with key 'groovy' must be indented with " + @@ -578,6 +623,16 @@ class VersionCatalogCheckerTest { ), ) + assertEquals( + emptyList(), + task.checkPlugins( + listOf( + 1..1 to "ktlint = { id = \"org.jlleitschuh.gradle.ktlint\", version.ref = \"ktlint\" } # This is a comment.", + 2..2 to "shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" }", + ), + ), + ) + assertEquals( listOf( "Line 1: Attributes of plugin with key 'ktlint' are not sorted correctly. Required order: id, version(.ref)", diff --git a/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogFormatterTest.kt b/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogFormatterTest.kt index 8be7c97..f7d631c 100644 --- a/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogFormatterTest.kt +++ b/src/test/kotlin/io/github/pemistahl/versioncatalog/linter/plugin/VersionCatalogFormatterTest.kt @@ -100,7 +100,7 @@ class VersionCatalogFormatterTest { 2..2 to "duns = \"V0\"", 3..3 to "slf4j = { prefer = \"1.7.25\", strictly = \"[1.7, 1.8[\" }", 4..4 to " exact = \"1.0\"", - 5..5 to "groovy = \"2.5.7\"", + 5..5 to "groovy = \"2.5.7\" # This is a comment. ", 6..6 to "axis = \"1.3\"", 7..7 to "ktlint = \"12.0.2\"", 8..8 to "byteBuddy = \"1.12.9\" ", @@ -132,7 +132,7 @@ class VersionCatalogFormatterTest { 19..19 to " jgoodiesFramework = \"com.jgoodies:jgoodies-framework:1.34.0\"", 20..20 to "jgoodiesDialogs = { group = \"com.jgoodies\", name = \"jgoodies-dialogs\", version = \"1.20.0\" }", 21..21 to "antisamy = { group = \"org.owasp.antisamy\", name = \"antisamy\", version = \"1.5.2\" }", - 22..22 to "antlr = { module = \"antlr:antlr\", version = \"2.7.7\" }", + 22..22 to "antlr = { module = \"antlr:antlr\", version = \"2.7.7\" } # This is a comment.", 23..23 to "apacheHttpClient = { group = \"org.apache.httpcomponents\", name = \"httpclient\", version = \"4.5.14\" }", 24..24 to "apacheHttpCore = { group = \"org.apache.httpcomponents\", name = \"httpcore\", version = \"4.4.16\" }", 25..25 to "apacheHttpMime = {name = \"httpmime\", version = \"4.5.14\", group = \"org.apache.httpcomponents\" }", @@ -157,7 +157,7 @@ class VersionCatalogFormatterTest { private fun createBundles(): Pair>, List> { return Pair( listOf( - 30..30 to "groovy = [\"groovyTemplates\", \"groovy\"]", + 30..30 to "groovy = [\"groovyTemplates\", \"groovy\"] # This is a comment.", 31..32 to " jgoodies = [ \"jgoodiesDesktop\", \n \"jgoodiesDialogs\", \"jgoodiesFramework\" ]", ), listOf( @@ -182,7 +182,7 @@ class VersionCatalogFormatterTest { return Pair( listOf( 35..35 to " shadowJar = { id = \"com.github.johnrengelman.shadow\", version = \"8.1.1\" } ", - 36..36 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" }", + 36..36 to "ktlint = { version.ref = \"ktlint\", id = \"org.jlleitschuh.gradle.ktlint\" } # This is a comment.", ), listOf( "ktlint = { id = \"org.jlleitschuh.gradle.ktlint\", version.ref = \"ktlint\" }", diff --git a/src/test/resources/inputVersionCatalog.toml b/src/test/resources/inputVersionCatalog.toml index 5a89f9f..da42bcb 100644 --- a/src/test/resources/inputVersionCatalog.toml +++ b/src/test/resources/inputVersionCatalog.toml @@ -10,9 +10,10 @@ jgoodiesDesktop = { group = "com.jgoodies", name = "jgoodies-desktop", version = jgoodiesDialogs = { group = "com.jgoodies", name = "jgoodies-dialogs", version = "1.20.0" } antisamy = { group = "org.owasp.antisamy", name = "antisamy", version = "1.5.2" } antlr = { module = "antlr:antlr", version = "2.7.7" } +# This is a single-line comment. apacheHttpClient = { group = "org.apache.httpcomponents", name = "httpclient", version = "4.5.14" } apacheHttpCore = { group = "org.apache.httpcomponents", name = "httpcore", version = "4.4.16" } -apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" } +apacheHttpMime = {name = "httpmime", version = "4.5.14", group = "org.apache.httpcomponents" } # This comment is for a key-value pair. groovyTemplates = {name = "groovy-templates", group = "org.codehaus.groovy", version.ref = "groovy" }