|
| 1 | +// Copyright (C) 2014, Microsoft Corporation. All rights reserved. |
| 2 | +// This code is governed by the BSD License found in the LICENSE file. |
| 3 | + |
| 4 | +module.exports = JSShellRunner; |
| 5 | + |
| 6 | +var fs = require('fs'); |
| 7 | +var cp = require('child_process'); |
| 8 | +var through = require('through'); |
| 9 | +var ConsoleRunner = require('./console'); |
| 10 | +var DEFAULT_BATCH_SIZE = 75; |
| 11 | +var counter = 0; |
| 12 | + |
| 13 | +function lineSplitter() { |
| 14 | + var splitLines = through(function(data) { |
| 15 | + this.buffer += data; |
| 16 | + var lines = this.buffer.split(/\r?\n/); |
| 17 | + this.buffer = lines.pop(); |
| 18 | + |
| 19 | + lines.forEach(function(line) { this.queue(line)}, this); |
| 20 | + }, function() { |
| 21 | + this.queue(this.buffer); |
| 22 | + }) |
| 23 | + |
| 24 | + splitLines.buffer = ""; |
| 25 | + |
| 26 | + return splitLines; |
| 27 | +} |
| 28 | + |
| 29 | +function JSShellRunner(args) { |
| 30 | + args.consolePrintCommand = args.consolePrintCommand || "print"; |
| 31 | + |
| 32 | + ConsoleRunner.apply(this, arguments); |
| 33 | + |
| 34 | + if(args.batch === true) args.batch = DEFAULT_BATCH_SIZE; |
| 35 | + |
| 36 | + if(args.batch) { |
| 37 | + this.batchTestStart = |
| 38 | + 'var env = evalcx("");\n' + |
| 39 | + 'env.print = print;' + |
| 40 | + 'try {\n'; |
| 41 | + |
| 42 | + this.batchTestEnd = |
| 43 | + '} catch(e) {\n' + |
| 44 | + 'if(e.hasOwnProperty("name")) ' + |
| 45 | + this._print('"test262/error " + e.name + " " + e.message') + |
| 46 | + 'else ' + this._print('"test262/error " + e') + |
| 47 | + '}\n'; |
| 48 | + } |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +JSShellRunner.prototype = Object.create(ConsoleRunner.prototype); |
| 53 | + |
| 54 | +JSShellRunner.prototype.executeBatch = function(batch, batchDone) { |
| 55 | + var runner = this; |
| 56 | + var baseFile = '__tmp' + counter++ + '_'; |
| 57 | + var script = ''; |
| 58 | + |
| 59 | + // write test files to disk |
| 60 | + batch.forEach(function(test, i) { |
| 61 | + script += this._print('"test262/test-start"') |
| 62 | + + this.batchTestStart |
| 63 | + + 'evalcx(' + JSON.stringify(test.contents) + ', env);\n' |
| 64 | + + this.batchTestEnd |
| 65 | + + this._print('"test262/test-end"'); |
| 66 | + }, this); |
| 67 | + |
| 68 | + var scriptFile = baseFile + 'main.js'; |
| 69 | + |
| 70 | + fs.writeFileSync(scriptFile, script); |
| 71 | + |
| 72 | + cp.exec(this.command + " " + scriptFile, function(err, stdout, stderr) { |
| 73 | + var results = { log: [] }; |
| 74 | + var lines = stdout.split(/\r?\n/); |
| 75 | + var index = 0; |
| 76 | + |
| 77 | + lines.forEach(function(line) { |
| 78 | + switch(line) { |
| 79 | + case 'test262/test-start': |
| 80 | + break; |
| 81 | + case 'test262/test-end': |
| 82 | + var test = batch[index++]; |
| 83 | + runner.validateResult(test, results); |
| 84 | + results = { log: [] }; |
| 85 | + |
| 86 | + break; |
| 87 | + default: |
| 88 | + results.log.push(line); |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + fs.unlink(scriptFile); |
| 93 | + batchDone(); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
0 commit comments