Skip to content

Commit

Permalink
chore: rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 17, 2024
1 parent a28f9d4 commit 62d8cd8
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 817 deletions.
1 change: 0 additions & 1 deletion map.js

This file was deleted.

27 changes: 0 additions & 27 deletions test/is-server-package.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/is-windows.js

This file was deleted.

287 changes: 115 additions & 172 deletions test/make-spawn-args.js
Original file line number Diff line number Diff line change
@@ -1,181 +1,124 @@
const t = require('tap')
const requireInject = require('require-inject')
const isWindows = require('../lib/is-windows.js')

if (!process.env.__FAKE_TESTING_PLATFORM__) {
const fake = isWindows ? 'posix' : 'win32'
t.spawn(process.execPath, [__filename, fake], { env: {
...process.env,
__FAKE_TESTING_PLATFORM__: fake,
} })
}

const { dirname } = require('path')
const resolve = (...args) => {
const root = isWindows ? 'C:\\Temp' : '/tmp'
return [root, ...args].join(isWindows ? '\\' : '/')
}

const makeSpawnArgs = requireInject('../lib/make-spawn-args.js', {
path: {
dirname,
resolve,
const spawk = require('spawk')
const runScript = require('..')

const pkg = {
name: '@npmcli/run-script-test-package',
version: '1.0.0-test',
config: {
test_string: 'a value',
test_array: ['a string', 'another string'],
test_null: null,
test_false: false,
},
})

if (isWindows) {
t.test('windows', t => {
const comSpec = process.env.ComSpec
process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe'
t.teardown(() => {
process.env.ComSpec = comSpec
})

t.test('simple script', (t) => {
const [cmd, args, opts] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
})
t.equal(cmd, 'script "quoted parameter"; second command')
t.strictSame(args, [])
t.hasStrict(opts, {
env: {
npm_package_json: 'C:\\Temp\\path\\package.json',
npm_lifecycle_event: 'event',
npm_lifecycle_script: 'script "quoted parameter"; second command',
npm_config_node_gyp: require.resolve('node-gyp/bin/node-gyp.js'),
},
shell: true,
stdio: undefined,
cwd: 'path',
}, 'got expected options')

t.end()
})

t.test('event with invalid characters runs', (t) => {
const [cmd, args, opts] = makeSpawnArgs({
event: 'event<:>\x03', // everything after the word "event" is invalid
path: 'path',
cmd: 'script "quoted parameter"; second command',
})
t.equal(cmd, 'script "quoted parameter"; second command')
t.strictSame(args, [])
t.hasStrict(opts, {
env: {
npm_package_json: 'C:\\Temp\\path\\package.json',
npm_lifecycle_event: 'event<:>\x03',
npm_lifecycle_script: 'script "quoted parameter"; second command',
npm_config_node_gyp: require.resolve('node-gyp/bin/node-gyp.js'),
},
shell: true,
stdio: undefined,
cwd: 'path',
}, 'got expected options')

t.end()
})

t.test('with a funky scriptShell', (t) => {
const [cmd, args, opts] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
scriptShell: 'blrpop',
})
t.equal(cmd, 'script "quoted parameter"; second command')
t.strictSame(args, [])
t.hasStrict(opts, {
env: {
npm_package_json: 'C:\\Temp\\path\\package.json',
npm_lifecycle_event: 'event',
npm_lifecycle_script: 'script "quoted parameter"; second command',
},
shell: 'blrpop',
stdio: undefined,
cwd: 'path',
}, 'got expected options')

t.end()
})

t.test('with cmd.exe as scriptShell', (t) => {
const [cmd, args, opts] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
scriptShell: 'cmd.exe',
})
t.equal(cmd, 'script')
t.strictSame(args, ['"quoted parameter";', 'second command'])
t.hasStrict(opts, {
env: {
npm_package_json: 'C:\\Temp\\path\\package.json',
npm_lifecycle_event: 'event',
npm_lifecycle_script: 'script',
},
shell: 'cmd.exe',
stdio: undefined,
cwd: 'path',
}, 'got expected options')

t.end()
})
engines: {
node: '>0.10',
npm: '>1.2.3',
},
bin: 'index.js',
scripts: {
test: 'echo test',
'weird<x>\x04': 'echo weird',
},
}

