Upload packaging folder
[platform/upstream/iotjs.git] / tools / test_runner.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 var testdriver = require('testdriver');
17 var console_wrapper = require('common_js/module/console');
18
19 function Runner(driver) {
20   process._exiting = false;
21
22   this.driver = driver;
23   this.test = driver.currentTest();
24   this.finished = false;
25   if (driver.skipModule) {
26     this.skipModule = driver.skipModule;
27     this.skipModuleLength = this.skipModule.length;
28   } else {
29     this.skipModuleLength = 0;
30   }
31   if (driver.options.quiet == 'yes') {
32     this.console = console;
33     console = console_wrapper;
34   }
35
36   return this;
37 }
38
39 Runner.prototype.cleanup = function() {
40   if (this.driver.options.quiet == 'yes') {
41     console = this.console;
42   }
43
44   this.driver = null;
45   this.attr = null;
46   if (this.timer != null) {
47     clearTimeout(this.timer);
48     this.timer = null;
49   }
50 };
51
52 Runner.prototype.spin = function() {
53   var that = this;
54   process.nextTick(function() {
55       var timerOnlyAlive = !testdriver.isAliveExceptFor(that.timer);
56       if (timerOnlyAlive) {
57         timerOnlyAlive = !process._onNextTick();
58       }
59
60       if (timerOnlyAlive) {
61         process.exit(0);
62       } else {
63         if (!that.finished) {
64           that.spin();
65         }
66       }
67   });
68 };
69
70 Runner.prototype.checkSkipModule = function() {
71   for (var i = 0; i < this.skipModuleLength; i++) {
72     if (this.test['name'].indexOf(this.skipModule[i]) >= 0) {
73       return true;
74     }
75   }
76
77   return false;
78 };
79
80
81 Runner.prototype.run = function() {
82   var skip = this.test['skip'];
83   if (skip) {
84     if ((skip.indexOf('all') >= 0) || (skip.indexOf(this.driver.os) >= 0)
85       || (skip.indexOf(this.driver.stability) >= 0)) {
86       this.finish('skip');
87       return;
88     }
89   }
90
91   if (this.skipModuleLength && this.checkSkipModule()) {
92     this.test.reason = 'exclude module';
93     this.finish('skip');
94     return;
95   }
96
97   this.timer = null;
98   if (this.test['timeout']) {
99     var timeout = this.test['timeout'];
100     if (timeout) {
101       var that = this;
102       this.timer = setTimeout(function () {
103         that.finish('timeout');
104       }, timeout * 1000);
105     }
106   }
107
108   try {
109     var source = this.driver.test();
110     eval(source);
111   } catch(e) {
112     if (this.test['expected-failure']) {
113       this.finish('pass');
114     } else if (this.test['uncaught']) {
115       throw e;
116     } else {
117       console.error(e);
118       this.finish('fail');
119     }
120   } finally {
121     if (!this.finished) {
122       this.spin();
123     }
124   }
125 };
126
127 Runner.prototype.finish = function(status) {
128   if (this.finished)
129     return;
130
131   this.finished = true;
132
133   this.driver.emitter.emit('nextTest', this.driver, status, this.test);
134 };
135
136 module.exports.Runner = Runner;