Skip to content
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

Add overrideScopes flag to lint check #310

Merged
merged 3 commits into from
Dec 12, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import java.util.EnumSet

internal const val CUSTOM_SCOPE_KEY = "autodispose.typesWithScope"
internal const val LENIENT = "autodispose.lenient"
internal const val OVERRIDE_SCOPES = "autodispose.overrideScopes"

/**
* Detector which checks if your stream subscriptions are handled by AutoDispose.
Expand Down Expand Up @@ -107,8 +108,8 @@ class AutoDisposeDetector: Detector(), SourceCodeScanner {
private var lenient: Boolean = false

override fun beforeCheckRootProject(context: Context) {
// Add the default Android scopes.
val scopes = HashSet(DEFAULT_SCOPES)
var overrideScopes = false
val scopes = mutableSetOf<String>()

// Add the custom scopes defined in configuration.
val props = Properties()
Expand All @@ -126,6 +127,13 @@ class AutoDisposeDetector: Detector(), SourceCodeScanner {
props.getProperty(LENIENT)?.toBoolean()?.let {
lenient = it
}
props.getProperty(OVERRIDE_SCOPES)?.toBoolean()?.let {
overrideScopes = it
}
}
// If scopes are not overriden, add the default ones.
if (!overrideScopes) {
scopes.addAll(DEFAULT_SCOPES)
}
appliedScopes = scopes
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,64 @@ class AutoDisposeDetectorTest {
.expectClean()
}

@Test fun overrideCustomScopeWithoutAutoDispose() {
val properties = projectProperties()
properties.property(CUSTOM_SCOPE_KEY, "com.uber.autodispose.sample.ClassWithCustomScope")
properties.property(OVERRIDE_SCOPES, "true")
properties.to(AutoDisposeDetector.PROPERTY_FILE)

lint().files(rxJava2(),
LIFECYCLE_OWNER,
ACTIVITY,
properties,
kotlin("""
package com.uber.autodispose.sample
import com.uber.autodispose.sample.ClassWithCustomScope
import androidx.appcompat.app.AppCompatActivity
import io.reactivex.Observable
import com.uber.autodispose.ScopeProvider

class MyCustomClass: AppCompatActivity {
lateinit var scopeProvider: ScopeProvider
fun doSomething() {
val observable = Observable.just(1, 2, 3)
observable.subscribe() // No error since the scopes are being overriden and only custom ones are considered.
}
}
""").indented())
.issues(AutoDisposeDetector.ISSUE)
.run()
.expectClean()
}

@Test fun overrideCustomScopeWithAutoDispose() {
val properties = projectProperties()
properties.property(CUSTOM_SCOPE_KEY, "com.uber.autodispose.sample.ClassWithCustomScope")
properties.property(OVERRIDE_SCOPES, "true")
properties.to(AutoDisposeDetector.PROPERTY_FILE)

lint().files(rxJava2(),
CUSTOM_SCOPE,
properties,
kotlin("""
package com.uber.autodispose.sample
import com.uber.autodispose.sample.ClassWithCustomScope
import io.reactivex.Observable
import com.uber.autodispose.ScopeProvider

class MyCustomClass: ClassWithCustomScope {
lateinit var scopeProvider: ScopeProvider
fun doSomething() {
val observable = Observable.just(1, 2, 3)
observable.autoDisposable(scopeProvider).subscribe()
}
}
""").indented())
.issues(AutoDisposeDetector.ISSUE)
.run()
.expectClean()
}

@Test fun capturedDisposable() {
val propertiesFile = lenientPropertiesFile()
lint().files(rxJava2(), propertiesFile, LIFECYCLE_OWNER, ACTIVITY, kotlin("""
Expand Down