run-tests.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // run-tests.js
  2. // MIT licensed, see LICENSE file
  3. // Copyright (c) 2013-2015 Olov Lassus <olov.lassus@gmail.com>
  4. "use strict";
  5. const ngAnnotate = require("./ng-annotate-main");
  6. const fs = require("fs");
  7. const os = require("os");
  8. const path = require("path");
  9. const diff = require("diff");
  10. const findLineColumn = require("find-line-column");
  11. const fmt = require("simple-fmt");
  12. const SourceMapConsumer = require("source-map").SourceMapConsumer;
  13. const coffee = require("coffee-script");
  14. const convertSourceMap = require("convert-source-map");
  15. function slurp(filename) {
  16. return String(fs.readFileSync(filename));
  17. }
  18. function time(str, fn) {
  19. const t0 = Date.now();
  20. fn();
  21. const t1 = Date.now();
  22. console.log(fmt(str, t1 - t0));
  23. }
  24. function test(correct, got, name) {
  25. if (got !== correct) {
  26. const patch = diff.createPatch(name, correct, got);
  27. process.stderr.write(patch);
  28. process.exit(-1);
  29. }
  30. }
  31. const renameOptions = [
  32. {"from": "$a", "to": "$aRenamed"},
  33. {"from": "$b", "to": "$bRenamed"},
  34. {"from": "$c", "to": "$cRenamed"},
  35. {"from": "$d", "to": "$dRenamed"},
  36. {"from": "$e", "to": "$eRenamed"},
  37. {"from": "$f", "to": "$fRenamed"},
  38. {"from": "$g", "to": "$gRenamed"},
  39. {"from": "$h", "to": "$hRenamed"},
  40. {"from": "$i", "to": "$iRenamed"},
  41. ];
  42. function testSourcemap(original, got, sourcemap) {
  43. const smc = new SourceMapConsumer(sourcemap);
  44. function stringRegExp(commentText) {
  45. return new RegExp("\"" + commentText + "\"");
  46. }
  47. function functionRegExp(functionName) {
  48. return new RegExp("(function)?\\(" + functionName + "_param1, " + functionName + "_param2\\)")
  49. }
  50. function testMapping(needle) {
  51. const gotResult = needle.exec(got);
  52. if (gotResult == null) {
  53. process.stderr.write(fmt("Couldn't find {0} in output source", needle));
  54. process.exit(-1);
  55. }
  56. const expectedResult = needle.exec(original);
  57. if (expectedResult == null) {
  58. process.stderr.write(fmt("Couldn't find {0} in expected source", needle));
  59. process.exit(-1);
  60. }
  61. const gotPosition = findLineColumn(got, gotResult.index);
  62. const originalPosition = smc.originalPositionFor({ line: gotPosition.line, column: gotPosition.col });
  63. const expectedPosition = findLineColumn(original, expectedResult.index);
  64. if (originalPosition.line !== expectedPosition.line || originalPosition.column !== expectedPosition.col) {
  65. process.stderr.write(fmt("Sourcemap mapping error for {0}. Expected: ({1},{2}) => ({3},{4}). Got: ({5},{6}) => ({3},{4}).",
  66. needle,
  67. expectedPosition.line, expectedPosition.col,
  68. gotPosition.line, gotPosition.col,
  69. originalPosition.line, originalPosition.column));
  70. process.exit(-1);
  71. }
  72. }
  73. testMapping(stringRegExp("before"));
  74. for (let i = 1; i <= 4; i++) {
  75. testMapping(functionRegExp("ctrl" + i));
  76. testMapping(stringRegExp("ctrl" + i + " body"));
  77. }
  78. testMapping(stringRegExp("after"));
  79. }
  80. function run(ngAnnotate) {
  81. const original = slurp("tests/original.js");
  82. console.log("testing adding annotations");
  83. const annotated = ngAnnotate(original, {add: true}).src;
  84. test(slurp("tests/with_annotations.js"), annotated, "with_annotations.js");
  85. const rename = slurp("tests/rename.js");
  86. console.log("testing adding annotations and renaming");
  87. const annotatedRenamed = ngAnnotate(rename, {
  88. add: true,
  89. rename: renameOptions,
  90. }).src;
  91. test(slurp("tests/rename.annotated.js"), annotatedRenamed, "rename.annotated.js");
  92. console.log("testing removing annotations");
  93. test(original, ngAnnotate(annotated, {remove: true}).src, "original.js");
  94. console.log("testing adding annotations twice");
  95. test(annotated, ngAnnotate(annotated, {add: true}).src, "with_annotations.js");
  96. console.log("testing rebuilding annotations");
  97. test(annotated, ngAnnotate(annotated, {add: true, remove: true}).src, "with_annotations.js");
  98. console.log("testing adding existing $inject annotations (no change)");
  99. test(slurp("tests/has_inject.js"), ngAnnotate(slurp("tests/has_inject.js"), {add: true}).src);
  100. console.log("testing removing existing $inject annotations");
  101. test(slurp("tests/has_inject_removed.js"), ngAnnotate(slurp("tests/has_inject.js"), {remove: true}).src);
  102. console.log("testing sourcemaps");
  103. const originalSourcemaps = slurp("tests/sourcemaps.coffee");
  104. const compiledSourcemaps = coffee.compile(originalSourcemaps, { sourceFiles: ["sourcemaps.coffee"], generatedFile: "sourcemaps.js", sourceMap: true });
  105. const annotatedSourcemaps = ngAnnotate(compiledSourcemaps.js, {remove: true, add: true, sourcemap: { sourceRoot: "/source/root/dir" }});
  106. test(slurp("tests/sourcemaps.annotated.js"), annotatedSourcemaps.src, "sourcemaps.annotated.js");
  107. testSourcemap(compiledSourcemaps.js, annotatedSourcemaps.src, annotatedSourcemaps.map, "sourcemaps.annotated.js.map");
  108. console.log("testing sourcemap combination");
  109. const inlinedCompiledSourcemaps = compiledSourcemaps.js +
  110. os.EOL +
  111. convertSourceMap.fromJSON(compiledSourcemaps.v3SourceMap).toComment();
  112. const combinedSourcemaps = ngAnnotate(inlinedCompiledSourcemaps, {remove: true, add: true, sourcemap: { inline: true, inFile: "sourcemaps.js", sourceRoot: "/source/root/dir" }});
  113. const combinedSourcemapsSrc = convertSourceMap.removeMapFileComments(combinedSourcemaps.src);
  114. const combinedSourcemapsMap = convertSourceMap.fromSource(combinedSourcemaps.src).toJSON();
  115. testSourcemap(originalSourcemaps, combinedSourcemapsSrc, combinedSourcemapsMap, "sourcemaps.annotated.js.map");
  116. const ngminOriginal = slurp("tests/ngmin-tests/ngmin_original.js");
  117. console.log("testing adding annotations (imported tests)");
  118. const ngminAnnotated = ngAnnotate(ngminOriginal, {add: true, regexp: "^myMod"}).src;
  119. test(slurp("tests/ngmin-tests/ngmin_with_annotations.js"), ngminAnnotated, "ngmin_with_annotations.js");
  120. console.log("testing removing annotations (imported tests)");
  121. test(ngminOriginal, ngAnnotate(ngminAnnotated, {remove: true, regexp: "^myMod"}).src, "ngmin_original.js");
  122. if (fs.existsSync("package.json")) {
  123. console.log("testing package.json")
  124. try {
  125. const json = JSON.parse(slurp("package.json"));
  126. const substr = JSON.stringify({
  127. dependencies: json.dependencies,
  128. devDependencies: json.devDependencies,
  129. }, null, 4);
  130. if (/\^/g.test(substr)) {
  131. console.error("package.json error: shouldn't use the ^ operator");
  132. console.error(substr);
  133. process.exit(-1);
  134. }
  135. } catch (e) {
  136. console.error("package.json error: invalid json");
  137. process.exit(-1);
  138. }
  139. }
  140. if (fs.existsSync("tests/angular.js")) {
  141. console.log("testing performance");
  142. const ng1 = String(fs.readFileSync("tests/angular.js"));
  143. const ng5 = ng1 + ng1 + ng1 + ng1 + ng1;
  144. time(" ng1 processed in {0} ms", function() { ngAnnotate(ng1, {add: true}) });
  145. time(" ng1 processed with sourcemaps in {0} ms", function() { ngAnnotate(ng1, {add: true, sourcemap: true}) });
  146. //time(" ng5 processed in {0} ms", function() { ngAnnotate(ng5, {add: true}) });
  147. //time(" ng5 processed with sourcemaps in {0} ms", function() { ngAnnotate(ng5, {add: true, sourcemap: true}) });
  148. }
  149. console.log("all ok");
  150. }
  151. run(ngAnnotate);