Esempio n. 1
0
YUITest.Util.mix(YUITest.CLI, {

    files: [],

    options: {
        verbose: false,
        webcompat: false,
        help: false,
        format: "xunit"
    },
    
    outputHelp: function(){
        this.print([
            "\nUsage: yuitest [options] [file|dir]*",
            " ",
            "Global Options",
            "  --groups groupname  Run only tests cases that are part of groupname.",
            "  --help              Displays this information.",
            "  --format <format>   Specifies output format (junitxml, tap, xunit).",
            "  --verbose           Display informational messages and warnings.",
            "  --webcompat         Load tests designed for use in browsers."   
        ].join("\n") + "\n\n");
    },
    
    processArguments: function(){

        var args    = this.args,  
            arg     = args.shift(), 
            files   = [];  
            
        while(arg){
            if (arg.indexOf("--") == 0){
                this.options[arg.substring(2)] = true;
                
                //get the next argument
                if (arg == "--groups" || arg == "--format"){
                    this.options[arg.substring(2)] = args.shift();
                }
            } else {
                
                //see if it's a directory or a file
                if (this.isDirectory(arg)){
                    files = files.concat(this.getFiles(arg));
                } else {
                    files.push(arg);
                }
            }
            arg = args.shift();
        }

        if (this.options.help || files.length === 0){
            this.outputHelp();
            this.quit(0);
        }

        this.files = files;
        
        //-----------------------------------------------------------------------------
        // Determine output format
        //-----------------------------------------------------------------------------

        switch(this.options.format){
            case "junitxml":
                if (this.options.verbose){
                    this.warn("[INFO] Using JUnitXML output format.\n");
                }
                YUITest.CLI.Format(YUITest.TestFormat.JUnitXML);
                break;
            case "tap":
                if (this.options.verbose){
                    this.warn("[INFO] Using TAP output format.\n");
                }
                YUITest.CLI.Format(YUITest.TestFormat.TAP);
                break;
            default:
                if (this.options.verbose){
                    this.warn("[INFO] Using XUnit output format.\n");
                }
                YUITest.CLI.XUnit();
        }    
    },
    
    start: function(){
    
        this.processArguments();
        this.processFiles();
        
        YUITest.TestRunner.run({
            groups: this.options.groups ? this.options.groups.split(",") : null
        });    
    }        
});
Esempio n. 2
0
var fs=require("fs"),path=require("path"),exists=fs.existsSync||path.existsSync,vm=null,YUITest=require("yuitest"),stdout=process.stdout,stderr=process.stderr||stdout;if(process.binding("natives").vm){vm=require("vm");}process.on("exit",function(){var a=YUITest.TestRunner.getResults();if(a&&a.failed){YUITest.CLI.quit(1);}});YUITest.CLI={args:process.argv.slice(2),print:function(a){fs.writeSync(1,a);},println:function(a){this.print(a+"\n");},warn:function(a){console.warn(a);},quit:function(a){process.exit(a||0);},isDirectory:function(a){if(exists(a)){var b=fs.statSync(a);return b.isDirectory();}else{this.warn("File Not Found: "+a);this.quit(1);}},getFiles:function(a){var c=[];try{fs.statSync(a);}catch(b){return[];}function d(f,e){e.push(f);fs.readdirSync(e.join("/")).forEach(function(g){var i=e.concat([g]).join("/"),h=fs.statSync(i);if(g[0]=="."){return;}else{if(h.isFile()&&/\.js$/.test(g)){c.push(i);}else{if(h.isDirectory()){d(g,e);}}}});e.pop();}d(a,[]);return c;},getFullPath:function(a){return path.resolve(process.cwd(),a);},readFile:function(a){return fs.readFileSync(a,"utf-8");},processFiles:function(){var f=this.files,e,g,d,a,b;if(f.length){for(d=0,a=f.length;d<a;d++){if(this.options.verbose){this.warn("[INFO] Loading "+f[d]+"\n");}if(this.options.webcompat){b=fs.readFileSync(f[d]);if(vm){vm.runInThisContext("(function(YUITest){\n"+b+"\n})",f[d])(YUITest);}else{process.compile("(function(YUITest){\n"+b+"\n})",f[d])(YUITest);}}else{try{require(path.resolve(process.cwd(),f[d]));}catch(c){this.warn("[ERROR] "+c.stack);this.warn("\n[ERROR] No tests loaded from "+f[d]+". If you're not using CommonJS module format, try running with --webcompat option.\n");this.quit(1);}}}}else{this.warn("[ERROR] No tests to run, exiting.\n");this.quit(1);}}};YUITest.CLI.Logger=function(){var c=YUITest.TestRunner,a=YUITest.CLI;function b(e){var d="";switch(e.type){case c.BEGIN_EVENT:d="Testing began at "+(new Date()).toString()+".";messageType="info";break;case c.COMPLETE_EVENT:d="Testing completed at "+(new Date()).toString()+".\n"+"Passed:"+e.results.passed+" Failed:"+e.results.failed+" Total:"+e.results.total+"("+e.results.ignored+" ignored)";messageType="info";break;case c.TEST_FAIL_EVENT:d=e.testName+": failed.\n"+e.error.getMessage();messageType="fail";break;case c.ERROR_EVENT:d=e.methodName+": error.\n"+e.error.message;messageType="error";break;case c.TEST_IGNORE_EVENT:d=e.testName+": ignored.";messageType="ignore";break;case c.TEST_PASS_EVENT:d=e.testName+": passed.";messageType="pass";break;case c.TEST_SUITE_BEGIN_EVENT:d='Test suite "'+e.testSuite.name+'" started.';messageType="info";break;case c.TEST_SUITE_COMPLETE_EVENT:d="Testing completed at "+(new Date()).toString()+".\n"+"Passed:"+e.results.passed+" Failed:"+e.results.failed+" Total:"+e.results.total+"("+e.results.ignored+" ignored)";messageType="info";break;case c.TEST_CASE_BEGIN_EVENT:d='Test case "'+e.testCase.name+'" started.';messageType="info";break;case c.TEST_CASE_COMPLETE_EVENT:d="Testing completed at "+(new Date()).toString()+".\n"+"Passed:"+e.results.passed+" Failed:"+e.results.failed+" Total:"+e.results.total+"("+e.results.ignored+" ignored)";messageType="info";break;default:d="Unexpected event "+e.type;messageType="info";}a.print(d+"\n");}c.subscribe(c.BEGIN_EVENT,b);c.subscribe(c.TEST_FAIL_EVENT,b);c.subscribe(c.TEST_PASS_EVENT,b);c.subscribe(c.TEST_IGNORE_EVENT,b);c.subscribe(c.TEST_CASE_BEGIN_EVENT,b);c.subscribe(c.TEST_CASE_COMPLETE_EVENT,b);c.subscribe(c.TEST_SUITE_BEGIN_EVENT,b);c.subscribe(c.TEST_SUITE_COMPLETE_EVENT,b);c.subscribe(c.COMPLETE_EVENT,b);};YUITest.CLI.XUnit=function(){var f=YUITest.TestRunner,b=YUITest.CLI,g=[],e=[],a=[];function d(k){if(k){var l=k.split("\n"),j=[],m,h;for(m=1,h=l.length;m<h;m++){if(l[m].indexOf("yuitest-node")>-1){break;}else{j.push(l[m]);}}return j.join("\n");}else{return"Unavailable.";}}function c(m){var l="",k=m.results,j,h;switch(m.type){case f.BEGIN_EVENT:l="YUITest@"+YUITest.version+"\n";if(f._groups){l+="Filtering on groups '"+f._groups.slice(1,-1)+"'\n";}break;case f.COMPLETE_EVENT:l="\nTotal tests: "+k.total+", Failures: "+k.failed+", Skipped: "+k.ignored+", Time: "+(k.duration/1000)+" seconds\n";if(e.length){l+="\nTests failed:\n";for(j=0,h=e.length;j<h;j++){l+="\n"+(j+1)+") "+e[j].name+" : "+e[j].error.getMessage()+"\n";if(e[j].error.stack){l+="Stack trace:\n"+d(e[j].error.stack)+"\n";}}l+="\n";}if(g.length){l+="\nErrors:\n";for(j=0,h=g.length;j<h;j++){l+="\n"+(j+1)+") "+g[j].name+" : "+g[j].error.message+"\n";if(g[j].error.stack){l+="Stack trace:\n"+d(g[j].error.stack)+"\n";}}l+="\n";}l+="\n\n";break;case f.TEST_FAIL_EVENT:l="F";e.push({name:a.concat([m.testName]).join(" > "),error:m.error});break;case f.ERROR_EVENT:g.push({name:a.concat([m.methodName]).join(" > "),error:m.error});break;case f.TEST_IGNORE_EVENT:l="S";break;case f.TEST_PASS_EVENT:l=".";break;case f.TEST_SUITE_BEGIN_EVENT:a.push(m.testSuite.name);break;case f.TEST_CASE_COMPLETE_EVENT:case f.TEST_SUITE_COMPLETE_EVENT:a.pop();break;case f.TEST_CASE_BEGIN_EVENT:a.push(m.testCase.name);break;}b.print(l);}f.subscribe(f.BEGIN_EVENT,c);f.subscribe(f.TEST_FAIL_EVENT,c);f.subscribe(f.TEST_PASS_EVENT,c);f.subscribe(f.ERROR_EVENT,c);f.subscribe(f.TEST_IGNORE_EVENT,c);f.subscribe(f.TEST_CASE_BEGIN_EVENT,c);f.subscribe(f.TEST_CASE_COMPLETE_EVENT,c);f.subscribe(f.TEST_SUITE_BEGIN_EVENT,c);f.subscribe(f.TEST_SUITE_COMPLETE_EVENT,c);f.subscribe(f.COMPLETE_EVENT,c);};YUITest.CLI.Format=function(d){var c=YUITest.TestRunner,a=YUITest.CLI;function b(f){var e=f.results;a.print(d(e));}c.subscribe(c.COMPLETE_EVENT,b);};YUITest.Util.mix(YUITest.CLI,{files:[],options:{verbose:false,webcompat:false,help:false,version:false,format:"xunit"},outputVersion:function(){this.print(YUITest.version+"\n");},outputHelp:function(){this.print(["\nUsage: yuitest [options] [file|dir]*"," ","Global Options","  --groups groupname  Run only tests cases that are part of groupname.","  --help, -h          Displays this information.","  --version, -v       Displays the current version.","  --format <format>   Specifies output format (junitxml, tap, xunit).","  --verbose           Display informational messages and warnings.","  --webcompat         Load tests designed for use in browsers."].join("\n")+"\n\n");
},processArguments:function(){var b=this.args,a=b.shift(),c=[];while(a){if(a.indexOf("--")==0){this.options[a.substring(2)]=true;if(a=="--groups"||a=="--format"){this.options[a.substring(2)]=b.shift();}}else{switch(a){case"-h":this.options.help=true;break;case"-v":this.options.version=true;break;default:if(this.isDirectory(a)){c=c.concat(this.getFiles(a));}else{c.push(a);}}}a=b.shift();}if(this.options.version){this.outputVersion();this.quit(0);}if(this.options.help||c.length===0){this.outputHelp();this.quit(0);}this.files=c;switch(this.options.format){case"junitxml":if(this.options.verbose){this.warn("[INFO] Using JUnitXML output format.\n");}YUITest.CLI.Format(YUITest.TestFormat.JUnitXML);break;case"tap":if(this.options.verbose){this.warn("[INFO] Using TAP output format.\n");}YUITest.CLI.Format(YUITest.TestFormat.TAP);break;default:if(this.options.verbose){this.warn("[INFO] Using XUnit output format.\n");}YUITest.CLI.XUnit();}},start:function(){this.processArguments();this.processFiles();YUITest.TestRunner.subscribe(YUITest.TestRunner.COMPLETE_EVENT,function(a){YUITest.CLI.quit(a.results.failed?1:0);});YUITest.TestRunner.run({groups:this.options.groups?this.options.groups.split(","):null});}});YUITest.CLI.start();