Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 1 | /** |
Josh Haberman | d6a186a | 2016-02-04 14:58:45 -0800 | [diff] [blame] | 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 |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 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"). |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | var lineReader = require('readline').createInterface({ |
| 35 | input: process.stdin, |
| 36 | output: process.stdout |
| 37 | }); |
| 38 | |
Josh Haberman | 77af5d0 | 2016-02-04 16:11:07 -0800 | [diff] [blame] | 39 | function 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 | |
Josh Haberman | 907ad4a | 2016-02-18 10:46:44 -0800 | [diff] [blame] | 46 | function 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 | |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 62 | var module = null; |
Josh Haberman | 77af5d0 | 2016-02-04 16:11:07 -0800 | [diff] [blame] | 63 | var pkg = null; |
Josh Haberman | 1523936 | 2016-03-31 14:18:34 -0700 | [diff] [blame] | 64 | |
| 65 | // Header: goes in every file at the top. |
| 66 | console.log("var global = Function('return this')();"); |
| 67 | console.log("var googleProtobuf = require('google-protobuf');"); |
| 68 | console.log("var testdeps = require('testdeps_commonjs');"); |
| 69 | console.log("global.goog = testdeps.goog;"); |
| 70 | console.log("global.jspb = testdeps.jspb;"); |
| 71 | console.log("var asserts = require('closure_asserts_commonjs');"); |
| 72 | console.log(""); |
| 73 | console.log("// Bring asserts into the global namespace."); |
| 74 | console.log("googleProtobuf.object.extend(global, asserts);"); |
| 75 | |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 76 | lineReader.on('line', function(line) { |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 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) { |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 81 | if (module) { // Skip goog.require() lines before the first directive. |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 82 | var fullSym = isRequire[1]; |
Joshua Haberman | e8aa635 | 2021-02-04 17:04:17 -0800 | [diff] [blame] | 83 | // Skip lines importing from jspb.*, these are handled by the header above. |
| 84 | if (fullSym.match(/^jspb\./)) return; |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 85 | var sym = tryStripPrefix(fullSym, pkg); |
| 86 | console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', global);'); |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 87 | } |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 88 | } else if (isLoadFromFile) { |
murgatroid99 | a862b6b | 2016-02-24 13:44:57 -0800 | [diff] [blame] | 89 | var module_path = isLoadFromFile[1].split('/'); |
| 90 | module = camelCase(module_path[module_path.length - 1]); |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 91 | pkg = isLoadFromFile[2]; |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 92 | |
Josh Haberman | 7726cd2 | 2016-02-16 15:29:49 -0800 | [diff] [blame] | 93 | if (module != "googleProtobuf") { // We unconditionally require this in the header. |
murgatroid99 | a862b6b | 2016-02-24 13:44:57 -0800 | [diff] [blame] | 94 | console.log("var " + module + " = require('./" + isLoadFromFile[1] + "');"); |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 95 | } |
Josh Haberman | c348af2 | 2016-02-17 17:06:46 -0800 | [diff] [blame] | 96 | } else if (!isSetTestOnly) { // Remove goog.setTestOnly() lines. |
Josh Haberman | e9f31ee | 2016-02-04 10:29:27 -0800 | [diff] [blame] | 97 | console.log(line); |
| 98 | } |
| 99 | }); |