Skip to content

Migrate all tests to kotlin, remove groovy plugin from buildSrc. #1532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

15 changes: 5 additions & 10 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

plugins {
id 'java-gradle-plugin'
id 'groovy'
id "org.gradle.kotlin.kotlin-dsl" version "1.2.6"
id "org.jlleitschuh.gradle.ktlint" version "9.2.1"
}

repositories {
Expand Down Expand Up @@ -42,21 +42,16 @@ dependencies {
implementation 'digital.wup:android-maven-publish:3.6.2'
implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20'
implementation 'org.json:json:20180813'

implementation 'io.opencensus:opencensus-api:0.18.0'
implementation 'io.opencensus:opencensus-exporter-stats-stackdriver:0.18.0'
runtime 'io.opencensus:opencensus-impl:0.18.0'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

implementation 'org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.17-g008'

implementation 'com.android.tools.build:gradle:3.4.1'
testImplementation 'junit:junit:4.12'
testImplementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
testImplementation('org.spockframework:spock-core:1.1-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
}

testImplementation 'junit:junit:4.13-rc-1'
testImplementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
testImplementation "com.google.truth:truth:1.0.1"
testImplementation 'commons-io:commons-io:2.6'

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.gradle.plugins

import com.google.common.truth.Truth.assertThat
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.File
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.testkit.runner.UnexpectedBuildFailure
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class LicenseResolverPluginTests {

@Rule
@JvmField
val testProjectDir = TemporaryFolder()
private lateinit var buildFile: File

val idempotentBuild: (taskName: String) -> BuildResult
get() = this::build.memoize()

@Before
fun setup() {
buildFile = testProjectDir.newFile("build.gradle")
testProjectDir.newFolder("src", "main", "java", "com", "example")
testProjectDir.newFile("src/main/java/com/example/Foo.java").writeText("package com.example; class Foo {}")
testProjectDir.newFile("src/main/AndroidManifest.xml").writeText(MANIFEST)

buildFile.writeText(BUILD_CONFIG)
}

@Test
fun `Generating licenses`() {
val result = idempotentBuild("generateLicenses")
assertThat(result.task(":generateLicenses")?.outcome).isEqualTo(TaskOutcome.SUCCESS)

val json = getLicenseJson()
val txt = getLicenseText()

assertThat(txt).isNotEmpty()

assertThat(json).containsKey("customLib1")

assertThat(txt).contains("customLib1")
assertThat(txt).contains("Test license")
val (start, length) = json["customLib1"]!!
assertThat(txt.substring(start, start + length).trim()).isEqualTo("Test license")
}

@Test
fun `License tasks throw useful exception if file URI not found`() {
buildFile.writeText("""
plugins {
id 'com.android.library'
id 'LicenseResolverPlugin'
}
android.compileSdkVersion = 26

thirdPartyLicenses {
add 'customLib', "file:///${File("non_existent_path.txt").absolutePath}"
}
""")

val thrown = Assert.assertThrows(UnexpectedBuildFailure::class.java) {
build("generateLicenses")
}

assertThat(thrown.message).contains("License file not found")
}

data class FileOffset(val start: Int, val length: Int)

private fun getLicenseJson(): Map<String, FileOffset> =
Gson().fromJson(
File("${testProjectDir.root}/build/generated/third_party_licenses/",
"third_party_licenses.json").readText(),
object : TypeToken<Map<String, FileOffset>>() {}.type)

private fun getLicenseText(): String =
File("${testProjectDir.root}/build/generated/third_party_licenses/",
"third_party_licenses.txt").readText()

private fun build(taskName: String): BuildResult = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments(taskName, "--stacktrace")
.withPluginClasspath()
.build()

companion object {
const val MANIFEST = """<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<uses-sdk android:minSdkVersion="14"/>
</manifest>
"""
val BUILD_CONFIG = """
buildscript {
repositories {
google()
jcenter()
}
}

plugins {
id 'com.android.library'
id 'LicenseResolverPlugin'
}

android.compileSdkVersion = 26

repositories {
jcenter()
google()
}
dependencies {
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.squareup.okhttp:okhttp:2.7.5'
}

thirdPartyLicenses {
add 'customLib1', "file:///${File("src/test/fixtures/license.txt").absolutePath}"
}
"""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.gradle.plugins

class Memoize1<in T, out R>(val f: (T) -> R) : (T) -> R {
private val values = mutableMapOf<T, R>()
override fun invoke(x: T): R {
return values.getOrPut(x, { f(x) })
}
}

fun <T, R> ((T) -> R).memoize(): (T) -> R = Memoize1(this)
Loading