diff --git a/scripts/src/java/org/oppia/android/scripts/coverage/CoverageRunner.kt b/scripts/src/java/org/oppia/android/scripts/coverage/CoverageRunner.kt index e1ff9cdb221..b27cb822dbb 100644 --- a/scripts/src/java/org/oppia/android/scripts/coverage/CoverageRunner.kt +++ b/scripts/src/java/org/oppia/android/scripts/coverage/CoverageRunner.kt @@ -60,12 +60,13 @@ class CoverageRunner( val sfStartIdx = coverageData.indexOfFirst { it.startsWith("SF:") && it.substringAfter("SF:").substringAfterLast("/") == extractedFileName } - if (sfStartIdx == -1) throw IllegalArgumentException("File not found") + require(sfStartIdx != -1) { + "Coverage data not found for the file: $extractedFileName" + } val eofIdx = coverageData.subList(sfStartIdx, coverageData.size).indexOfFirst { it.startsWith("end_of_record") } - if (eofIdx == -1) throw IllegalArgumentException("End of record not found") - + require(eofIdx != -1) { "End of record not found" } val fileSpecificCovDatLines = coverageData.subList(sfStartIdx, sfStartIdx + eofIdx + 1) val coverageDataProps = fileSpecificCovDatLines.groupBy { line -> @@ -77,7 +78,7 @@ class CoverageRunner( } val filePath = coverageDataProps["SF"]?.firstOrNull()?.get(0) - ?: throw IllegalArgumentException("File path not found") + requireNotNull(filePath) { "File path not found" } val linesFound = coverageDataProps["LF"]?.singleOrNull()?.single()?.toInt() ?: 0 val linesHit = coverageDataProps["LH"]?.singleOrNull()?.single()?.toInt() ?: 0 diff --git a/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt b/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt index bf65548aecc..db5d381a711 100644 --- a/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt +++ b/scripts/src/java/org/oppia/android/scripts/coverage/RunCoverage.kt @@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit * * Example: * bazel run //scripts:run_coverage -- $(pwd) - * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt HTML + * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt format=HTML * Example with custom process timeout: * bazel run //scripts:run_coverage -- $(pwd) * utility/src/main/java/org/oppia/android/util/parser/math/MathModel.kt processTimeout=10 @@ -33,10 +33,14 @@ import java.util.concurrent.TimeUnit fun main(vararg args: String) { val repoRoot = args[0] val filePath = args[1] - val format = args.getOrNull(2) - val reportFormat = when { - format.equals("HTML", ignoreCase = true) -> ReportFormat.HTML - format.equals("MARKDOWN", ignoreCase = true) || format == null -> ReportFormat.MARKDOWN + + val format = args.find { it.startsWith("format=", ignoreCase = true) } + ?.substringAfter("=") + ?.uppercase() ?: "MARKDOWN" + + val reportFormat = when (format) { + "HTML" -> ReportFormat.HTML + "MARKDOWN" -> ReportFormat.MARKDOWN else -> throw IllegalArgumentException("Unsupported report format: $format") } @@ -98,14 +102,15 @@ class RunCoverage( * @return a list of lists containing coverage data for each requested test target, if * the file is exempted from having a test file, an empty list is returned */ - fun execute(): String { + fun execute() { val testFileExemptionList = loadTestFileExemptionsProto(testFileExemptionTextProto) .testFileExemptionList .filter { it.testFileNotRequired } .map { it.exemptedFilePath } if (filePath in testFileExemptionList) { - return "This file is exempted from having a test file; skipping coverage check." + println("This file is exempted from having a test file; skipping coverage check.") + return } val testFilePaths = findTestFile(repoRoot, filePath) @@ -133,8 +138,6 @@ class RunCoverage( println("\nGenerated report at: $reportOutputPath\n") } } ?: println("No coverage reports generated.") - - return reportOutputPath } private fun runCoverageForTarget(testTarget: String): CoverageReport { diff --git a/scripts/src/java/org/oppia/android/scripts/testing/TestBazelWorkspace.kt b/scripts/src/java/org/oppia/android/scripts/testing/TestBazelWorkspace.kt index d9ba7d2fd70..482425d64c7 100644 --- a/scripts/src/java/org/oppia/android/scripts/testing/TestBazelWorkspace.kt +++ b/scripts/src/java/org/oppia/android/scripts/testing/TestBazelWorkspace.kt @@ -30,7 +30,14 @@ class TestBazelWorkspace(private val temporaryRootFolder: TemporaryFolder) { temporaryRootFolder.newFile(".bazelversion").also { it.writeText(BAZEL_VERSION) } } private val bazelRcFile by lazy { - temporaryRootFolder.newFile(".bazelrc").also { it.writeText("--noenable_bzlmod") } + temporaryRootFolder.newFile(".bazelrc").also { + it.writeText( + """ + --noenable_bzlmod + build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11 + """.trimIndent() + ) + } } private val testFileMap = mutableMapOf() diff --git a/scripts/src/javatests/org/oppia/android/scripts/coverage/BUILD.bazel b/scripts/src/javatests/org/oppia/android/scripts/coverage/BUILD.bazel index d67bf159ebe..b58902a767a 100644 --- a/scripts/src/javatests/org/oppia/android/scripts/coverage/BUILD.bazel +++ b/scripts/src/javatests/org/oppia/android/scripts/coverage/BUILD.bazel @@ -6,7 +6,9 @@ load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_test") kt_jvm_test( name = "RunCoverageTest", + size = "large", srcs = ["RunCoverageTest.kt"], + shard_count = 4, deps = [ "//scripts:test_file_check_assets", "//scripts/src/java/org/oppia/android/scripts/coverage:run_coverage_lib", diff --git a/scripts/src/javatests/org/oppia/android/scripts/coverage/CoverageReporterTest.kt b/scripts/src/javatests/org/oppia/android/scripts/coverage/CoverageReporterTest.kt index 67db3ebb1a4..155885cce3d 100644 --- a/scripts/src/javatests/org/oppia/android/scripts/coverage/CoverageReporterTest.kt +++ b/scripts/src/javatests/org/oppia/android/scripts/coverage/CoverageReporterTest.kt @@ -71,7 +71,7 @@ class CoverageReporterTest { } @Test - fun testCoverageReporter_generateHTMLReport_hasCorrectContentAndFormatting() { + fun testCoverageReporter_generateHtmlReport_hasCorrectContentAndFormatting() { val sourceFile = tempFolder.newFile("SampleFile.kt") sourceFile.writeText( """ @@ -95,7 +95,7 @@ class CoverageReporterTest { ) val (_, reportText) = reporter.generateRichTextReport() - val expectedHTML = + val expectedHtml = """ @@ -261,6 +261,6 @@ class CoverageReporterTest { """.trimIndent() - assertThat(reportText).isEqualTo(expectedHTML) + assertThat(reportText).isEqualTo(expectedHtml) } } diff --git a/scripts/src/javatests/org/oppia/android/scripts/coverage/RunCoverageTest.kt b/scripts/src/javatests/org/oppia/android/scripts/coverage/RunCoverageTest.kt index b00415fcc31..a61a580373b 100644 --- a/scripts/src/javatests/org/oppia/android/scripts/coverage/RunCoverageTest.kt +++ b/scripts/src/javatests/org/oppia/android/scripts/coverage/RunCoverageTest.kt @@ -23,21 +23,56 @@ class RunCoverageTest { private val originalOut: PrintStream = System.out private val scriptBgDispatcher by lazy { ScriptBackgroundCoroutineDispatcher() } - private val commandExecutor by lazy { CommandExecutorImpl(scriptBgDispatcher) } private val longCommandExecutor by lazy { initializeCommandExecutorWithLongProcessWaitTime() } private lateinit var testBazelWorkspace: TestBazelWorkspace - private lateinit var sampleFilePath: String - private lateinit var sampleMDOutputPath: String - private lateinit var sampleHTMLOutputPath: String + private lateinit var coverageDir: String + private lateinit var markdownOutputPath: String + private lateinit var htmlOutputPath: String + + private lateinit var sourceContent: String + private lateinit var testContent: String @Before fun setUp() { - sampleFilePath = "/path/to/Sample.kt" - sampleMDOutputPath = "${tempFolder.root}/coverage_reports/report.md" - sampleHTMLOutputPath = "${tempFolder.root}/coverage_reports/report.html" + coverageDir = "/coverage_reports" + markdownOutputPath = "${tempFolder.root}/coverage_reports/report.md" + htmlOutputPath = "${tempFolder.root}/coverage_reports/report.html" testBazelWorkspace = TestBazelWorkspace(tempFolder) - System.setOut(PrintStream(outContent)) + + sourceContent = + """ + package com.example + + class TwoSum { + companion object { + fun sumNumbers(a: Int, b: Int): Any { + return if (a == 0 && b == 0) { + "Both numbers are zero" + } else { + a + b + } + } + } + } + """.trimIndent() + + testContent = + """ + package com.example + + import org.junit.Assert.assertEquals + import org.junit.Test + + class TwoSumTest { + @Test + fun testSumNumbers() { + assertEquals(TwoSum.sumNumbers(0, 1), 1) + assertEquals(TwoSum.sumNumbers(3, 4), 7) + assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") + } + } + """.trimIndent() } @After @@ -68,64 +103,121 @@ class RunCoverageTest { assertThat(exception).hasMessageThat().contains("No appropriate test file found") } + @Test + fun testRunCoverage_invalidFormat_throwsException() { + testBazelWorkspace.initEmptyWorkspace() + val exception = assertThrows() { + main(tempFolder.root.absolutePath, "file.kt", "format=PDF") + } + + assertThat(exception).hasMessageThat().contains("Unsupported report format") + } + + @Test + fun testRunCoverage_ignoreCaseMarkdownArgument_returnsCoverageData() { + val filePath = "coverage/main/java/com/example/TwoSum.kt" + + testBazelWorkspace.initEmptyWorkspace() + testBazelWorkspace.addSourceAndTestFileWithContent( + filename = "TwoSum", + testFilename = "TwoSumTest", + sourceContent = sourceContent, + testContent = testContent, + sourceSubpackage = "coverage/main/java/com/example", + testSubpackage = "coverage/test/java/com/example" + ) + + main( + "${tempFolder.root}", + filePath, + "format=Markdown", + "processTimeout=10" + ) + + val outputFilePath = "${tempFolder.root}" + + "$coverageDir/${filePath.removeSuffix(".kt")}/coverage.md" + + assertThat(File(outputFilePath).exists()).isTrue() + } + + @Test + fun testRunCoverage_ignoreCaseHtmlArgument_returnsCoverageData() { + val filePath = "coverage/main/java/com/example/TwoSum.kt" + + testBazelWorkspace.initEmptyWorkspace() + testBazelWorkspace.addSourceAndTestFileWithContent( + filename = "TwoSum", + testFilename = "TwoSumTest", + sourceContent = sourceContent, + testContent = testContent, + sourceSubpackage = "coverage/main/java/com/example", + testSubpackage = "coverage/test/java/com/example" + ) + + main( + "${tempFolder.root}", + filePath, + "format=Html", + "processTimeout=10" + ) + + val outputFilePath = "${tempFolder.root}" + + "$coverageDir/${filePath.removeSuffix(".kt")}/coverage.html" + + assertThat(File(outputFilePath).exists()).isTrue() + } + + @Test + fun testRunCoverage_reorderedArguments_returnsCoverageData() { + val filePath = "coverage/main/java/com/example/TwoSum.kt" + + testBazelWorkspace.initEmptyWorkspace() + testBazelWorkspace.addSourceAndTestFileWithContent( + filename = "TwoSum", + testFilename = "TwoSumTest", + sourceContent = sourceContent, + testContent = testContent, + sourceSubpackage = "coverage/main/java/com/example", + testSubpackage = "coverage/test/java/com/example" + ) + + main( + "${tempFolder.root}", + filePath, + "processTimeout=10", + "format=MARKDOWN" + ) + + val outputFilePath = "${tempFolder.root}" + + "$coverageDir/${filePath.removeSuffix(".kt")}/coverage.md" + + assertThat(File(outputFilePath).exists()).isTrue() + } + @Test fun testRunCoverage_testFileExempted_noCoverage() { + System.setOut(PrintStream(outContent)) val exemptedFilePath = "app/src/main/java/org/oppia/android/app/activity/ActivityComponent.kt" - val result = RunCoverage( + RunCoverage( "${tempFolder.root}", exemptedFilePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, - commandExecutor, + markdownOutputPath, + longCommandExecutor, scriptBgDispatcher ).execute() - assertThat(result).isEqualTo( + assertThat(outContent.toString().trim()).isEqualTo( "This file is exempted from having a test file; skipping coverage check." ) } @Test fun testRunCoverage_sampleTestsDefaultFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val filePath = "coverage/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", testFilename = "TwoSumTest", @@ -137,65 +229,24 @@ class RunCoverageTest { main( "${tempFolder.root}", - "coverage/main/java/com/example/TwoSum.kt", + filePath, ) val outputReportText = File( - "${tempFolder.root}/coverage_reports/coverage/main/java/com/example/TwoSum/coverage.md" + "${tempFolder.root}" + + "$coverageDir/${filePath.removeSuffix(".kt")}/coverage.md" ).readText() - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** coverage/main/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test fun testRunCoverage_sampleTestsMarkdownFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val filePath = "coverage/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", testFilename = "TwoSumTest", @@ -207,67 +258,24 @@ class RunCoverageTest { RunCoverage( "${tempFolder.root}", - "coverage/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, + markdownOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleMDOutputPath).readText() - - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** coverage/main/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val outputReportText = File(markdownOutputPath).readText() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test fun testRunCoverage_scriptTestsMarkdownFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val filePath = "scripts/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", testFilename = "TwoSumTest", @@ -279,67 +287,24 @@ class RunCoverageTest { RunCoverage( "${tempFolder.root}", - "scripts/java/com/example/TwoSum.kt", + filePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, + markdownOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleMDOutputPath).readText() - - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** scripts/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val outputReportText = File(markdownOutputPath).readText() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test fun testRunCoverage_appTestsMarkdownFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val filePath = "app/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", testFilename = "TwoSumTest", @@ -351,50 +316,25 @@ class RunCoverageTest { RunCoverage( "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, + markdownOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleMDOutputPath).readText() - - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** app/main/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val outputReportText = File(markdownOutputPath).readText() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test fun testRunCoverage_localTestsMarkdownFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() + val filePath = "app/main/java/com/example/TwoSum.kt" - val testContent = + testBazelWorkspace.initEmptyWorkspace() + val testContentLocal = """ package com.example @@ -416,74 +356,31 @@ class RunCoverageTest { filename = "TwoSum", testFilename = "TwoSumLocalTest", sourceContent = sourceContent, - testContent = testContent, + testContent = testContentLocal, sourceSubpackage = "app/main/java/com/example", testSubpackage = "app/test/java/com/example" ) RunCoverage( "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, + markdownOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleMDOutputPath).readText() - - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** app/main/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val outputReportText = File(markdownOutputPath).readText() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test fun testRunCoverage_sharedTestsMarkdownFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val filePath = "app/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", testFilename = "TwoSumTest", @@ -495,67 +392,24 @@ class RunCoverageTest { RunCoverage( "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, + markdownOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleMDOutputPath).readText() - - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** app/main/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val outputReportText = File(markdownOutputPath).readText() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test fun testRunCoverage_sharedAndLocalTestsMarkdownFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContentShared = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val filePath = "app/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() val testContentLocal = """ package com.example @@ -577,1028 +431,126 @@ class RunCoverageTest { testBazelWorkspace.addMultiLevelSourceAndTestFileWithContent( filename = "TwoSum", sourceContent = sourceContent, - testContentShared = testContentShared, + testContentShared = testContent, testContentLocal = testContentLocal, subpackage = "app" ) RunCoverage( "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.MARKDOWN, - sampleMDOutputPath, + markdownOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleMDOutputPath).readText() - - val expectedResult = - """ - ## Coverage Report - - - **Covered File:** app/main/java/com/example/TwoSum.kt - - **Coverage percentage:** 75.00% covered - - **Line coverage:** 3 / 4 lines covered - """.trimIndent() + val outputReportText = File(markdownOutputPath).readText() + val expectedResult = getExpectedMarkdownText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test - fun testRunCoverage_sampleTestsHTMLFormat_returnsCoverageData() { + fun testRunCoverage_sampleTestsHtmlFormat_returnsCoverageData() { + val filePath = "coverage/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() + testBazelWorkspace.addSourceAndTestFileWithContent( + filename = "TwoSum", + testFilename = "TwoSumTest", + sourceContent = sourceContent, + testContent = testContent, + sourceSubpackage = "coverage/main/java/com/example", + testSubpackage = "coverage/test/java/com/example" + ) - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() + RunCoverage( + "${tempFolder.root}", + filePath, + ReportFormat.HTML, + htmlOutputPath, + longCommandExecutor, + scriptBgDispatcher + ).execute() - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val outputReportText = File(htmlOutputPath).readText() + val expectedResult = getExpectedHtmlText(filePath) + + assertThat(outputReportText).isEqualTo(expectedResult) + } + @Test + fun testRunCoverage_scriptTestsHtmlFormat_returnsCoverageData() { + val filePath = "scripts/java/com/example/TwoSum.kt" + + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", testFilename = "TwoSumTest", sourceContent = sourceContent, testContent = testContent, - sourceSubpackage = "coverage/main/java/com/example", - testSubpackage = "coverage/test/java/com/example" + sourceSubpackage = "scripts/java/com/example", + testSubpackage = "scripts/javatests/com/example" ) RunCoverage( "${tempFolder.root}", - "coverage/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.HTML, - sampleHTMLOutputPath, + htmlOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleHTMLOutputPath).readText() - - val expectedResult = - """ - - - - - - Coverage Report - - - -

Coverage Report

-
-
- Covered File: coverage/main/java/com/example/TwoSum.kt
-
-
- Covered -
- Uncovered -
-
-
-
Coverage percentage: 75.00%
-
Line coverage: 3 / 4 covered
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Line NoSource Code
1package com.example
2
3class TwoSum {
4
5 companion object {
6 fun sumNumbers(a: Int, b: Int): Any {
7 return if (a == 0 && b == 0) {
8 "Both numbers are zero"
9 } else {
10 a + b
11 }
12 }
13 }
14}
- - - """.trimIndent() - - assertThat(outputReportText).isEqualTo(expectedResult) - } - - @Test - fun testRunCoverage_scriptTestsHTMLFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() - - testBazelWorkspace.addSourceAndTestFileWithContent( - filename = "TwoSum", - testFilename = "TwoSumTest", - sourceContent = sourceContent, - testContent = testContent, - sourceSubpackage = "scripts/java/com/example", - testSubpackage = "scripts/javatests/com/example" - ) - - RunCoverage( - "${tempFolder.root}", - "scripts/java/com/example/TwoSum.kt", - ReportFormat.HTML, - sampleHTMLOutputPath, - longCommandExecutor, - scriptBgDispatcher - ).execute() - - val outputReportText = File(sampleHTMLOutputPath).readText() - - val expectedResult = - """ - - - - - - Coverage Report - - - -

Coverage Report

-
-
- Covered File: scripts/java/com/example/TwoSum.kt
-
-
- Covered -
- Uncovered -
-
-
-
Coverage percentage: 75.00%
-
Line coverage: 3 / 4 covered
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Line NoSource Code
1package com.example
2
3class TwoSum {
4
5 companion object {
6 fun sumNumbers(a: Int, b: Int): Any {
7 return if (a == 0 && b == 0) {
8 "Both numbers are zero"
9 } else {
10 a + b
11 }
12 }
13 }
14}
- - - """.trimIndent() - - assertThat(outputReportText).isEqualTo(expectedResult) - } - - @Test - fun testRunCoverage_appTestsHTMLFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() - - testBazelWorkspace.addSourceAndTestFileWithContent( - filename = "TwoSum", - testFilename = "TwoSumTest", - sourceContent = sourceContent, - testContent = testContent, - sourceSubpackage = "app/main/java/com/example", - testSubpackage = "app/test/java/com/example" - ) - - RunCoverage( - "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", - ReportFormat.HTML, - sampleHTMLOutputPath, - longCommandExecutor, - scriptBgDispatcher - ).execute() - - val outputReportText = File(sampleHTMLOutputPath).readText() - - val expectedResult = - """ - - - - - - Coverage Report - - - -

Coverage Report

-
-
- Covered File: app/main/java/com/example/TwoSum.kt
-
-
- Covered -
- Uncovered -
-
-
-
Coverage percentage: 75.00%
-
Line coverage: 3 / 4 covered
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Line NoSource Code
1package com.example
2
3class TwoSum {
4
5 companion object {
6 fun sumNumbers(a: Int, b: Int): Any {
7 return if (a == 0 && b == 0) {
8 "Both numbers are zero"
9 } else {
10 a + b
11 }
12 }
13 }
14}
- - - """.trimIndent() + val outputReportText = File(htmlOutputPath).readText() + val expectedResult = getExpectedHtmlText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test - fun testRunCoverage_localTestsHTMLFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() - - val testContent = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumLocalTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + fun testRunCoverage_appTestsHtmlFormat_returnsCoverageData() { + val filePath = "app/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", - testFilename = "TwoSumLocalTest", + testFilename = "TwoSumTest", sourceContent = sourceContent, testContent = testContent, sourceSubpackage = "app/main/java/com/example", testSubpackage = "app/test/java/com/example" ) - RunCoverage( - "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", - ReportFormat.HTML, - sampleHTMLOutputPath, - longCommandExecutor, - scriptBgDispatcher - ).execute() - - val outputReportText = File(sampleHTMLOutputPath).readText() - - val expectedResult = - """ - - - - - - Coverage Report - - - -

Coverage Report

-
-
- Covered File: app/main/java/com/example/TwoSum.kt
-
-
- Covered -
- Uncovered -
-
-
-
Coverage percentage: 75.00%
-
Line coverage: 3 / 4 covered
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Line NoSource Code
1package com.example
2
3class TwoSum {
4
5 companion object {
6 fun sumNumbers(a: Int, b: Int): Any {
7 return if (a == 0 && b == 0) {
8 "Both numbers are zero"
9 } else {
10 a + b
11 }
12 }
13 }
14}
- - - """.trimIndent() + RunCoverage( + "${tempFolder.root}", + filePath, + ReportFormat.HTML, + htmlOutputPath, + longCommandExecutor, + scriptBgDispatcher + ).execute() + + val outputReportText = File(htmlOutputPath).readText() + val expectedResult = getExpectedHtmlText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test - fun testRunCoverage_sharedTestsHTMLFormat_returnsCoverageData() { - testBazelWorkspace.initEmptyWorkspace() - - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() + fun testRunCoverage_localTestsHtmlFormat_returnsCoverageData() { + val filePath = "app/main/java/com/example/TwoSum.kt" - val testContent = + testBazelWorkspace.initEmptyWorkspace() + val testContentLocal = """ package com.example import org.junit.Assert.assertEquals import org.junit.Test - class TwoSumTest { + class TwoSumLocalTest { @Test fun testSumNumbers() { @@ -1611,245 +563,62 @@ class RunCoverageTest { testBazelWorkspace.addSourceAndTestFileWithContent( filename = "TwoSum", - testFilename = "TwoSumTest", + testFilename = "TwoSumLocalTest", sourceContent = sourceContent, - testContent = testContent, + testContent = testContentLocal, sourceSubpackage = "app/main/java/com/example", - testSubpackage = "app/sharedTest/java/com/example" + testSubpackage = "app/test/java/com/example" ) RunCoverage( "${tempFolder.root}", - "app/main/java/com/example/TwoSum.kt", + filePath, ReportFormat.HTML, - sampleHTMLOutputPath, + htmlOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleHTMLOutputPath).readText() - - val expectedResult = - """ - - - - - - Coverage Report - - - -

Coverage Report

-
-
- Covered File: app/main/java/com/example/TwoSum.kt
-
-
- Covered -
- Uncovered -
-
-
-
Coverage percentage: 75.00%
-
Line coverage: 3 / 4 covered
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Line NoSource Code
1package com.example
2
3class TwoSum {
4
5 companion object {
6 fun sumNumbers(a: Int, b: Int): Any {
7 return if (a == 0 && b == 0) {
8 "Both numbers are zero"
9 } else {
10 a + b
11 }
12 }
13 }
14}
- - - """.trimIndent() + val outputReportText = File(htmlOutputPath).readText() + val expectedResult = getExpectedHtmlText(filePath) assertThat(outputReportText).isEqualTo(expectedResult) } @Test - fun testRunCoverage_sharedAndLocalTestsHTMLFormat_returnsCoverageData() { + fun testRunCoverage_sharedTestsHtmlFormat_returnsCoverageData() { + val filePath = "app/main/java/com/example/TwoSum.kt" + testBazelWorkspace.initEmptyWorkspace() + testBazelWorkspace.addSourceAndTestFileWithContent( + filename = "TwoSum", + testFilename = "TwoSumTest", + sourceContent = sourceContent, + testContent = testContent, + sourceSubpackage = "app/main/java/com/example", + testSubpackage = "app/sharedTest/java/com/example" + ) - val sourceContent = - """ - package com.example - - class TwoSum { - - companion object { - fun sumNumbers(a: Int, b: Int): Any { - return if (a == 0 && b == 0) { - "Both numbers are zero" - } else { - a + b - } - } - } - } - """.trimIndent() + RunCoverage( + "${tempFolder.root}", + filePath, + ReportFormat.HTML, + htmlOutputPath, + longCommandExecutor, + scriptBgDispatcher + ).execute() - val testContentShared = - """ - package com.example - - import org.junit.Assert.assertEquals - import org.junit.Test - - class TwoSumTest { - - @Test - fun testSumNumbers() { - assertEquals(TwoSum.sumNumbers(0, 1), 1) - assertEquals(TwoSum.sumNumbers(3, 4), 7) - assertEquals(TwoSum.sumNumbers(0, 0), "Both numbers are zero") - } - } - """.trimIndent() + val outputReportText = File(htmlOutputPath).readText() + val expectedResult = getExpectedHtmlText(filePath) + + assertThat(outputReportText).isEqualTo(expectedResult) + } + @Test + fun testRunCoverage_sharedAndLocalTestsHtmlFormat_returnsCoverageData() { + val filePath = "app/main/java/com/example/TwoSum.kt" + + testBazelWorkspace.initEmptyWorkspace() val testContentLocal = """ package com.example @@ -1871,7 +640,7 @@ class RunCoverageTest { testBazelWorkspace.addMultiLevelSourceAndTestFileWithContent( filename = "TwoSum", sourceContent = sourceContent, - testContentShared = testContentShared, + testContentShared = testContent, testContentLocal = testContentLocal, subpackage = "app" ) @@ -1880,14 +649,32 @@ class RunCoverageTest { "${tempFolder.root}", "app/main/java/com/example/TwoSum.kt", ReportFormat.HTML, - sampleHTMLOutputPath, + htmlOutputPath, longCommandExecutor, scriptBgDispatcher ).execute() - val outputReportText = File(sampleHTMLOutputPath).readText() + val outputReportText = File(htmlOutputPath).readText() + val expectedResult = getExpectedHtmlText(filePath) + + assertThat(outputReportText).isEqualTo(expectedResult) + } + + private fun getExpectedMarkdownText(filePath: String): String { + val markdownText = + """ + ## Coverage Report + + - **Covered File:** $filePath + - **Coverage percentage:** 75.00% covered + - **Line coverage:** 3 / 4 lines covered + """.trimIndent() + + return markdownText + } - val expectedResult = + private fun getExpectedHtmlText(filePath: String): String { + val htmlText = """ @@ -1997,7 +784,7 @@ class RunCoverageTest {

Coverage Report

- Covered File: app/main/java/com/example/TwoSum.kt
+ Covered File: $filePath
Covered @@ -2028,36 +815,33 @@ class RunCoverageTest { class TwoSum { 4 - + companion object { 5 - companion object { + fun sumNumbers(a: Int, b: Int): Any { 6 - fun sumNumbers(a: Int, b: Int): Any { + return if (a == 0 && b == 0) { 7 - return if (a == 0 && b == 0) { + "Both numbers are zero" 8 - "Both numbers are zero" + } else { 9 - } else { + a + b 10 - a + b + } 11 - } + } 12 - } + } 13 - } - - 14 } @@ -2065,7 +849,7 @@ class RunCoverageTest { """.trimIndent() - assertThat(outputReportText).isEqualTo(expectedResult) + return htmlText } private fun initializeCommandExecutorWithLongProcessWaitTime(): CommandExecutorImpl { diff --git a/scripts/src/javatests/org/oppia/android/scripts/testing/TestBazelWorkspaceTest.kt b/scripts/src/javatests/org/oppia/android/scripts/testing/TestBazelWorkspaceTest.kt index aeab27fe8d3..00d7c167a71 100644 --- a/scripts/src/javatests/org/oppia/android/scripts/testing/TestBazelWorkspaceTest.kt +++ b/scripts/src/javatests/org/oppia/android/scripts/testing/TestBazelWorkspaceTest.kt @@ -67,7 +67,12 @@ class TestBazelWorkspaceTest { // A .bazelversion file should now exist with the correct flags. val bazelRcFile = File(tempFolder.root, ".bazelrc") assertThat(bazelRcFile.exists()).isTrue() - assertThat(bazelRcFile.readText().trim()).isEqualTo("--noenable_bzlmod") + assertThat(bazelRcFile.readText().trim()).isEqualTo( + """ + --noenable_bzlmod + build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11 + """.trimIndent() + ) } @Test @@ -132,7 +137,12 @@ class TestBazelWorkspaceTest { ) val bazelRcContent = tempFolder.getBazelRcFile().readText().trim() - assertThat(bazelRcContent).isEqualTo("--noenable_bzlmod") + assertThat(bazelRcContent).isEqualTo( + """ + --noenable_bzlmod + build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11 + """.trimIndent() + ) } @Test @@ -636,7 +646,12 @@ class TestBazelWorkspaceTest { ) val bazelRcContent = tempFolder.getBazelRcFile().readText().trim() - assertThat(bazelRcContent).isEqualTo("--noenable_bzlmod") + assertThat(bazelRcContent).isEqualTo( + """ + --noenable_bzlmod + build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11 + """.trimIndent() + ) } @Test @@ -911,7 +926,12 @@ class TestBazelWorkspaceTest { testBazelWorkspace.createTest(testName = "FirstTest") val bazelRcContent = tempFolder.getBazelRcFile().readText().trim() - assertThat(bazelRcContent).isEqualTo("--noenable_bzlmod") + assertThat(bazelRcContent).isEqualTo( + """ + --noenable_bzlmod + build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11 + """.trimIndent() + ) } @Test @@ -1131,7 +1151,12 @@ class TestBazelWorkspaceTest { testBazelWorkspace.createLibrary(dependencyName = "ExampleDep") val bazelRcContent = tempFolder.getBazelRcFile().readText().trim() - assertThat(bazelRcContent).isEqualTo("--noenable_bzlmod") + assertThat(bazelRcContent).isEqualTo( + """ + --noenable_bzlmod + build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11 + """.trimIndent() + ) } @Test