run-external-tests.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. const fs = require("fs");
  3. const globby = require("globby");
  4. const format = require("../src/cli-util").format;
  5. function tryFormat(file) {
  6. const content = fs.readFileSync(file, "utf8");
  7. try {
  8. format({ "debug-check": true }, content, {
  9. // Allow specifying the parser via an environment variable:
  10. parser: process.env.PARSER,
  11. // Use file extension detection otherwise:
  12. filepath: file
  13. });
  14. } catch (error) {
  15. return error;
  16. }
  17. return null;
  18. }
  19. function runExternalTests(patterns) {
  20. const testFiles = globby.sync(patterns);
  21. if (testFiles.length === 0) {
  22. throw new Error(`No matching files. Patterns tried: ${patterns.join(" ")}`);
  23. }
  24. const results = {
  25. good: [],
  26. skipped: [],
  27. bad: []
  28. };
  29. testFiles.forEach(file => {
  30. const error = tryFormat(file);
  31. if (error instanceof SyntaxError) {
  32. results.skipped.push({ file, error });
  33. } else if (error) {
  34. results.bad.push({ file, error });
  35. } else {
  36. results.good.push({ file });
  37. }
  38. process.stderr.write(
  39. `\r${results.good.length} good, ${results.skipped.length} skipped, ${
  40. results.bad.length
  41. } bad`
  42. );
  43. });
  44. return results;
  45. }
  46. function run(argv) {
  47. if (argv.length === 0) {
  48. console.error(
  49. [
  50. "You must provide at least one file or glob for test files!",
  51. "Examples:",
  52. ' node scripts/run-external-tests.js "../TypeScript/tests/**/*.ts"',
  53. ' node scripts/run-external-tests.js "../flow/tests/**/*.js"',
  54. ' PARSER=flow node scripts/run-external-tests.js "../flow/tests/**/*.js"'
  55. ].join("\n")
  56. );
  57. return 1;
  58. }
  59. let results = null;
  60. try {
  61. results = runExternalTests(argv);
  62. } catch (error) {
  63. console.error(`Failed to run external tests.\n${error}`);
  64. return 1;
  65. }
  66. console.log("");
  67. console.log(
  68. results.bad.map(data => `${data.file}\n${data.error}`).join("\n\n\n")
  69. );
  70. return 0;
  71. }
  72. if (require.main === module) {
  73. const exitCode = run(process.argv.slice(2));
  74. process.exit(exitCode);
  75. }