From 3c024ace60904c69e61da00e1fb56c0c1735804a Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Mon, 1 Aug 2022 14:29:56 -0400 Subject: feat: add npm query cmd (#5000) Co-authored-by: Gar --- test/lib/commands/query.js | 199 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 test/lib/commands/query.js (limited to 'test') 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') +}) -- cgit v1.2.3