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

Fix part of #5343: Exempt targets incompatible with code coverage #5480

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -323,3 +323,31 @@ oppia_android_test(
"//utility/src/main/java/org/oppia/android/util/networking:debug_module",
],
)

oppia_android_test(
name = "FirestoreDataControllerTest",
srcs = ["FirestoreDataControllerTest.kt"],
custom_package = "org.oppia.android.domain.oppialogger.analytics",
test_class = "org.oppia.android.domain.oppialogger.analytics.FirestoreDataControllerTest",
test_manifest = "//domain:test_manifest",
deps = [
"//:dagger",
"//domain/src/main/java/org/oppia/android/domain/oppialogger/analytics:cpu_module",
"//domain/src/main/java/org/oppia/android/domain/oppialogger/analytics:data_controller",
"//domain/src/main/java/org/oppia/android/domain/oppialogger/analytics:prod_module",
"//testing",
"//testing/src/main/java/org/oppia/android/testing/data:data_provider_test_monitor",
"//testing/src/main/java/org/oppia/android/testing/logging:event_log_subject",
"//testing/src/main/java/org/oppia/android/testing/robolectric:test_module",
"//testing/src/main/java/org/oppia/android/testing/threading:test_coroutine_dispatchers",
"//testing/src/main/java/org/oppia/android/testing/threading:test_module",
"//testing/src/main/java/org/oppia/android/testing/time:test_module",
"//third_party:androidx_test_ext_junit",
"//third_party:com_google_truth_truth",
"//third_party:junit_junit",
"//third_party:org_robolectric_robolectric",
"//third_party:robolectric_android-all",
"//utility/src/main/java/org/oppia/android/util/locale:prod_module",
"//utility/src/main/java/org/oppia/android/util/networking:debug_module",
],
)
1,290 changes: 1,106 additions & 184 deletions scripts/assets/test_file_exemptions.textproto

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class RunCoverage(
private val reportFormat: ReportFormat,
private val reportOutputPath: String,
private val commandExecutor: CommandExecutor,
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher
private val scriptBgDispatcher: ScriptBackgroundCoroutineDispatcher,
private val testFileExemptionTextProtoPath: String = "scripts/assets/test_file_exemptions.pb"
) {
private val bazelClient by lazy { BazelClient(File(repoRoot), commandExecutor) }

private val rootDirectory = File(repoRoot).absoluteFile
private val testFileExemptionTextProto = "scripts/assets/test_file_exemptions"

/**
* Executes coverage analysis for the specified file.
Expand All @@ -104,17 +104,24 @@ class RunCoverage(
* the file is exempted from having a test file, an empty list is returned
*/
fun execute() {
val testFileExemptionList = loadTestFileExemptionsProto(testFileExemptionTextProto)
.testFileExemptionList
.filter { it.testFileNotRequired }
.map { it.exemptedFilePath }

if (filePath in testFileExemptionList) {
println("This file is exempted from having a test file; skipping coverage check.")
val testFileExemptions = loadTestFileExemptionsProto(testFileExemptionTextProtoPath)
val filesNotNeedingTests =
testFileExemptions
.testFileExemptionList.filter { it.testFileNotRequired }.map { it.exemptedFilePath }
val filesIncompatibleWithCodeCoverage =
testFileExemptions
.testFileExemptionList
.filter { it.sourceFileIsIncompatibleWithCodeCoverage }
.map { it.exemptedFilePath }

if (filePath in filesNotNeedingTests || filePath in filesIncompatibleWithCodeCoverage) {
if (filePath in filesIncompatibleWithCodeCoverage) {
println("This file is incompatible with code coverage tooling; skipping coverage check.")
} else println("This file is exempted from having a test file; skipping coverage check.")
} else {
val testFilePaths = findTestFiles(repoRoot, filePath)
check(testFilePaths.isNotEmpty()) {
"No appropriate test file found for $filePath"
"No appropriate test file found for $filePath."
}

val testTargets = bazelClient.retrieveBazelTargets(testFilePaths)
Expand Down Expand Up @@ -218,8 +225,8 @@ private fun getReportOutputPath(
return "$repoRoot/coverage_reports/$fileWithoutExtension/$defaultFilename"
}

private fun loadTestFileExemptionsProto(testFileExemptiontextProto: String): TestFileExemptions {
return File("$testFileExemptiontextProto.pb").inputStream().use { stream ->
private fun loadTestFileExemptionsProto(testFileExemptionProtoPath: String): TestFileExemptions {
return File(testFileExemptionProtoPath).inputStream().use { stream ->
TestFileExemptions.newBuilder().also { builder ->
builder.mergeFrom(stream)
}.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ message TestFileExemptions {

// Overrides the minimum coverage percent required for the given file.
int32 override_min_coverage_percent_required = 3;

// TODO(#5481): Remove this property & all corresponding exemptions once all test
// configurations are compatible with code coverage tooling.
// A medium-term state to indicate that something about this source file's build config, or
// that of its test(s), are not compatible with the current code coverage tooling and thus
// should be ignored.
bool source_file_is_incompatible_with_code_coverage = 4;
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kt_jvm_test(
name = "RunCoverageTest",
size = "large",
srcs = ["RunCoverageTest.kt"],
shard_count = 4,
shard_count = 8,
deps = [
"//scripts:test_file_check_assets",
"//scripts/src/java/org/oppia/android/scripts/coverage:run_coverage_lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.oppia.android.scripts.common.CommandExecutorImpl
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher
import org.oppia.android.scripts.proto.TestFileExemptions
import org.oppia.android.scripts.testing.TestBazelWorkspace
import org.oppia.android.testing.assertThrows
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -197,20 +198,53 @@ class RunCoverageTest {
@Test
fun testRunCoverage_testFileExempted_noCoverage() {
System.setOut(PrintStream(outContent))
val exemptedFilePath = "app/src/main/java/org/oppia/android/app/activity/ActivityComponent.kt"
val exemptedFilePath = "SourceExemptedFromHavingTestFile.kt"
val testFileExemption = TestFileExemptions.TestFileExemption.newBuilder().apply {
this.exemptedFilePath = exemptedFilePath
this.testFileNotRequired = true
}.build()
val testFileExemptions = TestFileExemptions.newBuilder().apply {
addTestFileExemption(testFileExemption)
}.build()

RunCoverage(
"${tempFolder.root}",
exemptedFilePath,
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile(testFileExemptions)
).execute()

assertThat(outContent.toString().trim()).isEqualTo(
"This file is exempted from having a test file; skipping coverage check."
)
assertThat(outContent.toString().trim())
.isEqualTo("This file is exempted from having a test file; skipping coverage check.")
}

@Test
fun testRunCoverage_sourceFileIncompatibleWithCodeCoverage_exemptedFromCoverageAnalysis() {
System.setOut(PrintStream(outContent))
val incompatibleFilePath = "SourceIncompatibleWithCoverage.kt"
val testFileExemption = TestFileExemptions.TestFileExemption.newBuilder().apply {
this.exemptedFilePath = incompatibleFilePath
this.sourceFileIsIncompatibleWithCodeCoverage = true
}.build()
val testFileExemptions = TestFileExemptions.newBuilder().apply {
addTestFileExemption(testFileExemption)
}.build()

RunCoverage(
"${tempFolder.root}",
incompatibleFilePath,
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile(testFileExemptions)
).execute()

assertThat(outContent.toString().trim())
.isEqualTo("This file is incompatible with code coverage tooling; skipping coverage check.")
}

@Test
Expand Down Expand Up @@ -262,7 +296,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
Rd4dev marked this conversation as resolved.
Show resolved Hide resolved
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -291,7 +326,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -320,7 +356,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -367,7 +404,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -396,7 +434,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -458,7 +497,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -529,7 +569,8 @@ class RunCoverageTest {
ReportFormat.MARKDOWN,
markdownOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(markdownOutputPath).readText()
Expand Down Expand Up @@ -565,7 +606,8 @@ class RunCoverageTest {
ReportFormat.HTML,
htmlOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(htmlOutputPath).readText()
Expand Down Expand Up @@ -594,7 +636,8 @@ class RunCoverageTest {
ReportFormat.HTML,
htmlOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(htmlOutputPath).readText()
Expand Down Expand Up @@ -623,7 +666,8 @@ class RunCoverageTest {
ReportFormat.HTML,
htmlOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(htmlOutputPath).readText()
Expand Down Expand Up @@ -670,7 +714,8 @@ class RunCoverageTest {
ReportFormat.HTML,
htmlOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(htmlOutputPath).readText()
Expand Down Expand Up @@ -699,7 +744,8 @@ class RunCoverageTest {
ReportFormat.HTML,
htmlOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(htmlOutputPath).readText()
Expand Down Expand Up @@ -761,7 +807,8 @@ class RunCoverageTest {
ReportFormat.HTML,
htmlOutputPath,
longCommandExecutor,
scriptBgDispatcher
scriptBgDispatcher,
testFileExemptionTextProtoPath = createTestFileExemptionsProtoFile()
).execute()

val outputReportText = File(htmlOutputPath).readText()
Expand Down Expand Up @@ -1127,6 +1174,14 @@ class RunCoverageTest {
return htmlText
}

private fun createTestFileExemptionsProtoFile(
testFileExemptions: TestFileExemptions = TestFileExemptions.getDefaultInstance()
): String {
return tempFolder.newFile("test_file_exemptions.pb").also {
it.outputStream().use(testFileExemptions::writeTo)
}.path
}

private fun initializeCommandExecutorWithLongProcessWaitTime(): CommandExecutorImpl {
return CommandExecutorImpl(
scriptBgDispatcher, processTimeout = 5, processTimeoutUnit = TimeUnit.MINUTES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ Tests for Robolectric-specific utilities and configurations.

load("//:oppia_android_test.bzl", "oppia_android_test")

oppia_android_test(
name = "OppiaShadowActivityManagerTest",
srcs = ["OppiaShadowActivityManagerTest.kt"],
custom_package = "org.oppia.android.testing.robolectric",
test_class = "org.oppia.android.testing.robolectric.OppiaShadowActivityManagerTest",
test_manifest = "//testing:test_manifest",
deps = [
"//:dagger",
"//testing/src/main/java/org/oppia/android/testing/robolectric:oppia_shadow_activity_manager",
"//third_party:androidx_core_core",
"//third_party:androidx_test_ext_junit",
"//third_party:com_google_truth_truth",
"//third_party:junit_junit",
"//third_party:org_robolectric_robolectric",
"//third_party:robolectric_android-all",
],
)

oppia_android_test(
name = "OppiaShadowTrafficStatsTest",
srcs = ["OppiaShadowTrafficStatsTest.kt"],
custom_package = "org.oppia.android.testing.robolectric",
test_class = "org.oppia.android.testing.robolectric.OppiaShadowTrafficStatsTest",
test_manifest = "//testing:test_manifest",
deps = [
"//:dagger",
"//testing/src/main/java/org/oppia/android/testing/robolectric:oppia_shadow_traffic_stats",
"//third_party:androidx_core_core",
"//third_party:androidx_test_ext_junit",
"//third_party:com_google_truth_truth",
"//third_party:junit_junit",
"//third_party:org_robolectric_robolectric",
"//third_party:robolectric_android-all",
],
)

oppia_android_test(
name = "ShadowBidiFormatterTest",
srcs = ["ShadowBidiFormatterTest.kt"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ kt_android_library(
],
)

oppia_android_test(
name = "CurrentAppScreenNameIntentDecoratorTest",
srcs = ["CurrentAppScreenNameIntentDecoratorTest.kt"],
custom_package = "org.oppia.android.util.logging",
test_class = "org.oppia.android.util.logging.CurrentAppScreenNameIntentDecoratorTest",
test_manifest = "//utility:test_manifest",
deps = [
"//:dagger",
"//third_party:androidx_test_ext_junit",
"//third_party:androidx_test_ext_truth",
"//third_party:com_google_truth_truth",
"//third_party:org_robolectric_robolectric",
"//third_party:robolectric_android-all",
"//utility/src/main/java/org/oppia/android/util/logging:current_app_screen_name_intent_decorator",
],
)

oppia_android_test(
name = "EventBundleCreatorTest",
srcs = ["EventBundleCreatorTest.kt"],
Expand Down
Loading
Loading