Skip to content

Commit

Permalink
RUM-1520 improve the coverage tools
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Dec 22, 2023
1 parent 22761fc commit 8733d29
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
2 changes: 0 additions & 2 deletions detekt_test_pyramid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ datadog-test-pyramid:
active: true
ApiUsage:
active: true
apiPackageNamePrefix: "com.datadog"
includes: ['**/reliability/**']
ApiSurface:
active: true
apiPackageNamePrefix: "com.datadog"
includes: ['**/dd-sdk-android-*/**']
excludes: ['**/build/**', '**/test/**', '**/testDebug/**','**/testRelease/**', '**/androidTest/**', '**/testFixtures/**', '**/buildSrc/**', '**/*.kts', '**/instrumented/**', '**/sample/**', '**/tools/**']

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal class AndroidSpanLogsHandler(
timestampMicroseconds: Long? = null
) {
val logsFeature = sdkCore.getFeature(Feature.LOGS_FEATURE_NAME)
if (logsFeature != null) {
if (logsFeature != null && fields.isNotEmpty()) {
val message = fields.remove(Fields.MESSAGE)?.toString() ?: DEFAULT_EVENT_MESSAGE
fields[LogAttributes.DD_TRACE_ID] = span.traceId.toString()
fields[LogAttributes.DD_SPAN_ID] = span.spanId.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFunctionType
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtObjectLiteralExpression
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtParameterList
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtTypeElement
import org.jetbrains.kotlin.psi.KtUserType
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
import java.io.File

/**
Expand All @@ -50,6 +53,9 @@ class ApiSurface(
if (klass.hasModifier(KtTokens.PRIVATE_KEYWORD) || klass.hasModifier(KtTokens.INTERNAL_KEYWORD)) {
return
}
if (klass.isInterface()) {
return
}
super.visitClass(klass)
}

Expand All @@ -71,15 +77,30 @@ class ApiSurface(
outputFile.appendText(")\n")
}

@Suppress("ReturnCount")
override fun visitNamedFunction(function: KtNamedFunction) {
if (function.hasModifier(KtTokens.PRIVATE_KEYWORD) || function.hasModifier(KtTokens.INTERNAL_KEYWORD)) {
return
}
val parentName = function.containingClassOrObject?.fqName
?: function.containingKtFile.packageFqName
outputFile.appendText("$parentName.${function.nameAsSafeName}(")

val parameterList = function.getChildrenOfType<KtParameterList>().firstOrNull()
if (function.name == "toString" && parameterList?.children.isNullOrEmpty()) {
return
}
if (function.getStrictParentOfType<KtObjectLiteralExpression>() != null) {
// Function is overriding something in an anonymous object
// e.g.: val x = object : Interface { override fun foo() {} }
return
}
if (function.isExtensionDeclaration()) {
val target = function.receiverTypeReference?.nameForReceiverLabel()
val fqName = target?.resolveFullType()
outputFile.appendText("$fqName.${function.nameAsSafeName}(")
} else {
val parentName = function.containingClassOrObject?.fqName
?: function.containingKtFile.packageFqName
outputFile.appendText("$parentName.${function.nameAsSafeName}(")
}

parameterList?.children?.filterIsInstance<KtParameter>()?.forEachIndexed { idx, p ->
val typeElement = p.typeReference?.typeElement
if (idx > 0) outputFile.appendText(", ")
Expand Down Expand Up @@ -113,4 +134,6 @@ class ApiSurface(
private fun KtUserType.fullUserType(): String {
return referencedName?.resolveFullType() ?: text.resolveFullType()
}

// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class ApiUsage(
) : AbstractCallExpressionRule(config, simplifyLocalTypes = true, includeTypeArguments = false) {

private val outputFileName: String by config(defaultValue = "apiUsage.log")
private val apiPackageNamePrefix: String by config(defaultValue = "")
private val outputFile: File by lazy { File(outputFileName) }

private var visitingTestFunction = false
Expand Down Expand Up @@ -63,10 +62,8 @@ class ApiUsage(
expression: KtCallExpression,
resolvedCall: ResolvedFunCall
) {
if (resolvedCall.containerFqName.startsWith(apiPackageNamePrefix)) {
outputFile.appendText(resolvedCall.call)
outputFile.appendText("\n")
}
outputFile.appendText(resolvedCall.call)
outputFile.appendText("\n")
}

// endregion
Expand Down
5 changes: 5 additions & 0 deletions tools/unit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ android {
isDefault = true
}
}

packagingOptions {
exclude("META-INF/LICENSE.md")
exclude("META-INF/LICENSE-notice.md")
}
}

dependencies {
Expand Down

0 comments on commit 8733d29

Please sign in to comment.