t.end()
t.test('spawn args', async t => {
const testdir = t.testdir({})
await t.test('defaults', async t => {
spawk.spawn(
/.*/,
a => a.includes('echo test'),
e => {
return e.cwd === testdir &&
e.stdio === 'pipe' &&
e.stdioString === undefined &&
e.shell === false &&
e.env.npm_package_json.endsWith('package.json') &&
e.env.npm_package_name === pkg.name &&
e.env.npm_package_version === pkg.version &&
e.env.npm_package_config_test_null === '' &&
e.env.npm_package_config_test_false === '' &&
e.env.npm_package_config_test_string === pkg.config.test_string &&
e.env.npm_package_config_test_array === pkg.config.test_array.join('\n\n') &&
e.env.npm_package_bin === pkg.bin &&
e.env.npm_package_engines_npm === pkg.engines.npm &&
e.env.npm_package_engines_node === pkg.engines.node &&
e.env.npm_lifecycle_event === 'test' &&
e.env.npm_lifecycle_script === 'echo test' &&
e.env.npm_config_node_gyp === require.resolve('node-gyp/bin/node-gyp.js')
}
)
await t.resolves(() => runScript({
pkg,
path: testdir,
event: 'test',
}))
t.ok(spawk.done())
})
} else {
t.test('posix', t => {
t.test('simple script', (t) => {
const [cmd, args, opts] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
})
t.equal(cmd, 'script')
t.strictSame(args, ['"quoted parameter";', 'second command'])
t.hasStrict(opts, {
env: {
npm_package_json: '/tmp/path/package.json',
npm_lifecycle_event: 'event',
npm_lifecycle_script: 'script',
},
shell: true,
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: undefined,
}, 'got expected options')

t.end()
})
await t.test('provided env', async t => {
spawk.spawn(
/.*/,
a => a.includes('echo test'),
e => {
return e.env.test_fixture === 'a string'
}
)
await t.resolves(() => runScript({
pkg,
path: testdir,
env: {
test_fixture: 'a string',
},
event: 'test',
}))
t.ok(spawk.done())
})

t.test('event with invalid characters runs', (t) => {
const [cmd, args, opts] = makeSpawnArgs({
event: 'event<:>/\x04',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
})
t.equal(cmd, 'script')
t.strictSame(args, ['"quoted parameter";', 'second command'])
t.hasStrict(opts, {
env: {
npm_package_json: '/tmp/path/package.json',
npm_lifecycle_event: 'event<:>/\x04',
npm_lifecycle_script: 'script',
},
shell: true,
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: undefined,
}, 'got expected options')
await t.test('provided args', async t => {
spawk.spawn(
/.*/,
a => a.find(arg => arg.includes('echo test') && arg.includes('argtest'))
)
await t.resolves(() => runScript({
pkg,
path: testdir,
args: ['argtest'],
event: 'test',
}))
t.ok(spawk.done())
})

t.end()
})
t.test('event with invalid characters', async t => {
spawk.spawn(
/.*/,
a => a.includes('echo weird'),
e => {
return e.env.npm_lifecycle_event === 'weird<x>\x04' &&
e.env.npm_lifecycle_script === 'echo weird'
}
)
await t.resolves(() => runScript({
pkg,
path: testdir,
event: 'weird<x>\x04',
}))
t.ok(spawk.done())
})

t.end()
await t.test('provided binPaths', async t => {
spawk.spawn(
/.*/,
false,
e => e.env.PATH.startsWith('/tmp/test-fixture/binpath')
)
await t.resolves(() => runScript({
pkg,
binPaths: ['/tmp/test-fixture/binpath'],
path: testdir,
args: ['test arg'],
event: 'test',
}))
t.ok(spawk.done())
})
}
})
17 changes: 0 additions & 17 deletions test/node-gyp-bin.js

This file was deleted.

Loading

0 comments on commit 62d8cd8

Please sign in to comment.