Skip to content

Commit e59fa49

Browse files
committed
Cleanup, unittests for version logic
1 parent 35d2d8a commit e59fa49

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

firebase-installations/customer-lint-checks/customer-lint-checks.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
testImplementation "com.android.tools.lint:lint:$lintVersion"
2828
testImplementation "com.android.tools.lint:lint-tests:$lintVersion"
2929
testImplementation "com.android.tools:testutils:$lintVersion"
30+
testImplementation "com.google.truth:truth:$googleTruthVersion"
3031
}
3132

3233
compileKotlin.kotlinOptions.apiVersion = '1.2'

firebase-installations/customer-lint-checks/src/main/java/com/google/firebase/installations/lint/Checks.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import com.android.tools.lint.detector.api.Issue
2020

2121
class Checks : IssueRegistry() {
2222
override val issues: List<Issue>
23-
get() = listOf(IncompatibleIidVersionDetector.INCOMPATIBLE_IID_VERSION)
23+
get() = listOf(
24+
IncompatibleIidVersionDetector.INCOMPATIBLE_IID_VERSION,
25+
IncompatibleIidVersionDetector.IID_COMPATIBILITY_CHECK_FAILURE
26+
)
2427

2528
override val api: Int
2629
get() = CURRENT_API

firebase-installations/customer-lint-checks/src/main/java/com/google/firebase/installations/lint/IncompatibleIidVersionDetector.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.firebase.installations.lint
1616

17-
import com.android.builder.model.MavenCoordinates
1817
import com.android.builder.model.Variant
1918
import com.android.tools.lint.detector.api.Category
2019
import com.android.tools.lint.detector.api.Context
@@ -70,7 +69,7 @@ class IncompatibleIidVersionDetector : Detector() {
7069
for (lib in variant.mainArtifact.dependencies.libraries) {
7170
val coordinates = lib.resolvedCoordinates
7271
if (coordinates.groupId == "com.google.firebase" && coordinates.artifactId == "firebase-iid") {
73-
if (!isCompatibleVersion(coordinates)) {
72+
if (!isCompatibleVersion(coordinates.version)) {
7473
context.report(INCOMPATIBLE_IID_VERSION,
7574
Location.create(context.file),
7675
"Incompatible IID version found in variant ${variant.name}: ${lib.name.removeSuffix("@aar")}.\n" +
@@ -96,8 +95,8 @@ class IncompatibleIidVersionDetector : Detector() {
9695
return variantsMethod.invoke(model) as List<Variant>
9796
}
9897

99-
private fun isCompatibleVersion(coordinates: MavenCoordinates): Boolean {
100-
val versionComponents = coordinates.version.split('.', limit = 3).toTypedArray()
98+
internal fun isCompatibleVersion(version: String): Boolean {
99+
val versionComponents = version.split('.', limit = 3).toTypedArray()
101100

102101
// Incompatible if major version is before v20
103102
if (20 > versionComponents[0].toInt()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.installations.lint
16+
17+
import com.google.common.truth.Truth.assertThat
18+
import org.junit.Test
19+
import org.junit.runner.RunWith
20+
import org.junit.runners.JUnit4
21+
22+
@RunWith(JUnit4::class)
23+
class VersionTests {
24+
companion object Detector {
25+
val detector = IncompatibleIidVersionDetector()
26+
}
27+
28+
@Test
29+
fun `isCompatibleVersion with 19_0_0 should be false`() {
30+
assertThat(detector.isCompatibleVersion("19.0.0")).isFalse()
31+
}
32+
33+
@Test
34+
fun `isCompatibleVersion with 20_0_0 should be false`() {
35+
assertThat(detector.isCompatibleVersion("20.0.0")).isFalse()
36+
}
37+
38+
@Test
39+
fun `isCompatibleVersion with 20_1_0 should be true`() {
40+
assertThat(detector.isCompatibleVersion("20.1.0")).isTrue()
41+
}
42+
43+
@Test
44+
fun `isCompatibleVersion with 20_1_1 should be true`() {
45+
assertThat(detector.isCompatibleVersion("20.1.1")).isTrue()
46+
}
47+
48+
@Test
49+
fun `isCompatibleVersion with 21_0_0 should be true`() {
50+
assertThat(detector.isCompatibleVersion("21.0.0")).isTrue()
51+
}
52+
}

0 commit comments

Comments
 (0)