From 547b8660471335888793b31cb9bf71b5a3edc428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20G=C3=B3ral?= <60390247+jan-gogo@users.noreply.github.com> Date: Fri, 21 Aug 2020 10:58:05 +0200 Subject: [PATCH] Remove vars from updatedSavedMatrix (#1037) --- .../src/main/kotlin/ftl/json/SavedMatrix.kt | 113 ++++++++---------- .../outcome/CreateMatrixOutcomeSummary.kt | 4 +- .../kotlin/ftl/reports/outcome/TestOutcome.kt | 5 - 3 files changed, 53 insertions(+), 69 deletions(-) diff --git a/test_runner/src/main/kotlin/ftl/json/SavedMatrix.kt b/test_runner/src/main/kotlin/ftl/json/SavedMatrix.kt index baf48a9b82..d545df9a53 100644 --- a/test_runner/src/main/kotlin/ftl/json/SavedMatrix.kt +++ b/test_runner/src/main/kotlin/ftl/json/SavedMatrix.kt @@ -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 @@ -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?, - 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? = 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 @@ -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" ) diff --git a/test_runner/src/main/kotlin/ftl/reports/outcome/CreateMatrixOutcomeSummary.kt b/test_runner/src/main/kotlin/ftl/reports/outcome/CreateMatrixOutcomeSummary.kt index 5ec6e0dbb4..82b99c88dc 100644 --- a/test_runner/src/main/kotlin/ftl/reports/outcome/CreateMatrixOutcomeSummary.kt +++ b/test_runner/src/main/kotlin/ftl/reports/outcome/CreateMatrixOutcomeSummary.kt @@ -5,10 +5,10 @@ import com.google.api.services.toolresults.model.Environment fun TestOutcomeContext.createMatrixOutcomeSummary(): Pair = 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.hasOutcome() = isNotEmpty() && any { env -> diff --git a/test_runner/src/main/kotlin/ftl/reports/outcome/TestOutcome.kt b/test_runner/src/main/kotlin/ftl/reports/outcome/TestOutcome.kt index 691a467677..5f51c1965b 100644 --- a/test_runner/src/main/kotlin/ftl/reports/outcome/TestOutcome.kt +++ b/test_runner/src/main/kotlin/ftl/reports/outcome/TestOutcome.kt @@ -8,17 +8,14 @@ import ftl.util.StepOutcome data class TestOutcome( val outcome: String, - val matrixId: String, val testDetails: String ) fun List.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" ) @@ -27,12 +24,10 @@ private fun List.getOutcomeFromEnvironments(): Outcome? = maxByOrNu }?.environmentResult?.outcome fun List.createMatrixOutcomeSummaryUsingSteps( - testMatrixId: String, outcome: Outcome? = getOutcomeFromSteps(), testDetails: String? = outcome?.getDetails(createTestSuiteOverviewData()) ) = TestOutcome( outcome = outcome?.summary ?: "Unknown", - matrixId = testMatrixId, testDetails = testDetails ?: "Unknown outcome" )