Skip to content

Commit

Permalink
chore(smoke-tests): refactor smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Nov 9, 2022
1 parent a73a4c6 commit 6a15d8a
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 210 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is automatically added by @npmcli/template-oss. Do not edit.

name: CI - smoke-tests
name: CI - @npmcli/smoke-tests

on:
workflow_dispatch:
Expand Down Expand Up @@ -40,9 +40,9 @@ jobs:
- name: Reset Deps
run: node . run resetdeps
- name: Lint
run: node . run lint --ignore-scripts -w smoke-tests
run: node . run lint --ignore-scripts -w @npmcli/smoke-tests
- name: Post Lint
run: node . run postlint --ignore-scripts -w smoke-tests
run: node . run postlint --ignore-scripts -w @npmcli/smoke-tests

test:
name: Test - ${{ matrix.platform.name }} - ${{ matrix.node-version }}
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:
- name: Add Problem Matcher
run: echo "::add-matcher::.github/matchers/tap.json"
- name: Test
run: node . test --ignore-scripts -w smoke-tests
run: node . test --ignore-scripts -w @npmcli/smoke-tests
- name: Check Git Status
if: matrix && matrix.platform.os != 'windows-latest'
run: node scripts/git-dirty.js
24 changes: 14 additions & 10 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ graph LR;
npm-->npmcli-package-json["@npmcli/package-json"];
npm-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npm-->npmcli-run-script["@npmcli/run-script"];
npm-->npmcli-smoke-tests["@npmcli/smoke-tests"];
npm-->npmcli-template-oss["@npmcli/template-oss"];
npm-->npmlog;
npm-->pacote;
Expand Down Expand Up @@ -210,6 +211,10 @@ graph LR;
npmcli-run-script-->npmcli-node-gyp["@npmcli/node-gyp"];
npmcli-run-script-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-run-script-->read-package-json-fast;
npmcli-smoke-tests-->minify-registry-metadata;
npmcli-smoke-tests-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-smoke-tests-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-smoke-tests-->npmcli-template-oss["@npmcli/template-oss"];
npmlog-->are-we-there-yet;
npmlog-->gauge;
pacote-->cacache;
Expand Down Expand Up @@ -518,6 +523,7 @@ graph LR;
npm-->npmcli-package-json["@npmcli/package-json"];
npm-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npm-->npmcli-run-script["@npmcli/run-script"];
npm-->npmcli-smoke-tests["@npmcli/smoke-tests"];
npm-->npmcli-template-oss["@npmcli/template-oss"];
npm-->npmlog;
npm-->p-map;
Expand All @@ -533,7 +539,6 @@ graph LR;
npm-->remark;
npm-->rimraf;
npm-->semver;
npm-->smoke-tests;
npm-->spawk;
npm-->ssri;
npm-->tap;
Expand Down Expand Up @@ -662,6 +667,12 @@ graph LR;
npmcli-run-script-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-run-script-->read-package-json-fast;
npmcli-run-script-->which;
npmcli-smoke-tests-->minify-registry-metadata;
npmcli-smoke-tests-->npmcli-eslint-config["@npmcli/eslint-config"];
npmcli-smoke-tests-->npmcli-promise-spawn["@npmcli/promise-spawn"];
npmcli-smoke-tests-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-smoke-tests-->tap;
npmcli-smoke-tests-->which;
npmlog-->are-we-there-yet;
npmlog-->console-control-strings;
npmlog-->gauge;
Expand Down Expand Up @@ -705,13 +716,6 @@ graph LR;
readable-stream-->util-deprecate;
rimraf-->glob;
semver-->lru-cache;
smoke-tests-->minify-registry-metadata;
smoke-tests-->npmcli-eslint-config["@npmcli/eslint-config"];
smoke-tests-->npmcli-promise-spawn["@npmcli/promise-spawn"];
smoke-tests-->npmcli-template-oss["@npmcli/template-oss"];
smoke-tests-->rimraf;
smoke-tests-->tap;
smoke-tests-->which;
socks-->ip;
socks-->smart-buffer;
socks-proxy-agent-->agent-base;
Expand Down Expand Up @@ -761,5 +765,5 @@ packages higher up the chain.
- npm-registry-fetch, libnpmversion
- @npmcli/git, make-fetch-happen, @npmcli/config, init-package-json
- @npmcli/installed-package-contents, @npmcli/map-workspaces, cacache, npm-pick-manifest, @npmcli/run-script, read-package-json, promzard
- @npmcli/docs, @npmcli/fs, npm-bundled, read-package-json-fast, unique-filename, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, read
- @npmcli/eslint-config, @npmcli/template-oss, ignore-walk, semver, npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, fs-minipass, ssri, unique-slug, @npmcli/promise-spawn, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, minipass-fetch, @npmcli/query, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, treeverse, minify-registry-metadata, ini, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, npm-audit-report, npm-user-validate
- @npmcli/docs, @npmcli/smoke-tests, @npmcli/fs, npm-bundled, read-package-json-fast, unique-filename, npm-install-checks, npm-package-arg, npm-packlist, normalize-package-data, @npmcli/package-json, bin-links, nopt, npmlog, parse-conflict-json, read
- @npmcli/eslint-config, @npmcli/template-oss, ignore-walk, @npmcli/promise-spawn, minify-registry-metadata, semver, npm-normalize-package-bin, @npmcli/name-from-folder, json-parse-even-better-errors, fs-minipass, ssri, unique-slug, hosted-git-info, proc-log, validate-npm-package-name, @npmcli/node-gyp, minipass-fetch, @npmcli/query, cmd-shim, read-cmd-shim, write-file-atomic, abbrev, are-we-there-yet, gauge, treeverse, ini, @npmcli/disparity-colors, @npmcli/ci-detect, mute-stream, npm-audit-report, npm-user-validate
27 changes: 6 additions & 21 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,10 @@
"node": "^14.17.0 || ^16.13.0 || >=18.0.0"
}
},
"node_modules/@npmcli/smoke-tests": {
"resolved": "smoke-tests",
"link": true
},
"node_modules/@npmcli/template-oss": {
"version": "4.8.0",
"resolved": "https://registry.npmjs.org/@npmcli/template-oss/-/template-oss-4.8.0.tgz",
Expand Down Expand Up @@ -11301,10 +11305,6 @@
"npm": ">= 3.0.0"
}
},
"node_modules/smoke-tests": {
"resolved": "smoke-tests",
"link": true
},
"node_modules/socks": {
"version": "2.7.0",
"inBundle": true,
Expand Down Expand Up @@ -14890,36 +14890,21 @@
}
},
"smoke-tests": {
"name": "@npmcli/smoke-tests",
"version": "1.0.0",
"license": "ISC",
"devDependencies": {
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/promise-spawn": "^6.0.1",
"@npmcli/template-oss": "4.8.0",
"minify-registry-metadata": "^2.2.0",
"rimraf": "^3.0.2",
"tap": "^16.0.1",
"which": "^2.0.2"
"which": "^3.0.0"
},
"engines": {
"node": "^14.17.0 || ^16.13.0 || >=18.0.0"
}
},
"smoke-tests/node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"dev": true,
"dependencies": {
"isexe": "^2.0.0"
},
"bin": {
"node-which": "bin/node-which"
},
"engines": {
"node": ">= 8"
}
},
"workspaces/arborist": {
"name": "@npmcli/arborist",
"version": "6.1.1",
Expand Down
7 changes: 3 additions & 4 deletions smoke-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "smoke-tests",
"name": "@npmcli/smoke-tests",
"description": "The npm cli smoke tests",
"version": "1.0.0",
"private": true,
Expand All @@ -22,9 +22,8 @@
"@npmcli/promise-spawn": "^6.0.1",
"@npmcli/template-oss": "4.8.0",
"minify-registry-metadata": "^2.2.0",
"rimraf": "^3.0.2",
"tap": "^16.0.1",
"which": "^2.0.2"
"which": "^3.0.0"
},
"author": "GitHub Inc.",
"license": "ISC",
Expand All @@ -36,7 +35,7 @@
"tap": {
"no-coverage": true,
"timeout": 300,
"files": "test/index.js",
"files": "test/*.js",
"nyc-arg": [
"--exclude",
"tap-snapshots/**"
Expand Down
49 changes: 0 additions & 49 deletions smoke-tests/test/fixtures/server.js

This file was deleted.

157 changes: 157 additions & 0 deletions smoke-tests/test/fixtures/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const fs = require('fs')
const { join, resolve, sep, basename } = require('path')
const which = require('which').sync
const http = require('http')
const spawn = require('@npmcli/promise-spawn')

const { SMOKE_PUBLISH_NPM, CI, PATH, TAP_CHILD_ID = '0' } = process.env
const PORT = 12345 + Number(TAP_CHILD_ID)
const log = CI ? console.error : () => {}
const registry = `http://localhost:${PORT}/`
const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'

const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')

const cwd = resolve(__dirname, '..', '..', '..')
const npmCli = join('bin', 'npm-cli.js')
const execArgv = SMOKE_PUBLISH_NPM ? ['npm'] : [process.execPath, join(cwd, npmCli)]
const npmDir = SMOKE_PUBLISH_NPM ? fs.realpathSync(which('npm')).replace(sep + npmCli, '') : cwd

module.exports = (t) => {
// setup fixtures
const path = t.testdir({
'.npmrc': '',
cache: {},
project: {},
bin: {},
})
const localPrefix = resolve(path, 'project')
const userconfigLocation = resolve(path, '.npmrc')
const cacheLocation = resolve(path, 'cache')
const binLocation = resolve(path, 'bin')

// setup server
let server = null
t.before(() => new Promise((resolvePromise) => {
server = http.createServer((req, res) => {
res.setHeader('connection', 'close')

const f = join(__dirname, 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.statusCode = 404
res.setHeader('npm-notice', req.headers['user-agent'])
return res.end()
}

const isCorgi = req.headers.accept.includes('application/vnd.npm.install-v1+json')
const file = f + (
isCorgi && fs.existsSync(`${f}.min.json`) ? '.min.json'
: fs.existsSync(`${f}.json`) ? '.json'
: fs.existsSync(`${f}/index.json`) ? 'index.json'
: ''
)

try {
const body = fs.readFileSync(file)
res.setHeader('content-length', body.length)
res.setHeader('content-type', /\.min\.json$/.test(file) ? corgiDoc
: /\.json$/.test(file) ? 'application/json'
: 'application/octet-stream')
res.end(body)
} catch {
res.statusCode = 500
res.setHeader('content-type', 'text/plain')
res.end('bad')
}
}).listen(PORT, resolvePromise)
}))
t.teardown(() => server.close())

// update notifier should never be written
t.afterEach((t) => {
const updateExists = fs.existsSync(join(cacheLocation, '_update-notifier-last-checked'))
t.equal(updateExists, false)
})

t.cleanSnapshot = s =>
s
// sometimes we print normalized paths in snapshots regardless of
// platform so replace those first
.split(normalizePath(npmDir))
.join('{CWD}')
.split(normalizePath(cwd))
.join('{CWD}')
.split(registry)
.join('https://registry.npmjs.org/')
.split(normalizePath(process.execPath))
.join('node')
// then replace platform style paths
.split(npmDir)
.join('{CWD}')
.split(cwd)
.join('{CWD}')
.replace(/\\+/g, '/')
.replace(/\r\n/g, '\n')
.replace(/ \(in a browser\)/g, '')
.replace(/^npm@.* /gm, 'npm ')
.replace(/^.*debug-[0-9]+.log$/gm, '')

const exec = async (...args) => {
const cmd = []
const opts = [
`--registry=${registry}`,
`--cache=${cacheLocation}`,
`--userconfig=${userconfigLocation}`,
'--no-audit',
'--no-update-notifier',
'--loglevel=silly',
]
for (const arg of args) {
if (arg.startsWith('--')) {
opts.push(arg)
} else {
cmd.push(arg)
}
}

const hasWorkspaceOpt = opts.some(o => /^--workspaces?($|=)/.test(o))
// XXX: not sure why outdated fails with no-workspaces but works without it
if (!hasWorkspaceOpt && cmd[0] !== 'outdated') {
// This is required so we dont detect any workspace roots above the testdir
opts.push('--no-workspaces')
}

const spawnArgs = [execArgv[0], [...execArgv.slice(1), ...cmd, ...opts]]
log([spawnArgs[0], ...spawnArgs[1]].join('\n'))

const res = await spawn(...spawnArgs, {
cwd: localPrefix,
env: {
HOME: path,
PATH: `${PATH}:${binLocation}`,
COMSPEC: process.env.COMSPEC,
},
})

log(res.stderr)
return res.stdout
}

const readFile = (f) => fs.readFileSync(resolve(localPrefix, f), 'utf-8')
const writeFile = (f, d) => fs.writeFileSync(resolve(localPrefix, f), d, 'utf-8')
const rmDir = () => {
for (const f of fs.readdirSync(localPrefix)) {
fs.rmSync(join(localPrefix, f), { recursive: true, force: true })
}
}

return {
exec,
readFile,
writeFile,
rmDir,
isSmokePublish: !!SMOKE_PUBLISH_NPM,
}
}
Loading

0 comments on commit 6a15d8a

Please sign in to comment.