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:
authorRuy Adorno <ruyadorno@hotmail.com>2022-08-01 21:29:56 +0300
committerGitHub <noreply@github.com>2022-08-01 21:29:56 +0300
commit3c024ace60904c69e61da00e1fb56c0c1735804a (patch)
tree20455ad5af7395f9a25943127d6d4ed36421eab7 /test
parent050284d2abb6aa91a0f9ffad5b0c4f074e5dbf6d (diff)
feat: add npm query cmd (#5000)
Co-authored-by: Gar <gar+gh@danger.computer>
Diffstat (limited to 'test')
-rw-r--r--test/lib/commands/query.js199
1 files changed, 199 insertions, 0 deletions
diff --git a/test/lib/commands/query.js b/test/lib/commands/query.js
new file mode 100644
index 000000000..fb5b4843c
--- /dev/null
+++ b/test/lib/commands/query.js
@@ -0,0 +1,199 @@
+const t = require('tap')
+const { load: loadMockNpm } = require('../../fixtures/mock-npm')
+
+t.cleanSnapshot = (str) => {
+ const normalizePath = p => p
+ .replace(/\\+/g, '/')
+ .replace(/\r\n/g, '\n')
+ return normalizePath(str)
+ .replace(new RegExp(normalizePath(process.cwd()), 'g'), '{CWD}')
+ // normalize between windows and posix
+ .replace(new RegExp('lib/node_modules', 'g'), 'node_modules')
+}
+
+t.test('simple query', async t => {
+ const { npm, joinedOutput } = await loadMockNpm(t, {
+ prefixDir: {
+ node_modules: {
+ a: {
+ name: 'a',
+ version: '1.0.0',
+ },
+ b: {
+ name: 'b',
+ version: '^2.0.0',
+ },
+ },
+ 'package.json': JSON.stringify({
+ name: 'project',
+ dependencies: {
+ a: '^1.0.0',
+ b: '^1.0.0',
+ },
+ peerDependencies: {
+ c: '1.0.0',
+ },
+ }),
+ },
+ })
+ await npm.exec('query', [':root, :root > *'])
+ t.matchSnapshot(joinedOutput(), 'should return root object and direct children')
+})
+
+t.test('recursive tree', async t => {
+ const { npm, joinedOutput } = await loadMockNpm(t, {
+ prefixDir: {
+ node_modules: {
+ a: {
+ name: 'a',
+ version: '1.0.0',
+ },
+ b: {
+ name: 'b',
+ version: '^2.0.0',
+ dependencies: {
+ a: '1.0.0',
+ },
+ },
+ },
+ 'package.json': JSON.stringify({
+ name: 'project',
+ dependencies: {
+ a: '^1.0.0',
+ b: '^1.0.0',
+ },
+ }),
+ },
+ })
+ await npm.exec('query', ['*'])
+ t.matchSnapshot(joinedOutput(), 'should return everything in the tree, accounting for recursion')
+})
+t.test('workspace query', async t => {
+ const { npm, joinedOutput } = await loadMockNpm(t, {
+ config: {
+ workspaces: ['c'],
+ },
+ prefixDir: {
+ node_modules: {
+ a: {
+ name: 'a',
+ version: '1.0.0',
+ },
+ b: {
+ name: 'b',
+ version: '^2.0.0',
+ },
+ c: t.fixture('symlink', '../c'),
+ },
+ c: {
+ 'package.json': JSON.stringify({
+ name: 'c',
+ version: '1.0.0',
+ }),
+ },
+ 'package.json': JSON.stringify({
+ name: 'project',
+ workspaces: ['c'],
+ dependencies: {
+ a: '^1.0.0',
+ b: '^1.0.0',
+ },
+ }),
+ },
+ })
+ await npm.exec('query', [':scope'], ['c'])
+ t.matchSnapshot(joinedOutput(), 'should return workspace object')
+})
+
+t.test('include-workspace-root', async t => {
+ const { npm, joinedOutput } = await loadMockNpm(t, {
+ config: {
+ 'include-workspace-root': true,
+ workspaces: ['c'],
+ },
+ prefixDir: {
+ node_modules: {
+ a: {
+ name: 'a',
+ version: '1.0.0',
+ },
+ b: {
+ name: 'b',
+ version: '^2.0.0',
+ },
+ c: t.fixture('symlink', '../c'),
+ },
+ c: {
+ 'package.json': JSON.stringify({
+ name: 'c',
+ version: '1.0.0',
+ }),
+ },
+ 'package.json': JSON.stringify({
+ name: 'project',
+ workspaces: ['c'],
+ dependencies: {
+ a: '^1.0.0',
+ b: '^1.0.0',
+ },
+ }),
+ },
+ })
+ await npm.exec('query', [':scope'], ['c'])
+ t.matchSnapshot(joinedOutput(), 'should return workspace object and root object')
+})
+t.test('linked node', async t => {
+ const { npm, joinedOutput } = await loadMockNpm(t, {
+ prefixDir: {
+ node_modules: {
+ a: t.fixture('symlink', '../a'),
+ },
+ a: {
+ 'package.json': JSON.stringify({
+ name: 'a',
+ version: '1.0.0',
+ }),
+ },
+ 'package.json': JSON.stringify({
+ name: 'project',
+ dependencies: {
+ a: 'file:./a',
+ },
+ }),
+ },
+ })
+ await npm.exec('query', ['[name=a]'])
+ t.matchSnapshot(joinedOutput(), 'should return linked node res')
+})
+
+t.test('global', async t => {
+ const { npm, joinedOutput } = await loadMockNpm(t, {
+ config: {
+ global: true,
+ },
+ // This is a global dir that works in both windows and non-windows, that's
+ // why it has two node_modules folders
+ globalPrefixDir: {
+ node_modules: {
+ lorem: {
+ 'package.json': JSON.stringify({
+ name: 'lorem',
+ version: '2.0.0',
+ }),
+ },
+ },
+ lib: {
+ node_modules: {
+ lorem: {
+ 'package.json': JSON.stringify({
+ name: 'lorem',
+ version: '2.0.0',
+ }),
+ },
+ },
+ },
+ },
+ })
+ await npm.exec('query', ['[name=lorem]'])
+ t.matchSnapshot(joinedOutput(), 'should return global package')
+})