Skip to content

Commit 9c961d1

Browse files
Tan108hsinha610
andauthored
Get configuration dynamically (#37)
Co-authored-by: HARSHIT SINHA <[email protected]>
1 parent d13cbeb commit 9c961d1

File tree

9 files changed

+100
-70
lines changed

9 files changed

+100
-70
lines changed

src/main/kotlin/com/featurevisor/sdk/Conditions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ object Conditions {
114114
attributeValue is AttributeValue.StringValue && conditionValue is ConditionValue.ArrayValue -> {
115115
when (operator) {
116116
IN_ARRAY -> attributeValue.value in conditionValue.values
117-
NOT_IN_ARRAY -> (attributeValue.value in conditionValue.values).not()
117+
NOT_IN_ARRAY -> (attributeValue.value !in conditionValue.values)
118118
else -> false
119119
}
120120
}

src/main/kotlin/com/featurevisor/testRunner/BenchmarkFeature.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fun benchmarkFeature(option: BenchMarkOptions) {
2828

2929
val datafileBuildStart = System.nanoTime().toDouble()
3030

31-
val datafileContent = buildDataFileForStaging(option.projectRootPath)
31+
val datafileContent = buildDataFileAsPerEnvironment(option.projectRootPath,"staging")
3232

3333
val datafileBuildEnd = System.nanoTime().toDouble()
3434

src/main/kotlin/com/featurevisor/testRunner/CommandExecuter.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ private fun String.runCommand(workingDir: File): String? =
3939
printMessageInRedColor("Exception while executing command -> ${e.message}")
4040
null
4141
}
42+
43+
fun createCommandForConfiguration()=
44+
"npx featurevisor config --print --pretty"
45+
46+
fun getConfigurationJson(projectRootPath: String) =
47+
try {
48+
createCommandForConfiguration().runCommand(getFileForSpecificPath(projectRootPath))
49+
}catch (e:Exception){
50+
printMessageInRedColor("Exception in createCommandForConfiguration Commandline execution --> ${e.message}")
51+
null
52+
}
53+

src/main/kotlin/com/featurevisor/testRunner/Parser.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,7 @@ private fun parseConditionValue(value: Any?): ConditionValue {
257257
}
258258
}
259259

260+
fun parseConfiguration(projectRootPath: String) =
261+
json.decodeFromString(Configuration.serializer(),getConfigurationJson(projectRootPath)!!)
262+
263+

src/main/kotlin/com/featurevisor/testRunner/TestExecuter.kt

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,60 @@ data class TestProjectOption(
1010
val showDatafile: Boolean = false,
1111
val onlyFailures: Boolean = false,
1212
val fast: Boolean = false,
13-
val testDirPath: String = "tests",
14-
val projectRootPath: String = getRootProjectDir()
13+
val projectRootPath: String? = null
1514
)
1615

17-
fun startTest(option: TestProjectOption) {
16+
fun startTest(option: TestProjectOption) = option.projectRootPath?.let { it ->
17+
val projectConfig = parseConfiguration(it)
1818
var hasError = false
19-
val folder = File("${option.projectRootPath}/${option.testDirPath}")
19+
val folder = File(projectConfig.testsDirectoryPath)
2020
val listOfFiles = folder.listFiles()?.sortedBy { it }
21-
var executionResult: ExecutionResult? = null
21+
var executionResult: ExecutionResult?
2222
val startTime = System.currentTimeMillis()
2323
var passedTestsCount = 0
2424
var failedTestsCount = 0
2525
var passedAssertionsCount = 0
2626
var failedAssertionsCount = 0
27+
val datafileContentByEnvironment: MutableMap<String, DatafileContent> = mutableMapOf()
2728

28-
if (!listOfFiles.isNullOrEmpty()) {
29-
val datafile =
30-
if (option.fast) buildDataFileForBothEnvironments(projectRootPath = option.projectRootPath) else DataFile(
31-
null,
32-
null
29+
if (option.fast) {
30+
for (environment in projectConfig.environments) {
31+
val datafileContent = buildDataFileAsPerEnvironment(
32+
projectRootPath = it,
33+
environment = environment
3334
)
34-
if (option.fast && (datafile.stagingDataFiles == null || datafile.productionDataFiles == null)) {
35-
return
35+
datafileContentByEnvironment[environment] = datafileContent
3636
}
37+
}
38+
39+
if (!listOfFiles.isNullOrEmpty()) {
3740
for (file in listOfFiles) {
3841
if (file.isFile) {
3942
if (file.extension.equals("yml", true)) {
4043
val filePath = file.absoluteFile.path
41-
try {
42-
executionResult = executeTest(filePath, dataFile = datafile, option)
43-
} catch (e: Exception) {
44-
printMessageInRedColor("Exception while execution test --> ${e.message}")
45-
}
46-
47-
if (executionResult == null) {
48-
continue
49-
}
50-
51-
if (executionResult.passed) {
52-
passedTestsCount++
44+
if (listOfFiles.isNotEmpty()) {
45+
executionResult = try {
46+
executeTest(filePath, datafileContentByEnvironment, option, projectConfig)
47+
} catch (e: Exception) {
48+
printMessageInRedColor("Exception while executing assertion -> ${e.message}")
49+
null
50+
}
51+
if (executionResult == null) {
52+
continue
53+
}
54+
55+
if (executionResult.passed) {
56+
passedTestsCount++
57+
} else {
58+
hasError = true
59+
failedTestsCount++
60+
}
61+
62+
passedAssertionsCount += executionResult.assertionsCount.passed
63+
failedAssertionsCount += executionResult.assertionsCount.failed
5364
} else {
54-
hasError = true
55-
failedTestsCount++
65+
printMessageInRedColor("The file is not valid yml file")
5666
}
57-
58-
passedAssertionsCount += executionResult.assertionsCount.passed
59-
failedAssertionsCount += executionResult.assertionsCount.failed
60-
} else {
61-
printMessageInRedColor("The file is not valid yml file")
6267
}
6368
}
6469
}
@@ -81,17 +86,23 @@ fun startTest(option: TestProjectOption) {
8186
} else {
8287
printMessageInRedColor("Directory is Empty or not exists")
8388
}
84-
}
89+
} ?: printNormalMessage("Root Project Path Not Found")
90+
8591

86-
private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjectOption): ExecutionResult? {
92+
private fun executeTest(
93+
filePath: String,
94+
datafileContentByEnvironment: MutableMap<String, DatafileContent>,
95+
option: TestProjectOption,
96+
configuration: Configuration
97+
): ExecutionResult? {
8798
val test = parseTestFeatureAssertions(filePath)
8899

89100
val executionResult = ExecutionResult(
90101
passed = true,
91102
assertionsCount = AssertionsCount(0, 0)
92103
)
93104

94-
if (test != null){
105+
if (test != null) {
95106
val key = when (test) {
96107
is Test.Feature -> test.value.key
97108
is Test.Segment -> test.value.key
@@ -105,14 +116,15 @@ private fun executeTest(filePath: String, dataFile: DataFile, option: TestProjec
105116
is Test.Feature -> {
106117
testFeature(
107118
testFeature = test.value,
108-
dataFile = dataFile,
119+
datafileContentByEnvironment = datafileContentByEnvironment,
109120
option = option
110121
)
111122
}
112123

113124
is Test.Segment -> {
114125
testSegment(
115126
testSegment = test.value,
127+
configuration = configuration,
116128
option = option
117129
)
118130
}

src/main/kotlin/com/featurevisor/testRunner/TestFeature.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import kotlinx.serialization.decodeFromString
99
import kotlinx.serialization.json.Json
1010
import kotlinx.serialization.json.JsonElement
1111

12-
fun testFeature(testFeature: TestFeature, dataFile: DataFile, option: TestProjectOption): TestResult {
12+
fun testFeature(
13+
testFeature: TestFeature,
14+
datafileContentByEnvironment:MutableMap<String, DatafileContent>,
15+
option: TestProjectOption
16+
): TestResult {
1317
val testStartTime = System.currentTimeMillis()
1418
val featureKey = testFeature.key
1519

@@ -40,15 +44,12 @@ fun testFeature(testFeature: TestFeature, dataFile: DataFile, option: TestProjec
4044
return@forEach
4145
}
4246

43-
val datafileContent = if (option.fast) {
44-
if (it.environment.equals("staging", true)) dataFile.stagingDataFiles else dataFile.productionDataFiles
45-
} else {
46-
getDataFileContent(
47+
val datafileContent = datafileContentByEnvironment[it.environment]
48+
?: getDataFileContent(
4749
featureName = testFeature.key,
4850
environment = it.environment,
49-
projectRootPath = option.projectRootPath
51+
projectRootPath = option.projectRootPath.orEmpty()
5052
)
51-
}
5253

5354
if (option.showDatafile) {
5455
printNormalMessage("")

src/main/kotlin/com/featurevisor/testRunner/TestSegment.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.featurevisor.testRunner
22

33
import com.featurevisor.sdk.segmentIsMatched
4-
import com.featurevisor.types.TestResult
5-
import com.featurevisor.types.TestResultAssertion
6-
import com.featurevisor.types.TestResultAssertionError
7-
import com.featurevisor.types.TestSegment
4+
import com.featurevisor.types.*
85

9-
fun testSegment(testSegment: TestSegment, option: TestProjectOption): TestResult {
6+
fun testSegment(testSegment: TestSegment,configuration: Configuration,option: TestProjectOption): TestResult {
107
val testStartTime = System.currentTimeMillis()
118
val segmentKey = testSegment.key
129

@@ -36,7 +33,7 @@ fun testSegment(testSegment: TestSegment, option: TestProjectOption): TestResult
3633
return@forEach
3734
}
3835

39-
val yamlSegment = parseYamlSegment("${option.projectRootPath}/segments/$segmentKey.yml")
36+
val yamlSegment = parseYamlSegment("${configuration.segmentsDirectoryPath}/$segmentKey.yml")
4037
val expected = it.expectedToMatch
4138
val actual = segmentIsMatched(yamlSegment!!, it.context)
4239
val passed = actual == expected

src/main/kotlin/com/featurevisor/testRunner/Utils.kt

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.featurevisor.testRunner
22

33
import com.featurevisor.sdk.FeaturevisorInstance
44
import com.featurevisor.sdk.InstanceOptions
5+
import com.featurevisor.sdk.emptyDatafile
56
import com.featurevisor.types.*
67
import kotlinx.serialization.decodeFromString
78
import kotlinx.serialization.json.Json
@@ -237,29 +238,14 @@ fun checkJsonIsEquals(a: String, b: String): Boolean {
237238
return map1 == map2
238239
}
239240

240-
fun buildDataFileForBothEnvironments(projectRootPath: String): DataFile =
241-
DataFile(
242-
stagingDataFiles = buildDataFileForStaging(projectRootPath),
243-
productionDataFiles = buildDataFileForProduction(projectRootPath)
244-
)
245-
246-
fun buildDataFileForStaging(projectRootPath: String) = try {
247-
getJsonForDataFile(environment = "staging", projectRootPath = projectRootPath)?.run {
248-
convertToDataClass<DatafileContent>()
249-
}
250-
} catch (e: Exception) {
251-
printMessageInRedColor("Unable to parse staging data file")
252-
null
253-
}
254241

255-
fun buildDataFileForProduction(projectRootPath: String) = try {
256-
getJsonForDataFile(environment = "production", projectRootPath = projectRootPath)?.run {
242+
fun buildDataFileAsPerEnvironment(projectRootPath: String,environment: String) = try {
243+
getJsonForDataFile(environment = environment, projectRootPath = projectRootPath)?.run {
257244
convertToDataClass<DatafileContent>()
258-
}
259-
245+
} ?: emptyDatafile
260246
} catch (e: Exception) {
261-
printMessageInRedColor("Unable to parse production data file")
262-
null
247+
printMessageInRedColor("Unable to parse data file")
248+
emptyDatafile
263249
}
264250

265251
fun getDataFileContent(featureName: String, environment: String, projectRootPath: String) =

src/main/kotlin/com/featurevisor/types/Types.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,21 @@ data class DataFile(
434434
val stagingDataFiles: DatafileContent? = null,
435435
val productionDataFiles: DatafileContent? = null
436436
)
437+
438+
@Serializable
439+
data class Configuration(
440+
val environments:List<String>,
441+
val tags: List<String>,
442+
val defaultBucketBy:String,
443+
val prettyState:Boolean,
444+
val prettyDatafile:Boolean,
445+
val stringify:Boolean,
446+
val featuresDirectoryPath:String,
447+
val segmentsDirectoryPath:String,
448+
val attributesDirectoryPath:String,
449+
val groupsDirectoryPath:String,
450+
val testsDirectoryPath:String,
451+
val stateDirectoryPath:String,
452+
val outputDirectoryPath:String,
453+
val siteExportDirectoryPath:String
454+
)

0 commit comments

Comments
 (0)