summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/search.all-package-search.js
blob: c70f4f8e7ef07494f98130025dbce212ad44556e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var path = require('path')
var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var osenv = require('osenv')
var rimraf = require('rimraf')
var cacheFile = require('npm-cache-filename')
var test = require('tap').test
var Tacks = require('tacks')
var File = Tacks.File

var common = require('../common-tap.js')

var PKG_DIR = path.resolve(__dirname, 'search')
var CACHE_DIR = path.resolve(PKG_DIR, 'cache')
var cacheBase = cacheFile(CACHE_DIR)(common.registry + '/-/all')
var cachePath = path.join(cacheBase, '.cache.json')

var server

test('setup', function (t) {
  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
    t.ifError(err, 'registry mocked successfully')
    server = s
    t.pass('all set up')
    t.done()
  })
})

var searches = [
  {
    term: 'cool',
    description: 'non-regex search',
    location: 103
  },
  {
    term: '/cool/',
    description: 'regex search',
    location: 103
  },
  {
    term: 'cool',
    description: 'searches name field',
    location: 103
  },
  {
    term: 'ool',
    description: 'excludes matches for --searchexclude',
    location: 205,
    inject: {
      other: { name: 'other', description: 'this is a simple tool' }
    },
    extraOpts: ['--searchexclude', 'cool']
  },
  {
    term: 'neat lib',
    description: 'searches description field',
    location: 141,
    inject: {
      cool: {
        name: 'cool', version: '5.0.0', description: 'this is a neat lib'
      }
    }
  },
  {
    term: 'foo',
    description: 'skips description field with --no-description',
    location: 80,
    inject: {
      cool: {
        name: 'cool', version: '5.0.0', description: 'foo bar!'
      }
    },
    extraOpts: ['--no-description']
  },
  {
    term: 'zkat',
    description: 'searches maintainers by name',
    location: 155,
    inject: {
      cool: {
        name: 'cool',
        version: '5.0.0',
        maintainers: [{
          name: 'zkat'
        }]
      }
    }
  },
  {
    term: '=zkat',
    description: 'searches maintainers unambiguously by =name',
    location: 154,
    inject: {
      bar: { name: 'bar', description: 'zkat thing', version: '1.0.0' },
      cool: {
        name: 'cool',
        version: '5.0.0',
        maintainers: [{
          name: 'zkat'
        }]
      }
    }
  },
  {
    term: 'github.com',
    description: 'searches projects by url',
    location: 205,
    inject: {
      bar: {
        name: 'bar',
        url: 'gitlab.com/bar',
        // For historical reasons, `url` is only present if `versions` is there
        versions: ['1.0.0'],
        version: '1.0.0'
      },
      cool: {
        name: 'cool',
        version: '5.0.0',
        versions: ['1.0.0'],
        url: 'github.com/cool/cool'
      }
    }
  },
  {
    term: 'monad',
    description: 'searches projects by keywords',
    location: 197,
    inject: {
      cool: {
        name: 'cool',
        version: '5.0.0',
        keywords: ['monads']
      }
    }
  }
]

// These test classic hand-matched searches
searches.forEach(function (search) {
  test(search.description, function (t) {
    setup()
    server.get('/-/v1/search?text=' + encodeURIComponent(search.term) + '&size=20').once().reply(404, {})
    var now = Date.now()
    var cacheContents = {
      '_updated': now,
      bar: { name: 'bar', version: '1.0.0' },
      cool: { name: 'cool', version: '5.0.0' },
      foo: { name: 'foo', version: '2.0.0' },
      other: { name: 'other', version: '1.0.0' }
    }
    for (var k in search.inject) {
      cacheContents[k] = search.inject[k]
    }
    var fixture = new Tacks(File(cacheContents))
    fixture.create(cachePath)
    common.npm([
      'search', search.term,
      '--registry', common.registry,
      '--cache', CACHE_DIR,
      '--loglevel', 'error',
      '--color', 'always'
    ].concat(search.extraOpts || []),
    {},
    function (err, code, stdout, stderr) {
      t.equal(stderr, '', 'no error output')
      t.notEqual(stdout, '', 'got output')
      t.equal(code, 0, 'search finished successfully')
      t.ifErr(err, 'search finished successfully')
      // \033 == \u001B
      var markStart = '\u001B\\[[0-9][0-9]m'
      var markEnd = '\u001B\\[0m'

      var re = new RegExp(markStart + '.*?' + markEnd)

      var cnt = stdout.search(re)
      t.equal(
        cnt,
        search.location,
        search.description + ' search for ' + search.term
      )
      t.end()
    })
  })
})

test('cleanup', function (t) {
  cleanup()
  server.close()
  t.end()
})

function setup () {
  cleanup()
  mkdirp.sync(cacheBase)
}

function cleanup () {
  server.done()
  process.chdir(osenv.tmpdir())
  rimraf.sync(PKG_DIR)
}