Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: convert more util tests to use mock-npm #6475

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions tap-snapshots/test/lib/utils/open-url-prompt.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@
*/
'use strict'
exports[`test/lib/utils/open-url-prompt.js TAP opens a url > must match snapshot 1`] = `
Array [
Array [
String(
npm home:
https://www.npmjs.com
),
],
]
npm home:
https://www.npmjs.com
`

exports[`test/lib/utils/open-url-prompt.js TAP prints json output > must match snapshot 1`] = `
Array [
Array [
"{\\"title\\":\\"npm home\\",\\"url\\":\\"https://www.npmjs.com\\"}",
],
]
{"title":"npm home","url":"https://www.npmjs.com"}
`
15 changes: 0 additions & 15 deletions tap-snapshots/test/lib/utils/reify-finish.js.test.cjs

This file was deleted.

93 changes: 18 additions & 75 deletions test/lib/utils/completion/installed-deep.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
const { resolve } = require('path')
const t = require('tap')
const installedDeep = require('../../../../lib/utils/completion/installed-deep.js')

let prefix
let globalDir = 'MISSING_GLOBAL_DIR'
const _flatOptions = {
depth: Infinity,
global: false,
workspacesEnabled: true,
Arborist: require('@npmcli/arborist'),
get prefix () {
return prefix
},
}
const npm = {
flatOptions: _flatOptions,
get prefix () {
return _flatOptions.prefix
},
get globalDir () {
return globalDir
},
config: {
get (key) {
return _flatOptions[key]
},
},
}
const mockNpm = require('../../../fixtures/mock-npm')

const fixture = {
'package.json': JSON.stringify({
Expand Down Expand Up @@ -153,16 +127,23 @@ const globalFixture = {
},
}

t.test('get list of package names', async t => {
const fix = t.testdir({
local: fixture,
global: globalFixture,
const mockDeep = async (t, config) => {
const mock = await mockNpm(t, {
prefixDir: fixture,
globalPrefixDir: globalFixture,
config: {
depth: Infinity,
...config,
},
})

prefix = resolve(fix, 'local')
globalDir = resolve(fix, 'global/node_modules')
const res = await installedDeep(mock.npm)

const res = await installedDeep(npm, null)
return res
}

t.test('get list of package names', async t => {
const res = await mockDeep(t)
t.same(
res,
[
Expand All @@ -179,17 +160,7 @@ t.test('get list of package names', async t => {
})

t.test('get list of package names as global', async t => {
const fix = t.testdir({
local: fixture,
global: globalFixture,
})

prefix = resolve(fix, 'local')
globalDir = resolve(fix, 'global/node_modules')

_flatOptions.global = true

const res = await installedDeep(npm, null)
const res = await mockDeep(t, { global: true })
t.same(
res,
[
Expand All @@ -199,22 +170,10 @@ t.test('get list of package names as global', async t => {
],
'should return list of global packages with no extra flags'
)
_flatOptions.global = false
t.end()
})

t.test('limit depth', async t => {
const fix = t.testdir({
local: fixture,
global: globalFixture,
})

prefix = resolve(fix, 'local')
globalDir = resolve(fix, 'global/node_modules')

_flatOptions.depth = 0

const res = await installedDeep(npm, null)
const res = await mockDeep(t, { depth: 0 })
t.same(
res,
[
Expand All @@ -229,23 +188,10 @@ t.test('limit depth', async t => {
],
'should print only packages up to the specified depth'
)
_flatOptions.depth = 0
t.end()
})

t.test('limit depth as global', async t => {
const fix = t.testdir({
local: fixture,
global: globalFixture,
})

prefix = resolve(fix, 'local')
globalDir = resolve(fix, 'global/node_modules')

_flatOptions.global = true
_flatOptions.depth = 0

const res = await installedDeep(npm, null)
const res = await mockDeep(t, { depth: 0, global: true })
t.same(
res,
[
Expand All @@ -256,7 +202,4 @@ t.test('limit depth as global', async t => {
],
'should reorder so that packages above that level depth goes last'
)
_flatOptions.global = false
_flatOptions.depth = 0
t.end()
})
69 changes: 13 additions & 56 deletions test/lib/utils/completion/installed-shallow.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
const t = require('tap')
const { resolve } = require('path')
const installed = require('../../../../lib/utils/completion/installed-shallow.js')
const mockNpm = require('../../../fixtures/mock-npm')

const flatOptions = { global: false }
const npm = { flatOptions }

t.test('global not set, include globals with -g', async t => {
const dir = t.testdir({
global: {
const mockShallow = async (t, config) => {
const res = await mockNpm(t, {
globalPrefixDir: {
node_modules: {
x: {},
'@scope': {
y: {},
},
},
},
local: {
prefixDir: {
node_modules: {
a: {},
'@scope': {
b: {},
},
},
},
config: { global: false, ...config },
})
npm.globalDir = resolve(dir, 'global/node_modules')
npm.localDir = resolve(dir, 'local/node_modules')
flatOptions.global = false
return res
}

t.test('global not set, include globals with -g', async t => {
const { npm } = await mockShallow(t)
const opt = { conf: { argv: { remain: [] } } }
const res = await installed(npm, opt)
t.strictSame(res.sort(), [
Expand All @@ -35,64 +35,21 @@ t.test('global not set, include globals with -g', async t => {
'a',
'@scope/b',
].sort())
t.end()
})

t.test('global set, include globals and not locals', async t => {
const dir = t.testdir({
global: {
node_modules: {
x: {},
'@scope': {
y: {},
},
},
},
local: {
node_modules: {
a: {},
'@scope': {
b: {},
},
},
},
})
npm.globalDir = resolve(dir, 'global/node_modules')
npm.localDir = resolve(dir, 'local/node_modules')
flatOptions.global = true
const { npm } = await mockShallow(t, { global: true })
const opt = { conf: { argv: { remain: [] } } }
const res = await installed(npm, opt)
t.strictSame(res.sort(), [
'@scope/y',
'x',
].sort())
t.end()
})

t.test('more than 3 items in argv, skip it', async t => {
const dir = t.testdir({
global: {
node_modules: {
x: {},
'@scope': {
y: {},
},
},
},
local: {
node_modules: {
a: {},
'@scope': {
b: {},
},
},
},
})
npm.globalDir = resolve(dir, 'global/node_modules')
npm.localDir = resolve(dir, 'local/node_modules')
flatOptions.global = false
const { npm } = await mockShallow(t)
const opt = { conf: { argv: { remain: [1, 2, 3, 4, 5, 6] } } }
const res = await installed(npm, opt)
t.strictSame(res, null)
t.end()
})
Loading