1
- // @file:JvmName("TestExecuter")
2
1
package com.featurevisor.testRunner
3
2
4
3
import com.featurevisor.types.*
5
4
import java.io.File
6
5
7
- fun main (args : Array <String >) {
8
- when (args.size) {
9
- 0 -> {
10
- startTest()
11
- }
12
-
13
- 1 -> {
14
- val rootPathInParam = args[0 ]
15
- startTest(rootPathInParam)
16
- }
17
-
18
- else -> {
19
- val rootPathInParam = args[0 ]
20
- val testDirInParam = args[1 ]
21
- startTest(rootPathInParam, testDirInParam)
22
- }
23
- }
24
- }
25
-
26
- fun startTest (projectRootPath : String = "", testDirPath : String = "") {
27
- val rootPath = projectRootPath.ifEmpty {
28
- getRootProjectDir()
29
- }
30
- val testDir = testDirPath.ifEmpty {
31
- " tests"
32
- }
33
- getAllFilesInDirectory(rootPath, testDir)
34
- }
35
-
36
- internal fun getAllFilesInDirectory (projectRootPath : String , testDirPath : String ) {
37
- val folder = File (" $projectRootPath /$testDirPath " )
6
+ data class TestProjectOption (
7
+ val keyPattern : String = " " ,
8
+ val assertionPattern : String = " " ,
9
+ val verbose : Boolean = false ,
10
+ val showDatafile : Boolean = false ,
11
+ val onlyFailures : Boolean = false ,
12
+ val fast : Boolean = false ,
13
+ val testDirPath : String = " tests" ,
14
+ val projectRootPath : String = getRootProjectDir()
15
+ )
16
+
17
+ fun startTest (option : TestProjectOption ) {
18
+ var hasError = false
19
+ val folder = File (" ${option.projectRootPath} /${option.testDirPath} " )
38
20
val listOfFiles = folder.listFiles()
39
21
var executionResult: ExecutionResult ? = null
40
-
22
+ val startTime = System .currentTimeMillis()
41
23
var passedTestsCount = 0
42
24
var failedTestsCount = 0
43
-
44
25
var passedAssertionsCount = 0
45
26
var failedAssertionsCount = 0
46
27
47
28
if (! listOfFiles.isNullOrEmpty()) {
29
+ val datafile =
30
+ if (option.fast) buildDataFileForBothEnvironments(projectRootPath = option.projectRootPath) else DataFile (
31
+ null ,
32
+ null
33
+ )
34
+ if (option.fast && (datafile.stagingDataFiles == null || datafile.productionDataFiles == null )) {
35
+ return
36
+ }
48
37
for (file in listOfFiles) {
49
38
if (file.isFile) {
50
39
if (file.extension.equals(" yml" , true )) {
51
40
val filePath = file.absoluteFile.path
52
41
try {
53
- executionResult = testAssertion (filePath, projectRootPath )
42
+ executionResult = executeTest (filePath, dataFile = datafile, option )
54
43
} catch (e: Exception ) {
55
44
printMessageInRedColor(" Exception in $filePath --> ${e.message} " )
56
45
}
57
46
47
+ if (executionResult == null ) {
48
+ return
49
+ }
58
50
59
- if (executionResult? .passed == true ) {
51
+ if (executionResult.passed) {
60
52
passedTestsCount++
61
53
} else {
54
+ hasError = true
62
55
failedTestsCount++
63
56
}
64
57
65
- passedAssertionsCount + = executionResult?.assertionsCount?.passed ? : 0
66
- failedAssertionsCount + = executionResult?.assertionsCount?.failed ? : 0
67
-
58
+ passedAssertionsCount + = executionResult.assertionsCount.passed
59
+ failedAssertionsCount + = executionResult.assertionsCount.failed
68
60
} else {
69
61
printMessageInRedColor(" The file is not valid yml file" )
70
62
}
71
63
}
72
64
}
73
- printMessageInGreenColor(" Test specs: $passedTestsCount passed, $failedTestsCount failed" )
74
- printMessageInGreenColor(" Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed" )
75
- } else {
76
- printMessageInRedColor(" Directory is Empty or not exists" )
77
- }
78
- }
79
-
80
- fun testSingleFeature (featureKey : String , projectRootPath : String = "", testDirPath : String = "") {
81
- val rootPath = projectRootPath.ifEmpty { getRootProjectDir() }
82
- val testDir = testDirPath.ifEmpty { " tests" }
83
65
84
- val test = parseTestFeatureAssertions(" $rootPath /$testDir /$featureKey .feature.yml" )
85
-
86
- test?.let {
87
- val executionResult = ExecutionResult (
88
- passed = false ,
89
- assertionsCount = AssertionsCount (0 , 0 )
90
- )
66
+ val endTime = System .currentTimeMillis() - startTime
91
67
92
- val testResult = testFeature(testFeature = (test as Test .Feature ).value, projectRootPath)
93
-
94
- printTestResult(testResult)
95
-
96
- if (! testResult.passed) {
97
- executionResult.passed = false
98
-
99
- executionResult.assertionsCount.failed = testResult.assertions.count { ! it.passed }
100
- executionResult.assertionsCount.passed + = testResult.assertions.size - executionResult.assertionsCount.failed
101
- } else {
102
- executionResult.assertionsCount.passed = testResult.assertions.size
68
+ if (! option.onlyFailures || hasError) {
69
+ printNormalMessage(" \n ----" )
103
70
}
71
+ printNormalMessage(" " )
104
72
105
- printMessageInGreenColor(" Test Assertion: ${executionResult.assertionsCount.passed} passed, ${executionResult.assertionsCount.failed} failed" )
106
- }
107
-
108
- }
109
-
110
- fun testSingleSegment (segmentKey : String , projectRootPath : String = "", testDirPath : String = "") {
111
-
112
- val rootPath = projectRootPath.ifEmpty { getRootProjectDir() }
113
- val testDir = testDirPath.ifEmpty { " tests" }
114
-
115
- val test = parseTestFeatureAssertions(" $rootPath /$testDir /$segmentKey .segment.yml" )
116
-
117
- test?.let {
118
- val executionResult = ExecutionResult (
119
- passed = false ,
120
- assertionsCount = AssertionsCount (0 , 0 )
121
- )
122
-
123
- val testResult = testSegment(test = (test as Test .Segment ).value, projectRootPath)
124
-
125
- printTestResult(testResult)
126
-
127
- if (! testResult.passed) {
128
- executionResult.passed = false
129
-
130
- executionResult.assertionsCount.failed = testResult.assertions.count { ! it.passed }
131
- executionResult.assertionsCount.passed + = testResult.assertions.size - executionResult.assertionsCount.failed
73
+ if (hasError) {
74
+ printMessageInRedColor(" \n\n Test specs: $passedTestsCount passed, $failedTestsCount failed" )
75
+ printMessageInRedColor(" Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed" )
132
76
} else {
133
- executionResult.assertionsCount.passed = testResult.assertions.size
77
+ printMessageInGreenColor(" \n\n Test specs: $passedTestsCount passed, $failedTestsCount failed" )
78
+ printMessageInGreenColor(" Test Assertion: $passedAssertionsCount passed, $failedAssertionsCount failed" )
134
79
}
135
-
136
- printMessageInGreenColor( " Test Assertion: ${executionResult.assertionsCount.passed} passed, ${executionResult.assertionsCount.failed} failed " )
137
-
80
+ printBoldMessage( " Time: ${prettyDuration(endTime)} " )
81
+ } else {
82
+ printMessageInRedColor( " Directory is Empty or not exists " )
138
83
}
139
84
}
140
85
141
- private fun testAssertion (filePath : String , projectRootPath : String ): ExecutionResult {
86
+ private fun executeTest (filePath : String , dataFile : DataFile , option : TestProjectOption ): ExecutionResult {
142
87
val test = parseTestFeatureAssertions(filePath)
143
88
144
89
val executionResult = ExecutionResult (
@@ -147,17 +92,32 @@ private fun testAssertion(filePath: String, projectRootPath: String): ExecutionR
147
92
)
148
93
149
94
test?.let {
95
+ val key = when (test) {
96
+ is Test .Feature -> test.value.key
97
+ is Test .Segment -> test.value.key
98
+ }
99
+
100
+ if (option.keyPattern.isNotEmpty() && ! key.contains(option.keyPattern)) {
101
+ return @let
102
+ }
103
+
150
104
val testResult: TestResult = when (test) {
151
105
is Test .Feature -> {
152
- testFeature(test.value, projectRootPath )
106
+ testFeature(test.value, dataFile = dataFile, option )
153
107
}
154
108
155
109
is Test .Segment -> {
156
- testSegment(test.value, projectRootPath)
110
+ testSegment(test.value, option. projectRootPath)
157
111
}
158
112
}
159
113
160
- printTestResult(testResult)
114
+ if (! option.onlyFailures) {
115
+ printTestResult(testResult)
116
+ } else {
117
+ if (! testResult.passed) {
118
+ printTestResult(testResult)
119
+ }
120
+ }
161
121
162
122
if (! testResult.passed) {
163
123
executionResult.passed = false
0 commit comments