1
+ var fs = require ( 'fs' ) ;
2
+
3
+ var read_all_tests = function ( directory ) {
4
+ return fs . readdirSync ( directory ) . filter ( function ( element , index , array ) {
5
+ return element . indexOf ( ".js" ) != - 1 ;
6
+ } ) . map ( function ( element ) {
7
+ return directory + "/" + element ;
8
+ } )
9
+ }
10
+
11
+ var run_test = function ( url , file , number_of_times , concurrent , test_name , callback ) {
12
+ var final_results = { } ;
13
+ var number_of_test_to_run = 0 ;
14
+ // If we have not test_name set up
15
+ if ( typeof test_name == 'function' ) {
16
+ callback = test_name ;
17
+ test_name = null ;
18
+ }
19
+
20
+ // Load the file
21
+ var _module = require ( file ) ;
22
+ // No test set run all the available tests and store the results
23
+ if ( test_name == null ) {
24
+ // Execute the setup
25
+ var keys = Object . keys ( _module ) ;
26
+ var number_of_tests = keys . length ;
27
+ // Go over all the tests
28
+ for ( var i = 0 ; i < keys . length ; i ++ ) {
29
+ // Wrap the scope so we can execute it by itself
30
+ new function ( _key , _module_func ) {
31
+ run_single_test ( url , _key , _module_func , number_of_times , concurrent , function ( err , results ) {
32
+ // Final results
33
+ final_results [ _key ] = { results : results } ;
34
+ if ( err ) { final_results [ _key ] . err = err ; }
35
+ // Adjust number of tests left to run
36
+ number_of_tests = number_of_tests - 1 ;
37
+ if ( number_of_tests == 0 ) callback ( null , final_results )
38
+ } ) ;
39
+ } ( keys [ i ] , _module [ keys [ i ] ] )
40
+ }
41
+ } else {
42
+
43
+ }
44
+ }
45
+
46
+ var run_single_test = function ( url , func_name , func , number_of_times , concurrent , callback ) {
47
+ var results = [ ] ;
48
+ var test = func ( url ) ( ) ;
49
+
50
+ // Run the setup part first
51
+ test . setup ( function ( err , setup_result ) {
52
+ if ( err ) return callback ( err , null ) ;
53
+
54
+ if ( ! concurrent ) {
55
+ exceute_test_serially ( url , func_name , test , number_of_times , results , function ( err , test_results ) {
56
+ test . teardown ( function ( _t_err , _t_result ) {
57
+ if ( _t_err ) return callback ( _t_err , null ) ;
58
+ callback ( err , test_results )
59
+ } )
60
+ } ) ;
61
+ } else {
62
+ var number_left_to_run = number_of_times ;
63
+ // Execute all the tests
64
+ for ( var i = 0 ; i < number_of_times ; i ++ ) {
65
+ new function ( ) {
66
+ // Set start function
67
+ var start = new Date ( ) ;
68
+ // Execute function
69
+ test . test ( function ( err , result ) {
70
+ var end = new Date ( ) ;
71
+ var time = end . getTime ( ) - start . getTime ( ) ;
72
+ results . push ( { start : start , end : end , time : time } ) ;
73
+ // Adjust the number of tests to run
74
+ number_left_to_run = number_left_to_run - 1 ;
75
+ if ( number_left_to_run == 0 ) {
76
+ test . teardown ( function ( _t_err , _t_result ) {
77
+ if ( _t_err ) return callback ( _t_err , null ) ;
78
+ callback ( err , results )
79
+ } )
80
+ }
81
+ } ) ;
82
+ } ( ) ;
83
+ }
84
+ }
85
+ } )
86
+ }
87
+
88
+ var exceute_test_serially = function ( url , func_name , test , number_of_times , results , callback ) {
89
+ if ( number_of_times == 0 ) return callback ( null , results ) ;
90
+ number_of_times = number_of_times - 1 ;
91
+ // Set start function
92
+ var start = new Date ( ) ;
93
+ // Execute function
94
+ test . test ( function ( err , result ) {
95
+ var end = new Date ( ) ;
96
+ var time = end . getTime ( ) - start . getTime ( ) ;
97
+ results . push ( { start : start , end : end , time : time } ) ;
98
+ // Execute the next tick
99
+ process . nextTick ( function ( ) {
100
+ exceute_test_serially ( url , func_name , test , number_of_times - 1 , results , callback ) ;
101
+ } )
102
+ } ) ;
103
+ }
104
+
105
+
106
+ exports . read_all_tests = read_all_tests ;
107
+ exports . run_test = run_test ;
0 commit comments