Skip to content

Commit

Permalink
get testDir and segmentDir using configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tan108 committed Aug 5, 2024
1 parent d13cbeb commit 129dd69
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 53 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/com/featurevisor/testRunner/CommandExecuter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ private fun String.runCommand(workingDir: File): String? =
printMessageInRedColor("Exception while executing command -> ${e.message}")
null
}

fun createCommandForConfiguration()=
"npx featurevisor config --print --pretty"

fun getConfigurationJson(projectRootPath: String) =
try {
createCommandForConfiguration().runCommand(getFileForSpecificPath(projectRootPath))
}catch (e:Exception){
printMessageInRedColor("Exception in createCommandForConfiguration Commandline execution --> ${e.message}")
null
}

4 changes: 4 additions & 0 deletions src/main/kotlin/com/featurevisor/testRunner/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,7 @@ private fun parseConditionValue(value: Any?): ConditionValue {
}
}

fun parseConfiguration(projectRootPath: String) =
json.decodeFromString(Configuration.serializer(),getConfigurationJson(projectRootPath)!!)


88 changes: 44 additions & 44 deletions src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,40 @@ data class TestProjectOption(
val showDatafile: Boolean = false,
val onlyFailures: Boolean = false,
val fast: Boolean = false,
val testDirPath: String = "tests",
val projectRootPath: String = getRootProjectDir()
val projectRootPath: String? = null
)

fun startTest(option: TestProjectOption) {
var hasError = false
val folder = File("${option.projectRootPath}/${option.testDirPath}")
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

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
}
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)
} catch (e: Exception) {
printMessageInRedColor("Exception while execution test --> ${e.message}")
}
option.projectRootPath?.let {
val configurations = parseConfiguration(option.projectRootPath)
var hasError = false
val folder = File(configurations.testsDirectoryPath)
val listOfFiles = folder.listFiles()
var executionResult: ExecutionResult? = null
val startTime = System.currentTimeMillis()
var passedTestsCount = 0
var failedTestsCount = 0
var passedAssertionsCount = 0
var failedAssertionsCount = 0

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
}
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 (executionResult == null) {
continue
Expand Down Expand Up @@ -70,20 +71,22 @@ fun startTest(option: TestProjectOption) {
}
printNormalMessage("")

if (hasError) {
printMessageInRedColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed")
printMessageInRedColor("Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed")
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)}")
} else {
printMessageInGreenColor("\n\nTest specs: $passedTestsCount passed, $failedTestsCount failed")
printMessageInGreenColor("Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed")
printMessageInRedColor("Directory is Empty or not exists")
}
printBoldMessage("Time: ${prettyDuration(endTime)}")
} else {
printMessageInRedColor("Directory is Empty or not exists")
}
} ?: printNormalMessage("Root Project Path Not Found")

}

private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjectOption): ExecutionResult? {
private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjectOption,configuration: Configuration): ExecutionResult? {
val test = parseTestFeatureAssertions(filePath)

val executionResult = ExecutionResult(
Expand Down Expand Up @@ -111,10 +114,7 @@ private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjec
}

is Test.Segment -> {
testSegment(
testSegment = test.value,
option = option
)
testSegment(test.value, configuration)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/featurevisor/testRunner/TestFeature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun testFeature(testFeature: TestFeature, dataFile: DataFile, option: TestProjec
getDataFileContent(
featureName = testFeature.key,
environment = it.environment,
projectRootPath = option.projectRootPath
projectRootPath = option.projectRootPath.orEmpty()
)
}

Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/com/featurevisor/testRunner/TestSegment.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.featurevisor.testRunner

import com.featurevisor.sdk.segmentIsMatched
import com.featurevisor.types.TestResult
import com.featurevisor.types.TestResultAssertion
import com.featurevisor.types.TestResultAssertionError
import com.featurevisor.types.TestSegment
import com.featurevisor.types.*

fun testSegment(testSegment: TestSegment, option: TestProjectOption): TestResult {
fun testSegment(testSegment: TestSegment,configuration: Configuration): TestResult {
val testStartTime = System.currentTimeMillis()
val segmentKey = testSegment.key

Expand Down Expand Up @@ -36,9 +33,9 @@ fun testSegment(testSegment: TestSegment, option: TestProjectOption): TestResult
return@forEach
}

val yamlSegment = parseYamlSegment("${option.projectRootPath}/segments/$segmentKey.yml")
val expected = it.expectedToMatch
val actual = segmentIsMatched(yamlSegment!!, it.context)
val yamlSegment = parseYamlSegment("${configuration.segmentsDirectoryPath}/$segmentKey.yml")
val expected = assertion.expectedToMatch
val actual = segmentIsMatched(yamlSegment!!, assertion.context)
val passed = actual == expected

if (!passed) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/com/featurevisor/types/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,21 @@ data class DataFile(
val stagingDataFiles: DatafileContent? = null,
val productionDataFiles: DatafileContent? = null
)

@Serializable
data class Configuration(
val environments:List<String>,
val tags: List<String>,
val defaultBucketBy:String,
val prettyState:Boolean,
val prettyDatafile:Boolean,
val stringify:Boolean,
val featuresDirectoryPath:String,
val segmentsDirectoryPath:String,
val attributesDirectoryPath:String,
val groupsDirectoryPath:String,
val testsDirectoryPath:String,
val stateDirectoryPath:String,
val outputDirectoryPath:String,
val siteExportDirectoryPath:String
)

0 comments on commit 129dd69

Please sign in to comment.