-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntest.cfc
More file actions
161 lines (139 loc) · 4.73 KB
/
runtest.cfc
File metadata and controls
161 lines (139 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<cfcomponent output="false"><cfscript>
/**
* @Author Marcus Fernstrom
* @Copyright 2015
* @About Part of mScan
*/
/**
* @method runTests
* @public
* @param {any} form
* @return {any}
*/
public function runTests( form ) {
// If we have a path, we run tests
if( len( form._path ) > 0 ) {
// form.fieldnames contains the selected tests.
testList = '';
arrList = listToArray( form.fieldnames );
// Loop and create list of tests with proper names
for( item in arrList ){
if( item contains 'test_' ) {
testList = listAppend( testList, listFirst(listLast(item, '_'), '.') );
}
}
// If no file extension is specified, we default to .cfc and .cfm
if( len(form._fileExt) == 0 ){
console( 'No file extension specified, using default of .cfc, .cfm' );
form._fileExt = '.cfc,.cfm';
}
// If any tests have been selected, we do the thing.
console( 'Building list of files' );
if( listLen(testList) != 0 ){
// Instantiate variables
var fileOrDir = getFileInfo( form._path );
if ( fileOrDir.type == 'directory' ) {
console( 'Path is a directory' );
tempFileList = directoryList( form._path, true );
} else {
console( 'Path is a file' );
tempFileList = [ form._path ];
}
fileList = [];
checks = [];
session.results = {};
session.errorBreakdown = {};
threadList = '';
// Maketh thee into an array.
testList = listToArray( testList );
// Grab some info about each test.
for( item in testList ) {
tempObj = createObject( "component", "tests.#item#" );
tempStr = tempObj.info();
tempStr.file = item;
arrayAppend( checks, tempStr );
}
// Ensure only wanted files are on the list
console( 'Cleaning up list' );
if ( len(form._exclude) > 0 ){
theExcludes = listToArray(form._exclude, chr(10));
for ( item in tempFileList ) {
if ( form._fileExt contains listLast(item, '.') ) {
arrayAppend( fileList, item );
}
for ( item in theExcludes ) {
counter = 1;
for ( check in fileList ) {
if ( check contains trim(item) ) {
arrayDeleteAt( fileList, counter );
}
counter++;
}
}
}
} else {
for ( item in tempFileList ) {
if ( form._fileExt contains listLast(item, '.') ) {
arrayAppend( fileList, item );
}
}
}
arraySort( fileList, "textnocase" );
// Set up reference to the result helper component, needed due to threads
resultObj = createObject( "component", "utils.result" );
if( arrayLen( fileList ) > 0 ) {
// Loop each item in the filelist
for ( item in fileList ) {
// Threads are per file tested, not per test
threadList = listAppend( threadList, item );
console( 'Starting test for: ' & item );
// Run checks
try {
// Set up threads
thread name="#item#" checks = checks item = item resultObj = resultObj {
for ( check in checks ) {
////
//// Try dealing with memory usage issues.
//// The idea is simple, if memory usage is above the threshold then pause for 1 second, check again, rinse and repeat.
//// You can still run out of memory, but I found it to happen much more seldom with this check.
mf = CreateObject("java", "java.lang.management.ManagementFactory");
memBean = mf.getMemoryMXBean();
heapMem = memBean.getHeapMemoryUsage();
// Compare current memory with max allowed, if it's above the threshold then pause for a second.
while ( Round(heapMem.getUsed()/1024/1024) > (Round(heapMem.getMax()/1024/1024) * 0.7) ){
pause(1);
}
//// Seems to work. For the most part.
////
checkObj = createObject( "component", "tests.#check.file#" );
theFile = fileRead( item );
currentCheck = checkObj.run( theFile, item );
if( currentCheck._errors ) {
resultObj.joinTheData(
_itemName = listLast(item, '\'),
_short = check.short,
_fullPath = item,
_color = check.color,
_message = currentCheck._message
);
}
}
} // End of thread
} catch( any e ) {
console( 'Error in threads' );
console( e );
}
}
// Join the threads
console( 'Waiting for threads to finish..' );
thread action = "join" name = "#threadList#";
console( 'Threads have finished' );
}
}
}
return {
fileList: fileList,
testList: testList
};
}
</cfscript></cfcomponent>