• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const t = require('tap')
2const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
3const MockRegistry = require('@npmcli/mock-registry')
4const libnpmsearchResultFixture =
5  require('../../fixtures/libnpmsearch-stream-result.js')
6
7t.test('no args', async t => {
8  const { npm } = await loadMockNpm(t)
9  await t.rejects(
10    npm.exec('search', []),
11    /search must be called with arguments/,
12    'should throw usage instructions'
13  )
14})
15
16t.test('search <name> text', async t => {
17  const { npm, joinedOutput } = await loadMockNpm(t)
18  const registry = new MockRegistry({
19    tap: t,
20    registry: npm.config.get('registry'),
21  })
22
23  registry.search({ results: libnpmsearchResultFixture })
24  await npm.exec('search', ['libnpm'])
25  t.matchSnapshot(joinedOutput(), 'should have expected search results')
26})
27
28t.test('search <name> --json', async t => {
29  const { npm, joinedOutput } = await loadMockNpm(t, { config: { json: true } })
30  const registry = new MockRegistry({
31    tap: t,
32    registry: npm.config.get('registry'),
33  })
34
35  registry.search({ results: libnpmsearchResultFixture })
36
37  await npm.exec('search', ['libnpm'])
38
39  t.same(
40    JSON.parse(joinedOutput()),
41    libnpmsearchResultFixture,
42    'should have expected search results as json'
43  )
44})
45
46t.test('search <name> --parseable', async t => {
47  const { npm, joinedOutput } = await loadMockNpm(t, { config: { parseable: true } })
48  const registry = new MockRegistry({
49    tap: t,
50    registry: npm.config.get('registry'),
51  })
52
53  registry.search({ results: libnpmsearchResultFixture })
54  await npm.exec('search', ['libnpm'])
55  t.matchSnapshot(joinedOutput(), 'should have expected search results as parseable')
56})
57
58t.test('search <name> --color', async t => {
59  const { npm, joinedOutput } = await loadMockNpm(t, { config: { color: 'always' } })
60  const registry = new MockRegistry({
61    tap: t,
62    registry: npm.config.get('registry'),
63  })
64
65  registry.search({ results: libnpmsearchResultFixture })
66  await npm.exec('search', ['libnpm'])
67  t.matchSnapshot(joinedOutput(), 'should have expected search results with color')
68})
69
70t.test('search /<name>/--color', async t => {
71  const { npm, joinedOutput } = await loadMockNpm(t, { config: { color: 'always' } })
72  const registry = new MockRegistry({
73    tap: t,
74    registry: npm.config.get('registry'),
75  })
76
77  registry.search({ results: libnpmsearchResultFixture })
78  await npm.exec('search', ['/libnpm/'])
79  t.matchSnapshot(joinedOutput(), 'should have expected search results with color')
80})
81
82t.test('search <name>', async t => {
83  const { npm, joinedOutput } = await loadMockNpm(t)
84  const registry = new MockRegistry({
85    tap: t,
86    registry: npm.config.get('registry'),
87  })
88
89  registry.search({ results: [{
90    name: 'foo',
91    scope: 'unscoped',
92    version: '1.0.0',
93    description: '',
94    keywords: [],
95    date: null,
96    author: { name: 'Foo', email: 'foo@npmjs.com' },
97    publisher: { name: 'Foo', email: 'foo@npmjs.com' },
98    maintainers: [
99      { username: 'foo', email: 'foo@npmjs.com' },
100    ],
101  }, {
102    name: 'libnpmversion',
103    scope: 'unscoped',
104    version: '1.0.0',
105    description: '',
106    keywords: [],
107    date: null,
108    author: { name: 'Foo', email: 'foo@npmjs.com' },
109    publisher: { name: 'Foo', email: 'foo@npmjs.com' },
110    maintainers: [
111      { username: 'foo', email: 'foo@npmjs.com' },
112    ],
113  }] })
114
115  await npm.exec('search', ['foo'])
116
117  t.matchSnapshot(joinedOutput(), 'should have filtered expected search results')
118})
119
120t.test('empty search results', async t => {
121  const { npm, joinedOutput } = await loadMockNpm(t)
122  const registry = new MockRegistry({
123    tap: t,
124    registry: npm.config.get('registry'),
125  })
126
127  registry.search({ results: [] })
128  await npm.exec('search', ['foo'])
129
130  t.matchSnapshot(joinedOutput(), 'should have expected search results')
131})
132
133t.test('empty search results --json', async t => {
134  const { npm, joinedOutput } = await loadMockNpm(t, { config: { json: true } })
135  const registry = new MockRegistry({
136    tap: t,
137    registry: npm.config.get('registry'),
138  })
139
140  registry.search({ results: [] })
141
142  await npm.exec('search', ['foo'])
143  t.equal(joinedOutput(), '\n[]\n', 'should have expected empty square brackets')
144})
145
146t.test('search api response error', async t => {
147  const { npm } = await loadMockNpm(t)
148
149  const registry = new MockRegistry({
150    tap: t,
151    registry: npm.config.get('registry'),
152  })
153
154  registry.search({ error: 'ERR' })
155
156  await t.rejects(
157    npm.exec('search', ['foo']),
158    /ERR/,
159    'should throw response error'
160  )
161})
162
163t.test('search exclude string', async t => {
164  const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: 'libnpmversion' } })
165  const registry = new MockRegistry({
166    tap: t,
167    registry: npm.config.get('registry'),
168  })
169
170  registry.search({ results: libnpmsearchResultFixture })
171  await npm.exec('search', ['libnpm'])
172  t.matchSnapshot(joinedOutput(), 'results should not have libnpmversion')
173})
174
175t.test('search exclude username with upper case letters', async t => {
176  const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: 'NLF' } })
177  const registry = new MockRegistry({
178    tap: t,
179    registry: npm.config.get('registry'),
180  })
181
182  registry.search({ results: libnpmsearchResultFixture })
183  await npm.exec('search', ['libnpm'])
184  t.matchSnapshot(joinedOutput(), 'results should not have nlf')
185})
186
187t.test('search exclude regex', async t => {
188  const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: '/version/' } })
189  const registry = new MockRegistry({
190    tap: t,
191    registry: npm.config.get('registry'),
192  })
193
194  registry.search({ results: libnpmsearchResultFixture })
195  await npm.exec('search', ['libnpm'])
196  t.matchSnapshot(joinedOutput(), 'results should not have libnpmversion')
197})
198
199t.test('search exclude forward slash', async t => {
200  const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: '/version' } })
201  const registry = new MockRegistry({
202    tap: t,
203    registry: npm.config.get('registry'),
204  })
205
206  registry.search({ results: libnpmsearchResultFixture })
207  await npm.exec('search', ['libnpm'])
208  t.matchSnapshot(joinedOutput(), 'results should not have libnpmversion')
209})
210