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

Support multiple level inheritance for lib and app modules in case of KSP usage #5344

Merged
merged 3 commits into from
Mar 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,53 @@ class IntegrationLibraryGlideModuleTests(override val sourceType: SourceType) :
}
}

@Test
fun compile_withOnlyAppGlideModuleThroughBaseClass_generatesGeneratedAppGlideModule_thatCallsDependencyLibraryGlideModules() {
val kotlinAppModule =
KotlinSourceFile(
"AppModule.kt",
"""
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

class BaseAppModule : AppGlideModule()
@GlideModule class AppModule : BaseAppModule()
"""
)
val javaBaseAppModule =
JavaSourceFile(
"BaseAppModule.java",
"""
import com.bumptech.glide.module.AppGlideModule;

public class BaseAppModule extends AppGlideModule {
public BaseAppModule() {}
}
"""
)
val javaAppModule =
JavaSourceFile(
"AppModule.java",
"""
import com.bumptech.glide.annotation.GlideModule;

@GlideModule public class AppModule extends BaseAppModule {
public AppModule() {}
}
"""
)

compileCurrentSourceType(
kotlinAppModule,
javaBaseAppModule,
javaAppModule,
) {
assertThat(it.exitCode).isEqualTo(ExitCode.OK)
assertThat(it.generatedAppGlideModuleContents())
.hasSourceEqualTo(appGlideModuleWithOnlyDependencyLibraryModules)
}
}

@Test
fun compile_withValidLibraryGlideModule_andAppGlideModule_generatesGeneratedAppGlideModule_thatCallsAllLibraryAndDependencyAndAppGlideModules() {
val kotlinLibraryModule =
Expand Down Expand Up @@ -113,6 +160,85 @@ class IntegrationLibraryGlideModuleTests(override val sourceType: SourceType) :
}
}

@Test
fun compile_withValidLibraryGlideModule_andAppGlideModule_ThroughBaseClass_generatesGeneratedAppGlideModule_thatCallsAllLibraryAndDependencyAndAppGlideModules() {
val kotlinLibraryModule =
KotlinSourceFile(
"LibraryModule.kt",
"""
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.LibraryGlideModule

class BaseLibraryModule : LibraryGlideModule()
@GlideModule class LibraryModule : BaseLibraryModule()
"""
)
val kotlinAppModule =
KotlinSourceFile(
"AppModule.kt",
"""
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

class BaseAppModule : AppGlideModule()
@GlideModule class AppModule : BaseAppModule()
"""
)
val javaBaseLibraryModule =
JavaSourceFile(
"BaseLibraryModule.java",
"""
import com.bumptech.glide.module.LibraryGlideModule;

public class BaseLibraryModule extends LibraryGlideModule {}
"""
)
val javaLibraryModule =
JavaSourceFile(
"LibraryModule.java",
"""
import com.bumptech.glide.annotation.GlideModule;

@GlideModule public class LibraryModule extends BaseLibraryModule {}
"""
)
val javaBaseAppModule =
JavaSourceFile(
"BaseAppModule.java",
"""
import com.bumptech.glide.module.AppGlideModule;

public class BaseAppModule extends AppGlideModule {
public BaseAppModule() {}
}
"""
)
val javaAppModule =
JavaSourceFile(
"AppModule.java",
"""
import com.bumptech.glide.annotation.GlideModule;

@GlideModule public class AppModule extends BaseAppModule {
public AppModule() {}
}
"""
)

compileCurrentSourceType(
kotlinAppModule,
kotlinLibraryModule,
javaBaseAppModule,
javaAppModule,
javaBaseLibraryModule,
javaLibraryModule
) {
assertThat(it.exitCode).isEqualTo(ExitCode.OK)
assertThat(it.generatedAppGlideModuleContents())
.hasSourceEqualTo(appGlideModuleWithLibraryModuleAndDependencyLibraryModules)
}
}

@Test
fun compile_withDependencyModuleInExcludes_generatesGeneratedAppGlideModule_thatDoesNotCallDependencyLibraryGlideModules() {
val kotlinAppModule =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ object ModuleParser {
val appAndLibraryModuleNames = listOf(APP_MODULE_QUALIFIED_NAME, LIBRARY_MODULE_QUALIFIED_NAME)
val modulesBySuperType: Map<String?, List<KSClassDeclaration>> =
annotatedModules.filterIsInstance<KSClassDeclaration>().groupBy { classDeclaration ->
appAndLibraryModuleNames.singleOrNull { classDeclaration.hasSuperType(it) }
appAndLibraryModuleNames.firstOrNull { classDeclaration.hasSuperType(it) }
}

val (appModules, libraryModules) =
appAndLibraryModuleNames.map { modulesBySuperType[it] ?: emptyList() }
return GlideModules(appModules, libraryModules)
}

private fun KSClassDeclaration.hasSuperType(superTypeQualifiedName: String) =
superTypes
.map { superType -> superType.resolve().declaration.qualifiedName!!.asString() }
.contains(superTypeQualifiedName)
private fun KSClassDeclaration.hasSuperType(superTypeQualifiedName: String): Boolean {
val superDeclarations = superTypes
.map { superType -> superType.resolve().declaration }
val hasInDirectParent = superDeclarations
.map { it.qualifiedName!!.asString() }
.contains(superTypeQualifiedName)
return if (hasInDirectParent) {
true
} else {
superDeclarations.filterIsInstance(KSClassDeclaration::class.java)
.any { it.hasSuperType(superTypeQualifiedName) }
}
}

private const val APP_MODULE_QUALIFIED_NAME = "com.bumptech.glide.module.AppGlideModule"
private const val LIBRARY_MODULE_QUALIFIED_NAME = "com.bumptech.glide.module.LibraryGlideModule"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,86 @@ class LibraryGlideModuleTests(override val sourceType: SourceType) : PerSourceTy
}
}

