All files / lib/internal/test_runner tap_stream.js

93.78% Statements 181/193
85.71% Branches 36/42
93.75% Functions 15/16
93.78% Lines 181/193

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 1948x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 25x 25x 25x 25x 25x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 16x 16x 16x             16x 8x 8x     8x 8x 25x 25x 8x 8x 43x 43x 8x 8x 10x 10x 10x 10x 8x 8x 10x 10x 8x 8x 5x 5x 8x 8x 68x 68x 68x 68x 68x 25x 25x 25x 25x 75x 75x 75x 50x 50x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 13x 13x 13x 13x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 132x 132x 132x 132x 132x 101x 101x 132x 25x 25x 25x 19x 19x 19x 19x 19x 25x 25x 68x     68x 68x 68x 68x 8x 8x 39x 39x 8x 8x 5x 5x 8x 8x 68x 68x 68x 68x 68x 68x 68x 15x 15x 68x 68x 68x 68x 8x 8x 190x 190x 190x     190x 190x 190x 8x 8x 8x 113x 113x 113x 113x 113x 8x 8x  
'use strict';
const {
  ArrayPrototypeForEach,
  ArrayPrototypeJoin,
  ArrayPrototypePush,
  ArrayPrototypeShift,
  ObjectEntries,
  StringPrototypeReplace,
  StringPrototypeReplaceAll,
  StringPrototypeSplit,
} = primordials;
const Readable = require('internal/streams/readable');
const { isError } = require('internal/util');
const { inspect } = require('internal/util/inspect');
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, description, directive) {
    this.#test(indent, testNumber, 'not ok', description, directive);
  }
 
  ok(indent, testNumber, description, directive) {
    this.#test(indent, testNumber, 'ok', description, directive);
  }
 
  plan(indent, count, explanation) {
    const exp = `${explanation ? ` # ${tapEscape(explanation)}` : ''}`;
 
    this.#tryPush(`${indent}1..${count}${exp}\n`);
  }
 
  getSkip(reason) {
    return `SKIP${reason ? ` ${tapEscape(reason)}` : ''}`;
  }
 
  getTodo(reason) {
    return `TODO${reason ? ` ${tapEscape(reason)}` : ''}`;
  }
 
  details(indent, duration, error) {
    let details = `${indent}  ---\n`;
 
    details += `${indent}  duration_ms: ${duration}\n`;
 
    if (error !== null && typeof error === 'object') {
      const entries = ObjectEntries(error);
      const isErrorObj = isError(error);
 
      for (let i = 0; i < entries.length; i++) {
        const { 0: key, 1: value } = entries[i];
 
        if (isError && (key === 'cause' || key === 'code')) {
          continue;
        }
 
        details += `${indent}  ${key}: ${inspect(value, inspectOptions)}\n`;
      }
 
      if (isErrorObj) {
        const { kTestCodeFailure } = lazyLoadTest();
        const {
          cause,
          code,
          failureType,
          message,
          stack,
        } = error;
        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' && failureType === kTestCodeFailure) {
          errMsg = cause?.message ?? errMsg;
          errStack = cause?.stack ?? errStack;
          errCode = cause?.code ?? errCode;
        }
 
        details += `${indent}  error: ${inspect(errMsg, inspectOptions)}\n`;
 
        if (errCode) {
          details += `${indent}  code: ${errCode}\n`;
        }
 
        if (typeof errStack === 'string') {
          const frames = [];
 
          ArrayPrototypeForEach(
            StringPrototypeSplit(errStack, kLineBreakRegExp),
            (frame) => {
              const processed = StringPrototypeReplace(
                frame, kFrameStartRegExp, ''
              );
 
              if (processed.length > 0 && processed.length !== frame.length) {
                ArrayPrototypePush(frames, processed);
              }
            }
          );
 
          if (frames.length > 0) {
            const frameDelimiter = `\n${indent}    `;
 
            details += `${indent}  stack: |-${frameDelimiter}`;
            details += `${ArrayPrototypeJoin(frames, `${frameDelimiter}`)}\n`;
          }
        }
      }
    } else if (error !== null && error !== undefined) {
      details += `${indent}  error: ${inspect(error, inspectOptions)}\n`;
    }
 
    details += `${indent}  ...\n`;
    this.#tryPush(details);
  }
 
  diagnostic(indent, message) {
    this.#tryPush(`${indent}# ${tapEscape(message)}\n`);
  }
 
  version() {
    this.#tryPush('TAP version 13\n');
  }
 
  #test(indent, testNumber, status, description, directive) {
    let line = `${indent}${status} ${testNumber}`;
 
    if (description) {
      line += ` ${tapEscape(description)}`;
    }
 
    if (directive) {
      line += ` # ${directive}`;
    }
 
    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, '\\', '\\\\'), '#', '\\#'
  );
}
 
module.exports = { TapStream };