|
| 1 | +import{signal}from'./utils'; |
| 2 | +import{createRunner}from'./create-runner'; |
| 3 | + |
| 4 | +interfaceResult{ |
| 5 | +msg:string; |
| 6 | +taskPosition:number; |
| 7 | +pass:boolean; |
| 8 | +change:number; |
| 9 | +timedOut?:boolean; |
| 10 | +}; |
| 11 | + |
| 12 | +interfaceConfig{ |
| 13 | +dir:string; |
| 14 | +taskPosition:number; |
| 15 | +} |
| 16 | + |
| 17 | +exportdefaultfunctionrunner(testFile,config:Config,handleResult,handleLog){ |
| 18 | + |
| 19 | +letrunner=createRunner(config,testFile); |
| 20 | +varfinal=null; |
| 21 | +letsignalMatch=newRegExp(signal); |
| 22 | + |
| 23 | +returnnewPromise((resolve,reject)=>{ |
| 24 | +runner.stdout.on('data',function(data):void{ |
| 25 | + |
| 26 | +data=data.toString(); |
| 27 | + |
| 28 | +// parse only final output data |
| 29 | +// let match = signalMatch.exec(data); // 0 |
| 30 | +// |
| 31 | +// if (!match) { |
| 32 | +// handleLog(data); |
| 33 | +// return; |
| 34 | +// } |
| 35 | +// |
| 36 | +// /* Result */ |
| 37 | +// // transform string result into object |
| 38 | +// let resultString = data.substring(match.index + signal.length); |
| 39 | +letresult=JSON.parse(JSON.stringify(data)); |
| 40 | +// // why parse twice? I don't know, but it works |
| 41 | +if(typeofresult==='string'){ |
| 42 | +result=JSON.parse(result); |
| 43 | +} |
| 44 | + |
| 45 | +letfinish=result.asserts[result.asserts.length-1]; |
| 46 | +letobj=getIndexAndTitle(finish.comment); |
| 47 | + |
| 48 | +if(result.stats.failures>0){ |
| 49 | +// fail: return first failure |
| 50 | +final={ |
| 51 | +msg:obj.msg, |
| 52 | +taskPosition:obj.index-1 |
| 53 | +}; |
| 54 | +}else{ |
| 55 | +// pass |
| 56 | +final={ |
| 57 | +msg:`Task${obj.index} Complete`, |
| 58 | +taskPosition:obj.index |
| 59 | +}; |
| 60 | +} |
| 61 | +final.change=final.taskPosition-config.taskPosition; |
| 62 | +final.pass=final.change>0; |
| 63 | +// // return result to atom-coderoad |
| 64 | +handleResult(final); |
| 65 | +}); |
| 66 | + |
| 67 | +runner.stderr.on('data',function(data){ |
| 68 | +console.log('test error',data.toString()); |
| 69 | +}); |
| 70 | + |
| 71 | +runner.on('close',function(code){ |
| 72 | +if(code===0){ |
| 73 | +resolve(final); |
| 74 | +}else{ |
| 75 | +resolve(final); |
| 76 | +} |
| 77 | +}); |
| 78 | +}); |
| 79 | +} |
| 80 | + |
| 81 | +functiongetIndexAndTitle(title:string):{index:number,msg:string}{ |
| 82 | +// tests prefixed with task number: "01 title" |
| 83 | +letindexString=title.match(/^[0-9]+/); |
| 84 | +if(!indexString){ |
| 85 | +throw'Tests should begin with a number, indicating the task number'; |
| 86 | +} |
| 87 | +return{ |
| 88 | +index:parseInt(indexString[0]), |
| 89 | +msg:title.slice(indexString[0].length+1) |
| 90 | +}; |
| 91 | +} |