diff --git a/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt b/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt index 89efdce..fec08f5 100644 --- a/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt +++ b/src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt @@ -13,44 +13,51 @@ data class TestProjectOption( val projectRootPath: String? = null ) -fun startTest(option: TestProjectOption) { - option.projectRootPath?.let { it -> - val projectConfig = parseConfiguration(it) - var hasError = false - 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 (option.fast) { - for (environment in projectConfig.environments) { - val datafileContent = buildDataFileAsPerEnvironment(option.projectRootPath,environment) - datafileContentByEnvironment[environment] = datafileContent - } +fun startTest(option: TestProjectOption) = option.projectRootPath?.let { it -> + val projectConfig = parseConfiguration(it) + var hasError = false + val folder = File(projectConfig.testsDirectoryPath) + val listOfFiles = folder.listFiles()?.sortedBy { it } + var executionResult: ExecutionResult? + val startTime = System.currentTimeMillis() + var passedTestsCount = 0 + var failedTestsCount = 0 + var passedAssertionsCount = 0 + var failedAssertionsCount = 0 + val datafileContentByEnvironment: MutableMap = mutableMapOf() + + if (option.fast) { + for (environment in projectConfig.environments) { + val datafileContent = buildDataFileAsPerEnvironment( + projectRootPath = it, + environment = 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 - if (listOfFiles.isNotEmpty()){ - executionResult = executeTest(filePath, datafileContentByEnvironment, option, projectConfig) - if (executionResult == null) { - continue - } - - if (executionResult.passed) { - passedTestsCount++ - } else { - hasError = true - failedTestsCount++ - } + if (!listOfFiles.isNullOrEmpty()) { + for (file in listOfFiles) { + if (file.isFile) { + if (file.extension.equals("yml", true)) { + val filePath = file.absoluteFile.path + if (listOfFiles.isNotEmpty()) { + executionResult = try { + executeTest(filePath, datafileContentByEnvironment, option, projectConfig) + } catch (e: Exception) { + printMessageInRedColor("Exception while executing assertion -> ${e.message}") + null + } + if (executionResult == null) { + continue + } + + if (executionResult.passed) { + passedTestsCount++ + } else { + hasError = true + failedTestsCount++ + } passedAssertionsCount += executionResult.assertionsCount.passed failedAssertionsCount += executionResult.assertionsCount.failed @@ -59,30 +66,35 @@ fun startTest(option: TestProjectOption) { } } } + } - val endTime = System.currentTimeMillis() - startTime + val endTime = System.currentTimeMillis() - startTime - if (!option.onlyFailures || hasError) { - printNormalMessage("\n----") - } - printNormalMessage("") - - if (hasError) { - printMessageInRedColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed") - printMessageInRedColor("Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed") - } else { - printMessageInGreenColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed") - printMessageInGreenColor("Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed") - } - printBoldMessage("Time: ${prettyDuration(endTime)}") + if (!option.onlyFailures || hasError) { + printNormalMessage("\n----") + } + printNormalMessage("") + + if (hasError) { + printMessageInRedColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed") + printMessageInRedColor("Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed") } else { - printMessageInRedColor("Directory is Empty or not exists") + printMessageInGreenColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed") + printMessageInGreenColor("Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed") } - } ?: printNormalMessage("Root Project Path Not Found") + printBoldMessage("Time: ${prettyDuration(endTime)}") + } else { + printMessageInRedColor("Directory is Empty or not exists") + } +} ?: printNormalMessage("Root Project Path Not Found") -} -private fun executeTest(filePath: String, datafileContentByEnvironment:MutableMap, option: TestProjectOption,configuration: Configuration): ExecutionResult? { +private fun executeTest( + filePath: String, + datafileContentByEnvironment: MutableMap, + option: TestProjectOption, + configuration: Configuration +): ExecutionResult? { val test = parseTestFeatureAssertions(filePath) val executionResult = ExecutionResult( @@ -90,7 +102,7 @@ private fun executeTest(filePath: String, datafileContentByEnvironment:MutableMa assertionsCount = AssertionsCount(0, 0) ) - if (test != null){ + if (test != null) { val key = when (test) { is Test.Feature -> test.value.key is Test.Segment -> test.value.key @@ -110,12 +122,11 @@ private fun executeTest(filePath: String, datafileContentByEnvironment:MutableMa } is Test.Segment -> { -// testSegment( -// testSegment = test.value, -// configuration = configuration, -// option = option -// ) - testSegment(test.value, configuration) + testSegment( + testSegment = test.value, + configuration = configuration, + option = option + ) } }