All files / lib/internal/test_runner tap_stream.js

99.13% Statements 230/232
96.61% Branches 57/59
94.44% Functions 17/18
99.13% Lines 230/232

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 23397x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 128x 128x 128x 128x 128x 97x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 243x 243x 243x 13x 13x 13x 10x 10x 13x 243x 128x 128x     128x 128x 128x 128x 128x 128x 128x 128x 518x 518x 518x 518x 128x 128x 176x 176x 176x 176x 128x 128x 57x 57x 128x 128x 10x 10x 128x 128x 646x 646x 128x 128x 646x 646x 646x 646x 646x 646x 646x 128x 128x 716x 716x 716x 128x 128x 101x 101x 128x 128x 646x 646x 646x 646x 646x 646x 646x 67x 646x 646x 646x 646x 646x 128x 128x 2944x 2931x 2944x 13x 13x 2944x 2944x 2944x 128x 97x 97x 2046x 2046x 2046x 2046x 2046x 97x 1696x 1696x 526x 526x 1170x 1319x 1042x 1042x 1042x 666x 666x 376x 376x 376x 1042x 363x 363x 13x 13x 13x 433x 196x 196x 13x 13x 13x 128x 128x 128x 128x 128x 942x 380x 380x 380x 232x 232x 148x 148x 148x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 62x 62x 62x 46x 46x 62x 128x 128x 128x 128x 128x 128x 128x 128x 120x 120x 120x 120x 120x 683x 683x 683x 683x 683x 548x 548x 683x 120x 120x 120x 80x 80x 80x 80x 80x 120x 128x 128x 128x 1696x 97x 97x  
'use strict';
const {
  ArrayPrototypeForEach,
  ArrayPrototypeJoin,
  ArrayPrototypeMap,
  ArrayPrototypePush,
  ArrayPrototypeShift,
  ObjectEntries,
  StringPrototypeReplaceAll,
  StringPrototypeSplit,
  RegExpPrototypeSymbolReplace,
} = primordials;
const { inspectWithNoCustomRetry } = require('internal/errors');
const Readable = require('internal/streams/readable');
const { isError, kEmptyObject } = require('internal/util');
const kFrameStartRegExp = /^ {4}at /;
const kLineBreakRegExp = /\n|\r\n/;
const inspectOptions = { colors: false, breakLength: Infinity };
let testModule; // Lazy loaded due to circular dependency.
 
function lazyLoadTest() {
  testModule ??= require('internal/test_runner/test');
 
  return testModule;
}
 
class TapStream extends Readable {
  #buffer;
  #canPush;
 
  constructor() {
    super();
    this.#buffer = [];
    this.#canPush = true;
  }
 
  _read() {
    this.#canPush = true;
 
    while (this.#buffer.length > 0) {
      const line = ArrayPrototypeShift(this.#buffer);
 
      if (!this.#tryPush(line)) {
        return;
      }
    }
  }
 
  bail(message) {
    this.#tryPush(`Bail out!${message ? ` ${tapEscape(message)}` : ''}\n`);
  }
 
  fail(indent, testNumber, name, duration, error, directive) {
    this.emit('test:fail', { __proto__: null, name, testNumber, duration, ...directive, error });
    this.#test(indent, testNumber, 'not ok', name, directive);
    this.#details(indent, duration, error);
  }
 
  ok(indent, testNumber, name, duration, directive) {
    this.emit('test:pass', { __proto__: null, name, testNumber, duration, ...directive });
    this.#test(indent, testNumber, 'ok', name, directive);
    this.#details(indent, duration, null);
  }
 
  plan(indent, count, explanation) {
    const exp = `${explanation ? ` # ${tapEscape(explanation)}` : ''}`;
 
    this.#tryPush(`${indent}1..${count}${exp}\n`);
  }
 
  getSkip(reason) {
    return { __proto__: null, skip: reason };
  }
 
  getTodo(reason) {
    return { __proto__: null, todo: reason };
  }
 
  subtest(indent, name) {
    this.#tryPush(`${indent}# Subtest: ${tapEscape(name)}\n`);
  }
 
  #details(indent, duration, error) {
    let details = `${indent}  ---\n`;
 
    details += jsToYaml(indent, 'duration_ms', duration);
    details += jsToYaml(indent, null, error);
    details += `${indent}  ...\n`;
    this.#tryPush(details);
  }
 
