run-tests.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const cp = require('child_process')
  2. function runAllSpecs(files) {
  3. runSpecs(files, [])
  4. }
  5. function runSpecs(files, retries) {
  6. let env = process.env
  7. env.ATOM_JASMINE_REPORTER='list'
  8. if(retries.length > 0) {
  9. // Escape possible tests that can generate a regexp that will not match...
  10. const escaped = retries.map(str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
  11. env.SPEC_FILTER = escaped.join("|")
  12. }
  13. const res = cp.spawn('yarn', ['start', '--test', ...files], {
  14. cwd: process.cwd(),
  15. env: env
  16. })
  17. let out;
  18. res.stdout.on('data', data => {
  19. process.stdout.write(data.toString());
  20. });
  21. res.stderr.on('data', data => {
  22. const strData = data.toString();
  23. process.stderr.write(strData);
  24. if(strData.match(/ALL TESTS THAT FAILED:/)) {
  25. out = '';
  26. } else if(out !== undefined) {
  27. out += strData;
  28. }
  29. });
  30. res.on('close', code => {
  31. if(code !== 0 && retries.length === 0) {
  32. const failed = filterSpecs(out)
  33. console.log(`*********************
  34. Tests failed. Retrying failed tests...
  35. *********************
  36. `)
  37. runSpecs(files, failed)
  38. } else {
  39. process.exit(code)
  40. }
  41. });
  42. }
  43. function filterSpecs(output) {
  44. if(!output) return ''
  45. let descriptions = []
  46. let start = true
  47. for(let out of output.split("\n")) {
  48. if(start) {
  49. if(out !== '') {
  50. start = false
  51. descriptions.push(out)
  52. }
  53. } else if(out !== '') {
  54. descriptions.push(out)
  55. } else {
  56. return descriptions
  57. }
  58. }
  59. }
  60. if(process.argv[0] === __filename) {
  61. runAllSpecs(process.argv.splice(1))
  62. } else if(process.argv[1] === __filename) {
  63. runAllSpecs(process.argv.splice(2))
  64. } else {
  65. module.exports = runAllSpecs
  66. }