Skip to content

Commit

Permalink
updating function containsTestCode (#705)
Browse files Browse the repository at this point in the history
* updating function containsTestCode which is used in calculating code coverage. The old implementation only worked if you separated your tests from your code. However, some repos colocate files so the coverage was off.

* lint

* removing comparison on localName to string test
  • Loading branch information
bkern authored Sep 6, 2024
1 parent 3ecc7a3 commit 2f532c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,10 @@ object UnitTesting {
def testPackagePath = fqn"Morphir.UnitTest:Test:foo".packagePath

def containsTestCode(
globals: GlobalDefs,
fqn: FQName,
definition: TypedDefinition,
testNames: List[FQName]
): Boolean = {
def knownTestsPackages: List[PackageName] = testNames.map(_.packagePath).distinct
fqn.packagePath == testPackagePath ||
definition.outputType == testType || knownTestsPackages.contains(fqn.packagePath)
}
definition: TypedDefinition
): Boolean =
fqn.packagePath == testPackagePath || definition.outputType == testType

/**
* Run all tests in the given distributions. By this point, the distributions need to include the actual test
Expand All @@ -57,7 +52,7 @@ object UnitTesting {
val globals = GlobalDefs.fromDistributions(dists)
RTAction.environmentWithPure[MorphirSdk] { env =>
val testNames = collectTests(globals)
val userDefinedFunctions = collectNonTests(globals, testNames)
val userDefinedFunctions = collectNonTests(globals)
val testIRs = testNames.map(fqn => Value.Reference.Typed(testType, fqn))
if (testIRs.isEmpty) {
val emptySummary = TestSummary(
Expand Down Expand Up @@ -200,7 +195,7 @@ object UnitTesting {
val withResults = TestSet.processExpects(newGlobals, withExpects)

// begin collecting coverage related information
val userDefinedFunctions = collectNonTests(globals, testNames)
val userDefinedFunctions = collectNonTests(globals)
val referencedFunctions = testNames.flatMap(name => GlobalDefs.getStaticallyReachable(name, globals)).toSet
val coverageInfo = CoverageInfo(
referencedFunctions,
Expand Down Expand Up @@ -237,15 +232,12 @@ object UnitTesting {
*
* @param globals
* The global definitions to collect non tests from
* @param testNames
* The list of tests names so we can identify test helper functions to exclude
*/
private[runtime] def collectNonTests(
globals: GlobalDefs,
testNames: List[FQName]
globals: GlobalDefs
): Set[FQName] =
globals.definitions.collect {
case (fqn -> SDKValue.SDKValueDefinition(definition: TypedDefinition))
if !containsTestCode(globals, fqn, definition, testNames) => fqn
if !containsTestCode(fqn, definition) => fqn
}.toSet
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ object UnitTestingSpec extends MorphirBaseSpec {
test("Coverage is calculated") {
getTestSummary.map { result =>
assertTrue(result.coverageCounts == CoverageCounts(
covered = 2,
covered = 11,
uncovered = 0
))
}
Expand Down Expand Up @@ -179,7 +179,7 @@ object UnitTestingSpec extends MorphirBaseSpec {
test("Coverage is calculated") {
getTestSummary.map { result =>
assertTrue(result.coverageCounts == CoverageCounts(
covered = 2,
covered = 11,
uncovered = 0
))
}
Expand All @@ -200,7 +200,7 @@ object UnitTestingSpec extends MorphirBaseSpec {
test("Project coverage is calculated") {
getTestSummary.map { result =>
assertTrue(result.coverageCounts == CoverageCounts(
covered = 1,
covered = 3,
uncovered = 1
))
}
Expand All @@ -220,7 +220,7 @@ object UnitTestingSpec extends MorphirBaseSpec {
test("Project coverage is calculated") {
getTestSummary.map { result =>
assertTrue(result.coverageCounts == CoverageCounts(
covered = 1,
covered = 3,
uncovered = 1
))
}
Expand Down

0 comments on commit 2f532c5

Please sign in to comment.