michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function chunkifyTests(tests, totalChunks, thisChunk, chunkByDir, logger) { michael@0: var total_chunks = parseInt(totalChunks); michael@0: // this_chunk is in the range [1,total_chunks] michael@0: var this_chunk = parseInt(thisChunk); michael@0: var returnTests; michael@0: michael@0: // We want to split the tests up into chunks according to which directory michael@0: // they're in michael@0: if (chunkByDir) { michael@0: chunkByDir = parseInt(chunkByDir); michael@0: var tests_by_dir = {}; michael@0: var test_dirs = [] michael@0: for (var i = 0; i < tests.length; ++i) { michael@0: var test_path = tests[i]; michael@0: if (test_path[0] == '/') { michael@0: test_path = test_path.substr(1); michael@0: } michael@0: // mochitest-chrome and mochitest-browser-chrome pass an array of chrome:// michael@0: // URIs michael@0: var protocolRegexp = /^[a-zA-Z]+:\/\//; michael@0: if (protocolRegexp.test(test_path)) { michael@0: test_path = test_path.replace(protocolRegexp, ""); michael@0: } michael@0: var dir = test_path.split("/"); michael@0: // We want the first chunkByDir+1 components, or everything but the michael@0: // last component, whichever is less. michael@0: // we add 1 to chunkByDir since 'tests' is always part of the path, and michael@0: // want to ignore the last component since it's the test filename. michael@0: dir = dir.slice(0, Math.min(chunkByDir+1, dir.length-1)); michael@0: // reconstruct a directory name michael@0: dir = dir.join("/"); michael@0: if (!(dir in tests_by_dir)) { michael@0: tests_by_dir[dir] = [tests[i]]; michael@0: test_dirs.push(dir); michael@0: } else { michael@0: tests_by_dir[dir].push(tests[i]); michael@0: } michael@0: } michael@0: var tests_per_chunk = test_dirs.length / total_chunks; michael@0: var start = Math.round((this_chunk-1) * tests_per_chunk); michael@0: var end = Math.round(this_chunk * tests_per_chunk); michael@0: returnTests = []; michael@0: var dirs = [] michael@0: for (var i = start; i < end; ++i) { michael@0: var dir = test_dirs[i]; michael@0: dirs.push(dir); michael@0: returnTests = returnTests.concat(tests_by_dir[dir]); michael@0: } michael@0: if (logger) michael@0: logger.log("Running tests in " + dirs.join(", ")); michael@0: } else { michael@0: var tests_per_chunk = tests.length / total_chunks; michael@0: var start = Math.round((this_chunk-1) * tests_per_chunk); michael@0: var end = Math.round(this_chunk * tests_per_chunk); michael@0: returnTests = tests.slice(start, end); michael@0: if (logger) michael@0: logger.log("Running tests " + (start+1) + "-" + end + "/" + tests.length); michael@0: } michael@0: michael@0: return returnTests; michael@0: } michael@0: michael@0: function skipTests(tests, startTestPattern, endTestPattern) { michael@0: var startIndex = 0, endIndex = tests.length - 1; michael@0: for (var i = 0; i < tests.length; ++i) { michael@0: var test_path = tests[i]; michael@0: if (startTestPattern && test_path.endsWith(startTestPattern)) { michael@0: startIndex = i; michael@0: } michael@0: michael@0: if (endTestPattern && test_path.endsWith(endTestPattern)) { michael@0: endIndex = i; michael@0: } michael@0: } michael@0: michael@0: return tests.slice(startIndex, endIndex + 1); michael@0: }