All files / lib/internal/main test_runner.js

97.51% Statements 157/161
97.29% Branches 36/37
100% Functions 5/5
97.51% Lines 157/161

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 1626x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 44x 44x 44x 44x 26x 26x 26x 18x 18x 43x 17x 17x 17x 4x 4x 13x 13x 13x 13x 13x 13x 13x 17x 4x 4x 13x 13x 13x 17x 37x 37x 13x 13x 13x 44x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 7x 7x 7x 7x 6x 1x 1x 1x 1x       5x 5x 6x 6x 26x 26x 26x 6x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x   18x 18x 18x 18x 18x 18x 86x 18x 18x 18x 14x 18x 18x 18x 18x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 12x 12x 18x 18x 18x 18x 6x 6x 6x 6x 6x 18x 18x 6x  
'use strict';
const {
  ArrayFrom,
  ArrayPrototypeFilter,
  ArrayPrototypeIncludes,
  ArrayPrototypePush,
  ArrayPrototypeSlice,
  ArrayPrototypeSort,
  Promise,
  PromiseAll,
  SafeArrayIterator,
  SafeSet,
} = primordials;
const {
  prepareMainThreadExecution,
} = require('internal/bootstrap/pre_execution');
const { spawn } = require('child_process');
const { readdirSync, statSync } = require('fs');
const { finished } = require('internal/streams/end-of-stream');
const console = require('internal/console/global');
const {
  codes: {
    ERR_TEST_FAILURE,
  },
} = require('internal/errors');
const { test } = require('internal/test_runner/harness');
const { kSubtestsFailed } = require('internal/test_runner/test');
const {
  isSupportedFileType,
  doesPathMatchFilter,
} = require('internal/test_runner/utils');
const { basename, join, resolve } = require('path');
const kFilterArgs = ['--test'];
 
prepareMainThreadExecution(false);
markBootstrapComplete();
 
// TODO(cjihrig): Replace this with recursive readdir once it lands.
function processPath(path, testFiles, options) {
  const stats = statSync(path);
 
  if (stats.isFile()) {
    if (options.userSupplied ||
        (options.underTestDir && isSupportedFileType(path)) ||
        doesPathMatchFilter(path)) {
      testFiles.add(path);
    }
  } else if (stats.isDirectory()) {
    const name = basename(path);
 
    if (!options.userSupplied && name === 'node_modules') {
      return;
    }
 
    // 'test' directories get special treatment. Recursively add all .js,
    // .cjs, and .mjs files in the 'test' directory.
    const isTestDir = name === 'test';
    const { underTestDir } = options;
    const entries = readdirSync(path);
 
    if (isTestDir) {
      options.underTestDir = true;
    }
 
    options.userSupplied = false;
 
    for (let i = 0; i < entries.length; i++) {
      processPath(join(path, entries[i]), testFiles, options);
    }
 
    options.underTestDir = underTestDir;
  }
}
 
function createTestFileList() {
  const cwd = process.cwd();
  const hasUserSuppliedPaths = process.argv.length > 1;
  const testPaths = hasUserSuppliedPaths ?
    ArrayPrototypeSlice(process.argv, 1) : [cwd];
  const testFiles = new SafeSet();
 
  try {
    for (let i = 0; i < testPaths.length; i++) {
      const absolutePath = resolve(testPaths[i]);
 
      processPath(absolutePath, testFiles, { userSupplied: true });
    }
  } catch (err) {
    if (err?.code === 'ENOENT') {
      console.error(`Could not find '${err.path}'`);
      process.exit(1);
    }

    throw err;
  }
 
  return ArrayPrototypeSort(ArrayFrom(testFiles));
}
 
function filterExecArgv(arg) {
  return !ArrayPrototypeIncludes(kFilterArgs, arg);
}
 
function runTestFile(path) {
  return test(path, () => {
    return new Promise((resolve, reject) => {
      const args = ArrayPrototypeFilter(process.execArgv, filterExecArgv);
      ArrayPrototypePush(args, path);
 
      const child = spawn(process.execPath, args);
      // TODO(cjihrig): Implement a TAP parser to read the child's stdout
      // instead of just displaying it all if the child fails.
      let stdout = '';
      let stderr = '';
      let err;
 
      child.on('error', (error) => {
        err = error;
      });
 
      child.stdout.setEncoding('utf8');
      child.stderr.setEncoding('utf8');
 
      child.stdout.on('data', (chunk) => {
        stdout += chunk;
      });
 
      child.stderr.on('data', (chunk) => {
        stderr += chunk;
      });
 
      child.once('exit', async (code, signal) => {
        if (code !== 0 || signal !== null) {
          if (!err) {
            await PromiseAll(new SafeArrayIterator([finished(child.stderr), finished(child.stdout)]));
            err = new ERR_TEST_FAILURE('test failed', kSubtestsFailed);
            err.exitCode = code;
            err.signal = signal;
            err.stdout = stdout;
            err.stderr = stderr;
            // The stack will not be useful since the failures came from tests
            // in a child process.
            err.stack = undefined;
          }
 
          return reject(err);
        }
 
        resolve();
      });
    });
  });
}
 
(async function main() {
  const testFiles = createTestFileList();
 
  for (let i = 0; i < testFiles.length; i++) {
    runTestFile(testFiles[i]);
  }
})();