Skip to content

Commit

Permalink
More asset tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Nov 11, 2023
1 parent 4ee48b8 commit 4001044
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 167 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion licensee-for-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -32,9 +33,13 @@ public abstract class CodeGenerationTask : DefaultTask() {
@get:InputFile
public abstract val inputFile: RegularFileProperty

@get:Input
public abstract val packageName : Property<String>

@TaskAction
@ExperimentalSerializationApi
public fun action() {
val packageName = packageName.get()
val licenseeTypesGenerator = LicenseeTypesGenerator(packageName)

FileSpec.builder(packageName, "Artifact")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public open class LicenseeForAndroidExtension(objectFactory: ObjectFactory) {
*/
public val enableKotlinCodeGeneration: Property<Boolean> = objectFactory.property(default = false)

public val generatedPackageName: Property<String> = objectFactory.property(default = "io.github.usefulness.licensee")

/**
* Enable asset generation. Will copy licensee report to
* android asset directory making it available as 'androidAssetFileName'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class LicenseeForAndroidPlugin : Plugin<Project> {
CodeGenerationTask::class.java,
) {
it.inputFile.set(artifactsFile)
it.packageName.set(extension.generatedPackageName)
}

// Do NOT use `.kotlin` here: https://issuetracker.google.com/issues/268248348
Expand Down
14 changes: 9 additions & 5 deletions sample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ android {
lint {
checkReleaseBuilds = false
}

testOptions {
unitTests.includeAndroidResources true
}
buildFeatures {
compose = true
buildConfig = true
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion sample/app/config/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>FunctionNaming:OssArtifactView.kt$@OptIn(ExperimentalSerializationApi::class) @Composable fun AssetsOssView()</ID>
<ID>FunctionNaming:OssArtifactView.kt$@Composable fun AssetsOssView()</ID>
<ID>FunctionNaming:OssKotlinCodeView.kt$@Composable fun ProgrammaticOssView()</ID>
<ID>FunctionNaming:Theme.kt$@Composable fun GrossTheme( darkTheme: Boolean = isSystemInDarkTheme(), // Dynamic color is available on Android 12+ dynamicColor: Boolean = true, content: @Composable () -&gt; Unit, )</ID>
<ID>InvalidPackageDeclaration:MainActivity.kt$package io.githhub.usefulness.licensee.android.app</ID>
Expand Down
6 changes: 2 additions & 4 deletions sample/app/src/main/kotlin/se/premex/gross/OssArtifactView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ 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
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()) }

Expand Down
10 changes: 5 additions & 5 deletions sample/app/src/main/kotlin/se/premex/gross/OssKotlinCodeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Context>()
val source = runBlocking { AssetLicenseeParser(context.assets).readFromAssets() }

else -> error("Not supported")
}
assertThat(Licensee.artifacts).isNotEmpty()
checkLoadedArtifacts(
artifacts = source,
isViewPager2Dependency = { groupId == "androidx.viewpager2" && artifactId == "viewpager2" },
)
}
}
Original file line number Diff line number Diff line change
@@ -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 <T> checkLoadedArtifacts(artifacts: List<T>, 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")
}
}
2 changes: 1 addition & 1 deletion sample/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.premex.gross.core
package io.github.usefulness.licensee.core

import kotlinx.serialization.Serializable

Expand All @@ -24,7 +24,7 @@ data class Artifact(
val artifactId: String,
val version: String,
val name: String? = null,
val spdxLicenses: List<SpdxLicenses>? = null,
val spdxLicenses: List<SpdxLicenses> = emptyList(),
val scm: Scm? = null,
val unknownLicenses: List<UnknownLicenses>? = null,
val unknownLicenses: List<UnknownLicenses> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -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<List<Artifact>>(source)
}
Original file line number Diff line number Diff line change
@@ -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 = """[
{
Expand Down Expand Up @@ -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())
}
}
7 changes: 6 additions & 1 deletion sample/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand All @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion sample/ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Artifact> = withContext(Dispatchers.IO) {
val source = assetManager.open("licensee_artifacts.json").source().buffer()
decode(source)
LicenseeParser.decode(source)
}
}

Expand Down
2 changes: 1 addition & 1 deletion sample/ui/src/main/kotlin/se/premex/gross/ui/OssView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Artifact>, modifier: Modifier = Modifier) {
Expand Down
Loading

0 comments on commit 4001044

Please sign in to comment.