diff --git a/smoke-tests/index.js b/smoke-tests/index.js index 9235c8960a26a..5e2d5e071ae12 100644 --- a/smoke-tests/index.js +++ b/smoke-tests/index.js @@ -1,8 +1,9 @@ const fs = require('fs') const { promisify } = require('util') const execAsync = promisify(require('child_process').exec) -const { resolve } = require('path') +const { join, resolve } = require('path') const t = require('tap') +const rimraf = promisify(require('rimraf')) const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/') const cwd = normalizePath(process.cwd()) @@ -47,6 +48,43 @@ const exec = async cmd => { const readFile = filename => String(fs.readFileSync(resolve(localPrefix, filename))) +// this test must come first, its package.json will be destroyed and the one +// created in the next test (npm init) will create a new one that must be +// present for later tests +t.test('npm install sends correct user-agent', async t => { + const pkgPath = join(localPrefix, 'package.json') + const pkgContent = JSON.stringify({ + name: 'smoke-test-workspaces', + workspaces: ['packages/*'], + }) + fs.writeFileSync(pkgPath, pkgContent, { encoding: 'utf8' }) + + const wsRoot = join(localPrefix, 'packages') + fs.mkdirSync(wsRoot) + + const wsPath = join(wsRoot, 'foo') + fs.mkdirSync(wsPath) + + const wsPkgPath = join(wsPath, 'package.json') + const wsContent = JSON.stringify({ + name: 'foo', + }) + fs.writeFileSync(wsPkgPath, wsContent, { encoding: 'utf8' }) + t.teardown(async () => { + await rimraf(`${localPrefix}/*`) + }) + + const cmd = `${npmBin} install fail_reflect_user_agent` + await t.rejects(exec(cmd), { + stderr: /workspaces\/false/, + }, 'workspaces/false is present in output') + + const wsCmd = `${npmBin} install fail_reflect_user_agent --workspaces` + await t.rejects(exec(wsCmd), { + stderr: /workspaces\/true/, + }, 'workspaces/true is present in output') +}) + t.test('npm init', async t => { const cmd = `${npmBin} init -y` const cmdRes = await exec(cmd) diff --git a/smoke-tests/server.js b/smoke-tests/server.js index b864114af64a8..e0ac0c94eb5ab 100644 --- a/smoke-tests/server.js +++ b/smoke-tests/server.js @@ -1,5 +1,5 @@ /* istanbul ignore file */ -const {join, dirname} = require('path') +const {join, dirname, basename} = require('path') const {existsSync, readFileSync, writeFileSync} = require('fs') const PORT = 12345 + (+process.env.TAP_CHILD_ID || 0) const http = require('http') @@ -150,6 +150,12 @@ const startServer = () => new Promise((res, rej) => { } const f = join(__dirname, 'content', join('/', req.url.replace(/@/, '').replace(/%2f/i, '/'))) + // a magic package that causes us to return an error that will be logged + if (basename(f) === 'fail_reflect_user_agent') { + res.setHeader('npm-notice', req.headers['user-agent']) + res.writeHead(404) + return res.end() + } const isCorgi = req.headers.accept.includes('application/vnd.npm.install-v1+json') const file = f + ( isCorgi && existsSync(`${f}.min.json`) ? '.min.json'