testing/mochitest/chunkifyTests.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/chunkifyTests.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function chunkifyTests(tests, totalChunks, thisChunk, chunkByDir, logger) {
     1.9 +  var total_chunks = parseInt(totalChunks);
    1.10 +  // this_chunk is in the range [1,total_chunks]
    1.11 +  var this_chunk = parseInt(thisChunk);
    1.12 +  var returnTests;
    1.13 +
    1.14 +  // We want to split the tests up into chunks according to which directory
    1.15 +  // they're in
    1.16 +  if (chunkByDir) {
    1.17 +    chunkByDir = parseInt(chunkByDir);
    1.18 +    var tests_by_dir = {};
    1.19 +    var test_dirs = []
    1.20 +    for (var i = 0; i < tests.length; ++i) {
    1.21 +      var test_path = tests[i];
    1.22 +      if (test_path[0] == '/') {
    1.23 +        test_path = test_path.substr(1);
    1.24 +      }
    1.25 +      // mochitest-chrome and mochitest-browser-chrome pass an array of chrome://
    1.26 +      // URIs
    1.27 +      var protocolRegexp = /^[a-zA-Z]+:\/\//;
    1.28 +      if (protocolRegexp.test(test_path)) {
    1.29 +        test_path = test_path.replace(protocolRegexp, "");
    1.30 +      }
    1.31 +      var dir = test_path.split("/");
    1.32 +      // We want the first chunkByDir+1 components, or everything but the
    1.33 +      // last component, whichever is less.
    1.34 +      // we add 1 to chunkByDir since 'tests' is always part of the path, and
    1.35 +      // want to ignore the last component since it's the test filename.
    1.36 +      dir = dir.slice(0, Math.min(chunkByDir+1, dir.length-1));
    1.37 +      // reconstruct a directory name
    1.38 +      dir = dir.join("/");
    1.39 +      if (!(dir in tests_by_dir)) {
    1.40 +        tests_by_dir[dir] = [tests[i]];
    1.41 +        test_dirs.push(dir);
    1.42 +      } else {
    1.43 +        tests_by_dir[dir].push(tests[i]);
    1.44 +      }
    1.45 +    }
    1.46 +    var tests_per_chunk = test_dirs.length / total_chunks;
    1.47 +    var start = Math.round((this_chunk-1) * tests_per_chunk);
    1.48 +    var end = Math.round(this_chunk * tests_per_chunk);
    1.49 +    returnTests = [];
    1.50 +    var dirs = []
    1.51 +    for (var i = start; i < end; ++i) {
    1.52 +      var dir = test_dirs[i];
    1.53 +      dirs.push(dir);
    1.54 +      returnTests = returnTests.concat(tests_by_dir[dir]);
    1.55 +    }
    1.56 +    if (logger)
    1.57 +      logger.log("Running tests in " + dirs.join(", "));
    1.58 +  } else {
    1.59 +    var tests_per_chunk = tests.length / total_chunks;
    1.60 +    var start = Math.round((this_chunk-1) * tests_per_chunk);
    1.61 +    var end = Math.round(this_chunk * tests_per_chunk);
    1.62 +    returnTests = tests.slice(start, end);
    1.63 +    if (logger)
    1.64 +      logger.log("Running tests " + (start+1) + "-" + end + "/" + tests.length);
    1.65 +  }
    1.66 +
    1.67 +  return returnTests;
    1.68 +}
    1.69 +
    1.70 +function skipTests(tests, startTestPattern, endTestPattern) {
    1.71 +  var startIndex = 0, endIndex = tests.length - 1;
    1.72 +  for (var i = 0; i < tests.length; ++i) {
    1.73 +    var test_path = tests[i];
    1.74 +    if (startTestPattern && test_path.endsWith(startTestPattern)) {
    1.75 +      startIndex = i;
    1.76 +    }
    1.77 +
    1.78 +    if (endTestPattern && test_path.endsWith(endTestPattern)) {
    1.79 +      endIndex = i;
    1.80 +    }
    1.81 +  }
    1.82 +
    1.83 +  return tests.slice(startIndex, endIndex + 1);
    1.84 +}

mercurial