|
| 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 | +} |
0 commit comments