-
Notifications
You must be signed in to change notification settings - Fork 0
/
runTestForC.js
146 lines (126 loc) · 4.37 KB
/
runTestForC.js
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
var fs = require('fs');
var argv = process.argv.slice(2);
var child_process = require('child_process');
var chalk = require('chalk');
var errorColor = chalk.bold.red;
var output = chalk.bold.green;
var testfile;
function printUsage() {
var usage = [
'Usage :',
'node runTestForC.js test_file.c dependency_file.c -w==> runs all tests',
'node runTestForC.js test_file.c dependency_file.c -w -list ==> lists all tests',
'node runTestForC.js test_file.c dependency_file.c -w -stop ==> stops on first failure',
'node runTestForC.js test_file.c dependency_file.c -w -only namePart ==> runs all tests that match the namePart',
'-w is optional to avoid compiler warning'
];
console.log(usage.join('\t\n'));
}
function isOption (arg){return (arg[0] =='-'&&arg.length>2);};
function isGccCommand (arg){return (arg[0] =='-'&&arg.length==2);};
function isFile(argv){
return argv.join(' ').match(/\b\w+\.\w+/g);
};
function readFile(fileName) {
try {
return fs.readFileSync('./' + fileName, 'utf-8');
} catch (e) {
console.log(e.message);
}
};
function extractTests(fileContent) {
var tests = fileContent.match(/(\btest_\w+)/g);
if(tests == null) return [];
return tests.map(function(test) {
return test + "\(\);";
});
};
function printFormattedErr(stderr,err) {
process.stdout.write(errorColor(stderr));
err&&console.log(errorColor(err));
}
function printResult(test, allTests, summary,dependency,stop) {
return function(err, stdout, stderr) {
printTestName(test);
if (stdout) console.log(stdout);
if (err || stderr) summary.failed++,printFormattedErr(stderr,err);
else summary.passed++
console.log('--------------');
runAllTests(allTests, summary,dependency,stop);
}
}
function createFile(test) {
var sample = ["#include <stdio.h>",
"#include \"" + testfile + "\"",
"int main(void) {"
];
return sample.join('\n') + test + 'return 0;}';
}
function printTestName(test) {
console.log('===>', test.substr(0, test.length - 3));
}
function listTestNames(tests) {
console.log("loading tests from " + testfile + "\n--------------");
tests.forEach(printTestName);
}
function printTestCounts(summary) {
console.log('Passed/Total :\t', (summary.passed) + '/' + (summary.passed+summary.failed));
if(fs.existsSync('all_test_c_'))
child_process.execSync('rm all_test_c_ test_runner_file_.c');
};
function runAllTests(tests, summary,dependency,stop) {
if ((tests.length==0)||(stop&&summary.failed)) {
printTestCounts(summary);
return;
}
var test = tests.shift();
var mainFile = createFile(test,testfile);
fs.writeFileSync('test_runner_file_.c', mainFile);
var command = 'gcc -o all_test_c_ test_runner_file_.c ';
if(dependency) command += dependency;
try{
child_process.execSync(command);
child_process.exec('./all_test_c_', printResult(test, tests, summary,dependency,stop));
}catch(e){ console.log(e.message)};
};
function matchedTest(option){
return function(test){
return test.match(option);
};
};
function optionManager(tests,option,dependency){
var summary = {failed: 0,passed:0 };
if(option == '-list')
listTestNames(tests);
if(option == '-help')
printUsage();
if(option == '-stop'){
runAllTests(tests,summary,dependency,true);
};
if(option.length>1&&option[0]=='-only'){
tests = tests.filter(matchedTest(option[1]));
runAllTests(tests,summary,dependency);
}
}
function main() {
var files = isFile(argv)||[];
var gccCommand = argv.filter(isGccCommand).join('');
var gccCommandIndex = argv.indexOf(gccCommand)>=0?1:0;
var lastFileIndex = argv.indexOf(files[files.length-1]);
var option = argv.slice(lastFileIndex+gccCommandIndex+1);
testfile = files[0];
var dependency = files.slice(1).join(' ') +' '+gccCommand;
if (testfile) {
var fileContent = readFile(testfile);
var tests = extractTests(fileContent);
var summary = {failed: 0,passed:0 };
if (option.length>0)
optionManager(tests,option,dependency);
else{
console.log("loading tests from " + testfile + "\n--------------");
runAllTests(tests, summary,dependency);
};
} else
printUsage();
};
main();