From 4ee48b8257634a4a448c300192f33afcf87d5bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Sat, 11 Nov 2023 10:08:25 +0100 Subject: [PATCH 1/3] Cleanup + more tests --- .github/workflows/default.yml | 2 +- gradle/libs.versions.toml | 1 + licensee-for-android/build.gradle | 1 + .../licensee/ArtifactCodeGenerator.kt | 99 ++++++----- .../usefulness/licensee/CodeGenerationTask.kt | 1 - .../licensee/ArtifactGeneratorTest.kt | 164 ++++++++++-------- .../licensee/LicenseeTypesGeneratorTest.kt | 8 +- sample/app/build.gradle | 20 ++- .../licensee/PluginIntegrationTest.kt | 39 +++++ sample/core/build.gradle | 11 +- .../se/premex/gross/oss/LicenseParserTest.kt | 8 +- sample/gradle/libs.versions.toml | 5 +- sample/ui/build.gradle | 12 +- .../se/premex/gross/oss/LicenseParserTest.kt | 7 +- 14 files changed, 224 insertions(+), 154 deletions(-) create mode 100644 sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index c24b2af..c3333c5 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -101,7 +101,7 @@ jobs: with: build-root-directory: sample gradle-version: ${{ matrix.gradle }} - arguments: assemble -PagpVersion=${{ matrix.agp }} --continue + arguments: check -PagpVersion=${{ matrix.agp }} --continue - name: Upload reports if: ${{ failure() }} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d43e5e4..a1a4bdd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,7 @@ maven-binarycompatiblity = "0.13.2" [libraries] junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" } +junit-jupiter-launcher = { module = "org.junit.platform:junit-platform-launcher" } assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" } agp-gradle-api = { module = "com.android.tools.build:gradle-api", version.ref = "google-agp" } kotlin-gradle-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "maven-kotlin" } diff --git a/licensee-for-android/build.gradle b/licensee-for-android/build.gradle index 7898231..2f9ab8c 100644 --- a/licensee-for-android/build.gradle +++ b/licensee-for-android/build.gradle @@ -19,6 +19,7 @@ dependencies { implementation(libs.kotlinx.serialization.json.okio) implementation(libs.com.squareup.okio) + testRuntimeOnly(libs.junit.jupiter.launcher) testRuntimeOnly(libs.junit.jupiter.engine) testImplementation(libs.junit.jupiter.api) diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/ArtifactCodeGenerator.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/ArtifactCodeGenerator.kt index c45b39c..11c611c 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/ArtifactCodeGenerator.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/ArtifactCodeGenerator.kt @@ -4,6 +4,7 @@ import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.MemberName import com.squareup.kotlinpoet.TypeSpec +import com.squareup.kotlinpoet.withIndent import io.github.usefulness.licensee.core.Artifact internal class ArtifactCodeGenerator( @@ -13,59 +14,57 @@ internal class ArtifactCodeGenerator( private val unknownLicensesTypeSpec: TypeSpec, ) { - @Suppress("SpreadOperator") - fun artifactCodeBlock(artifact: Artifact): CodeBlock { - val arguments = mutableListOf() + fun artifactCodeBlock(artifact: Artifact) = CodeBlock.builder() + .withIndent { + addStatement("Artifact(") + withIndent { + addStatement("groupId = %S,", artifact.groupId) + addStatement("artifactId = %S,", artifact.artifactId) + addStatement("version = %S,", artifact.version) + addStatement("name = %S,", artifact.name) - val statement = buildString { - appendLine( - """Artifact( - |groupId = %S, - |artifactId = %S, - |version = %S, - """.trimMargin(), - ) - arguments.add(artifact.groupId) - arguments.add(artifact.artifactId) - arguments.add(artifact.version) - if (artifact.name != null) { - appendLine("""name = %S,""") - arguments.add(artifact.name) - } else { - appendLine("""name = null,""") - } + if (artifact.spdxLicenses.isNullOrEmpty()) { + addStatement("spdxLicenses = %M(),", MemberName("kotlin.collections", "emptyList")) + } else { + addStatement("spdxLicenses = %M(", MemberName("kotlin.collections", "listOf")) + withIndent { + artifact.spdxLicenses.forEach { license -> + addStatement("%T(", ClassName(packageName, spdxLicensesTypeSpec.name!!)) + withIndent { + addStatement("identifier = %S,", license.identifier) + addStatement("name = %S,", license.name) + addStatement("url = %S,", license.url) + } + addStatement("),") + } + } + addStatement("),") + } - appendLine("""spdxLicenses = %M(""") - arguments.add(MemberName("kotlin.collections", "listOf")) - artifact.spdxLicenses?.forEach { - appendLine("""%T(identifier = %S, name = %S, url = %S)""".trimMargin()) - arguments.add(ClassName(packageName, spdxLicensesTypeSpec.name!!)) - arguments.add(it.identifier) - arguments.add(it.name) - arguments.add(it.url) - } - appendLine("""),""") + if (artifact.scm == null) { + addStatement("scm = null,") + } else { + addStatement("scm = %T(url = %S),", ClassName(packageName, scmTypeSpec.name!!), artifact.scm.url) + } - if (artifact.scm != null) { - appendLine("""scm = %T(%S), """.trimMargin()) - arguments.add(ClassName(packageName, scmTypeSpec.name!!)) - arguments.add(artifact.scm.url) - } else { - appendLine("""scm = null, """.trimMargin()) + if (artifact.unknownLicenses.isNullOrEmpty()) { + addStatement("unknownLicenses = %M(),", MemberName("kotlin.collections", "emptyList")) + } else { + addStatement("unknownLicenses = %M(", MemberName("kotlin.collections", "listOf")) + withIndent { + artifact.unknownLicenses.forEach { license -> + addStatement("%T(", ClassName(packageName, unknownLicensesTypeSpec.name!!)) + withIndent { + addStatement("name = %S,", license.name) + addStatement("url = %S,", license.url) + } + addStatement("),") + } + } + addStatement("),") + } } - - appendLine("""unknownLicenses = %M(""") - arguments.add(MemberName("kotlin.collections", "listOf")) - artifact.unknownLicenses?.forEach { - appendLine("""%T(name = %S, url = %S)""".trimMargin()) - arguments.add(ClassName(packageName, unknownLicensesTypeSpec.name!!)) - arguments.add(it.name) - arguments.add(it.url) - } - appendLine("""),""") - - appendLine("""|)""".trimMargin()) + addStatement("),") } - return CodeBlock.builder().addStatement(statement, *arguments.toTypedArray()).build() - } + .build() } diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt index bd75637..a4fea73 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt @@ -57,7 +57,6 @@ public abstract class CodeGenerationTask : DefaultTask() { addStatement("%M(", MemberName("kotlin.collections", "listOf")) artifacts.forEach { artifact -> add(artifactCodeGenerator.artifactCodeBlock(artifact)) - addStatement(",") } addStatement(")") }.build() diff --git a/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/ArtifactGeneratorTest.kt b/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/ArtifactGeneratorTest.kt index 2083d11..a11cc2e 100644 --- a/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/ArtifactGeneratorTest.kt +++ b/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/ArtifactGeneratorTest.kt @@ -9,116 +9,126 @@ import org.junit.jupiter.api.Test class ArtifactGeneratorTest { - private val packageName = "se.premex.gross" + private val packageName = "io.github.usefulness.licensee" - private val licenseeTypesGenerator = - LicenseeTypesGenerator(packageName = packageName) + private val licenseeTypesGenerator = LicenseeTypesGenerator(packageName = packageName) - private val artifactCodeGenerator = - ArtifactCodeGenerator( - packageName = packageName, - licenseeTypesGenerator.unknownLicensesTypeSpec, - licenseeTypesGenerator.spdxLicensesTypeSpec, - licenseeTypesGenerator.scmTypeSpec, - ) + private val artifactCodeGenerator = ArtifactCodeGenerator( + packageName = packageName, + licenseeTypesGenerator.unknownLicensesTypeSpec, + licenseeTypesGenerator.spdxLicensesTypeSpec, + licenseeTypesGenerator.scmTypeSpec, + ) @Test fun `test all nullables are null`() { val artifactCodeBlock = artifactCodeGenerator.artifactCodeBlock( Artifact( - "testGroup", - "testArtifact", - "testVersion", - null, - null, - null, - null, + groupId = "testGroup", + artifactId = "testArtifact", + version = "testVersion", + name = null, + spdxLicenses = null, + scm = null, + unknownLicenses = null, ), ) - assertThat(artifactCodeBlock).asString().isEqualTo( - """Artifact( - groupId = "testGroup", - artifactId = "testArtifact", - version = "testVersion", - name = null, - spdxLicenses = kotlin.collections.listOf( - ), - scm = null, - unknownLicenses = kotlin.collections.listOf( - ), - ) -""", - ) + // language + val expected = """ + Artifact( + groupId = "testGroup", + artifactId = "testArtifact", + version = "testVersion", + name = null, + spdxLicenses = kotlin.collections.emptyList(), + scm = null, + unknownLicenses = kotlin.collections.emptyList(), + ), + + """.trimIndent() + assertThat(artifactCodeBlock).asString().isEqualToIgnoringWhitespace(expected) } @Test fun `test all lists are empty`() { val artifactCodeBlock = artifactCodeGenerator.artifactCodeBlock( Artifact( - "testGroup", - "testArtifact", - "testVersion", - "testName", - listOf(), - Scm("testUrl"), - listOf(), + groupId = "testGroup", + artifactId = "testArtifact", + version = "testVersion", + name = "testName", + spdxLicenses = listOf(), + scm = Scm("testUrl"), + unknownLicenses = listOf(), ), ) - assertThat(artifactCodeBlock).asString().isEqualTo( - """Artifact( - groupId = "testGroup", - artifactId = "testArtifact", - version = "testVersion", - name = "testName", - spdxLicenses = kotlin.collections.listOf( - ), - scm = se.premex.gross.SpdxLicenses("testUrl"), - unknownLicenses = kotlin.collections.listOf( - ), - ) + val expected = """ + Artifact( + groupId = "testGroup", + artifactId = "testArtifact", + version = "testVersion", + name = "testName", + spdxLicenses = kotlin.collections.emptyList(), + scm = io.github.usefulness.licensee.SpdxLicenses(url = "testUrl"), + unknownLicenses = kotlin.collections.emptyList(), + ), -""", - ) + """.trimIndent() + assertThat(artifactCodeBlock).asString().isEqualToIgnoringWhitespace(expected) } @Test fun `test all lists have items`() { val artifactCodeBlock = artifactCodeGenerator.artifactCodeBlock( Artifact( - "testGroup", - "testArtifact", - "testVersion", - "testName", - listOf( + groupId = "testGroup", + artifactId = "testArtifact", + version = "testVersion", + name = "testName", + spdxLicenses = listOf( SpdxLicenses("spdxId1", "spdxName1", "spdxUrl1"), SpdxLicenses("spdxId2", "spdxName2", "spdxUrl2"), ), - Scm("testUrl"), - listOf( + scm = Scm("testUrl"), + unknownLicenses = listOf( UnknownLicenses("unknown1", "unknown1"), UnknownLicenses("unknown2", "unknown2"), ), ), ) - assertThat(artifactCodeBlock).asString().isEqualTo( - """Artifact( - groupId = "testGroup", - artifactId = "testArtifact", - version = "testVersion", - name = "testName", - spdxLicenses = kotlin.collections.listOf( - se.premex.gross.UnknownLicenses(identifier = "spdxId1", name = "spdxName1", url = "spdxUrl1") - se.premex.gross.UnknownLicenses(identifier = "spdxId2", name = "spdxName2", url = "spdxUrl2") - ), - scm = se.premex.gross.SpdxLicenses("testUrl"), - unknownLicenses = kotlin.collections.listOf( - se.premex.gross.Scm(name = "unknown1", url = "unknown1") - se.premex.gross.Scm(name = "unknown2", url = "unknown2") - ), - ) + val expected = """ + Artifact( + groupId = "testGroup", + artifactId = "testArtifact", + version = "testVersion", + name = "testName", + spdxLicenses = kotlin.collections.listOf( + io.github.usefulness.licensee.UnknownLicenses( + identifier = "spdxId1", + name = "spdxName1", + url = "spdxUrl1", + ), + io.github.usefulness.licensee.UnknownLicenses( + identifier = "spdxId2", + name = "spdxName2", + url = "spdxUrl2", + ), + ), + scm = io.github.usefulness.licensee.SpdxLicenses(url = "testUrl"), + unknownLicenses = kotlin.collections.listOf( + io.github.usefulness.licensee.Scm( + name = "unknown1", + url = "unknown1", + ), + io.github.usefulness.licensee.Scm( + name = "unknown2", + url = "unknown2", + ), + ), + ), -""", - ) + """.trimIndent() + assertThat(artifactCodeBlock).asString().isEqualToIgnoringWhitespace(expected) } } diff --git a/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/LicenseeTypesGeneratorTest.kt b/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/LicenseeTypesGeneratorTest.kt index 178f099..f69b901 100644 --- a/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/LicenseeTypesGeneratorTest.kt +++ b/licensee-for-android/src/test/kotlin/io/github/usefulness/licensee/LicenseeTypesGeneratorTest.kt @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test class LicenseeTypesGeneratorTest { - private val packageName = "se.premex.gross" + private val packageName = "io.github.usefulness.licensee" @Test fun testSpdxLicenses() { @@ -55,9 +55,9 @@ class LicenseeTypesGeneratorTest { public val artifactId: kotlin.String, public val version: kotlin.String, public val name: kotlin.String?, - public val spdxLicenses: kotlin.collections.List, - public val scm: se.premex.gross.Scm?, - public val unknownLicenses: kotlin.collections.List, + public val spdxLicenses: kotlin.collections.List, + public val scm: io.github.usefulness.licensee.Scm?, + public val unknownLicenses: kotlin.collections.List, ) """ assertThat(licenseeTypesGenerator.artifactTypeSpec.toString()).isEqualTo(artifactDefinition) diff --git a/sample/app/build.gradle b/sample/app/build.gradle index 478d57f..5aed786 100644 --- a/sample/app/build.gradle +++ b/sample/app/build.gradle @@ -11,7 +11,6 @@ licensee { licenseeForAndroid { enableKotlinCodeGeneration = true enableAndroidAssetGeneration = true - singularVariantName = "release" } android { namespace "io.githhub.usefulness.licensee.android.app" @@ -35,6 +34,7 @@ android { buildFeatures { compose = true + buildConfig = true } composeOptions { kotlinCompilerExtensionVersion = libs.versions.androidx.compose.compiler.get() @@ -45,19 +45,29 @@ android { } } } +tasks.withType(Test).configureEach { + useJUnitPlatform() +} dependencies { + implementation(platform(libs.androidx.compose.bom)) implementation(libs.core.ktx) implementation(libs.lifecycle.runtime.ktx) implementation(libs.activity.compose) - implementation(platform(libs.androidx.compose.bom)) implementation(libs.ui) implementation(libs.ui.graphics) - implementation(libs.ui.tooling.preview) implementation(libs.material3) + implementation(project(":ui")) - debugImplementation(libs.ui.tooling) - debugImplementation(libs.ui.test.manifest) + //noinspection UseTomlInstead, see `PluginIntegrationTest` + debugImplementation("androidx.viewpager2:viewpager2:1.0.0") + debugImplementation(libs.ui.tooling.preview) + + testRuntimeOnly(libs.junit.jupiter.launcher) + testRuntimeOnly(libs.junit.jupiter.engine) + + testImplementation(libs.junit.jupiter.api) + testImplementation(libs.assertj.core) } diff --git a/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt b/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt new file mode 100644 index 0000000..2d2c0b7 --- /dev/null +++ b/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt @@ -0,0 +1,39 @@ +package io.github.usefulness.licensee + +import io.githhub.usefulness.licensee.android.app.BuildConfig +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class PluginIntegrationTest { + + @Test + fun checkGeneratedCode() { + assertThat(Licensee.artifacts).isNotEmpty() + val viewPagers = Licensee.artifacts.filter { it.groupId == "androidx.viewpager2" && it.artifactId == "viewpager2" } + + @Suppress("KotlinConstantConditions") + when (BuildConfig.BUILD_TYPE) { + "release" -> assertThat(viewPagers).isEmpty() + "debug" -> assertThat(viewPagers.single()).isEqualTo( + Artifact( + groupId = "androidx.viewpager2", + artifactId = "viewpager2", + version = "1.0.0", + name = "AndroidX Widget ViewPager2", + spdxLicenses = listOf( + SpdxLicenses( + identifier = "Apache-2.0", + name = "Apache License 2.0", + url = "https://www.apache.org/licenses/LICENSE-2.0", + ), + ), + scm = Scm(url = "http://source.android.com"), + unknownLicenses = emptyList(), + ), + ) + + else -> error("Not supported") + } + assertThat(Licensee.artifacts).isNotEmpty() + } +} diff --git a/sample/core/build.gradle b/sample/core/build.gradle index a8334af..9d164ee 100644 --- a/sample/core/build.gradle +++ b/sample/core/build.gradle @@ -3,11 +3,18 @@ plugins { alias(libs.plugins.kotlin.serialization) } +tasks.withType(Test).configureEach { + useJUnitPlatform() +} + dependencies { api(libs.kotlinx.serialization.json) implementation(libs.kotlinx.serialization.json.okio) api(libs.com.squareup.okio) - testImplementation(libs.org.jetbrains.kotlin.kotlin.test.junit) - testImplementation(libs.org.jetbrains.kotlinx.kotlinx.coroutines.test) + testRuntimeOnly(libs.junit.jupiter.launcher) + testRuntimeOnly(libs.junit.jupiter.engine) + + testImplementation(libs.junit.jupiter.api) + testImplementation(libs.assertj.core) } diff --git a/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt b/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt index a895268..7238cbc 100644 --- a/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt +++ b/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt @@ -1,11 +1,10 @@ package se.premex.gross.oss -import kotlinx.coroutines.test.runTest import kotlinx.serialization.ExperimentalSerializationApi import okio.buffer import okio.source +import org.junit.jupiter.api.Test import se.premex.gross.core.LicenseParser -import kotlin.test.Test private const val ARTIFACTS_SMALL = """[ { @@ -78,16 +77,17 @@ private const val ARTIFACTS_MEDIUM = """[ ]""" class LicenseParserTest { + @Test @ExperimentalSerializationApi - fun testSmall() = runTest { + fun testSmall() { val licenseParser = object : LicenseParser {} licenseParser.decode(ARTIFACTS_SMALL.byteInputStream().source().buffer()) } @Test @ExperimentalSerializationApi - fun testMedium() = runTest { + fun testMedium() { val licenseParser = object : LicenseParser {} licenseParser.decode(ARTIFACTS_MEDIUM.byteInputStream().source().buffer()) } diff --git a/sample/gradle/libs.versions.toml b/sample/gradle/libs.versions.toml index 1c9a455..88e441d 100644 --- a/sample/gradle/libs.versions.toml +++ b/sample/gradle/libs.versions.toml @@ -18,6 +18,7 @@ lifecycle-runtime-ktx = "2.6.2" [libraries] junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" } +junit-jupiter-launcher = { module = "org.junit.platform:junit-platform-launcher" } assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "maven-kotlin-serialization" } @@ -43,15 +44,11 @@ androidx-compose-runtime-runtime-livedata = { module = "androidx.compose.runtime androidx-compose-ui-ui-text = { module = "androidx.compose.ui:ui-text" } androidx-core-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core" } core-ktx = { module = "androidx.core:core-ktx", version.ref = "core-ktx" } -espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso-core" } org-jetbrains-kotlinx-kotlinx-coroutines-test = "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3" lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle-runtime-ktx" } material3 = { module = "androidx.compose.material3:material3" } -org-jetbrains-kotlin-kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "maven-kotlin" } ui = { module = "androidx.compose.ui:ui" } ui-graphics = { module = "androidx.compose.ui:ui-graphics" } -ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" } -ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" } ui-tooling = { module = "androidx.compose.ui:ui-tooling" } ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" } io-nlopez-compose = "io.nlopez.compose.rules:ktlint:0.3.3" diff --git a/sample/ui/build.gradle b/sample/ui/build.gradle index 7914211..2c6da02 100644 --- a/sample/ui/build.gradle +++ b/sample/ui/build.gradle @@ -16,6 +16,11 @@ android { kotlinCompilerExtensionVersion = libs.versions.androidx.compose.compiler.get() } } + +tasks.withType(Test).configureEach { + useJUnitPlatform() +} + dependencies { api(project(":core")) implementation(platform(libs.androidx.compose.bom)) @@ -31,8 +36,11 @@ dependencies { implementation(libs.androidx.compose.material.material.icons.extended) implementation(libs.androidx.core.core.ktx) - testImplementation(libs.org.jetbrains.kotlin.kotlin.test.junit) - testImplementation(libs.org.jetbrains.kotlinx.kotlinx.coroutines.test) + testRuntimeOnly(libs.junit.jupiter.launcher) + testRuntimeOnly(libs.junit.jupiter.engine) + + testImplementation(libs.junit.jupiter.api) + testImplementation(libs.assertj.core) ktlintRuleSet(libs.io.nlopez.compose) } diff --git a/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt b/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt index a895268..7d7d267 100644 --- a/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt +++ b/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt @@ -1,11 +1,10 @@ package se.premex.gross.oss -import kotlinx.coroutines.test.runTest import kotlinx.serialization.ExperimentalSerializationApi import okio.buffer import okio.source +import org.junit.jupiter.api.Test import se.premex.gross.core.LicenseParser -import kotlin.test.Test private const val ARTIFACTS_SMALL = """[ { @@ -80,14 +79,14 @@ private const val ARTIFACTS_MEDIUM = """[ class LicenseParserTest { @Test @ExperimentalSerializationApi - fun testSmall() = runTest { + fun testSmall() { val licenseParser = object : LicenseParser {} licenseParser.decode(ARTIFACTS_SMALL.byteInputStream().source().buffer()) } @Test @ExperimentalSerializationApi - fun testMedium() = runTest { + fun testMedium() { val licenseParser = object : LicenseParser {} licenseParser.decode(ARTIFACTS_MEDIUM.byteInputStream().source().buffer()) } From 4001044fcf86d60fe70343bd9ea26f95735cd313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Sat, 11 Nov 2023 11:07:03 +0100 Subject: [PATCH 2/3] More asset tests --- gradle/libs.versions.toml | 2 +- licensee-for-android/build.gradle | 2 +- .../usefulness/licensee/CodeGenerationTask.kt | 7 +- .../licensee/LicenseeForAndroidExtension.kt | 2 + .../licensee/LicenseeForAndroidPlugin.kt | 1 + sample/app/build.gradle | 14 ++- sample/app/config/baseline.xml | 2 +- .../kotlin/se/premex/gross/OssArtifactView.kt | 6 +- .../se/premex/gross/OssKotlinCodeView.kt | 10 +- .../licensee/PluginIntegrationTest.kt | 49 +++++----- .../github/usefulness/licensee/TestUtils.kt | 33 +++++++ sample/core/build.gradle | 2 +- .../usefulness/licensee}/core/Artifact.kt | 6 +- .../licensee/core/LicenseeParser.kt} | 7 +- ...nseParserTest.kt => LicenseeParserTest.kt} | 13 +-- sample/gradle/libs.versions.toml | 7 +- sample/ui/build.gradle | 2 +- ...icenseParser.kt => AssetLicenseeParser.kt} | 14 ++- .../main/kotlin/se/premex/gross/ui/OssView.kt | 2 +- .../kotlin/se/premex/gross/ui/OssViewModel.kt | 2 +- .../se/premex/gross/oss/LicenseParserTest.kt | 93 ------------------- 21 files changed, 109 insertions(+), 167 deletions(-) create mode 100644 sample/app/src/test/kotlin/io/github/usefulness/licensee/TestUtils.kt rename sample/core/src/main/kotlin/{se/premex/gross => io/github/usefulness/licensee}/core/Artifact.kt (74%) rename sample/core/src/main/kotlin/{se/premex/gross/core/LicenseParser.kt => io/github/usefulness/licensee/core/LicenseeParser.kt} (71%) rename sample/core/src/test/kotlin/se/premex/gross/oss/{LicenseParserTest.kt => LicenseeParserTest.kt} (83%) rename sample/ui/src/main/kotlin/se/premex/gross/ui/{AssetLicenseParser.kt => AssetLicenseeParser.kt} (67%) delete mode 100644 sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a1a4bdd..50561f2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,7 +12,7 @@ maven-binarycompatiblity = "0.13.2" [libraries] junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" } -junit-jupiter-launcher = { module = "org.junit.platform:junit-platform-launcher" } +junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" } assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" } agp-gradle-api = { module = "com.android.tools.build:gradle-api", version.ref = "google-agp" } kotlin-gradle-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "maven-kotlin" } diff --git a/licensee-for-android/build.gradle b/licensee-for-android/build.gradle index 2f9ab8c..d6ace0a 100644 --- a/licensee-for-android/build.gradle +++ b/licensee-for-android/build.gradle @@ -19,7 +19,7 @@ dependencies { implementation(libs.kotlinx.serialization.json.okio) implementation(libs.com.squareup.okio) - testRuntimeOnly(libs.junit.jupiter.launcher) + testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.junit.jupiter.engine) testImplementation(libs.junit.jupiter.api) diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt index a4fea73..eb02c99 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt @@ -14,7 +14,9 @@ import okio.source import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.PathSensitive @@ -23,7 +25,6 @@ import org.gradle.api.tasks.TaskAction @CacheableTask public abstract class CodeGenerationTask : DefaultTask() { - private val packageName = "io.github.usefulness.licensee" @get:OutputDirectory public abstract val outputDirectory: DirectoryProperty @@ -32,9 +33,13 @@ public abstract class CodeGenerationTask : DefaultTask() { @get:InputFile public abstract val inputFile: RegularFileProperty + @get:Input + public abstract val packageName : Property + @TaskAction @ExperimentalSerializationApi public fun action() { + val packageName = packageName.get() val licenseeTypesGenerator = LicenseeTypesGenerator(packageName) FileSpec.builder(packageName, "Artifact") diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt index c4da43c..87d0b4f 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt @@ -11,6 +11,8 @@ public open class LicenseeForAndroidExtension(objectFactory: ObjectFactory) { */ public val enableKotlinCodeGeneration: Property = objectFactory.property(default = false) + public val generatedPackageName: Property = objectFactory.property(default = "io.github.usefulness.licensee") + /** * Enable asset generation. Will copy licensee report to * android asset directory making it available as 'androidAssetFileName' diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidPlugin.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidPlugin.kt index 1acd1b2..dfaa163 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidPlugin.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidPlugin.kt @@ -58,6 +58,7 @@ public class LicenseeForAndroidPlugin : Plugin { CodeGenerationTask::class.java, ) { it.inputFile.set(artifactsFile) + it.packageName.set(extension.generatedPackageName) } // Do NOT use `.kotlin` here: https://issuetracker.google.com/issues/268248348 diff --git a/sample/app/build.gradle b/sample/app/build.gradle index 5aed786..4c1f219 100644 --- a/sample/app/build.gradle +++ b/sample/app/build.gradle @@ -31,7 +31,9 @@ android { lint { checkReleaseBuilds = false } - + testOptions { + unitTests.includeAndroidResources true + } buildFeatures { compose = true buildConfig = true @@ -58,16 +60,18 @@ dependencies { implementation(libs.ui.graphics) implementation(libs.material3) - implementation(project(":ui")) //noinspection UseTomlInstead, see `PluginIntegrationTest` debugImplementation("androidx.viewpager2:viewpager2:1.0.0") debugImplementation(libs.ui.tooling.preview) - testRuntimeOnly(libs.junit.jupiter.launcher) - testRuntimeOnly(libs.junit.jupiter.engine) + testRuntimeOnly(libs.junit.platform.launcher) + testRuntimeOnly(libs.junit.vintage.engine) - testImplementation(libs.junit.jupiter.api) + testImplementation(libs.junit4) testImplementation(libs.assertj.core) + testImplementation(libs.androidx.test.core) + testImplementation(libs.org.robolectric.core) + testImplementation(libs.kotlinx.serialization.json.okio) } diff --git a/sample/app/config/baseline.xml b/sample/app/config/baseline.xml index 2253a08..8b5675f 100644 --- a/sample/app/config/baseline.xml +++ b/sample/app/config/baseline.xml @@ -2,7 +2,7 @@ - FunctionNaming:OssArtifactView.kt$@OptIn(ExperimentalSerializationApi::class) @Composable fun AssetsOssView() + FunctionNaming:OssArtifactView.kt$@Composable fun AssetsOssView() FunctionNaming:OssKotlinCodeView.kt$@Composable fun ProgrammaticOssView() FunctionNaming:Theme.kt$@Composable fun GrossTheme( darkTheme: Boolean = isSystemInDarkTheme(), // Dynamic color is available on Android 12+ dynamicColor: Boolean = true, content: @Composable () -> Unit, ) InvalidPackageDeclaration:MainActivity.kt$package io.githhub.usefulness.licensee.android.app diff --git a/sample/app/src/main/kotlin/se/premex/gross/OssArtifactView.kt b/sample/app/src/main/kotlin/se/premex/gross/OssArtifactView.kt index df045ec..6f4e4ae 100644 --- a/sample/app/src/main/kotlin/se/premex/gross/OssArtifactView.kt +++ b/sample/app/src/main/kotlin/se/premex/gross/OssArtifactView.kt @@ -9,8 +9,7 @@ import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import io.githhub.usefulness.licensee.android.app.R -import kotlinx.serialization.ExperimentalSerializationApi -import se.premex.gross.ui.AssetLicenseParser +import se.premex.gross.ui.AssetLicenseeParser import se.premex.gross.ui.ErrorView import se.premex.gross.ui.LoadingView import se.premex.gross.ui.OssView @@ -18,12 +17,11 @@ import se.premex.gross.ui.OssViewState import se.premex.gross.ui.State import java.io.IOException -@OptIn(ExperimentalSerializationApi::class) @Composable fun AssetsOssView() { val assetManager = LocalContext.current.assets - val licenseParser = remember { AssetLicenseParser(assetManager) } + val licenseParser = remember { AssetLicenseeParser(assetManager) } val uiState = remember { mutableStateOf(OssViewState()) } diff --git a/sample/app/src/main/kotlin/se/premex/gross/OssKotlinCodeView.kt b/sample/app/src/main/kotlin/se/premex/gross/OssKotlinCodeView.kt index d9d779d..f2b0f7f 100644 --- a/sample/app/src/main/kotlin/se/premex/gross/OssKotlinCodeView.kt +++ b/sample/app/src/main/kotlin/se/premex/gross/OssKotlinCodeView.kt @@ -5,12 +5,12 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.res.stringResource import io.githhub.usefulness.licensee.android.app.R -import se.premex.gross.core.Artifact -import se.premex.gross.core.Scm -import se.premex.gross.core.SpdxLicenses -import se.premex.gross.core.UnknownLicenses -import se.premex.gross.ui.OssView import io.github.usefulness.licensee.Licensee +import io.github.usefulness.licensee.core.Artifact +import io.github.usefulness.licensee.core.Scm +import io.github.usefulness.licensee.core.SpdxLicenses +import io.github.usefulness.licensee.core.UnknownLicenses +import se.premex.gross.ui.OssView @Composable fun ProgrammaticOssView() { diff --git a/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt b/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt index 2d2c0b7..c1ce494 100644 --- a/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt +++ b/sample/app/src/test/kotlin/io/github/usefulness/licensee/PluginIntegrationTest.kt @@ -1,39 +1,32 @@ package io.github.usefulness.licensee -import io.githhub.usefulness.licensee.android.app.BuildConfig -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import kotlinx.coroutines.runBlocking +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import se.premex.gross.ui.AssetLicenseeParser +@RunWith(RobolectricTestRunner::class) class PluginIntegrationTest { @Test fun checkGeneratedCode() { - assertThat(Licensee.artifacts).isNotEmpty() - val viewPagers = Licensee.artifacts.filter { it.groupId == "androidx.viewpager2" && it.artifactId == "viewpager2" } + checkLoadedArtifacts( + artifacts = Licensee.artifacts, + isViewPager2Dependency = { groupId == "androidx.viewpager2" && artifactId == "viewpager2" }, + ) + } - @Suppress("KotlinConstantConditions") - when (BuildConfig.BUILD_TYPE) { - "release" -> assertThat(viewPagers).isEmpty() - "debug" -> assertThat(viewPagers.single()).isEqualTo( - Artifact( - groupId = "androidx.viewpager2", - artifactId = "viewpager2", - version = "1.0.0", - name = "AndroidX Widget ViewPager2", - spdxLicenses = listOf( - SpdxLicenses( - identifier = "Apache-2.0", - name = "Apache License 2.0", - url = "https://www.apache.org/licenses/LICENSE-2.0", - ), - ), - scm = Scm(url = "http://source.android.com"), - unknownLicenses = emptyList(), - ), - ) + @Test + fun checkAssets() { + val context = ApplicationProvider.getApplicationContext() + val source = runBlocking { AssetLicenseeParser(context.assets).readFromAssets() } - else -> error("Not supported") - } - assertThat(Licensee.artifacts).isNotEmpty() + checkLoadedArtifacts( + artifacts = source, + isViewPager2Dependency = { groupId == "androidx.viewpager2" && artifactId == "viewpager2" }, + ) } } diff --git a/sample/app/src/test/kotlin/io/github/usefulness/licensee/TestUtils.kt b/sample/app/src/test/kotlin/io/github/usefulness/licensee/TestUtils.kt new file mode 100644 index 0000000..3736d08 --- /dev/null +++ b/sample/app/src/test/kotlin/io/github/usefulness/licensee/TestUtils.kt @@ -0,0 +1,33 @@ +package io.github.usefulness.licensee + +import io.githhub.usefulness.licensee.android.app.BuildConfig +import org.assertj.core.api.Assertions.assertThat + +internal fun checkLoadedArtifacts(artifacts: List, isViewPager2Dependency: T.() -> Boolean) { + assertThat(artifacts).isNotEmpty() + val viewPagers = artifacts.filter { it.isViewPager2Dependency() } + + @Suppress("KotlinConstantConditions") + when (BuildConfig.BUILD_TYPE) { + "release" -> assertThat(viewPagers).isEmpty() + "debug" -> assertThat(viewPagers.single().toString()).isEqualTo( + Artifact( + groupId = "androidx.viewpager2", + artifactId = "viewpager2", + version = "1.0.0", + name = "AndroidX Widget ViewPager2", + spdxLicenses = listOf( + SpdxLicenses( + identifier = "Apache-2.0", + name = "Apache License 2.0", + url = "https://www.apache.org/licenses/LICENSE-2.0", + ), + ), + scm = Scm(url = "http://source.android.com"), + unknownLicenses = emptyList(), + ).toString(), + ) + + else -> error("Not supported") + } +} diff --git a/sample/core/build.gradle b/sample/core/build.gradle index 9d164ee..df24a01 100644 --- a/sample/core/build.gradle +++ b/sample/core/build.gradle @@ -12,7 +12,7 @@ dependencies { implementation(libs.kotlinx.serialization.json.okio) api(libs.com.squareup.okio) - testRuntimeOnly(libs.junit.jupiter.launcher) + testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.junit.jupiter.engine) testImplementation(libs.junit.jupiter.api) diff --git a/sample/core/src/main/kotlin/se/premex/gross/core/Artifact.kt b/sample/core/src/main/kotlin/io/github/usefulness/licensee/core/Artifact.kt similarity index 74% rename from sample/core/src/main/kotlin/se/premex/gross/core/Artifact.kt rename to sample/core/src/main/kotlin/io/github/usefulness/licensee/core/Artifact.kt index e88404c..ca443d2 100644 --- a/sample/core/src/main/kotlin/se/premex/gross/core/Artifact.kt +++ b/sample/core/src/main/kotlin/io/github/usefulness/licensee/core/Artifact.kt @@ -1,4 +1,4 @@ -package se.premex.gross.core +package io.github.usefulness.licensee.core import kotlinx.serialization.Serializable @@ -24,7 +24,7 @@ data class Artifact( val artifactId: String, val version: String, val name: String? = null, - val spdxLicenses: List? = null, + val spdxLicenses: List = emptyList(), val scm: Scm? = null, - val unknownLicenses: List? = null, + val unknownLicenses: List = emptyList(), ) diff --git a/sample/core/src/main/kotlin/se/premex/gross/core/LicenseParser.kt b/sample/core/src/main/kotlin/io/github/usefulness/licensee/core/LicenseeParser.kt similarity index 71% rename from sample/core/src/main/kotlin/se/premex/gross/core/LicenseParser.kt rename to sample/core/src/main/kotlin/io/github/usefulness/licensee/core/LicenseeParser.kt index 4a135ff..034584c 100644 --- a/sample/core/src/main/kotlin/se/premex/gross/core/LicenseParser.kt +++ b/sample/core/src/main/kotlin/io/github/usefulness/licensee/core/LicenseeParser.kt @@ -1,11 +1,12 @@ -package se.premex.gross.core +package io.github.usefulness.licensee.core import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json import kotlinx.serialization.json.okio.decodeFromBufferedSource import okio.BufferedSource -interface LicenseParser { - @ExperimentalSerializationApi +object LicenseeParser { + + @OptIn(ExperimentalSerializationApi::class) fun decode(source: BufferedSource) = Json.decodeFromBufferedSource>(source) } diff --git a/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt b/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseeParserTest.kt similarity index 83% rename from sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt rename to sample/core/src/test/kotlin/se/premex/gross/oss/LicenseeParserTest.kt index 7238cbc..96730df 100644 --- a/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt +++ b/sample/core/src/test/kotlin/se/premex/gross/oss/LicenseeParserTest.kt @@ -1,10 +1,9 @@ package se.premex.gross.oss -import kotlinx.serialization.ExperimentalSerializationApi +import io.github.usefulness.licensee.core.LicenseeParser import okio.buffer import okio.source import org.junit.jupiter.api.Test -import se.premex.gross.core.LicenseParser private const val ARTIFACTS_SMALL = """[ { @@ -76,19 +75,15 @@ private const val ARTIFACTS_MEDIUM = """[ } ]""" -class LicenseParserTest { +class LicenseeParserTest { @Test - @ExperimentalSerializationApi fun testSmall() { - val licenseParser = object : LicenseParser {} - licenseParser.decode(ARTIFACTS_SMALL.byteInputStream().source().buffer()) + LicenseeParser.decode(ARTIFACTS_SMALL.byteInputStream().source().buffer()) } @Test - @ExperimentalSerializationApi fun testMedium() { - val licenseParser = object : LicenseParser {} - licenseParser.decode(ARTIFACTS_MEDIUM.byteInputStream().source().buffer()) + LicenseeParser.decode(ARTIFACTS_MEDIUM.byteInputStream().source().buffer()) } } diff --git a/sample/gradle/libs.versions.toml b/sample/gradle/libs.versions.toml index 88e441d..f61598f 100644 --- a/sample/gradle/libs.versions.toml +++ b/sample/gradle/libs.versions.toml @@ -3,6 +3,7 @@ gradle-starter = "0.65.0" maven-kotlin-serialization = "1.6.0" maven-kotlin = "1.9.20" maven-junit = "5.10.1" +maven-junit4 = "4.13.2" maven-assertj = "3.24.2" activity-compose = "1.8.0" @@ -18,7 +19,9 @@ lifecycle-runtime-ktx = "2.6.2" [libraries] junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" } -junit-jupiter-launcher = { module = "org.junit.platform:junit-platform-launcher" } +junit-vintage-engine = { module = "org.junit.vintage:junit-vintage-engine", version.ref = "maven-junit" } +junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" } +junit4 = { module = "junit:junit", version.ref = "maven-junit4" } assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "maven-kotlin-serialization" } @@ -29,6 +32,8 @@ activity-compose = { module = "androidx.activity:activity-compose", version.ref org-jetbrains-kotlin-kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "maven-kotlin" } androidx-lifecycle-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycle-runtime-ktx" } androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-ext-junit" } +androidx-test-core = "androidx.test:core-ktx:1.5.0" +org-robolectric-core = "org.robolectric:robolectric:4.11.1" com-squareup-moshi = { module = "com.squareup.moshi:moshi", version.ref = "com-squareup-moshi" } com-squareup-moshi-moshi-kotlin-codegen = { module = "com.squareup.moshi:moshi-kotlin-codegen", version.ref = "com-squareup-moshi" } androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "compose-bom" } diff --git a/sample/ui/build.gradle b/sample/ui/build.gradle index 2c6da02..da80100 100644 --- a/sample/ui/build.gradle +++ b/sample/ui/build.gradle @@ -36,7 +36,7 @@ dependencies { implementation(libs.androidx.compose.material.material.icons.extended) implementation(libs.androidx.core.core.ktx) - testRuntimeOnly(libs.junit.jupiter.launcher) + testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.junit.jupiter.engine) testImplementation(libs.junit.jupiter.api) diff --git a/sample/ui/src/main/kotlin/se/premex/gross/ui/AssetLicenseParser.kt b/sample/ui/src/main/kotlin/se/premex/gross/ui/AssetLicenseeParser.kt similarity index 67% rename from sample/ui/src/main/kotlin/se/premex/gross/ui/AssetLicenseParser.kt rename to sample/ui/src/main/kotlin/se/premex/gross/ui/AssetLicenseeParser.kt index 83a05dc..80c6bad 100644 --- a/sample/ui/src/main/kotlin/se/premex/gross/ui/AssetLicenseParser.kt +++ b/sample/ui/src/main/kotlin/se/premex/gross/ui/AssetLicenseeParser.kt @@ -1,21 +1,19 @@ package se.premex.gross.ui import android.content.res.AssetManager +import io.github.usefulness.licensee.core.Artifact +import io.github.usefulness.licensee.core.LicenseeParser +import io.github.usefulness.licensee.core.SpdxLicenses +import io.github.usefulness.licensee.core.UnknownLicenses import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import kotlinx.serialization.ExperimentalSerializationApi import okio.buffer import okio.source -import se.premex.gross.core.Artifact -import se.premex.gross.core.LicenseParser -import se.premex.gross.core.SpdxLicenses -import se.premex.gross.core.UnknownLicenses -class AssetLicenseParser(private val assetManager: AssetManager) : LicenseParser { - @ExperimentalSerializationApi +class AssetLicenseeParser(private val assetManager: AssetManager) { suspend fun readFromAssets(): List = withContext(Dispatchers.IO) { val source = assetManager.open("licensee_artifacts.json").source().buffer() - decode(source) + LicenseeParser.decode(source) } } diff --git a/sample/ui/src/main/kotlin/se/premex/gross/ui/OssView.kt b/sample/ui/src/main/kotlin/se/premex/gross/ui/OssView.kt index 4e8a2a4..f2fc4ca 100644 --- a/sample/ui/src/main/kotlin/se/premex/gross/ui/OssView.kt +++ b/sample/ui/src/main/kotlin/se/premex/gross/ui/OssView.kt @@ -32,7 +32,7 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import io.githhub.usefulness.licensee.android.ui.R -import se.premex.gross.core.Artifact +import io.github.usefulness.licensee.core.Artifact @Composable fun OssView(artifacts: List, modifier: Modifier = Modifier) { diff --git a/sample/ui/src/main/kotlin/se/premex/gross/ui/OssViewModel.kt b/sample/ui/src/main/kotlin/se/premex/gross/ui/OssViewModel.kt index 66d667c..4ade3f9 100644 --- a/sample/ui/src/main/kotlin/se/premex/gross/ui/OssViewModel.kt +++ b/sample/ui/src/main/kotlin/se/premex/gross/ui/OssViewModel.kt @@ -1,6 +1,6 @@ package se.premex.gross.ui -import se.premex.gross.core.Artifact +import io.github.usefulness.licensee.core.Artifact sealed class State { class Loading : State() diff --git a/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt b/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt deleted file mode 100644 index 7d7d267..0000000 --- a/sample/ui/src/test/kotlin/se/premex/gross/oss/LicenseParserTest.kt +++ /dev/null @@ -1,93 +0,0 @@ -package se.premex.gross.oss - -import kotlinx.serialization.ExperimentalSerializationApi -import okio.buffer -import okio.source -import org.junit.jupiter.api.Test -import se.premex.gross.core.LicenseParser - -private const val ARTIFACTS_SMALL = """[ - { - "groupId": "androidx.activity", - "artifactId": "activity", - "version": "1.7.0", - "name": "Activity", - "spdxLicenses": [ - { - "identifier": "Apache-2.0", - "name": "Apache License 2.0", - "url": "https://www.apache.org/licenses/LICENSE-2.0" - } - ], - "scm": { - "url": "https://cs.android.com/androidx/platform/frameworks/support" - } - } -]""" - -private const val ARTIFACTS_MEDIUM = """[ - { - "groupId": "androidx.activity", - "artifactId": "activity", - "version": "1.7.0", - "name": "Activity", - "spdxLicenses": [ - { - "identifier": "Apache-2.0", - "name": "Apache License 2.0", - "url": "https://www.apache.org/licenses/LICENSE-2.0" - } - ], - "scm": { - "url": "https://cs.android.com/androidx/platform/frameworks/support" - } - }, - { - "groupId": "androidx.activity", - "artifactId": "activity-compose", - "version": "1.7.0", - "name": "Activity Compose", - "spdxLicenses": [ - { - "identifier": "Apache-2.0", - "name": "Apache License 2.0", - "url": "https://www.apache.org/licenses/LICENSE-2.0" - } - ], - "scm": { - "url": "https://cs.android.com/androidx/platform/frameworks/support" - } - }, - { - "groupId": "androidx.activity", - "artifactId": "activity-ktx", - "version": "1.7.0", - "name": "Activity Kotlin Extensions", - "spdxLicenses": [ - { - "identifier": "Apache-2.0", - "name": "Apache License 2.0", - "url": "https://www.apache.org/licenses/LICENSE-2.0" - } - ], - "scm": { - "url": "https://cs.android.com/androidx/platform/frameworks/support" - } - } -]""" - -class LicenseParserTest { - @Test - @ExperimentalSerializationApi - fun testSmall() { - val licenseParser = object : LicenseParser {} - licenseParser.decode(ARTIFACTS_SMALL.byteInputStream().source().buffer()) - } - - @Test - @ExperimentalSerializationApi - fun testMedium() { - val licenseParser = object : LicenseParser {} - licenseParser.decode(ARTIFACTS_MEDIUM.byteInputStream().source().buffer()) - } -} From 566d27235a88825b66214fcbd3d89bcea36f7f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Sat, 11 Nov 2023 11:22:46 +0100 Subject: [PATCH 3/3] Check library integration --- README.md | 10 ++++++---- .../api/licensee-for-android.api | 2 ++ .../usefulness/licensee/CodeGenerationTask.kt | 2 +- .../licensee/LicenseeForAndroidExtension.kt | 4 ++++ sample/ui/build.gradle | 16 ++++++++++++++++ .../licensee/PluginLibaryIntegrationTest.kt | 13 +++++++++++++ 6 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 sample/ui/src/test/kotlin/io/github/usefulness/licensee/PluginLibaryIntegrationTest.kt diff --git a/README.md b/README.md index 2750e94..69a5745 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,16 @@ Options can be configured in the `licenseeForAndroid` extension: ```groovy licenseeForAndroid { - enableKotlinCodeGeneration = false - enableAndroidAssetGeneration = true - androidAssetFileName = "licensee_artifacts.json" - singularVariantName = null + enableKotlinCodeGeneration = false + generatedPackageName = "io.github.usefulness.licensee" + enableAndroidAssetGeneration = true + androidAssetFileName = "licensee_artifacts.json" + singularVariantName = null } ``` - `enableKotlinCodeGeneration` - Generates a static list of open source assets +- `generatedPackageName` - Generate Kotlin code under given package - `enableAndroidAssetGeneration` - Enable asset generation. Will copy licensee report to android asset directory making it available as `androidAssetFileName` - `androidAssetFileName` - The name of the asset file the licensee report gets copied to. - `singularVariantName` - The name of the build variant that all variants will use to have always the same licensed, regardless of app variant. (i.e. "productionRelease") diff --git a/licensee-for-android/api/licensee-for-android.api b/licensee-for-android/api/licensee-for-android.api index 22d282b..4371d4d 100644 --- a/licensee-for-android/api/licensee-for-android.api +++ b/licensee-for-android/api/licensee-for-android.api @@ -11,6 +11,7 @@ public abstract class io/github/usefulness/licensee/CodeGenerationTask : org/gra public final fun action ()V public abstract fun getInputFile ()Lorg/gradle/api/file/RegularFileProperty; public abstract fun getOutputDirectory ()Lorg/gradle/api/file/DirectoryProperty; + public abstract fun getPackageName ()Lorg/gradle/api/provider/Property; } public class io/github/usefulness/licensee/LicenseeForAndroidExtension { @@ -18,6 +19,7 @@ public class io/github/usefulness/licensee/LicenseeForAndroidExtension { public final fun getAndroidAssetFileName ()Lorg/gradle/api/provider/Property; public final fun getEnableAndroidAssetGeneration ()Lorg/gradle/api/provider/Property; public final fun getEnableKotlinCodeGeneration ()Lorg/gradle/api/provider/Property; + public final fun getGeneratedPackageName ()Lorg/gradle/api/provider/Property; public final fun getSingularVariantName ()Lorg/gradle/api/provider/Property; } diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt index eb02c99..1d5eb86 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/CodeGenerationTask.kt @@ -34,7 +34,7 @@ public abstract class CodeGenerationTask : DefaultTask() { public abstract val inputFile: RegularFileProperty @get:Input - public abstract val packageName : Property + public abstract val packageName: Property @TaskAction @ExperimentalSerializationApi diff --git a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt index 87d0b4f..b38c10c 100644 --- a/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt +++ b/licensee-for-android/src/main/kotlin/io/github/usefulness/licensee/LicenseeForAndroidExtension.kt @@ -11,6 +11,10 @@ public open class LicenseeForAndroidExtension(objectFactory: ObjectFactory) { */ public val enableKotlinCodeGeneration: Property = objectFactory.property(default = false) + /** + * Generate kotlin code under given package + * Default: `io.github.usefulness.licensee` + */ public val generatedPackageName: Property = objectFactory.property(default = "io.github.usefulness.licensee") /** diff --git a/sample/ui/build.gradle b/sample/ui/build.gradle index da80100..cbd3e9e 100644 --- a/sample/ui/build.gradle +++ b/sample/ui/build.gradle @@ -1,5 +1,21 @@ +import app.cash.licensee.LicenseeTask +import io.github.usefulness.licensee.CodeGenerationTask + plugins { alias(libs.plugins.starter.library.android) + alias(libs.plugins.app.cash.licensee) + id("io.github.usefulness.licensee-for-android") +} + +licensee { + allow("Apache-2.0") +} + +licenseeForAndroid { + enableKotlinCodeGeneration = true + enableAndroidAssetGeneration = false + generatedPackageName = "example.generated.from.library" + androidAssetFileName = "library_asset.json" } android { diff --git a/sample/ui/src/test/kotlin/io/github/usefulness/licensee/PluginLibaryIntegrationTest.kt b/sample/ui/src/test/kotlin/io/github/usefulness/licensee/PluginLibaryIntegrationTest.kt new file mode 100644 index 0000000..fda7bb3 --- /dev/null +++ b/sample/ui/src/test/kotlin/io/github/usefulness/licensee/PluginLibaryIntegrationTest.kt @@ -0,0 +1,13 @@ +package io.github.usefulness.licensee + +import example.generated.from.library.Licensee +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class PluginLibaryIntegrationTest { + + @Test + fun checkGeneratedCode() { + assertThat(Licensee.artifacts).isNotEmpty() + } +}