Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGar <gar+gh@danger.computer>2022-03-03 07:06:18 +0300
committerRuy Adorno <ruyadorno@hotmail.com>2022-03-10 18:40:11 +0300
commitd521a25db2b19239a8d7a66a4e10f30dc15e7cf0 (patch)
treee33c77446eacba33403d7a400aeec640f04bb5e8 /test
parentec7f36ff9e6c973ae5d5998a783bcff16027c282 (diff)
chore: mocknpm improvements
make tests pass in prefixDir instead of testdir allow for cache and globalPrefix dirs
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/mock-npm.js11
-rw-r--r--test/lib/commands/access.js16
-rw-r--r--test/lib/commands/completion.js2
-rw-r--r--test/lib/commands/pack.js14
-rw-r--r--test/lib/commands/repo.js4
-rw-r--r--test/lib/commands/restart.js2
-rw-r--r--test/lib/commands/shrinkwrap.js8
-rw-r--r--test/lib/commands/start.js2
-rw-r--r--test/lib/commands/stop.js2
-rw-r--r--test/lib/commands/test.js2
-rw-r--r--test/lib/npm.js16
-rw-r--r--test/lib/utils/error-message.js16
12 files changed, 50 insertions, 45 deletions
diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js
index 4846d9a48..b00f072dc 100644
--- a/test/fixtures/mock-npm.js
+++ b/test/fixtures/mock-npm.js
@@ -49,7 +49,9 @@ const result = (fn, ...args) => typeof fn === 'function' ? fn(...args) : fn
const LoadMockNpm = async (t, {
init = true,
load = init,
- testdir = {},
+ prefixDir = {},
+ cacheDir = {},
+ globalDir = {},
config = {},
mocks = {},
globals = null,
@@ -77,9 +79,10 @@ const LoadMockNpm = async (t, {
// Set log level as early as possible since
setLoglevel(t, config.loglevel)
- const dir = t.testdir({ root: testdir, cache: {} })
- const prefix = path.join(dir, 'root')
+ const dir = t.testdir({ prefix: prefixDir, cache: cacheDir, global: globalDir })
+ const prefix = path.join(dir, 'prefix')
const cache = path.join(dir, 'cache')
+ const globalPrefix = path.join(dir, 'global')
// Set cache to testdir via env var so it is available when load is run
// XXX: remove this for a solution where cache argv is passed in
@@ -104,6 +107,7 @@ const LoadMockNpm = async (t, {
setLoglevel(t, config.loglevel, false)
npm.prefix = prefix
npm.cache = cache
+ npm.globalPrefix = globalPrefix
}
return {
@@ -111,6 +115,7 @@ const LoadMockNpm = async (t, {
Npm,
npm,
prefix,
+ testdir: dir,
cache,
debugFile: async () => {
const readFiles = npm.logFiles.map(f => fs.readFile(f))
diff --git a/test/lib/commands/access.js b/test/lib/commands/access.js
index 298897e4f..81e29146b 100644
--- a/test/lib/commands/access.js
+++ b/test/lib/commands/access.js
@@ -61,7 +61,7 @@ t.test('edit', async t => {
t.test('access public on unscoped package', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'npm-access-public-pkg',
}),
@@ -91,7 +91,7 @@ t.test('access public on scoped package', async t => {
},
},
},
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({ name }),
},
})
@@ -109,7 +109,7 @@ t.test('access public on missing package.json', async t => {
t.test('access public on invalid package.json', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': '{\n',
node_modules: {},
},
@@ -123,7 +123,7 @@ t.test('access public on invalid package.json', async t => {
t.test('access restricted on unscoped package', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'npm-access-restricted-pkg',
}),
@@ -153,7 +153,7 @@ t.test('access restricted on scoped package', async t => {
},
},
},
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({ name }),
},
})
@@ -171,7 +171,7 @@ t.test('access restricted on missing package.json', async t => {
t.test('access restricted on invalid package.json', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': '{\n',
node_modules: {},
},
@@ -240,7 +240,7 @@ t.test('access grant current cwd', async t => {
},
},
},
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'yargs',
}),
@@ -406,7 +406,7 @@ t.test('access ls-collaborators on current', async t => {
},
},
},
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'yargs',
}),
diff --git a/test/lib/commands/completion.js b/test/lib/commands/completion.js
index dd571baf7..045054b74 100644
--- a/test/lib/commands/completion.js
+++ b/test/lib/commands/completion.js
@@ -42,7 +42,7 @@ const loadMockCompletionComp = async (t, word, line) =>
t.test('completion', async t => {
t.test('completion completion', async t => {
const { outputs, completion, prefix } = await loadMockCompletion(t, {
- testdir: {
+ prefixDir: {
'.bashrc': 'aaa',
'.zshrc': 'aaa',
},
diff --git a/test/lib/commands/pack.js b/test/lib/commands/pack.js
index 51453dae9..f287d93dc 100644
--- a/test/lib/commands/pack.js
+++ b/test/lib/commands/pack.js
@@ -10,7 +10,7 @@ t.afterEach(t => {
t.test('should pack current directory with no arguments', async t => {
const { npm, outputs, logs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'test-package',
version: '1.0.0',
@@ -27,7 +27,7 @@ t.test('should pack current directory with no arguments', async t => {
t.test('follows pack-destination config', async t => {
const { npm, outputs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'test-package',
version: '1.0.0',
@@ -45,7 +45,7 @@ t.test('follows pack-destination config', async t => {
t.test('should pack given directory for scoped package', async t => {
const { npm, outputs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: '@npm/test-package',
version: '1.0.0',
@@ -61,7 +61,7 @@ t.test('should pack given directory for scoped package', async t => {
t.test('should log output as valid json', async t => {
const { npm, outputs, logs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'test-package',
version: '1.0.0',
@@ -79,7 +79,7 @@ t.test('should log output as valid json', async t => {
t.test('dry run', async t => {
const { npm, outputs, logs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'test-package',
version: '1.0.0',
@@ -97,7 +97,7 @@ t.test('dry run', async t => {
t.test('invalid packument', async t => {
const { npm, outputs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': '{}',
},
})
@@ -111,7 +111,7 @@ t.test('invalid packument', async t => {
t.test('workspaces', async t => {
const loadWorkspaces = (t) => loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify(
{
name: 'workspaces-test',
diff --git a/test/lib/commands/repo.js b/test/lib/commands/repo.js
index 93eb6d031..e06a28944 100644
--- a/test/lib/commands/repo.js
+++ b/test/lib/commands/repo.js
@@ -188,10 +188,10 @@ const openUrl = async (npm, url, errMsg) => {
}
t.afterEach(() => opened = {})
-const loadMockNpm = async (t, prefix) => {
+const loadMockNpm = async (t, prefixDir) => {
const res = await _loadMockNpm(t, {
mocks: { '../../lib/utils/open-url.js': openUrl },
- testdir: prefix,
+ prefixDir,
})
return res
}
diff --git a/test/lib/commands/restart.js b/test/lib/commands/restart.js
index 7730f1a30..83773eae9 100644
--- a/test/lib/commands/restart.js
+++ b/test/lib/commands/restart.js
@@ -14,7 +14,7 @@ const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')
t.test('should run restart script from package.json', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'x',
version: '1.2.3',
diff --git a/test/lib/commands/shrinkwrap.js b/test/lib/commands/shrinkwrap.js
index 2b9e46c70..e3fc1f935 100644
--- a/test/lib/commands/shrinkwrap.js
+++ b/test/lib/commands/shrinkwrap.js
@@ -19,14 +19,14 @@ t.formatSnapshot = obj =>
2
)
-// Run shrinkwrap against a specified testdir with config items
+// Run shrinkwrap against a specified prefixDir with config items
// and make some assertions that should always be true. Sets
// the results on t.context for use in child tests
-const shrinkwrap = async (t, testdir = {}, config = {}, mocks = {}) => {
+const shrinkwrap = async (t, prefixDir = {}, config = {}, mocks = {}) => {
const { npm, logs } = await loadMockNpm(t, {
mocks,
config,
- testdir,
+ prefixDir,
})
await npm.exec('shrinkwrap', [])
@@ -38,7 +38,7 @@ const shrinkwrap = async (t, testdir = {}, config = {}, mocks = {}) => {
t.same(logs.warn, [], 'no warnings')
t.teardown(() => delete t.context)
t.context = {
- localPrefix: testdir,
+ localPrefix: prefixDir,
config,
shrinkwrap: JSON.parse(fs.readFileSync(newFile)),
logs: logs.notice.map(([, m]) => m),
diff --git a/test/lib/commands/start.js b/test/lib/commands/start.js
index 4f7dc366d..c9312c8e2 100644
--- a/test/lib/commands/start.js
+++ b/test/lib/commands/start.js
@@ -15,7 +15,7 @@ const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')
t.test('should run start script from package.json', async t => {
t.plan(2)
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'x',
version: '1.2.3',
diff --git a/test/lib/commands/stop.js b/test/lib/commands/stop.js
index 53d057b71..f5db4a047 100644
--- a/test/lib/commands/stop.js
+++ b/test/lib/commands/stop.js
@@ -14,7 +14,7 @@ const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')
t.test('should run stop script from package.json', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'x',
version: '1.2.3',
diff --git a/test/lib/commands/test.js b/test/lib/commands/test.js
index a3dbd3ff4..665df7148 100644
--- a/test/lib/commands/test.js
+++ b/test/lib/commands/test.js
@@ -14,7 +14,7 @@ const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')
t.test('should run test script from package.json', async t => {
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
'package.json': JSON.stringify({
name: 'x',
version: '1.2.3',
diff --git a/test/lib/npm.js b/test/lib/npm.js
index b2eedde72..3ae2af35c 100644
--- a/test/lib/npm.js
+++ b/test/lib/npm.js
@@ -77,7 +77,7 @@ t.test('npm.load', async t => {
t.test('basic loading', async t => {
const { npm, logs, prefix: dir, cache } = await loadMockNpm(t, {
- testdir: { node_modules: {} },
+ prefixDir: { node_modules: {} },
})
t.equal(npm.loaded, true)
@@ -165,7 +165,7 @@ t.test('npm.load', async t => {
],
})
const { npm, logs, outputs, prefix } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
bin: t.fixture('symlink', dirname(process.execPath)),
},
globals: ({ prefix }) => ({
@@ -237,7 +237,7 @@ t.test('npm.load', async t => {
})
const { npm } = await loadMockNpm(t, {
load: false,
- testdir: {
+ prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
@@ -270,7 +270,7 @@ t.test('npm.load', async t => {
],
})
const { npm, outputs } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
@@ -329,7 +329,7 @@ t.test('npm.load', async t => {
],
})
const { npm } = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
@@ -531,7 +531,7 @@ t.test('explicit workspace rejection', async t => {
],
})
const mock = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
@@ -556,7 +556,7 @@ t.test('explicit workspace rejection', async t => {
t.test('implicit workspace rejection', async t => {
const mock = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
@@ -590,7 +590,7 @@ t.test('implicit workspace rejection', async t => {
t.test('implicit workspace accept', async t => {
const mock = await loadMockNpm(t, {
- testdir: {
+ prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
diff --git a/test/lib/utils/error-message.js b/test/lib/utils/error-message.js
index ddc88c1d9..3fec501ef 100644
--- a/test/lib/utils/error-message.js
+++ b/test/lib/utils/error-message.js
@@ -16,10 +16,10 @@ mockGlobals(t, {
},
})
-const loadMockNpm = async (t, { load, command, testdir, config } = {}) => {
+const loadMockNpm = async (t, { load, command, prefixDir, config } = {}) => {
const { npm, ...rest } = await _loadMockNpm(t, {
load,
- testdir,
+ prefixDir,
config,
mocks: {
'../../package.json': {
@@ -210,7 +210,7 @@ t.test('json parse', t => {
mockGlobals(t, { 'process.argv': ['arg', 'v'] })
t.test('merge conflict in package.json', async t => {
- const testdir = {
+ const prefixDir = {
'package.json': `
{
"array": [
@@ -250,7 +250,7 @@ t.test('json parse', t => {
}
`,
}
- const npm = await loadMockNpm(t, { testdir })
+ const npm = await loadMockNpm(t, { prefixDir })
t.matchSnapshot(errorMessage(Object.assign(new Error('conflicted'), {
code: 'EJSONPARSE',
path: path.resolve(npm.prefix, 'package.json'),
@@ -259,10 +259,10 @@ t.test('json parse', t => {
})
t.test('just regular bad json in package.json', async t => {
- const testdir = {
+ const prefixDir = {
'package.json': 'not even slightly json',
}
- const npm = await loadMockNpm(t, { testdir })
+ const npm = await loadMockNpm(t, { prefixDir })
t.matchSnapshot(errorMessage(Object.assign(new Error('not json'), {
code: 'EJSONPARSE',
path: path.resolve(npm.prefix, 'package.json'),
@@ -271,10 +271,10 @@ t.test('json parse', t => {
})
t.test('json somewhere else', async t => {
- const testdir = {
+ const prefixDir = {
'blerg.json': 'not even slightly json',
}
- const npm = await loadMockNpm(t, { testdir })
+ const npm = await loadMockNpm(t, { prefixDir })
t.matchSnapshot(errorMessage(Object.assign(new Error('not json'), {
code: 'EJSONPARSE',
path: path.resolve(npm.prefix, 'blerg.json'),