rewrite_tests_for_commonjs.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. lineReader.on('line', function(line) {
  61. var isRequire = line.match(/goog\.require\('([^']*)'\)/);
  62. var isLoadFromFile = line.match(/CommonJS-LoadFromFile: (\S*) (.*)/);
  63. var isSetTestOnly = line.match(/goog.setTestOnly()/);
  64. if (isRequire) {
  65. if (module) { // Skip goog.require() lines before the first directive.
  66. var fullSym = isRequire[1];
  67. var sym = tryStripPrefix(fullSym, pkg);
  68. console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', global);');
  69. }
  70. } else if (isLoadFromFile) {
  71. if (!module) {
  72. console.log("var googleProtobuf = require('google-protobuf');");
  73. console.log("var asserts = require('closure_asserts_commonjs');");
  74. console.log("var global = Function('return this')();");
  75. console.log("");
  76. console.log("// Bring asserts into the global namespace.");
  77. console.log("googleProtobuf.object.extend(global, asserts);");
  78. }
  79. module = camelCase(isLoadFromFile[1])
  80. pkg = isLoadFromFile[2];
  81. if (module != "googleProtobuf") { // We unconditionally require this in the header.
  82. console.log("var " + module + " = require('" + isLoadFromFile[1] + "');");
  83. }
  84. } else if (!isSetTestOnly) { // Remove goog.setTestOnly() lines.
  85. console.log(line);
  86. }
  87. });