Skip to content

Commit fa70efa

Browse files
authored
Merge pull request #5 from bdw429s/improve-test-performance
New method of running
2 parents b0a7d7b + d1f0989 commit fa70efa

File tree

4 files changed

+67
-58
lines changed

4 files changed

+67
-58
lines changed

CodewarsReporter.cfc

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@
55
component {
66

77
/**
8-
* @print a print buffer to use
98
* @testData test results from TestBox
109
*/
11-
function render( print, testData ){
10+
function render( testData ){
1211
for ( thisBundle in testData.bundleStats ) {
1312
// Check if the bundle threw a global exception
1413
if ( !isSimpleValue( thisBundle.globalException ) ) {
1514
var message = escapeLF(
1615
"#thisBundle.globalException.type#:#thisBundle.globalException.message#:#thisBundle.globalException.detail#"
1716
);
18-
print.line( prependLF( "<ERROR::>#message#" ) );
17+
printLine( prependLF( "<ERROR::>#message#" ) );
1918

2019
// ACF has an array for the stack trace
2120
if ( isSimpleValue( thisBundle.globalException.stacktrace ) ) {
22-
print.line( prependLF( "<LOG::-Stacktrace>#escapeLF( thisBundle.globalException.stacktrace )#" ) );
21+
printLine( prependLF( "<LOG::-Stacktrace>#escapeLF( thisBundle.globalException.stacktrace )#" ) );
2322
}
2423
}
2524

2625
var debugMap = prepareDebugBuffer( thisBundle.debugBuffer );
2726

2827
// Generate reports for each suite
2928
for ( var suiteStats in thisBundle.suiteStats ) {
30-
genSuiteReport( suiteStats = suiteStats, bundleStats = thisBundle, print = print, debugMap = debugMap );
29+
genSuiteReport( suiteStats = suiteStats, bundleStats = thisBundle, debugMap = debugMap );
3130
}
3231
}
3332
}
@@ -36,28 +35,27 @@ component {
3635
* Recursive Output for suites
3736
* @suiteStats Suite stats
3837
* @bundleStats Bundle stats
39-
* @print The print Buffer
4038
*/
41-
function genSuiteReport( required suiteStats, required bundleStats, required print, debugMap={}, labelPrefix='' ){
39+
function genSuiteReport( required suiteStats, required bundleStats, debugMap={}, labelPrefix='' ){
4240
labelPrefix &= '/' & arguments.suiteStats.name;
43-
print.line( prependLF( "<DESCRIBE::>#arguments.suiteStats.name#" ) );
41+
printLine( prependLF( "<DESCRIBE::>#arguments.suiteStats.name#" ) );
4442

4543
for ( local.thisSpec in arguments.suiteStats.specStats ) {
4644
var thisSpecLabel = labelPrefix & '/' & local.thisSpec.name;
47-
print.line( prependLF( "<IT::>#local.thisSpec.name#" ) );
45+
printLine( prependLF( "<IT::>#local.thisSpec.name#" ) );
4846

4947
if( debugMap.keyExists( thisSpecLabel ) ) {
50-
print.line( debugMap[ thisSpecLabel ] )
48+
printLine( debugMap[ thisSpecLabel ] )
5149
}
5250

5351
if ( local.thisSpec.status == "passed" ) {
54-
print.line( prependLF( "<PASSED::>Test Passed" ) );
52+
printLine( prependLF( "<PASSED::>Test Passed" ) );
5553
} else if ( local.thisSpec.status == "failed" ) {
56-
print.line( prependLF( "<FAILED::>#escapeLF( local.thisSpec.failMessage )#" ) );
54+
printLine( prependLF( "<FAILED::>#escapeLF( local.thisSpec.failMessage )#" ) );
5755
} else if ( local.thisSpec.status == "skipped" ) {
58-
print.line( prependLF( "<FAILED::>Test Skipped" ) );
56+
printLine( prependLF( "<FAILED::>Test Skipped" ) );
5957
} else if ( local.thisSpec.status == "error" ) {
60-
print.line( prependLF( "<ERROR::>#escapeLF( local.thisSpec.error.message )#" ) );
58+
printLine( prependLF( "<ERROR::>#escapeLF( local.thisSpec.error.message )#" ) );
6159

6260
var errorStack = [];
6361
// If there's a tag context, show the file name and line number where the error occurred
@@ -78,23 +76,23 @@ component {
7876
return "at #item.template#:#item.line#";
7977
} )
8078
.toList( "<:LF:>" );
81-
print.line( prependLF( "<LOG::-Stacktrace>#stacktrace#" ) );
79+
printLine( prependLF( "<LOG::-Stacktrace>#stacktrace#" ) );
8280
}
8381
} else {
84-
print.line( prependLF( "<ERROR::>Unknown test status: #local.thisSpec.status#" ) );
82+
printLine( prependLF( "<ERROR::>Unknown test status: #local.thisSpec.status#" ) );
8583
}
8684

87-
print.line( prependLF( "<COMPLETEDIN::>#local.thisSpec.totalDuration#" ) );
85+
printLine( prependLF( "<COMPLETEDIN::>#local.thisSpec.totalDuration#" ) );
8886
}
8987

9088
// Handle nested Suites
9189
if ( arguments.suiteStats.suiteStats.len() ) {
9290
for ( local.nestedSuite in arguments.suiteStats.suiteStats ) {
93-
genSuiteReport( local.nestedSuite, arguments.bundleStats, print, debugMap, labelPrefix )
91+
genSuiteReport( local.nestedSuite, arguments.bundleStats, debugMap, labelPrefix )
9492
}
9593
}
9694

97-
print.line( prependLF( "<COMPLETEDIN::>#arguments.suiteStats.totalDuration#" ) );
95+
printLine( prependLF( "<COMPLETEDIN::>#arguments.suiteStats.totalDuration#" ) );
9896
}
9997

10098
private function escapeLF( required text ){
@@ -115,4 +113,8 @@ component {
115113

116114
}
117115

116+
private function printLine( string str ) {
117+
systemoutput( str, 1 );
118+
}
119+
118120
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## testbox-codewars
22

33
- `CodewarsReporter.cfc`: Custom reporter for TestBox to produce Codewars format
4-
- `TestRunner.cfc`: Runs TestBox tests and produces output for Codewars
4+
- `TestRunner.cfm`: Runs TestBox tests and produces output for Codewars
55
- `CodewarsBaseSpec.cfc`: Base tests for CFML test bundles that captures spec-level debugging output
66

77
### Usage
@@ -21,5 +21,5 @@ To test locally, create a `Solution.cfc` and `SolutionTest.cfc` in the root dir.
2121
Run the task runner with the following command:
2222

2323
```bash
24-
box task run TestRunner
24+
box -clishellpath=/path/to/TestRunner.cfm
2525
```

TestRunner.cfc

Lines changed: 0 additions & 37 deletions
This file was deleted.

TestRunner.cfm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<cfsilent>
2+
<!---
3+
Runs TestBox test and outputs in Codewars format.
4+
To use, run:
5+
box -clishellpath=/path/to/TestRunner.cfm
6+
--->
7+
<cfscript>
8+
// Bootstrap TestBox framework
9+
currentDir = getDirectoryFrompath( getCurrentTemplatePath());
10+
createMapping( '/testbox', currentDir & 'testbox' );
11+
createMapping( '/codewarsRoot', currentDir );
12+
13+
// Create TestBox and run the tests
14+
testData = new testbox.system.TestBox( options={ coverage : { enabled : false } } )
15+
.runRaw(
16+
directory = {
17+
// Find all CFCs in this directory that ends with Test.
18+
mapping : '/codewarsRoot',
19+
recurse : false,
20+
filter = function( path ){
21+
return path.reFindNoCase( "Test\.cfc$" );
22+
}
23+
}
24+
)
25+
.getMemento( includeDebugBuffer=true );
26+
27+
new CodewarsReporter().render( testData );
28+
29+
// Set exit code to 1 on failure
30+
if ( testData.totalFail || testData.totalError ) {
31+
createObject( 'java', 'java.lang.System' ).setProperty( 'cfml.cli.exitCode', 1 );
32+
}
33+
34+
35+
function createMapping( mappingName, mappingPath ) {
36+
var mappings = getApplicationSettings().mappings;
37+
if( !structKeyExists( mappings, mappingName ) || mappings[ mappingName ] != mappingPath ) {
38+
mappings[ mappingName ] = mappingPath;
39+
application action='update' mappings='#mappings#';
40+
}
41+
}
42+
43+
44+
</cfscript></cfsilent>

0 commit comments

Comments
 (0)