File: saucelabs.js

package info (click to toggle)
node-lodash 2.4.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,024 kB
  • ctags: 1,590
  • sloc: php: 747; sh: 22; makefile: 14
file content (353 lines) | stat: -rw-r--r-- 10,551 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
;(function() {
  'use strict';

  /** Environment shortcut */
  var env = process.env;

  if (isFinite(env.TRAVIS_PULL_REQUEST)) {
    console.error('Testing skipped for pull requests');
    process.exit(0);
  }

  /** Load Node.js modules */
  var http = require('http'),
      path = require('path'),
      url = require('url');

  /** Load other modules */
  var _ = require('../lodash.js'),
      ecstatic = require('ecstatic'),
      request = require('request'),
      SauceTunnel = require('sauce-tunnel');

  /** Used by `logInline` to clear previously logged messages */
  var prevLine = '';

  /** Used to display the wait throbber */
  var throbberId,
      throbberDelay = 500,
      waitCount = -1;

  /** Used as request `auth` and `options` values */
  var accessKey = env.SAUCE_ACCESS_KEY,
      build = env.TRAVIS_COMMIT.slice(0, 10),
      port = 9001,
      tunnelId = 'lodash_' + env.TRAVIS_JOB_NUMBER,
      username = env.SAUCE_USERNAME;

  var compatMode = process.argv.reduce(function(result, value) {
    return optionToValue('compatMode', value) || result;
  }, null);

  var runner = process.argv.reduce(function(result, value) {
    value = optionToValue('runner', value);
    return value == null
      ? result
      : '/' + value.replace(/^\W+/, '');
  }, '/test/index.html');

  var sessionName = process.argv.reduce(function(result, value) {
    return optionToValue('name', value) || result;
  }, 'lodash tests');

  var tags = process.argv.reduce(function(result, value) {
    value = optionToArray('tags', value);
    return value.length
      ? _.union(result, value)
      : result;
  }, []);

  /** List of platforms to load the runner on */
  var platforms = [
    ['Windows 8.1', 'chrome', '31'],
    ['Windows 8.1', 'chrome', '28'],
    ['Windows 8.1', 'chrome', '26'],
    ['OS X 10.6', 'firefox', '25'],
    ['OS X 10.6', 'firefox', '20'],
    ['OS X 10.6', 'firefox', '10'],
    ['OS X 10.6', 'firefox', '6'],
    ['OS X 10.6', 'firefox', '4'],
    ['Windows 7', 'firefox', '3.6'],
    ['Windows 8.1', 'internet explorer', '11'],
    ['Windows 8', 'internet explorer', '10'],
    ['Windows 7', 'internet explorer', '9'],
    ['Windows 7', 'internet explorer', '8'],
    ['Windows XP', 'internet explorer', '7'],
    ['Windows XP', 'internet explorer', '6'],
    //['Windows 7', 'opera', '12'],
    //['Windows 7', 'opera', '11'],
    ['OS X 10.8', 'safari', '6'],
    ['Windows 7', 'safari', '5'],
    ['Windows XP', 'safari', '4']
  ];

  /** Used to tailor the `platforms` array */
  var runnerQuery = url.parse(runner, true).query,
      isBackbone = /\bbackbone\b/i.test(runner),
      isMobile = /\bmobile\b/i.test(runnerQuery.build),
      isModern = /\bmodern\b/i.test(runnerQuery.build);

  // platforms to test IE compat mode
  if (compatMode) {
    platforms = [
      ['Windows 8.1', 'internet explorer', '11'],
      ['Windows 8', 'internet explorer', '10'],
      ['Windows 7', 'internet explorer', '9'],
      ['Windows 7', 'internet explorer', '8']
    ];
  }
  // platforms for Backbone tests
  if (isBackbone) {
    platforms = platforms.filter(function(platform) {
      var browser = platform[1],
          version = +platform[2];

      switch (browser) {
        case 'firefox': return version >= 4;
        case 'opera': return version >= 12;
      }
      return true;
    });
  }
  // platforms for mobile and modern builds
  if (isMobile || isModern) {
    platforms = platforms.filter(function(platform) {
      var browser = platform[1],
          version = +platform[2];

      switch (browser) {
        case 'firefox': return version >= 10;
        case 'internet explorer': return version >= 9;
        case 'opera': return version >= 12;
        case 'safari': return version >= (isMobile ? 3 : 6);
      }
      return true;
    });
  }

  /*--------------------------------------------------------------------------*/

  /**
   * Writes an inline message to standard output.
   *
   * @private
   * @param {string} text The text to log.
   */
  function logInline(text) {
    var blankLine = repeat(' ', prevLine.length);
    if (text.length > 40) {
      text = text.slice(0, 37) + '...';
    }
    prevLine = text;
    process.stdout.write(text + blankLine.slice(text.length) + '\r');
  }

  /**
   * Writes the wait throbber to standard output.
   *
   * @private
   */
  function logThrobber() {
    logInline('Please wait' + repeat('.', (++waitCount % 3) + 1));
  }

  /**
   * Converts a comma separated option value into an array.
   *
   * @private
   * @param {string} name The name of the option to inspect.
   * @param {string} string The options string.
   * @returns {Array} Returns the new converted array.
   */
  function optionToArray(name, string) {
    return _.compact(_.isArray(string)
      ? string
      : _.invoke((optionToValue(name, string) || '').split(/, */), 'trim')
    );
  }

  /**
   * Extracts the option value from an option string.
   *
   * @private
   * @param {string} name The name of the option to inspect.
   * @param {string} string The options string.
   * @returns {string|undefined} Returns the option value, else `undefined`.
   */
  function optionToValue(name, string) {
    var result = (result = string.match(RegExp('^' + name + '=([\\s\\S]+)$'))) && result[1].trim();
    return result || undefined;
  }

  /**
   * Creates a string with `text` repeated `n` number of times.
   *
   * @private
   * @param {string} text The text to repeat.
   * @param {number} n The number of times to repeat `text`.
   * @returns {string} The created string.
   */
  function repeat(text, n) {
    return Array(n + 1).join(text);
  }

  /*--------------------------------------------------------------------------*/

  /**
   * Processes the result object of the test session.
   *
   * @private
   * @param {Object} results The result object to process.
   */
  function handleTestResults(results) {
    var failingTests = results.filter(function(test) {
      var result = test.result;
      return !result || result.failed || /\berror\b/i.test(result.message);
    });

    var failingPlatforms = failingTests.map(function(test) {
      return test.platform;
    });

    if (!failingTests.length) {
      console.log('Tests passed');
    }
    else {
      console.error('Tests failed on platforms: ' + JSON.stringify(failingPlatforms));

      failingTests.forEach(function(test) {
        var result = test.result || {},
            details = 'See ' + test.url + ' for details.',
            failed = result.failed,
            platform = JSON.stringify(test.platform);

        if (failed) {
          console.error(failed + ' failures on ' + platform + '. ' + details);
        } else {
          var message = result.message || 'no results available. ' + details;
          console.error('Testing on ' + platform + ' failed; ' + message);
        }
      });
    }

    clearInterval(throbberId);
    console.log('Shutting down Sauce Connect tunnel...');

    tunnel.stop(function() {
      process.exit(failingTests.length ? 1 : 0);
    });
  }

  /**
   * Makes a request for Sauce Labs to start the test session.
   *
   * @private
   */
  function runTests() {
    var options = {
      'build': build,
      'framework': 'qunit',
      'idle-timeout': 300,
      'max-duration': 600,
      'name': sessionName,
      'public': 'public',
      'platforms': platforms,
      'record-screenshots': false,
      'tags': tags,
      'tunnel': 'tunnel-identifier:' + tunnelId,
      'url': 'http://localhost:' + port + runner,
      'video-upload-on-pass': false
    };

    console.log('Starting saucelabs tests: ' + JSON.stringify(options));

    request.post('https://saucelabs.com/rest/v1/' + username + '/js-tests', {
      'auth': { 'user': username, 'pass': accessKey },
      'json': options
    }, function(error, response, body) {
      var statusCode = response && response.statusCode;
      if (statusCode == 200) {
        waitForTestCompletion(body);
      } else {
        console.error('Failed to submit test to Sauce Labs; status: ' +  statusCode + ', body:\n' + JSON.stringify(body));
        if (error) {
          console.error(error);
        }
        process.exit(3);
      }
    });

    // initialize the wait throbber
    if (!throbberId) {
      throbberId = setInterval(logThrobber, throbberDelay);
      logThrobber();
    }
  }

  /**
   * Checks the status of the test session. If the session has completed it
   * passes the result object to `handleTestResults`, else it checks the status
   * again in five seconds.
   *
   * @private
   * @param {Object} testIdentifier The object used to identify the session.
   */
  function waitForTestCompletion(testIdentifier) {
    request.post('https://saucelabs.com/rest/v1/' + username + '/js-tests/status', {
      'auth': { 'user': username, 'pass': accessKey },
      'json': testIdentifier
    }, function(error, response, body) {
        var statusCode = response && response.statusCode;
        if (statusCode == 200) {
          if (body.completed) {
            logInline('');
            handleTestResults(body['js tests']);
          }
          else {
            setTimeout(function() {
              waitForTestCompletion(testIdentifier);
            }, 5000);
          }
        } else {
          logInline('');
          console.error('Failed to check test status on Sauce Labs; status: ' + statusCode + ', body:\n' + JSON.stringify(body));
          if (error) {
            console.error(error);
          }
          process.exit(4);
        }
    });
  }

  /*--------------------------------------------------------------------------*/

  // create a web server for the local dir
  var mount = ecstatic({
    'cache': false,
    'root': process.cwd()
  });

  http.createServer(function(req, res) {
    // see http://msdn.microsoft.com/en-us/library/ff955275(v=vs.85).aspx
    if (compatMode && path.extname(url.parse(req.url).pathname) == '.html') {
      res.setHeader('X-UA-Compatible', 'IE=' + compatMode);
    }
    mount(req, res);
  }).listen(port);

  // set up Sauce Connect so we can use this server from Sauce Labs
  var tunnelTimeout = 10000,
      tunnel = new SauceTunnel(username, accessKey, tunnelId, true, tunnelTimeout);

  console.log('Opening Sauce Connect tunnel...');

  tunnel.start(function(success) {
    if (success) {
      console.log('Sauce Connect tunnel opened');
      runTests();
    } else {
      console.error('Failed to open Sauce Connect tunnel');
      process.exit(2);
    }
  });
}());