1458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org//
3458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// Use of this source code is governed by a BSD-style license
4458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// that can be found in the LICENSE file in the root of the source
5458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// tree. An additional intellectual property rights grant can be found
6458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// in the file PATENTS.  All contributing project authors may
7458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// be found in the AUTHORS file in the root of the source tree.
8458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org//
9458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// This script loads all the test/* files into a very small context that
10458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// only exposes a minimal set of functions that allows to register tests.
11458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org//
12458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// Once all files are loaded it runs the specific test on the command line.
13458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// If no arguments are given it lists all the registered tests.
14458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org//
15458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// Note: the small context where the scripts are loaded is intended to keep
16458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// nodejs-isms away from the test code and isolate implementation details away
17458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org// from them.
18458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgvar fs = require('fs');
19458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgvar vm = require('vm');
20458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgvar Test = require('./test.js');
21458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
22458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgvar testSuites = {};
23458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
24458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction registerTest(name, func) {
25458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  testSuites[name] = func;
26458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
27458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
28458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction registerBotTest(name, func, bots) {
29458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  registerTest(name, bootstrap);
30458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
31458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  function bootstrap(test) {
32458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    var callbacks = [];
33458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    for (var i = 0; i != bots.length; ++i)
34458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      callbacks.push(test.spawnBot.bind(test, "", bots[i]));
35458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
36458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    test.wait(callbacks, func.bind(test, test));
37458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  }
38458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
39458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
40458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction loadTestFile(filename, doneCallback) {
41458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  var loadTestContext = {
42458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    setTimeout: setTimeout,
43458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    registerTest: registerTest,
44458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    registerBotTest: registerBotTest
45458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  };
46458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  var script = vm.createScript(fs.readFileSync(filename), filename);
47458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  script.runInNewContext(loadTestContext);
48458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  doneCallback();
49458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
50458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
51458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction iterateOverTestFiles(foreachCallback, doneCallback) {
52458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  fs.readdir('test', function (error, list) {
53458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    function iterateNextFile() {
54458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      if (list.length === 0) {
55458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org        doneCallback();
56458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      } else {
57458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org        var filename = list.pop();
58458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org        if (filename[0] === '.' || filename.slice(-3) !== '.js') {
59458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org          // Skip hidden and non .js files on that directory.
60458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org          iterateNextFile();
61458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org        } else {
62458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org          foreachCallback('test/' + filename, iterateNextFile);
63458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org        }
64458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      }
65458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    }
66458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
67458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    if (error !== null) {
68458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      throw error;
69458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    }
70458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    iterateNextFile();
71458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  });
72458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
73458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
74458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction runTest(testname) {
75458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  if (testname in testSuites) {
76458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    console.log("Running test: " + testname);
77458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    var test = new Test();
78458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    testSuites[testname](test);
79458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  } else {
80458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    console.log("Unknown test: " + testname);
81458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  }
82458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
83458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
84458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction printUsage() {
85458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  console.log('Run as:\n $ '
86458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      + process.argv[0] + ' ' + process.argv[1]
87458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org      + ' <testname>');
88458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  console.log('These are the existent ones:');
89458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  for (var testname in testSuites)
90458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    console.log('  ' + testname);
91458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
92458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
93458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgfunction main() {
94458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  // TODO(andresp): support multiple tests.
95458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  var testList = process.argv.slice(2);
96458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  if (testList.length === 1)
97458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    runTest(testList[0]);
98458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org  else
99458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org    printUsage();
100458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org}
101458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.org
102458c2c3b06e9115d875f5cb4059906f94c11b1a0andresp@webrtc.orgiterateOverTestFiles(loadTestFile, main);
103