]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/search.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / search.js
1 var fs = require('graceful-fs')
2 var path = require('path')
3
4 var mkdirp = require('mkdirp')
5 var mr = require('npm-registry-mock')
6 var osenv = require('osenv')
7 var rimraf = require('rimraf')
8 var test = require('tap').test
9
10 var common = require('../common-tap.js')
11
12 var pkg = path.resolve(__dirname, 'search')
13 var cache = path.resolve(pkg, 'cache')
14 var registryCache = path.resolve(cache, 'localhost_1337', '-', 'all')
15 var cacheJsonFile = path.resolve(registryCache, '.cache.json')
16
17 var timeMock = {
18   epoch: 1411727900,
19   future: 1411727900 + 100,
20   all: 1411727900 + 25,
21   since: 0  // filled by since server callback
22 }
23
24 var EXEC_OPTS = {}
25
26 var mocks = {
27   /* Since request, always response with an _update time > the time requested */
28   sinceFuture: function (server) {
29     server.filteringPathRegEx(/startkey=[^&]*/g, function (s) {
30       var _allMock = JSON.parse(JSON.stringify(allMock))
31       timeMock.since = _allMock._updated = s.replace('startkey=', '')
32       server.get('/-/all/since?stale=update_after&' + s)
33         .reply(200, _allMock)
34       return s
35     })
36   },
37   allFutureUpdatedOnly: function (server) {
38     server.get('/-/all')
39       .reply(200, stringifyUpdated(timeMock.future))
40   },
41   all: function (server) {
42     server.get('/-/all')
43       .reply(200, allMock)
44   }
45 }
46
47 test('No previous cache, init cache triggered by first search', function (t) {
48   cleanup()
49
50   mr({ port: common.port, plugin: mocks.allFutureUpdatedOnly }, function (err, s) {
51     t.ifError(err, 'mock registry started')
52     common.npm([
53       'search', 'do not do extra search work on my behalf',
54       '--registry', common.registry,
55       '--cache', cache,
56       '--loglevel', 'silent',
57       '--color', 'always'
58     ],
59     EXEC_OPTS,
60     function (err, code) {
61       s.close()
62       t.equal(code, 0, 'search finished successfully')
63       t.ifErr(err, 'search finished successfully')
64
65       t.ok(
66         fs.existsSync(cacheJsonFile),
67         cacheJsonFile + ' expected to have been created'
68       )
69
70       var cacheData = JSON.parse(fs.readFileSync(cacheJsonFile, 'utf8'))
71       t.equal(cacheData._updated, String(timeMock.future))
72       t.end()
73     })
74   })
75 })
76
77 test('previous cache, _updated set, should trigger since request', function (t) {
78   setupCache()
79
80   function m (server) {
81     [ mocks.all, mocks.sinceFuture ].forEach(function (m) {
82       m(server)
83     })
84   }
85   mr({ port: common.port, plugin: m }, function (err, s) {
86     t.ifError(err, 'mock registry started')
87     common.npm([
88       'search', 'do not do extra search work on my behalf',
89       '--registry', common.registry,
90       '--cache', cache,
91       '--loglevel', 'silly',
92       '--color', 'always'
93     ],
94     EXEC_OPTS,
95     function (err, code) {
96       s.close()
97       t.equal(code, 0, 'search finished successfully')
98       t.ifErr(err, 'search finished successfully')
99
100       var cacheData = JSON.parse(fs.readFileSync(cacheJsonFile, 'utf8'))
101       t.equal(
102         cacheData._updated,
103         timeMock.since,
104         'cache update time gotten from since response'
105       )
106       t.end()
107     })
108   })
109 })
110
111 var searches = [
112   {
113     term: 'f36b6a6123da50959741e2ce4d634f96ec668c56',
114     description: 'non-regex',
115     location: 241
116   },
117   {
118     term: '/f36b6a6123da50959741e2ce4d634f96ec668c56/',
119     description: 'regex',
120     location: 241
121   }
122 ]
123
124 searches.forEach(function (search) {
125   test(search.description + ' search in color', function (t) {
126     cleanup()
127     mr({ port: common.port, plugin: mocks.all }, function (er, s) {
128       common.npm([
129         'search', search.term,
130         '--registry', common.registry,
131         '--cache', cache,
132         '--loglevel', 'silent',
133         '--color', 'always'
134       ],
135       EXEC_OPTS,
136       function (err, code, stdout) {
137         s.close()
138         t.equal(code, 0, 'search finished successfully')
139         t.ifErr(err, 'search finished successfully')
140         // \033 == \u001B
141         var markStart = '\u001B\\[[0-9][0-9]m'
142         var markEnd = '\u001B\\[0m'
143
144         var re = new RegExp(markStart + '.*?' + markEnd)
145
146         var cnt = stdout.search(re)
147         t.equal(
148           cnt,
149           search.location,
150           search.description + ' search for ' + search.term
151         )
152         t.end()
153       })
154     })
155   })
156 })
157
158 test('cleanup', function (t) {
159   cleanup()
160   t.end()
161 })
162
163 function cleanup () {
164   process.chdir(osenv.tmpdir())
165   rimraf.sync(pkg)
166 }
167
168 function setupCache () {
169   cleanup()
170   mkdirp.sync(cache)
171   mkdirp.sync(registryCache)
172   var res = fs.writeFileSync(cacheJsonFile, stringifyUpdated(timeMock.epoch))
173   if (res) throw new Error('Creating cache file failed')
174 }
175
176 function stringifyUpdated (time) {
177   return JSON.stringify({ _updated: String(time) })
178 }
179
180 var allMock = {
181   '_updated': timeMock.all,
182   'generator-frontcow': {
183       'name': 'generator-frontcow',
184       'description': 'f36b6a6123da50959741e2ce4d634f96ec668c56 This is a fake description to ensure we do not accidentally search the real npm registry or use some kind of cache',
185       'dist-tags': {
186           'latest': '0.1.19'
187       },
188       'maintainers': [
189           {
190               'name': 'bcabanes',
191               'email': 'contact@benjamincabanes.com'
192           }
193       ],
194       'homepage': 'https://github.com/bcabanes/generator-frontcow',
195       'keywords': [
196           'sass',
197           'frontend',
198           'yeoman-generator',
199           'atomic',
200           'design',
201           'sass',
202           'foundation',
203           'foundation5',
204           'atomic design',
205           'bourbon',
206           'polyfill',
207           'font awesome'
208       ],
209       'repository': {
210           'type': 'git',
211           'url': 'https://github.com/bcabanes/generator-frontcow'
212       },
213       'author': {
214           'name': 'ben',
215           'email': 'contact@benjamincabanes.com',
216           'url': 'https://github.com/bcabanes'
217       },
218       'bugs': {
219           'url': 'https://github.com/bcabanes/generator-frontcow/issues'
220       },
221       'license': 'MIT',
222       'readmeFilename': 'README.md',
223       'time': {
224           'modified': '2014-10-03T02:26:18.406Z'
225       },
226       'versions': {
227           '0.1.19': 'latest'
228       }
229   },
230   'marko': {
231       'name': 'marko',
232       'description': 'Marko is an extensible, streaming, asynchronous, high performance, HTML-based templating language that can be used in Node.js or in the browser.',
233       'dist-tags': {
234           'latest': '1.2.16'
235       },
236       'maintainers': [
237           {
238               'name': 'pnidem',
239               'email': 'pnidem@gmail.com'
240           },
241           {
242               'name': 'philidem',
243               'email': 'phillip.idem@gmail.com'
244           }
245       ],
246       'homepage': 'https://github.com/raptorjs/marko',
247       'keywords': [
248           'templating',
249           'template',
250           'async',
251           'streaming'
252       ],
253       'repository': {
254           'type': 'git',
255           'url': 'https://github.com/raptorjs/marko.git'
256       },
257       'author': {
258           'name': 'Patrick Steele-Idem',
259           'email': 'pnidem@gmail.com'
260       },
261       'bugs': {
262           'url': 'https://github.com/raptorjs/marko/issues'
263       },
264       'license': 'Apache License v2.0',
265       'readmeFilename': 'README.md',
266       'users': {
267           'pnidem': true
268       },
269       'time': {
270           'modified': '2014-10-03T02:27:31.775Z'
271       },
272       'versions': {
273           '1.2.16': 'latest'
274       }
275   }
276 }