@Test
fun compile_withValidLibraryGlideModule_andAppGlideModule_ThroughBaseClass_generatesGeneratedAppGlideModule_andCallsBothLibraryAndAppGlideModules() {
val kotlinLibraryModule =
KotlinSourceFile(
"LibraryModule.kt",
"""
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.LibraryGlideModule

class BaseLibraryModule : LibraryGlideModule()
@GlideModule class LibraryModule : BaseLibraryModule()
"""
)
val kotlinAppModule =
KotlinSourceFile(
"AppModule.kt",
"""
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

class BaseAppModule : AppGlideModule()
@GlideModule class AppModule : BaseAppModule()
"""
)
val javaBaseLibraryModule =
JavaSourceFile(
"BaseLibraryModule.java",
"""
import com.bumptech.glide.module.LibraryGlideModule;

public class BaseLibraryModule extends LibraryGlideModule {}
"""
)
val javaLibraryModule =
JavaSourceFile(
"LibraryModule.java",
"""
import com.bumptech.glide.annotation.GlideModule;

@GlideModule public class LibraryModule extends BaseLibraryModule {}
"""
)
val javaBaseAppModule =
JavaSourceFile(
"BaseAppModule.java",
"""
import com.bumptech.glide.module.AppGlideModule;

public class BaseAppModule extends AppGlideModule {
public BaseAppModule() {}
}
"""
)
val javaAppModule =
JavaSourceFile(
"AppModule.java",
"""
import com.bumptech.glide.annotation.GlideModule;

@GlideModule public class AppModule extends BaseAppModule {
public AppModule() {}
}
"""
)

compileCurrentSourceType(
kotlinAppModule,
kotlinLibraryModule,
javaBaseAppModule,
javaAppModule,
javaBaseLibraryModule,
javaLibraryModule
) {
assertThat(it.messages).doesNotContainMatch("[we]: \\[ksp] .*")
assertThat(it.exitCode).isEqualTo(ExitCode.OK)
assertThat(it.generatedAppGlideModuleContents())
.hasSourceEqualTo(appGlideModuleWithLibraryModule)
}
}

@Test
fun compile_withMultipleLibraryGlideModules_andAppGlideModule_callsAllLibraryGlideModulesFromGeneratedAppGlideModule() {
val kotlinLibraryModule1 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ class OnlyAppGlideModuleTests(override val sourceType: SourceType) : PerSourceTy
}
}

@Test
fun compile_withGlideModuleOnValidAppGlideModuleThroughBaseClass_generatedGeneratedAppGlideModule() {
val kotlinModule =
KotlinSourceFile(
"AppModule.kt",
"""
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
class BaseAppModule : AppGlideModule()
@GlideModule class AppModule : BaseAppModule()
"""
)
val javaBaseAppModule =
JavaSourceFile(
"BaseAppModule.java",
"""
import com.bumptech.glide.module.AppGlideModule;

public class BaseAppModule extends AppGlideModule {}
"""
.trimIndent()
)
val javaModule =
JavaSourceFile(
"AppModule.java",
"""
import com.bumptech.glide.annotation.GlideModule;

@GlideModule public class AppModule extends BaseAppModule {}
"""
.trimIndent()
)

compileCurrentSourceType(kotlinModule, javaBaseAppModule, javaModule) {
assertThat(it.generatedAppGlideModuleContents())
.hasSourceEqualTo(CommonSources.simpleAppGlideModule)
assertThat(it.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
}
}

@Test
fun compile_withAppGlideModuleConstructorAcceptingOnlyContext_generatesGeneratedAppGlideModule() {
val kotlinModule =
Expand Down Expand Up @@ -179,7 +219,7 @@ class OnlyAppGlideModuleTests(override val sourceType: SourceType) : PerSourceTy

// This is quite weird, we could probably pretty reasonably just assert that this doesn't happen.
@Test
fun compile_withAppGlideModuleWithOneEmptyrConstructor_andOneContextOnlyConstructor_usesTheContextOnlyConstructor() {
fun compile_withAppGlideModuleWithOneEmptyConstructor_andOneContextOnlyConstructor_usesTheContextOnlyConstructor() {
val kotlinModule =
KotlinSourceFile(
"AppModule.kt",
Expand Down Expand Up @@ -217,7 +257,7 @@ class OnlyAppGlideModuleTests(override val sourceType: SourceType) : PerSourceTy
}

@Test
fun copmile_withMultipleAppGlideModules_failes() {
fun compile_withMultipleAppGlideModules_fails() {
val firstKtModule =
KotlinSourceFile(
"Module1.kt",
Expand Down
Loading