rewrite_tests_for_commonjs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var lineReader = require('readline').createInterface({
  34. input: process.stdin,
  35. output: process.stdout
  36. });
  37. function tryStripPrefix(str, prefix) {
  38. if (str.lastIndexOf(prefix) !== 0) {
  39. throw "String: " + str + " didn't start with: " + prefix;
  40. }
  41. return str.substr(prefix.length);
  42. }
  43. function camelCase(str) {
  44. var ret = '';
  45. var ucaseNext = false;
  46. for (var i = 0; i < str.length; i++) {
  47. if (str[i] == '-') {
  48. ucaseNext = true;
  49. } else if (ucaseNext) {
  50. ret += str[i].toUpperCase();
  51. ucaseNext = false;
  52. } else {
  53. ret += str[i];
  54. }
  55. }
  56. return ret;
  57. }
  58. var module = null;
  59. var pkg = null;
  60. // Header: goes in every file at the top.
  61. console.log("var global = Function('return this')();");
  62. console.log("var googleProtobuf = require('google-protobuf');");
  63. console.log("var testdeps = require('testdeps_commonjs');");
  64. console.log("global.goog = testdeps.goog;");
  65. console.log("global.jspb = testdeps.jspb;");
  66. console.log("var asserts = require('closure_asserts_commonjs');");
  67. console.log("");
  68. console.log("// Bring asserts into the global namespace.");
  69. console.log("googleProtobuf.object.extend(global, asserts);");
  70. lineReader.on('line', function(line) {
  71. var isRequire = line.match(/goog\.require\('([^']*)'\)/);
  72. var isLoadFromFile = line.match(/CommonJS-LoadFromFile: (\S*) (.*)/);
  73. var isSetTestOnly = line.match(/goog.setTestOnly()/);
  74. if (isRequire) {
  75. if (module) { // Skip goog.require() lines before the first directive.
  76. var fullSym = isRequire[1];
  77. // Skip lines importing from jspb.*, these are handled by the header above.
  78. if (fullSym.match(/^jspb\./)) return;
  79. var sym = tryStripPrefix(fullSym, pkg);
  80. console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', global);');
  81. }
  82. } else if (isLoadFromFile) {
  83. var module_path = isLoadFromFile[1].split('/');
  84. module = camelCase(module_path[module_path.length - 1]);
  85. pkg = isLoadFromFile[2];
  86. if (module != "googleProtobuf") { // We unconditionally require this in the header.
  87. console.log("var " + module + " = require('./" + isLoadFromFile[1] + "');");
  88. }
  89. } else if (!isSetTestOnly) { // Remove goog.setTestOnly() lines.
  90. console.log(line);
  91. }
  92. });