Debian Codegraph @

nyc/test/helpers/run-nyc.js

raw
1 'use strict'
2
3 const { nycBin, fixturesCLI } = require('./paths')
4 const spawn = require('./spawn')
5
6 const envPath = {
7 PATH: process.env.PATH
8 }
9
10 // Work around a Windows issue with `APPDATA`,
11 // https://github.com/istanbuljs/nyc/issues/1248
12 if ('APPDATA' in process.env) {
13 envPath.APPDATA = process.env.APPDATA
14 }
15
16 function sanitizeString (str, cwd, leavePathSep) {
17 /*
18 * File paths are different on different systems:
19 * - make everything relative to cwd
20 * - replace full node path with 'node'
21 * - replace all Windows path separators ('\\') with POSIX path separators
22 */
23 str = str
24 .split(cwd).join('.')
25 .split(process.execPath).join('node')
26
27 if (!leavePathSep) {
28 str = str.replace(/\\/g, '/')
29 }
30
31 return str
32 }
33
34 async function runNYC ({ args, tempDir, leavePathSep, cwd = fixturesCLI, env = {} }) {
35 const runArgs = [nycBin].concat(tempDir ? ['--temp-dir', tempDir] : [], args)
36 const { status, stderr, stdout } = await spawn(process.execPath, runArgs, {
37 cwd: cwd,
38 env: {
39 ...envPath,
40 ...env
41 }
42 })
43
44 return {
45 status,
46 originalText: {
47 stderr,
48 stdout
49 },
50 stderr: sanitizeString(stderr, cwd, leavePathSep),
51 stdout: sanitizeString(stdout, cwd, leavePathSep)
52 }
53 }
54
55 module.exports = runNYC