Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / testswarm / testswarm.js
1 var request = require( "request" ),
2         querystring = require( "querystring" ).stringify,
3         url = require( "url" );
4
5 function extend( a, b ) {
6         for (var key in b) {
7                 a[key] = b[key];
8         }
9         return a;
10 }
11
12 function baseUrl( config ) {
13         // make sure there's always a trailing slash
14         return url.format( config.urlParts).replace( /\/$/, "" ) + "/";
15 }
16
17 function continueRunning( job ) {
18         return job.runs.filter(function( run ) {
19                 return Object.keys(run.uaRuns).filter(function( uaRun ) {
20                         return (/new|progress/).test( run.uaRuns[ uaRun ].runStatus );
21                 }).length;
22         }).length;
23 }
24
25 function runStats( uaID, uaRun ) {
26         var base = uaID + ": " + uaRun.runStatus;
27         if ( uaRun.runResultsUrl ) {
28                 base +=  " (" + uaRun.runResultsLabel + ")" + ": " + uaRun.runResultsUrl;
29         }
30         return base;
31 }
32
33 function logResults( config, job, state ) {
34         var passed = true;
35         console.log( "Job " + job.jobInfo.id + ":\n\t" + job.jobInfo.name + "\nState:\n\t" + state );
36         console.log( "\nResults: " );
37         job.runs.filter(function( run ) {
38                 var uaID;
39                 console.log( "\n - " + run.info.name + ":" );
40                 for ( uaID in run.uaRuns ) {
41                         console.log( runStats( uaID, run.uaRuns[uaID] ) );
42                         // "new", "failed", "error", "timedout", ..
43                         if ( run.uaRuns[uaID].runStatus !== "passed" ) {
44                                 passed = false;
45                         }
46                 }
47         });
48         config.done( passed );
49 }
50
51 function pollResults( config, job ) {
52         process.stdout.write( "." );
53         request.get( baseUrl( config ) + "api.php?" + querystring({
54                 action: "job",
55                 item: job.id
56         }), function ( error, response, body ) {
57                 if ( error ) {
58                         throw error;
59                 }
60                 var result = JSON.parse( body );
61                 if ( !result.job ) {
62                         console.log( "API returned error, can't continue. Response was: " + body );
63                         config.done( false );
64                         return;
65                 }
66                 if ( continueRunning( result.job ) ) {
67                         if ( config.started + config.timeout < +new Date() ) {
68                                 process.stdout.write( "\n\n" );
69                                 logResults( config, result.job, "Timed out after " + config.timeout + 'ms' );
70                         }
71                         setTimeout(function () {
72                                 pollResults( config, job );
73                         }, config.pollInterval );
74                 } else {
75                         process.stdout.write( "\n\n" );
76                         logResults( config, result.job, "Finished" );
77                 }
78         });
79 }
80
81 module.exports = function ( config, addjobParams ) {
82         config = extend({
83                 // default config
84                 // url: {String} required, no default
85                 // done: {Function} required, no default
86                 pollInterval: 5000,
87                 // 15 minutes
88                 timeout: 1000 * 60 * 15,
89                 started: +new Date(),
90                 urlParts: url.parse( config.url )
91         }, config);
92         addjobParams = extend(addjobParams, {
93                 action: "addjob"
94         });
95         request.post({
96                 url: baseUrl( config ) + "api.php",
97                 form: addjobParams
98         }, function ( error, response, body ) {
99                 var result, jobInfo;
100                 if ( error ) {
101                         throw error;
102                 }
103                 try {
104                         result = JSON.parse( body );
105                 } catch( e ) {
106                         console.log( "Failed parsing body as json, was: " + body );
107                         config.done( false );
108                 }
109                 if ( !result.addjob ) {
110                         console.log( "API returned error, can't continue. Response was: " + body );
111                         config.done( false );
112                         return;
113                 }
114                 jobInfo = result.addjob;
115                 console.log( "Submited job " + jobInfo.id + " " + baseUrl( config ) + "job/" + jobInfo.id );
116                 process.stdout.write( "Polling for results" );
117                 pollResults( config, jobInfo );
118         });
119
120 };