Skip to content

Commit

Permalink
Remove vars from updatedSavedMatrix (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-goral authored Aug 21, 2020
1 parent 2d27280 commit 547b866
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 69 deletions.
113 changes: 51 additions & 62 deletions test_runner/src/main/kotlin/ftl/json/SavedMatrix.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ftl.json

import com.google.api.services.testing.model.TestMatrix
import ftl.reports.outcome.BillableMinutes
import ftl.reports.outcome.TestOutcome
import ftl.reports.outcome.createMatrixOutcomeSummary
import ftl.reports.outcome.fetchTestOutcomeContext
import ftl.util.MatrixState.FINISHED
Expand All @@ -17,22 +19,22 @@ import ftl.util.webLinkWithoutExecutionDetails

// execution gcs paths aren't API accessible.
data class SavedMatrix(
val matrixId: String,
val state: String,
val gcsPath: String,
val webLink: String,
val downloaded: Boolean,
val billableVirtualMinutes: Long,
val billablePhysicalMinutes: Long,
val outcome: String,
val outcomeDetails: String?,
val clientDetails: Map<String, String>?,
val gcsPathWithoutRootBucket: String,
val gcsRootBucket: String,
val webLinkWithoutExecutionDetails: String?
val matrixId: String = "",
val state: String = "",
val gcsPath: String = "",
val webLink: String = "",
val downloaded: Boolean = false,
val billableVirtualMinutes: Long = 0,
val billablePhysicalMinutes: Long = 0,
val outcome: String = "",
val outcomeDetails: String = "",
val clientDetails: Map<String, String>? = null,
val gcsPathWithoutRootBucket: String = "",
val gcsRootBucket: String = "",
val webLinkWithoutExecutionDetails: String? = "",
)

fun createSavedMatrix(testMatrix: TestMatrix) = defaultSavedMatrix().updateWithMatrix(testMatrix)
fun createSavedMatrix(testMatrix: TestMatrix) = SavedMatrix().updateWithMatrix(testMatrix)

fun SavedMatrix.canceledByUser() = outcomeDetails == ABORTED_BY_USER_MESSAGE

Expand Down Expand Up @@ -63,56 +65,43 @@ internal fun SavedMatrix.updateWithMatrix(newMatrix: TestMatrix): SavedMatrix =
if (needsUpdate(newMatrix)) updatedSavedMatrix(newMatrix)
else this

private fun SavedMatrix.updatedSavedMatrix(newMatrix: TestMatrix): SavedMatrix {
var outcomeDetails = outcomeDetails.takeUnless { it.isNullOrEmpty() }.orEmpty()
var outcome = this.outcome
var billableVirtualMinutes = this.billableVirtualMinutes
var billablePhysicalMinutes = this.billablePhysicalMinutes
if (state != newMatrix.state) {
when (newMatrix.state) {
FINISHED -> {
newMatrix.fetchTestOutcomeContext().createMatrixOutcomeSummary().let { (billableMinutes, summary) ->
outcome = summary.outcome
outcomeDetails = summary.testDetails
billableVirtualMinutes = billableMinutes.virtual
billablePhysicalMinutes = billableMinutes.physical
}
}
INVALID -> {
outcomeDetails = "Matrix is invalid"
outcome = "---"
}
}
private fun SavedMatrix.updatedSavedMatrix(
newMatrix: TestMatrix
): SavedMatrix = when (newMatrix.state) {
state -> this

FINISHED -> newMatrix.fetchTestOutcomeContext().createMatrixOutcomeSummary().let { (billableMinutes, outcome) ->
updateProperties(newMatrix).updateOutcome(outcome).updateBillableMinutes(billableMinutes)
}
return copy(
matrixId = newMatrix.testMatrixId,
state = newMatrix.state,
gcsPath = newMatrix.getGcsPath(),
webLink = newMatrix.webLink(),
downloaded = false,
clientDetails = newMatrix.getClientDetails(),
gcsPathWithoutRootBucket = newMatrix.getGcsPathWithoutRootBucket(),
gcsRootBucket = newMatrix.getGcsRootBucket(),
webLinkWithoutExecutionDetails = newMatrix.webLinkWithoutExecutionDetails(),
billableVirtualMinutes = billableVirtualMinutes,
billablePhysicalMinutes = billablePhysicalMinutes,
outcome = outcome,
outcomeDetails = outcomeDetails
)

INVALID -> updateProperties(newMatrix).updateOutcome(invalidTestOutcome())

else -> updateProperties(newMatrix)
}

private fun defaultSavedMatrix() = SavedMatrix(
matrixId = "",
state = "",
gcsPath = "",
webLink = "",
private fun SavedMatrix.updateProperties(newMatrix: TestMatrix) = copy(
matrixId = newMatrix.testMatrixId,
state = newMatrix.state,
gcsPath = newMatrix.getGcsPath(),
webLink = newMatrix.webLink(),
downloaded = false,
billableVirtualMinutes = 0,
billablePhysicalMinutes = 0,
outcome = "",
outcomeDetails = "",
clientDetails = null,
gcsPathWithoutRootBucket = "",
gcsRootBucket = "",
webLinkWithoutExecutionDetails = ""
clientDetails = newMatrix.getClientDetails(),
gcsPathWithoutRootBucket = newMatrix.getGcsPathWithoutRootBucket(),
gcsRootBucket = newMatrix.getGcsRootBucket(),
webLinkWithoutExecutionDetails = newMatrix.webLinkWithoutExecutionDetails()
)

private fun SavedMatrix.updateBillableMinutes(billableMinutes: BillableMinutes) = copy(
billablePhysicalMinutes = billableMinutes.physical,
billableVirtualMinutes = billableMinutes.virtual,
)

private fun SavedMatrix.updateOutcome(testOutcome: TestOutcome) = copy(
outcome = testOutcome.outcome,
outcomeDetails = testOutcome.testDetails
)

private fun invalidTestOutcome() = TestOutcome(
outcome = "---",
testDetails = "Matrix is invalid"
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.google.api.services.toolresults.model.Environment
fun TestOutcomeContext.createMatrixOutcomeSummary(): Pair<BillableMinutes, TestOutcome> =
steps.calculateAndroidBillableMinutes(projectId, testTimeout) to
if (environments.hasOutcome())
environments.createMatrixOutcomeSummaryUsingEnvironments(matrixId)
environments.createMatrixOutcomeSummaryUsingEnvironments()
else {
if (steps.isEmpty()) println("No test results found, something went wrong. Try re-running the tests.")
steps.createMatrixOutcomeSummaryUsingSteps(matrixId)
steps.createMatrixOutcomeSummaryUsingSteps()
}

private fun List<Environment>.hasOutcome() = isNotEmpty() && any { env ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import ftl.util.StepOutcome

data class TestOutcome(
val outcome: String,
val matrixId: String,
val testDetails: String
)

fun List<Environment>.createMatrixOutcomeSummaryUsingEnvironments(
testMatrixId: String,
outcome: Outcome? = getOutcomeFromEnvironments(),
testDetails: String? = outcome?.getDetails(map { it.createTestSuiteOverviewData() }.foldTestSuiteOverviewData())
) = TestOutcome(
outcome = outcome?.summary ?: "Unknown",
matrixId = testMatrixId,
testDetails = testDetails ?: "Unknown outcome"
)

Expand All @@ -27,12 +24,10 @@ private fun List<Environment>.getOutcomeFromEnvironments(): Outcome? = maxByOrNu
}?.environmentResult?.outcome

fun List<Step>.createMatrixOutcomeSummaryUsingSteps(
testMatrixId: String,
outcome: Outcome? = getOutcomeFromSteps(),
testDetails: String? = outcome?.getDetails(createTestSuiteOverviewData())
) = TestOutcome(
outcome = outcome?.summary ?: "Unknown",
matrixId = testMatrixId,
testDetails = testDetails ?: "Unknown outcome"
)

Expand Down

0 comments on commit 547b866

Please sign in to comment.