Skip to content

Commit 631484a

Browse files
authored
Migrate all tests to kotlin, remove groovy plugin from buildSrc. (#1532)
* Migrate all tests to kotlin, remove groovy plugin from buildSrc. * Remove unused deps.
1 parent 386f602 commit 631484a

40 files changed

+687
-14
lines changed

buildSrc/$project.buildDir/generated/third_party_licenses/third_party_licenses.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

buildSrc/$project.buildDir/generated/third_party_licenses/third_party_licenses.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

buildSrc/build.gradle

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

1515
plugins {
16-
id 'java-gradle-plugin'
17-
id 'groovy'
16+
id "org.gradle.kotlin.kotlin-dsl" version "1.2.6"
17+
id "org.jlleitschuh.gradle.ktlint" version "9.2.1"
1818
}
1919

2020
repositories {
@@ -42,21 +42,16 @@ dependencies {
4242
implementation 'digital.wup:android-maven-publish:3.6.2'
4343
implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20'
4444
implementation 'org.json:json:20180813'
45-
4645
implementation 'io.opencensus:opencensus-api:0.18.0'
4746
implementation 'io.opencensus:opencensus-exporter-stats-stackdriver:0.18.0'
4847
runtime 'io.opencensus:opencensus-impl:0.18.0'
4948
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
50-
5149
implementation 'org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.17-g008'
52-
5350
implementation 'com.android.tools.build:gradle:3.4.1'
54-
testImplementation 'junit:junit:4.12'
55-
testImplementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
56-
testImplementation('org.spockframework:spock-core:1.1-groovy-2.4') {
57-
exclude group: 'org.codehaus.groovy'
58-
}
5951

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

6257
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.gradle.plugins
16+
17+
import com.google.common.truth.Truth.assertThat
18+
import com.google.gson.Gson
19+
import com.google.gson.reflect.TypeToken
20+
import java.io.File
21+
import org.gradle.testkit.runner.BuildResult
22+
import org.gradle.testkit.runner.GradleRunner
23+
import org.gradle.testkit.runner.TaskOutcome
24+
import org.gradle.testkit.runner.UnexpectedBuildFailure
25+
import org.junit.Assert
26+
import org.junit.Before
27+
import org.junit.Rule
28+
import org.junit.Test
29+
import org.junit.rules.TemporaryFolder
30+
import org.junit.runner.RunWith
31+
import org.junit.runners.JUnit4
32+
33+
@RunWith(JUnit4::class)
34+
class LicenseResolverPluginTests {
35+
36+
@Rule
37+
@JvmField
38+
val testProjectDir = TemporaryFolder()
39+
private lateinit var buildFile: File
40+
41+
val idempotentBuild: (taskName: String) -> BuildResult
42+
get() = this::build.memoize()
43+
44+
@Before
45+
fun setup() {
46+
buildFile = testProjectDir.newFile("build.gradle")
47+
testProjectDir.newFolder("src", "main", "java", "com", "example")
48+
testProjectDir.newFile("src/main/java/com/example/Foo.java").writeText("package com.example; class Foo {}")
49+
testProjectDir.newFile("src/main/AndroidManifest.xml").writeText(MANIFEST)
50+
51+
buildFile.writeText(BUILD_CONFIG)
52+
}
53+
54+
@Test
55+
fun `Generating licenses`() {
56+
val result = idempotentBuild("generateLicenses")
57+
assertThat(result.task(":generateLicenses")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
58+
59+
val json = getLicenseJson()
60+
val txt = getLicenseText()
61+
62+
assertThat(txt).isNotEmpty()
63+
64+
assertThat(json).containsKey("customLib1")
65+
66+
assertThat(txt).contains("customLib1")
67+
assertThat(txt).contains("Test license")
68+
val (start, length) = json["customLib1"]!!
69+
assertThat(txt.substring(start, start + length).trim()).isEqualTo("Test license")
70+
}
71+
72+
@Test
73+
fun `License tasks throw useful exception if file URI not found`() {
74+
buildFile.writeText("""
75+
plugins {
76+
id 'com.android.library'
77+
id 'LicenseResolverPlugin'
78+
}
79+
android.compileSdkVersion = 26
80+
81+
thirdPartyLicenses {
82+
add 'customLib', "file:///${File("non_existent_path.txt").absolutePath}"
83+
}
84+
""")
85+
86+
val thrown = Assert.assertThrows(UnexpectedBuildFailure::class.java) {
87+
build("generateLicenses")
88+
}
89+
90+
assertThat(thrown.message).contains("License file not found")
91+
}
92+
93+
data class FileOffset(val start: Int, val length: Int)
94+
95+
private fun getLicenseJson(): Map<String, FileOffset> =
96+
Gson().fromJson(
97+
File("${testProjectDir.root}/build/generated/third_party_licenses/",
98+
"third_party_licenses.json").readText(),
99+
object : TypeToken<Map<String, FileOffset>>() {}.type)
100+
101+
private fun getLicenseText(): String =
102+
File("${testProjectDir.root}/build/generated/third_party_licenses/",
103+
"third_party_licenses.txt").readText()
104+
105+
private fun build(taskName: String): BuildResult = GradleRunner.create()
106+
.withProjectDir(testProjectDir.root)
107+
.withArguments(taskName, "--stacktrace")
108+
.withPluginClasspath()
109+
.build()
110+
111+
companion object {
112+
const val MANIFEST = """<?xml version="1.0" encoding="utf-8"?>
113+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
114+
package="com.example">
115+
<uses-sdk android:minSdkVersion="14"/>
116+
</manifest>
117+
"""
118+
val BUILD_CONFIG = """
119+
buildscript {
120+
repositories {
121+
google()
122+
jcenter()
123+
}
124+
}
125+
126+
plugins {
127+
id 'com.android.library'
128+
id 'LicenseResolverPlugin'
129+
}
130+
131+
android.compileSdkVersion = 26
132+
133+
repositories {
134+
jcenter()
135+
google()
136+
}
137+
dependencies {
138+
implementation 'com.squareup.picasso:picasso:2.71828'
139+
implementation 'com.squareup.okhttp:okhttp:2.7.5'
140+
}
141+
142+
thirdPartyLicenses {
143+
add 'customLib1', "file:///${File("src/test/fixtures/license.txt").absolutePath}"
144+
}
145+
"""
146+
}
147+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.gradle.plugins
16+
17+
class Memoize1<in T, out R>(val f: (T) -> R) : (T) -> R {
18+
private val values = mutableMapOf<T, R>()
19+
override fun invoke(x: T): R {
20+
return values.getOrPut(x, { f(x) })
21+
}
22+
}
23+
24+
fun <T, R> ((T) -> R).memoize(): (T) -> R = Memoize1(this)

0 commit comments

Comments
 (0)