Skip to content

Instantly share code, notes, and snippets.

@sonota88
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonota88/9274262 to your computer and use it in GitHub Desktop.
Save sonota88/9274262 to your computer and use it in GitHub Desktop.
// jrunscript/Rhino
/**
* @param writer java.io.Writer
*/
function FileWriter(writer){
this.writer = writer;
}
FileWriter.prototype.close = function(){
this.writer.close();
};
FileWriter.prototype.print = function(str){
this.writer.write(str);
};
FileWriter.prototype.puts = function(str){
this.print(str + "\n");
};
////////////////////////////////
/**
* @param reader java.io.Reader
*/
function FileReader(reader){
this.reader = reader;
}
FileReader.prototype.close = function(){
this.reader.close();
};
FileReader.prototype.eachLine = function(func){
var line;
var retVal;
while(true){
line = this.reader.readLine();
if(line === null){ break; }
retVal = func(line);
if(retVal === false){
break;
}
}
};
////////////////////////////////
function withFileOutputStream(file, func){
var fos;
try{
fos = new FileOutputStream(file);
func(fos);
}finally{
fos.close();
}
}
function withOutputStreamWriter(fos, func){
var osw;
try{
osw = new OutputStreamWriter(fos, "UTF-8");
func(osw);
}finally{
osw.close();
}
}
function withBufferedWriter(osw, func){
var bw;
try{
bw = new BufferedWriter(osw);
func(bw);
}finally{
bw.close();
}
}
function withFileInputStream(file, func){
var fis;
try{
fis = new FileInputStream(file);
func(fis);
}finally{
fis.close();
}
}
function withInputStreamReader(fis, func){
var isr;
try{
isr = new InputStreamReader(fis, "UTF-8");
func(isr);
}finally{
isr.close();
}
}
function withBufferedReader(isr, func){
var br;
try{
br = new BufferedReader(isr);
func(br);
}finally{
br.close();
}
}
function open(file, mode_enc, func){
mode_enc = mode_enc || "r";
if(mode_enc === "r"){
if(func){
withFileInputStream(file, function(fis){
withInputStreamReader(fis, function(isr){
withBufferedReader(isr, function(br){
var fr = new FileReader(br);
func(fr);
fr.close();
});
});
});
}else{
var fis = new FileInputStream(file);
var isr = new InputStreamReader(fis, "UTF-8");
var br = new BufferedReader(isr);
var fr = new FileReader(br);
return fr;
}
}else{
if(func){
withFileOutputStream(file, function(fos){
withOutputStreamWriter(fos, function(osw){
withBufferedWriter(osw, function(bw){
var fw = new FileWriter(bw);
func(fw);
fw.close();
});
});
});
}else{
var fos = new FileOutputStream(file);
var osw = new OutputStreamWriter(fos, "UTF-8");
var bw = new BufferedWriter(osw);
var fw = new FileWriter(bw);
return fw;
}
}
};
function readFile(path){
var text = "";
open(path, "r", function(f){
f.eachLine(function(line){
text += line + "\n";
});
});
return text;
}
function _parseInt(val){
return parseInt(val, 10);
}
////////////////////////////////
function getResult(path){
var errors = 0
, failures = 0
, timestamp
;
var testSuiteLines = [];
open(path, "r", function(f){
f.eachLine(function(line){
if(line.match(/<testsuite /)){
testSuiteLine.push(line);
}
});
});
testSuiteLines.forEach(function(line, i){
line.match( /errors="(\d+)"/ );
errors += parseInt(RegExp.$1, 10);
line.match( /failures="(\d+)"/ );
failures += parseInt(RegExp.$1, 10);
});
return {
errors: errors
, failures: failures
};
}
function updateOverviewSummary(reportHtmlDir, color){
var overviewSummary = readFile(reportHtmlDir + "/overview-summary.html");
open(reportHtmlDir + "/overview-summary.html", "w", function(f){
f.puts(overviewSummary.replace(
/<h1>.+<\/h1>/
, '<h1 style="background: ' + color + ';'
+ ' padding: 1rem; font-weight: bold; color: #fff;">'
+ 'Unit Test Results.<br />' + new Date()
+ '</h1>'
));
});
}
function main(reportDir, reportHtmlDir){
var result = getResult(reportDir + "/TESTS-TestSuites.xml");
var color;
if(result.errors === 0 && result.failures === 0){
color = "#228800";
}else{
color = "#dd0000";
}
updateOverviewSummary(reportHtmlDir, color);
}
main("test_report"
, "test_report/html"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment