blob: 82eef18ed8ca722d7e9e4ce1a328016eaaeb84d4 [file] [log] [blame]
Josh Habermane9f31ee2016-02-04 10:29:27 -08001/**
Josh Habermand6a186a2016-02-04 14:58:45 -08002 * @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 Habermanc348af22016-02-17 17:06:46 -080022 * 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 Habermane9f31ee2016-02-04 10:29:27 -080032 */
33
34var lineReader = require('readline').createInterface({
35 input: process.stdin,
36 output: process.stdout
37});
38
Josh Haberman77af5d02016-02-04 16:11:07 -080039function 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 Haberman907ad4a2016-02-18 10:46:44 -080046function 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 Habermane9f31ee2016-02-04 10:29:27 -080062var module = null;
Josh Haberman77af5d02016-02-04 16:11:07 -080063var pkg = null;
Josh Haberman15239362016-03-31 14:18:34 -070064
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
Josh Habermane9f31ee2016-02-04 10:29:27 -080076lineReader.on('line', function(line) {
Josh Habermanc348af22016-02-17 17:06:46 -080077 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 Habermane9f31ee2016-02-04 10:29:27 -080081 if (module) { // Skip goog.require() lines before the first directive.
Josh Habermanc348af22016-02-17 17:06:46 -080082 var fullSym = isRequire[1];
Joshua Habermane8aa6352021-02-04 17:04:17 -080083 // Skip lines importing from jspb.*, these are handled by the header above.
84 if (fullSym.match(/^jspb\./)) return;
Josh Habermanc348af22016-02-17 17:06:46 -080085 var sym = tryStripPrefix(fullSym, pkg);
86 console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', global);');
Josh Habermane9f31ee2016-02-04 10:29:27 -080087 }
Josh Habermanc348af22016-02-17 17:06:46 -080088 } else if (isLoadFromFile) {
murgatroid99a862b6b2016-02-24 13:44:57 -080089 var module_path = isLoadFromFile[1].split('/');
90 module = camelCase(module_path[module_path.length - 1]);
Josh Habermanc348af22016-02-17 17:06:46 -080091 pkg = isLoadFromFile[2];
Josh Habermane9f31ee2016-02-04 10:29:27 -080092
Josh Haberman7726cd22016-02-16 15:29:49 -080093 if (module != "googleProtobuf") { // We unconditionally require this in the header.
murgatroid99a862b6b2016-02-24 13:44:57 -080094 console.log("var " + module + " = require('./" + isLoadFromFile[1] + "');");
Josh Habermane9f31ee2016-02-04 10:29:27 -080095 }
Josh Habermanc348af22016-02-17 17:06:46 -080096 } else if (!isSetTestOnly) { // Remove goog.setTestOnly() lines.
Josh Habermane9f31ee2016-02-04 10:29:27 -080097 console.log(line);
98 }
99});