Skip to content

Commit

Permalink
chore: lint and refactor arborist test setup fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Feb 7, 2023
1 parent 12ec7ee commit 4c5bd6e
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 347 deletions.
3 changes: 1 addition & 2 deletions workspaces/arborist/.eslintrc.local.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"ignorePatterns": [
"test/fixtures/",
"!test/fixtures/*.js"
"test/fixtures/*/"
],
"rules": {
"max-len": "off",
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/scripts/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const currentSha = String(execSync(shaCmd)).trim() + (dirty ? '-dirty' : '')
const { green, red } = require('chalk')
const lastBenchmark = resolve(__dirname, 'benchmark/saved/last-benchmark.json')
const { linkSync, writeFileSync, readdirSync } = require('fs')
const registryServer = require('../test/fixtures/registry-mocks/server.js')
const registryServer = require('../test/fixtures/server.js')

const options = {
previous: null,
Expand Down
31 changes: 8 additions & 23 deletions workspaces/arborist/test/arborist/audit.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
const t = require('tap')
const Arborist = require('../../lib/arborist/index.js')

const { resolve } = require('path')
const Arborist = require('../../lib/arborist/index.js')
const { normalizePath, printTree } = require('../fixtures/utils.js')
const { auditResponse, advisoryBulkResponse, ...mockRegistry } = require('../fixtures/server.js')

const fixtures = resolve(__dirname, '../fixtures')

const fixture = (t, p) => require(fixtures + '/reify-cases/' + p)(t)

const {
start,
stop,
registry,
auditResponse,
advisoryBulkResponse,
} = require('../fixtures/registry-mocks/server.js')
const cache = t.testdir()

t.before(start)
t.teardown(stop)

const {
normalizePath,
printTree,
} = require('../fixtures/utils.js')
t.before(mockRegistry.start)
t.teardown(mockRegistry.stop)

const cache = t.testdir()
const newArb = (path, options = {}) =>
new Arborist({ path, cache, registry, ...options })
new Arborist({ path, cache, registry: mockRegistry.registry, ...options })

const cwd = normalizePath(process.cwd())
t.cleanSnapshot = s => s.split(cwd).join('{CWD}')
.split(registry).join('https://registry.npmjs.org/')
.split(mockRegistry.registry).join('https://registry.npmjs.org/')

t.test('audit finds the bad deps', async t => {
const path = resolve(fixtures, 'deprecated-dep')
t.teardown(auditResponse(resolve(fixtures, 'audit-nyc-mkdirp/audit.json')))

const arb = newArb(path)

const report = await arb.audit()
t.equal(report.topVulns.size, 0)
t.equal(report.size, 2)
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/test/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Arborist = require('../..')
const fixtures = resolve(__dirname, '../fixtures')
// load the symbolic links that we depend on
require(fixtures)
const { start, stop, registry, auditResponse } = require('../fixtures/registry-mocks/server.js')
const { start, stop, registry, auditResponse } = require('../fixtures/server.js')
const npa = require('npm-package-arg')
const fs = require('fs')
const nock = require('nock')
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/test/arborist/deduper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const t = require('tap')
const Arborist = require('../../lib/arborist/index.js')

const { start, stop, registry } = require('../fixtures/registry-mocks/server.js')
const { start, stop, registry } = require('../fixtures/server.js')
t.before(start)
t.teardown(stop)

Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/test/arborist/pruner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { resolve } = require('path')
const t = require('tap')
const Arborist = require('../../lib/arborist/index.js')

const { start, stop, registry } = require('../fixtures/registry-mocks/server.js')
const { start, stop, registry } = require('../fixtures/server.js')

t.before(start)
t.teardown(stop)
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/test/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const {
stop,
registry,
advisoryBulkResponse,
} = require('../fixtures/registry-mocks/server.js')
} = require('../fixtures/server.js')

t.before(start)
t.teardown(stop)
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/test/audit-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
auditResponse,
failAudit,
advisoryBulkResponse,
} = require('./fixtures/registry-mocks/server.js')
} = require('./fixtures/server.js')
t.before(start)
t.teardown(stop)

Expand Down
11 changes: 5 additions & 6 deletions workspaces/arborist/test/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The reify test case fixtures are stored in `test/fixtures/reify-cases/`.
To generate a new test case:

```
node test/fixtures/reify-cases/create.js <folder>
node test/fixtures/create-reify-case.js <folder>
```

where `<folder>` is a path to the test case example you wish to store.
Expand All @@ -61,22 +61,21 @@ much garbage or extraneous files that aren't relevant to the test.

## Mock Registry

The mock registry is in `test/fixtures/registry-mocks`. The server itself
is in `test/fixtures/registry-mocks/server.js`. The contents are in
The mock registry server is in `test/fixtures/server.js` and the contents are in
`test/fixtures/registry-mocks/content/`.

You can run the server directly by running:

```
node test/fixtures/registry-mocks/server.js
node test/fixtures/server.js
```

In tests, it's loaded by requiring the server module, and then running the
exported method, passing in a `tap` test object, and using the exported
`registry` property as the registry config for all its business.

```js
const {start, stop, registry} = require('../fixtures/registry-mocks/server.js')
const {start, stop, registry} = require('../fixtures/server.js')
t.before(start)
t.teardown(stop)

Expand All @@ -103,7 +102,7 @@ The server will fetch whatever it doesn't have from the registry, and save
it in the appropriate place under `test/fixtures/registry-mocks/content/`.

There is also a script at
`test/fixtures/registry-mocks/fetch-lock-contents.js` which will fetch and
`test/fixtures/fetch-lock-contents.js` which will fetch and
store the packuments and tarballs referenced in a `package-lock.json` file.

And of course, you could just delete the entire `content` folder and run
Expand Down
11 changes: 7 additions & 4 deletions workspaces/arborist/test/fixtures/cleanup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { resolve } = require('path')
const { readdirSync, lstatSync } = require('fs')

process.env.ARBORIST_FIXTURE_CLEANUP = '1'
require('./index.js')

// now make sure it actually did clean up everything
const { readdirSync, lstatSync } = require('fs')
const readdir = (path, opt) => {
const ents = readdirSync(path, opt)
if (typeof ents[0] === 'string') {
Expand All @@ -13,15 +15,16 @@ const readdir = (path, opt) => {
return ents
}

const { resolve } = require('path')
const walk = dir => {
for (const entry of readdir(dir, { withFileTypes: true })) {
if (entry.isDirectory())
if (entry.isDirectory()) {
walk(resolve(dir, entry.name))
else if (entry.isSymbolicLink())
} else if (entry.isSymbolicLink()) {
throw Object.assign(new Error('symlink left in fixtures dir'), {
path: resolve(dir, entry.name),
})
}
}
}

walk(__dirname)
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ const {
writeFileSync,
} = require('fs')

const {resolve, relative, basename} = require('path')
const { resolve, relative, basename } = require('path')

if (!process.argv[2]) {
console.error('pass in a folder as an argument')
process.exit(1)
}

const fixture = resolve(process.argv[2])
const rel = relative(resolve(__dirname, '../../..'), fixture)
const outFile = resolve(__dirname, basename(fixture)) + '.js'
const rel = relative(resolve(__dirname, '../..'), fixture)
const outFile = resolve(__dirname, 'reify-cases', basename(fixture)) + '.js'

// we build up an object and then JSON.stringify it, and string replace
// the tokens with `t.fixture('symlink', ${symlinks.get(token)})`
Expand All @@ -34,18 +34,20 @@ const hiddenLocks = []
const readFixture = dir => {
const res = {}
for (const ent of readdirSync(dir, { withFileTypes: true })) {
if (/^\..*\.swp/.test(ent.name))
if (/^\..*\.swp/.test(ent.name)) {
continue
}

const p = resolve(dir, ent.name)
if (ent.isDirectory())
if (ent.isDirectory()) {
res[ent.name] = readFixture(p)
else if (ent.isFile()) {
} else if (ent.isFile()) {
const content = readFileSync(p, 'utf8')
const buf = readFileSync(p)
// make sure hidden lockfiles are newer than the contents they cover
if (ent.name === '.package-lock.json')
if (ent.name === '.package-lock.json') {
hiddenLocks.push(relative(fixture, p))
}

// if it's JSON, then store it in a way that's easier to look at
try {
Expand All @@ -60,8 +62,9 @@ const readFixture = dir => {
const t = token(p)
longFiles.set(t, content)
res[ent.name] = t
} else
} else {
res[ent.name] = content
}
} else {
// save as a buffer if it's binary data
const t = token(p)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
// fetch all the deps and tarballs in a v2 lockfile
const pacote = require('pacote')
const Arborist = require('../../index.js')
const url = require('url')
const mkdirp = require('mkdirp')
const {dirname, resolve} = require('path')
const {writeFileSync} = require('fs')
const dir = resolve(__dirname, 'content')
const nm = /^.*node_modules\/(@[^\/]+\/[^\/]+|[^@\/][^\/]*)$/
const { dirname, resolve } = require('path')
const fs = require('fs/promises')
const Arborist = require('./index.js')

const dir = resolve(__dirname, 'registry-mocks', 'content')
const nm = /^.*node_modules\/(@[^/]+\/[^/]+|[^@/][^/]*)$/

const main = async lock => {
for (const [loc, meta] of Object.entries(lock.packages)) {
if (!loc || meta.link || !nm.test(loc))
if (!loc || meta.link || !nm.test(loc)) {
continue
}

const name = meta.name || loc.replace(nm, '$1')

console.error('FETCHING', name)

const paku = await pacote.packument(name, { fullMetadata: true })
const saveTo = resolve(dir, name.replace(/^@/, '') + '.json')
mkdirp.sync(dirname(saveTo))
writeFileSync(saveTo, JSON.stringify(paku, 0, 2))
await fs.mkdir(dirname(saveTo), { recursive: true })
await fs.writeFile(saveTo, JSON.stringify(paku, 0, 2))

const corgi = await pacote.packument(name, {})
const corgiSaveTo = resolve(dir, name.replace(/^@/, '') + '.min.json')
writeFileSync(corgiSaveTo, JSON.stringify(corgi, 0, 2))
await fs.writeFile(corgiSaveTo, JSON.stringify(corgi, 0, 2))

// bundled deps sometimes don't have a resolved value
if (!meta.resolved)
if (!meta.resolved) {
continue
}
// eslint-disable-next-line node/no-deprecated-api
const path = url.parse(meta.resolved).pathname.replace(/^\/@?/, '')
const tgzFile = resolve(dir, path)
await pacote.tarball.file(meta.resolved, tgzFile, { Arborist })
Expand Down
24 changes: 14 additions & 10 deletions workspaces/arborist/test/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const mkdirp = require('mkdirp').sync
const { mkdirSync } = require('fs')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const { unlinkSync, symlinkSync, readFileSync, writeFileSync } = require('fs')
const { relative, resolve, dirname } = require('path')
const mkdirp = (p) => mkdirSync(p, { recursive: true })

const fixtures = __dirname

Expand Down Expand Up @@ -43,9 +44,9 @@ const symlinks = {
'../../..',
'other/node_modules/glob':
'../../root/node_modules/@scope/x/node_modules/glob',
'linkedroot': 'root',
linkedroot: 'root',
'deep/root': '../root',
'deeproot': 'deep',
deeproot: 'deep',
'badlink/node_modules/foo': 'foo',
'badlink/node_modules/bar': 'baz',
'testing-peer-deps-link': 'testing-peer-deps',
Expand Down Expand Up @@ -141,10 +142,11 @@ const symlinks = {
const cleanup = () => Object.keys(symlinks).forEach(s => {
try {
unlinkSync(resolve(__dirname, s))
} catch (er) {}
} catch {
// ok if cleanup fails
}
})


const setup = () => {
const links = []
let didSomething = false
Expand All @@ -154,12 +156,13 @@ const setup = () => {
const rel = relative(resolve(__dirname), p)
links.push('/' + rel.replace(/\\/g, '/'))

// it's fine for this to throw, since it typically means
// that the links already exist, and that's fine.
try {
symlinkSync(symlinks[s], p, 'junction')
didSomething = true
} catch (_) {}
} catch {
// it's fine for this to throw, since it typically means
// that the links already exist, and that's fine.
}
})
if (didSomething) {
const gifile = resolve(__dirname, './.gitignore')
Expand All @@ -177,10 +180,11 @@ ${links.sort(localeCompare).join('\n')}
const doCleanup = process.argv[2] === 'cleanup' && require.main === module ||
process.env.ARBORIST_FIXTURE_CLEANUP === '1'

if (doCleanup)
if (doCleanup) {
cleanup()
else
} else {
setup()
}

module.exports = {
roots,
Expand Down
Loading

0 comments on commit 4c5bd6e

Please sign in to comment.