  diagnostic(indent, message) {
    this.emit('test:diagnostic', message);
    this.#tryPush(`${indent}# ${tapEscape(message)}\n`);
  }
 
  version() {
    this.#tryPush('TAP version 13\n');
  }
 
  #test(indent, testNumber, status, name, directive = kEmptyObject) {
    let line = `${indent}${status} ${testNumber}`;
 
    if (name) {
      line += ` ${tapEscape(`- ${name}`)}`;
    }
 
    line += ArrayPrototypeJoin(ArrayPrototypeMap(ObjectEntries(directive), ({ 0: key, 1: value }) => (
      ` # ${key.toUpperCase()}${value ? ` ${tapEscape(value)}` : ''}`
    )), '');
 
    line += '\n';
    this.#tryPush(line);
  }
 
  #tryPush(message) {
    if (this.#canPush) {
      this.#canPush = this.push(message);
    } else {
      ArrayPrototypePush(this.#buffer, message);
    }
 
    return this.#canPush;
  }
}
 
// In certain places, # and \ need to be escaped as \# and \\.
function tapEscape(input) {
  return StringPrototypeReplaceAll(
    StringPrototypeReplaceAll(input, '\\', '\\\\'), '#', '\\#'
  );
}
 
function jsToYaml(indent, name, value) {
  if (value === null || value === undefined) {
    return '';
  }
 
  if (typeof value !== 'object') {
    const prefix = `${indent}  ${name}: `;
 
    if (typeof value !== 'string') {
      return `${prefix}${inspectWithNoCustomRetry(value, inspectOptions)}\n`;
    }
 
    const lines = StringPrototypeSplit(value, kLineBreakRegExp);
 
    if (lines.length === 1) {
      return `${prefix}${inspectWithNoCustomRetry(value, inspectOptions)}\n`;
    }
 
    let str = `${prefix}|-\n`;
 
    for (let i = 0; i < lines.length; i++) {
      str += `${indent}    ${lines[i]}\n`;
    }
 
    return str;
  }
 
  const entries = ObjectEntries(value);
  const isErrorObj = isError(value);
  let result = '';
 
  for (let i = 0; i < entries.length; i++) {
    const { 0: key, 1: value } = entries[i];
 
    if (isErrorObj && (key === 'cause' || key === 'code')) {
      continue;
    }
 
    result += jsToYaml(indent, key, value);
  }
 
  if (isErrorObj) {
    const { kTestCodeFailure, kUnwrapErrors } = lazyLoadTest();
    const {
      cause,
      code,
      failureType,
      message,
      stack,
    } = value;
    let errMsg = message ?? '<unknown error>';
    let errStack = stack;
    let errCode = code;
 
    // If the ERR_TEST_FAILURE came from an error provided by user code,
    // then try to unwrap the original error message and stack.
    if (code === 'ERR_TEST_FAILURE' && kUnwrapErrors.has(failureType)) {
      errStack = cause?.stack ?? errStack;
      errCode = cause?.code ?? errCode;
      if (failureType === kTestCodeFailure) {
        errMsg = cause?.message ?? errMsg;
      }
    }
 
    result += jsToYaml(indent, 'error', errMsg);
 
    if (errCode) {
      result += jsToYaml(indent, 'code', errCode);
    }
 
    if (typeof errStack === 'string') {
      const frames = [];
 
      ArrayPrototypeForEach(
        StringPrototypeSplit(errStack, kLineBreakRegExp),
        (frame) => {
          const processed = RegExpPrototypeSymbolReplace(
            kFrameStartRegExp, frame, ''
          );
 
          if (processed.length > 0 && processed.length !== frame.length) {
            ArrayPrototypePush(frames, processed);
          }
        }
      );
 
      if (frames.length > 0) {
        const frameDelimiter = `\n${indent}    `;
 
        result += `${indent}  stack: |-${frameDelimiter}`;
        result += `${ArrayPrototypeJoin(frames, `${frameDelimiter}`)}\n`;
      }
    }
  }
 
  return result;
}
 
module.exports = { TapStream };