Debian Codegraph @

nyc/test/issue-190.js

raw
1 'use strict'
2
3 const fs = require('../lib/fs-promises')
4 const path = require('path')
5
6 const t = require('tap')
7 const isWindows = require('is-windows')()
8
9 const { spawn, fixturesCLI, nycBin } = require('./helpers')
10
11 const fakebin = path.resolve(fixturesCLI, 'fakebin')
12
13 const spawnOptions = {
14 cwd: path.resolve(fixturesCLI, 'run-npm-test-recursive'),
15 env: {
16 PATH: fakebin + ':' + process.env.PATH
17 }
18 }
19
20 async function writeFakeNPM (shebang) {
21 const targetPath = path.resolve(fakebin, 'npm')
22 const source = await fs.readFile(path.resolve(fakebin, 'npm-template.js'))
23 await fs.writeFile(targetPath, '#!' + shebang + '\n' + source)
24 await fs.chmod(targetPath, 493) // 0o755
25 }
26
27 async function runFakeNPM (t) {
28 const args = [nycBin, 'npm', 'test']
29 const { stderr, status } = await spawn(process.execPath, args, spawnOptions)
30
31 t.strictEqual(status, 0)
32 t.strictEqual(stderr, '')
33 }
34
35 t.beforeEach(() => Promise.all([
36 fs.unlink(path.resolve(fakebin, 'node')).catch(() => {}),
37 fs.unlink(path.resolve(fakebin, 'npm')).catch(() => {})
38 ]))
39
40 t.test('can run "npm test", absolute shebang edition', async t => {
41 if (isWindows) {
42 return
43 }
44
45 await writeFakeNPM(process.execPath)
46 await runFakeNPM(t)
47 })
48
49 t.test('can run "npm test", weird bash+dirname shebang edition', async t => {
50 if (isWindows) {
51 return
52 }
53
54 // This string is taken verbatim from tools/install.py in Node core v5.x
55 await writeFakeNPM('/bin/sh\n// 2>/dev/null; exec "`dirname "$0"`/node" "$0" "$@"')
56 await fs.symlink(process.execPath, path.resolve(fakebin, 'node'))
57 await runFakeNPM(t)
58 })