diff --git a/src/main/kotlin/com/featurevisor/testRunner/BenchmarkFeature.kt b/src/main/kotlin/com/featurevisor/testRunner/BenchmarkFeature.kt index f3a05c8..a4f0ca2 100644 --- a/src/main/kotlin/com/featurevisor/testRunner/BenchmarkFeature.kt +++ b/src/main/kotlin/com/featurevisor/testRunner/BenchmarkFeature.kt @@ -28,7 +28,7 @@ fun benchmarkFeature(option: BenchMarkOptions) { val datafileBuildStart = System.nanoTime().toDouble() - val datafileContent = buildDataFileForStaging(option.projectRootPath) + val datafileContent = buildDataFileAsPerEnvironment(option.projectRootPath,"staging") val datafileBuildEnd = System.nanoTime().toDouble() diff --git a/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt b/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt index 128bd9d..00e9faa 100644 --- a/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt +++ b/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt @@ -14,62 +14,58 @@ data class TestProjectOption( ) fun startTest(option: TestProjectOption) { - option.projectRootPath?.let { - val configurations = parseConfiguration(option.projectRootPath) + option.projectRootPath?.let { it -> + val projectConfig = parseConfiguration(it) var hasError = false - val folder = File(configurations.testsDirectoryPath) - val listOfFiles = folder.listFiles() + val folder = File(projectConfig.testsDirectoryPath) + val listOfFiles = folder.listFiles()?.sortedBy { it } var executionResult: ExecutionResult? = null val startTime = System.currentTimeMillis() var passedTestsCount = 0 var failedTestsCount = 0 var passedAssertionsCount = 0 var failedAssertionsCount = 0 + val datafileContentByEnvironment: MutableMap = mutableMapOf() - if (!listOfFiles.isNullOrEmpty()) { - val datafile = - if (option.fast) buildDataFileForBothEnvironments(projectRootPath = option.projectRootPath) else DataFile( - null, - null - ) - if (option.fast && (datafile.stagingDataFiles == null || datafile.productionDataFiles == null)) { - return + if (option.fast) { + for (environment in projectConfig.environments) { + val datafileContent = buildDataFileAsPerEnvironment(option.projectRootPath,environment) + datafileContentByEnvironment[environment] = datafileContent } + } + + if (!listOfFiles.isNullOrEmpty()) { for (file in listOfFiles) { if (file.isFile) { if (file.extension.equals("yml", true)) { val filePath = file.absoluteFile.path - try { - executionResult = executeTest(filePath, dataFile = datafile, option, configurations) - } catch (e: Exception) { - printMessageInRedColor("Exception in $filePath --> ${e.message}") + if (listOfFiles.isNotEmpty()){ + executionResult = executeTest(filePath, datafileContentByEnvironment, option, projectConfig) + if (executionResult == null) { + continue + } + + if (executionResult.passed) { + passedTestsCount++ + } else { + hasError = true + failedTestsCount++ } - if (executionResult == null) { - continue - } - - if (executionResult.passed) { - passedTestsCount++ + passedAssertionsCount += executionResult.assertionsCount.passed + failedAssertionsCount += executionResult.assertionsCount.failed } else { - hasError = true - failedTestsCount++ + printMessageInRedColor("The file is not valid yml file") } - - passedAssertionsCount += executionResult.assertionsCount.passed - failedAssertionsCount += executionResult.assertionsCount.failed - } else { - printMessageInRedColor("The file is not valid yml file") } } - } - val endTime = System.currentTimeMillis() - startTime + val endTime = System.currentTimeMillis() - startTime - if (!option.onlyFailures || hasError) { - printNormalMessage("\n----") - } - printNormalMessage("") + if (!option.onlyFailures || hasError) { + printNormalMessage("\n----") + } + printNormalMessage("") if (hasError) { printMessageInRedColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed") @@ -86,7 +82,7 @@ fun startTest(option: TestProjectOption) { } -private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjectOption,configuration: Configuration): ExecutionResult? { +private fun executeTest(filePath: String, datafileContentByEnvironment:MutableMap, option: TestProjectOption,configuration: Configuration): ExecutionResult? { val test = parseTestFeatureAssertions(filePath) val executionResult = ExecutionResult( @@ -108,12 +104,17 @@ private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjec is Test.Feature -> { testFeature( testFeature = test.value, - dataFile = dataFile, + datafileContentByEnvironment = datafileContentByEnvironment, option = option ) } is Test.Segment -> { +// testSegment( +// testSegment = test.value, +// configuration = configuration, +// option = option +// ) testSegment(test.value, configuration) } } diff --git a/src/main/kotlin/com/featurevisor/testRunner/TestFeature.kt b/src/main/kotlin/com/featurevisor/testRunner/TestFeature.kt index ed9a257..2f78537 100644 --- a/src/main/kotlin/com/featurevisor/testRunner/TestFeature.kt +++ b/src/main/kotlin/com/featurevisor/testRunner/TestFeature.kt @@ -9,7 +9,11 @@ import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement -fun testFeature(testFeature: TestFeature, dataFile: DataFile, option: TestProjectOption): TestResult { +fun testFeature( + testFeature: TestFeature, + datafileContentByEnvironment:MutableMap, + option: TestProjectOption +): TestResult { val testStartTime = System.currentTimeMillis() val featureKey = testFeature.key @@ -40,15 +44,23 @@ fun testFeature(testFeature: TestFeature, dataFile: DataFile, option: TestProjec return@forEach } - val datafileContent = if (option.fast) { - if (it.environment.equals("staging", true)) dataFile.stagingDataFiles else dataFile.productionDataFiles - } else { - getDataFileContent( + val datafileContent = datafileContentByEnvironment[it.environment] + ?: getDataFileContent( featureName = testFeature.key, environment = it.environment, projectRootPath = option.projectRootPath.orEmpty() ) - } + + +// if (option.fast) { +// if (it.environment.equals("staging", true)) dataFile.stagingDataFiles else dataFile.productionDataFiles +// } else { +// getDataFileContent( +// featureName = testFeature.key, +// environment = it.environment, +// projectRootPath = option.projectRootPath.orEmpty() +// ) +// } if (option.showDatafile) { printNormalMessage("") diff --git a/src/main/kotlin/com/featurevisor/testRunner/TestSegment.kt b/src/main/kotlin/com/featurevisor/testRunner/TestSegment.kt index 45c5853..c6ef7e4 100644 --- a/src/main/kotlin/com/featurevisor/testRunner/TestSegment.kt +++ b/src/main/kotlin/com/featurevisor/testRunner/TestSegment.kt @@ -3,7 +3,7 @@ package com.featurevisor.testRunner import com.featurevisor.sdk.segmentIsMatched import com.featurevisor.types.* -fun testSegment(testSegment: TestSegment,configuration: Configuration): TestResult { +fun testSegment(testSegment: TestSegment,configuration: Configuration,option: TestProjectOption): TestResult { val testStartTime = System.currentTimeMillis() val segmentKey = testSegment.key diff --git a/src/main/kotlin/com/featurevisor/testRunner/Utils.kt b/src/main/kotlin/com/featurevisor/testRunner/Utils.kt index e140ca3..d83a328 100644 --- a/src/main/kotlin/com/featurevisor/testRunner/Utils.kt +++ b/src/main/kotlin/com/featurevisor/testRunner/Utils.kt @@ -2,6 +2,7 @@ package com.featurevisor.testRunner import com.featurevisor.sdk.FeaturevisorInstance import com.featurevisor.sdk.InstanceOptions +import com.featurevisor.sdk.emptyDatafile import com.featurevisor.types.* import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json @@ -237,29 +238,14 @@ fun checkJsonIsEquals(a: String, b: String): Boolean { return map1 == map2 } -fun buildDataFileForBothEnvironments(projectRootPath: String): DataFile = - DataFile( - stagingDataFiles = buildDataFileForStaging(projectRootPath), - productionDataFiles = buildDataFileForProduction(projectRootPath) - ) - -fun buildDataFileForStaging(projectRootPath: String) = try { - getJsonForDataFile(environment = "staging", projectRootPath = projectRootPath)?.run { - convertToDataClass() - } -} catch (e: Exception) { - printMessageInRedColor("Unable to parse staging data file") - null -} -fun buildDataFileForProduction(projectRootPath: String) = try { - getJsonForDataFile(environment = "production", projectRootPath = projectRootPath)?.run { +fun buildDataFileAsPerEnvironment(projectRootPath: String,environment: String) = try { + getJsonForDataFile(environment = environment, projectRootPath = projectRootPath)?.run { convertToDataClass() - } - + } ?: emptyDatafile } catch (e: Exception) { - printMessageInRedColor("Unable to parse production data file") - null + printMessageInRedColor("Unable to parse data file") + emptyDatafile } fun getDataFileContent(featureName: String, environment: String, projectRootPath: String) =