1/** 2 * @fileoverview Utility to translate test files to CommonJS imports. 3 * 4 * This is a somewhat hacky tool designed to do one very specific thing. 5 * All of the test files in *_test.js are written with Closure-style 6 * imports (goog.require()). This works great for running the tests 7 * against Closure-style generated code, but we also want to run the 8 * tests against CommonJS-style generated code without having to fork 9 * the tests. 10 * 11 * Closure-style imports import each individual type by name. This is 12 * very different than CommonJS imports which are by file. So we put 13 * special comments in these tests like: 14 * 15 * // CommonJS-LoadFromFile: test_pb 16 * goog.require('proto.jspb.test.CloneExtension'); 17 * goog.require('proto.jspb.test.Complex'); 18 * goog.require('proto.jspb.test.DefaultValues'); 19 * 20 * This script parses that special comment and uses it to generate proper 21 * CommonJS require() statements so that the tests can run and pass using 22 * CommonJS imports. The script will change the above statements into: 23 * 24 * var test_pb = require('test_pb'); 25 * googleProtobuf.exportSymbol('proto.jspb.test.CloneExtension', test_pb.CloneExtension, global); 26 * googleProtobuf.exportSymbol('proto.jspb.test.Complex', test_pb.Complex, global); 27 * googleProtobuf.exportSymbol('proto.jspb.test.DefaultValues', test_pb.DefaultValues, global); 28 * 29 * (The "exportSymbol" function will define the given names in the global 30 * namespace, taking care not to overwrite any previous value for 31 * "proto.jspb.test"). 32 */ 33 34var lineReader = require('readline').createInterface({ 35 input: process.stdin, 36 output: process.stdout 37}); 38 39function tryStripPrefix(str, prefix) { 40 if (str.lastIndexOf(prefix) !== 0) { 41 throw "String: " + str + " didn't start with: " + prefix; 42 } 43 return str.substr(prefix.length); 44} 45 46function camelCase(str) { 47 var ret = ''; 48 var ucaseNext = false; 49 for (var i = 0; i < str.length; i++) { 50 if (str[i] == '-') { 51 ucaseNext = true; 52 } else if (ucaseNext) { 53 ret += str[i].toUpperCase(); 54 ucaseNext = false; 55 } else { 56 ret += str[i]; 57 } 58 } 59 return ret; 60} 61 62var module = null; 63var pkg = null; 64 65// Header: goes in every file at the top. 66console.log("var global = Function('return this')();"); 67console.log("var googleProtobuf = require('google-protobuf');"); 68console.log("var testdeps = require('testdeps_commonjs');"); 69console.log("global.goog = testdeps.goog;"); 70console.log("global.jspb = testdeps.jspb;"); 71console.log("var asserts = require('closure_asserts_commonjs');"); 72console.log(""); 73console.log("// Bring asserts into the global namespace."); 74console.log("googleProtobuf.object.extend(global, asserts);"); 75 76lineReader.on('line', function(line) { 77 var isRequire = line.match(/goog\.require\('([^']*)'\)/); 78 var isLoadFromFile = line.match(/CommonJS-LoadFromFile: (\S*) (.*)/); 79 var isSetTestOnly = line.match(/goog.setTestOnly()/); 80 if (isRequire) { 81 if (module) { // Skip goog.require() lines before the first directive. 82 var fullSym = isRequire[1]; 83 var sym = tryStripPrefix(fullSym, pkg); 84 console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', global);'); 85 } 86 } else if (isLoadFromFile) { 87 var module_path = isLoadFromFile[1].split('/'); 88 module = camelCase(module_path[module_path.length - 1]); 89 pkg = isLoadFromFile[2]; 90 91 if (module != "googleProtobuf") { // We unconditionally require this in the header. 92 console.log("var " + module + " = require('./" + isLoadFromFile[1] + "');"); 93 } 94 } else if (!isSetTestOnly) { // Remove goog.setTestOnly() lines. 95 console.log(line); 96 } 97}); 98