mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-24 10:01:48 -04:00
140 lines
4.0 KiB
TypeScript
140 lines
4.0 KiB
TypeScript
import tape = require('tape')
|
|
import promisifyTape from 'tape-promise'
|
|
import {installPkgs} from '../../src'
|
|
import {
|
|
prepare,
|
|
testDefaults,
|
|
execPnpmSync,
|
|
} from '../utils'
|
|
import path = require('path')
|
|
import loadJsonFile = require('load-json-file')
|
|
|
|
const pkgRoot = path.join(__dirname, '..', '..')
|
|
const pnpmPkg = loadJsonFile.sync(path.join(pkgRoot, 'package.json'))
|
|
|
|
const test = promisifyTape(tape)
|
|
|
|
test('run pre/postinstall scripts', async function (t) {
|
|
const project = prepare(t)
|
|
await installPkgs(['pre-and-postinstall-scripts-example'], testDefaults())
|
|
|
|
const generatedByPreinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-preinstall')
|
|
t.ok(typeof generatedByPreinstall === 'function', 'generatedByPreinstall() is available')
|
|
|
|
const generatedByPostinstall = project.requireModule('pre-and-postinstall-scripts-example/generated-by-postinstall')
|
|
t.ok(typeof generatedByPostinstall === 'function', 'generatedByPostinstall() is available')
|
|
})
|
|
|
|
test('run install scripts', async function (t) {
|
|
const project = prepare(t)
|
|
await installPkgs(['install-script-example'], testDefaults())
|
|
|
|
const generatedByInstall = project.requireModule('install-script-example/generated-by-install')
|
|
t.ok(typeof generatedByInstall === 'function', 'generatedByInstall() is available')
|
|
})
|
|
|
|
test('installation fails if lifecycle script fails', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
preinstall: 'exit 1'
|
|
},
|
|
})
|
|
|
|
const result = execPnpmSync('install')
|
|
|
|
t.equal(result.status, 1, 'installation failed')
|
|
|
|
t.end()
|
|
})
|
|
|
|
test('lifecycle script runs with the correct user agent', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
preinstall: 'node --eval "console.log(process.env.npm_config_user_agent)"'
|
|
},
|
|
})
|
|
|
|
const result = execPnpmSync('install')
|
|
|
|
t.equal(result.status, 0, 'installation was successfull')
|
|
const expectedUserAgent = `${pnpmPkg.name}/${pnpmPkg.version} npm/? node/${process.version} ${process.platform} ${process.arch}`
|
|
t.ok(result.stdout.toString().indexOf(expectedUserAgent) !== -1, 'correct npm_config_user_agent value')
|
|
|
|
t.end()
|
|
})
|
|
|
|
test('preinstall is executed before general installation', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
preinstall: 'echo "Hello world!"'
|
|
}
|
|
})
|
|
|
|
const result = execPnpmSync('install')
|
|
|
|
t.equal(result.status, 0, 'installation was successfull')
|
|
t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'preinstall script was executed')
|
|
|
|
t.end()
|
|
})
|
|
|
|
test('postinstall is executed after general installation', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
postinstall: 'echo "Hello world!"'
|
|
}
|
|
})
|
|
|
|
const result = execPnpmSync('install')
|
|
|
|
t.equal(result.status, 0, 'installation was successfull')
|
|
t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'postinstall script was executed')
|
|
|
|
t.end()
|
|
})
|
|
|
|
test('postinstall is not executed after named installation', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
postinstall: 'echo "Hello world!"'
|
|
}
|
|
})
|
|
|
|
const result = execPnpmSync('install', 'is-negative')
|
|
|
|
t.equal(result.status, 0, 'installation was successfull')
|
|
t.ok(result.stdout.toString().indexOf('Hello world!') === -1, 'postinstall script was not executed')
|
|
|
|
t.end()
|
|
})
|
|
|
|
test('prepublish is not executed after installation with arguments', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
prepublish: 'echo "Hello world!"'
|
|
}
|
|
})
|
|
|
|
const result = execPnpmSync('install', 'is-negative')
|
|
|
|
t.equal(result.status, 0, 'installation was successfull')
|
|
t.ok(result.stdout.toString().indexOf('Hello world!') === -1, 'prepublish script was not executed')
|
|
|
|
t.end()
|
|
})
|
|
|
|
test('prepublish is executed after argumentless installation', t => {
|
|
const project = prepare(t, {
|
|
scripts: {
|
|
prepublish: 'echo "Hello world!"'
|
|
}
|
|
})
|
|
|
|
const result = execPnpmSync('install')
|
|
|
|
t.equal(result.status, 0, 'installation was successfull')
|
|
t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'prepublish script was executed')
|
|
|
|
t.end()
|
|
})
|