list.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utilsBundle = require("playwright-core/lib/utilsBundle");
  7. var _base = require("./base");
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. // Allow it in the Visual Studio Code Terminal and the new Windows Terminal
  24. const DOES_NOT_SUPPORT_UTF8_IN_TERMINAL = process.platform === 'win32' && process.env.TERM_PROGRAM !== 'vscode' && !process.env.WT_SESSION;
  25. const POSITIVE_STATUS_MARK = DOES_NOT_SUPPORT_UTF8_IN_TERMINAL ? 'ok' : '✓';
  26. const NEGATIVE_STATUS_MARK = DOES_NOT_SUPPORT_UTF8_IN_TERMINAL ? 'x' : '✘';
  27. class ListReporter extends _base.BaseReporter {
  28. constructor(options = {}) {
  29. super(options);
  30. this._lastRow = 0;
  31. this._lastColumn = 0;
  32. this._testRows = new Map();
  33. this._stepRows = new Map();
  34. this._resultIndex = new Map();
  35. this._stepIndex = new Map();
  36. this._needNewLine = false;
  37. this._printSteps = void 0;
  38. this._printSteps = _base.isTTY && (options.printSteps || !!process.env.PW_TEST_DEBUG_REPORTERS_PRINT_STEPS);
  39. }
  40. printsToStdio() {
  41. return true;
  42. }
  43. onBegin(suite) {
  44. super.onBegin(suite);
  45. const startingMessage = this.generateStartingMessage();
  46. if (startingMessage) {
  47. console.log(startingMessage);
  48. console.log();
  49. }
  50. }
  51. onTestBegin(test, result) {
  52. super.onTestBegin(test, result);
  53. if (!_base.isTTY) return;
  54. this._maybeWriteNewLine();
  55. const index = String(this._resultIndex.size + 1);
  56. this._resultIndex.set(result, index);
  57. this._testRows.set(test, this._lastRow);
  58. const prefix = this._testPrefix(index, '');
  59. const line = _base.colors.dim((0, _base.formatTestTitle)(this.config, test)) + this._retrySuffix(result);
  60. this._appendLine(line, prefix);
  61. }
  62. onStdOut(chunk, test, result) {
  63. super.onStdOut(chunk, test, result);
  64. this._dumpToStdio(test, chunk, process.stdout);
  65. }
  66. onStdErr(chunk, test, result) {
  67. super.onStdErr(chunk, test, result);
  68. this._dumpToStdio(test, chunk, process.stderr);
  69. }
  70. onStepBegin(test, result, step) {
  71. super.onStepBegin(test, result, step);
  72. if (step.category !== 'test.step') return;
  73. const testIndex = this._resultIndex.get(result) || '';
  74. if (!this._printSteps) {
  75. if (_base.isTTY) this._updateLine(this._testRows.get(test), _base.colors.dim((0, _base.formatTestTitle)(this.config, test, step)) + this._retrySuffix(result), this._testPrefix(testIndex, ''));
  76. return;
  77. }
  78. const ordinal = (result[lastStepOrdinalSymbol] || 0) + 1;
  79. result[lastStepOrdinalSymbol] = ordinal;
  80. const stepIndex = `${testIndex}.${ordinal}`;
  81. this._stepIndex.set(step, stepIndex);
  82. if (_base.isTTY) {
  83. this._maybeWriteNewLine();
  84. this._stepRows.set(step, this._lastRow);
  85. const prefix = this._testPrefix(stepIndex, '');
  86. const line = test.title + _base.colors.dim((0, _base.stepSuffix)(step));
  87. this._appendLine(line, prefix);
  88. }
  89. }
  90. onStepEnd(test, result, step) {
  91. super.onStepEnd(test, result, step);
  92. if (step.category !== 'test.step') return;
  93. const testIndex = this._resultIndex.get(result) || '';
  94. if (!this._printSteps) {
  95. if (_base.isTTY) this._updateLine(this._testRows.get(test), _base.colors.dim((0, _base.formatTestTitle)(this.config, test, step.parent)) + this._retrySuffix(result), this._testPrefix(testIndex, ''));
  96. return;
  97. }
  98. const index = this._stepIndex.get(step);
  99. const title = test.title + _base.colors.dim((0, _base.stepSuffix)(step));
  100. const prefix = this._testPrefix(index, '');
  101. let text = '';
  102. if (step.error) text = _base.colors.red(title);else text = title;
  103. text += _base.colors.dim(` (${(0, _utilsBundle.ms)(step.duration)})`);
  104. this._updateOrAppendLine(this._stepRows.get(step), text, prefix);
  105. }
  106. _maybeWriteNewLine() {
  107. if (this._needNewLine) {
  108. this._needNewLine = false;
  109. process.stdout.write('\n');
  110. }
  111. }
  112. _updateLineCountAndNewLineFlagForOutput(text) {
  113. this._needNewLine = text[text.length - 1] !== '\n';
  114. if (!_base.ttyWidth) return;
  115. for (const ch of text) {
  116. if (ch === '\n') {
  117. this._lastColumn = 0;
  118. ++this._lastRow;
  119. continue;
  120. }
  121. ++this._lastColumn;
  122. if (this._lastColumn > _base.ttyWidth) {
  123. this._lastColumn = 0;
  124. ++this._lastRow;
  125. }
  126. }
  127. }
  128. _dumpToStdio(test, chunk, stream) {
  129. if (this.config.quiet) return;
  130. const text = chunk.toString('utf-8');
  131. this._updateLineCountAndNewLineFlagForOutput(text);
  132. stream.write(chunk);
  133. }
  134. onTestEnd(test, result) {
  135. super.onTestEnd(test, result);
  136. const title = (0, _base.formatTestTitle)(this.config, test);
  137. let prefix = '';
  138. let text = '';
  139. // In TTY mode test index is incremented in onTestStart
  140. // and in non-TTY mode it is incremented onTestEnd.
  141. let index = this._resultIndex.get(result);
  142. if (!index) {
  143. index = String(this._resultIndex.size + 1);
  144. this._resultIndex.set(result, index);
  145. }
  146. if (result.status === 'skipped') {
  147. prefix = this._testPrefix(index, _base.colors.green('-'));
  148. // Do not show duration for skipped.
  149. text = _base.colors.cyan(title) + this._retrySuffix(result);
  150. } else {
  151. const statusMark = result.status === 'passed' ? POSITIVE_STATUS_MARK : NEGATIVE_STATUS_MARK;
  152. if (result.status === test.expectedStatus) {
  153. prefix = this._testPrefix(index, _base.colors.green(statusMark));
  154. text = title;
  155. } else {
  156. prefix = this._testPrefix(index, _base.colors.red(statusMark));
  157. text = _base.colors.red(title);
  158. }
  159. text += this._retrySuffix(result) + _base.colors.dim(` (${(0, _utilsBundle.ms)(result.duration)})`);
  160. }
  161. this._updateOrAppendLine(this._testRows.get(test), text, prefix);
  162. }
  163. _updateOrAppendLine(row, text, prefix) {
  164. if (_base.isTTY) {
  165. this._updateLine(row, text, prefix);
  166. } else {
  167. this._maybeWriteNewLine();
  168. this._appendLine(text, prefix);
  169. }
  170. }
  171. _appendLine(text, prefix) {
  172. const line = prefix + this.fitToScreen(text, prefix);
  173. if (process.env.PW_TEST_DEBUG_REPORTERS) {
  174. process.stdout.write(this._lastRow + ' : ' + line + '\n');
  175. } else {
  176. process.stdout.write(line);
  177. process.stdout.write('\n');
  178. }
  179. ++this._lastRow;
  180. }
  181. _updateLine(row, text, prefix) {
  182. const line = prefix + this.fitToScreen(text, prefix);
  183. if (process.env.PW_TEST_DEBUG_REPORTERS) process.stdout.write(row + ' : ' + line + '\n');else this._updateLineForTTY(row, line);
  184. }
  185. _updateLineForTTY(row, line) {
  186. // Go up if needed
  187. if (row !== this._lastRow) process.stdout.write(`\u001B[${this._lastRow - row}A`);
  188. // Erase line, go to the start
  189. process.stdout.write('\u001B[2K\u001B[0G');
  190. process.stdout.write(line);
  191. // Go down if needed.
  192. if (row !== this._lastRow) process.stdout.write(`\u001B[${this._lastRow - row}E`);
  193. if (process.env.PWTEST_TTY_WIDTH) process.stdout.write('\n'); // For testing.
  194. }
  195. _testPrefix(index, statusMark) {
  196. const statusMarkLength = (0, _base.stripAnsiEscapes)(statusMark).length;
  197. return ' ' + statusMark + ' '.repeat(3 - statusMarkLength) + _base.colors.dim(index + ' ');
  198. }
  199. _retrySuffix(result) {
  200. return result.retry ? _base.colors.yellow(` (retry #${result.retry})`) : '';
  201. }
  202. onError(error) {
  203. super.onError(error);
  204. this._maybeWriteNewLine();
  205. const message = (0, _base.formatError)(error, _base.colors.enabled).message + '\n';
  206. this._updateLineCountAndNewLineFlagForOutput(message);
  207. process.stdout.write(message);
  208. }
  209. async onEnd(result) {
  210. await super.onEnd(result);
  211. process.stdout.write('\n');
  212. this.epilogue(true);
  213. }
  214. }
  215. const lastStepOrdinalSymbol = Symbol('lastStepOrdinal');
  216. var _default = exports.default = ListReporter;