|
| 1 | +/** |
| 2 | +* I am a CommandBox task runner which you can use to test your implementation of this exercise against the |
| 3 | +* provided test suite. To use me, open the CommandBox CLI and run this: |
| 4 | +* |
| 5 | +* CommandBox> task run TestRunner |
| 6 | +* |
| 7 | +* To start up a test watcher that will automatically rerun the test suite every time you save a file change, run this: |
| 8 | +* |
| 9 | +* CommandBox> task run TestRunner --watcher |
| 10 | +* |
| 11 | +*/ |
| 12 | +component { |
| 13 | + |
| 14 | + /** |
| 15 | + * @solution Runs the tests against the solution |
| 16 | + * @watcher Start up a file watch that re-runs the tests on file changes. Use Ctrl-C to stop |
| 17 | + */ |
| 18 | + function run( boolean solution=false, boolean watcher=false ) { |
| 19 | + |
| 20 | + ensureTestBox(); |
| 21 | + |
| 22 | + if( watcher ) { |
| 23 | + |
| 24 | + // Tabula rasa |
| 25 | + command( 'cls' ).run(); |
| 26 | + |
| 27 | + // Start watcher |
| 28 | + watch() |
| 29 | + .paths( '*.cfc' ) |
| 30 | + .inDirectory( getCWD() ) |
| 31 | + .withDelay( 500 ) |
| 32 | + .onChange( function() { |
| 33 | + |
| 34 | + // Clear the screen |
| 35 | + command( 'cls' ) |
| 36 | + .run(); |
| 37 | + |
| 38 | + // This is neccessary so changes to tests get picked up right away. |
| 39 | + pagePoolClear(); |
| 40 | + |
| 41 | + runTests( solution ); |
| 42 | + |
| 43 | + } ) |
| 44 | + .start(); |
| 45 | + |
| 46 | + } else { |
| 47 | + runTests( solution ); |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Make sure the TestBox framework is installed |
| 54 | + */ |
| 55 | + private function ensureTestBox() { |
| 56 | + var excerciseRoot = getCWD(); |
| 57 | + var testBoxRoot = excerciseRoot & '/testbox'; |
| 58 | + |
| 59 | + if( !directoryExists( testBoxRoot ) ) { |
| 60 | + |
| 61 | + print.boldYellowLine( 'Installing some missing dependencies for you!' ).toConsole(); |
| 62 | + command( 'install' ) |
| 63 | + .inWorkingDirectory( excerciseRoot ) |
| 64 | + .run(); |
| 65 | + } |
| 66 | + |
| 67 | + // Bootstrap TestBox framework |
| 68 | + filesystemUtil.createMapping( '/testbox', testBoxRoot ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Invoke TestBox to run the test suite |
| 73 | + */ |
| 74 | + private function runTests( boolean solution=false ) { |
| 75 | + |
| 76 | + // Create TestBox and run the tests |
| 77 | + testData = new testbox.system.TestBox() |
| 78 | + .runRaw( directory = { |
| 79 | + // Find all CFCs... |
| 80 | + mapping = filesystemUtil.makePathRelative( getCWD() ), |
| 81 | + // ... in this directory ... |
| 82 | + recurse = false, |
| 83 | + // ... whose name ends in "test" |
| 84 | + filter = function( path ) { |
| 85 | + return path.reFind( ( solution ? 'Solution' : '' ) & 'Test.cfc$' ); |
| 86 | + } |
| 87 | + } ) |
| 88 | + .getMemento(); |
| 89 | + |
| 90 | + // Print out the results with ANSI formatting for the CLI |
| 91 | + getInstance( 'CLIRenderer@testbox-commands' ) |
| 92 | + .render( print, testData, true ); |
| 93 | + |
| 94 | + print.toConsole(); |
| 95 | + |
| 96 | + // Set proper exit code |
| 97 | + if( testData.totalFail || testData.totalError ) { |
| 98 | + setExitCode( 1 ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
0 commit comments