Skip to content

Commit

Permalink
fixup! fix(config): user-agent properly shows ci
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Sep 14, 2021
1 parent 0de45b8 commit 39b3ba2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
40 changes: 39 additions & 1 deletion 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())
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion 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')
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 39b3ba2

Please sign in to comment.