1 var fs = require('graceful-fs')
2 var path = require('path')
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
10 var common = require('../common-tap.js')
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')
19 future: 1411727900 + 100,
21 since: 0 // filled by since server callback
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)
37 allFutureUpdatedOnly: function (server) {
39 .reply(200, stringifyUpdated(timeMock.future))
41 all: function (server) {
47 test('No previous cache, init cache triggered by first search', function (t) {
50 mr({ port: common.port, plugin: mocks.allFutureUpdatedOnly }, function (err, s) {
51 t.ifError(err, 'mock registry started')
53 'search', 'do not do extra search work on my behalf',
54 '--registry', common.registry,
56 '--loglevel', 'silent',
60 function (err, code) {
62 t.equal(code, 0, 'search finished successfully')
63 t.ifErr(err, 'search finished successfully')
66 fs.existsSync(cacheJsonFile),
67 cacheJsonFile + ' expected to have been created'
70 var cacheData = JSON.parse(fs.readFileSync(cacheJsonFile, 'utf8'))
71 t.equal(cacheData._updated, String(timeMock.future))
77 test('previous cache, _updated set, should trigger since request', function (t) {
81 [ mocks.all, mocks.sinceFuture ].forEach(function (m) {
85 mr({ port: common.port, plugin: m }, function (err, s) {
86 t.ifError(err, 'mock registry started')
88 'search', 'do not do extra search work on my behalf',
89 '--registry', common.registry,
91 '--loglevel', 'silly',
95 function (err, code) {
97 t.equal(code, 0, 'search finished successfully')
98 t.ifErr(err, 'search finished successfully')
100 var cacheData = JSON.parse(fs.readFileSync(cacheJsonFile, 'utf8'))
104 'cache update time gotten from since response'
113 term: 'f36b6a6123da50959741e2ce4d634f96ec668c56',
114 description: 'non-regex',
118 term: '/f36b6a6123da50959741e2ce4d634f96ec668c56/',
119 description: 'regex',
124 searches.forEach(function (search) {
125 test(search.description + ' search in color', function (t) {
127 mr({ port: common.port, plugin: mocks.all }, function (er, s) {
129 'search', search.term,
130 '--registry', common.registry,
132 '--loglevel', 'silent',
136 function (err, code, stdout) {
138 t.equal(code, 0, 'search finished successfully')
139 t.ifErr(err, 'search finished successfully')
141 var markStart = '\u001B\\[[0-9][0-9]m'
142 var markEnd = '\u001B\\[0m'
144 var re = new RegExp(markStart + '.*?' + markEnd)
146 var cnt = stdout.search(re)
150 search.description + ' search for ' + search.term
158 test('cleanup', function (t) {
163 function cleanup () {
164 process.chdir(osenv.tmpdir())
168 function setupCache () {
171 mkdirp.sync(registryCache)
172 var res = fs.writeFileSync(cacheJsonFile, stringifyUpdated(timeMock.epoch))
173 if (res) throw new Error('Creating cache file failed')
176 function stringifyUpdated (time) {
177 return JSON.stringify({ _updated: String(time) })
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',
191 'email': 'contact@benjamincabanes.com'
194 'homepage': 'https://github.com/bcabanes/generator-frontcow',
211 'url': 'https://github.com/bcabanes/generator-frontcow'
215 'email': 'contact@benjamincabanes.com',
216 'url': 'https://github.com/bcabanes'
219 'url': 'https://github.com/bcabanes/generator-frontcow/issues'
222 'readmeFilename': 'README.md',
224 'modified': '2014-10-03T02:26:18.406Z'
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.',
239 'email': 'pnidem@gmail.com'
243 'email': 'phillip.idem@gmail.com'
246 'homepage': 'https://github.com/raptorjs/marko',
255 'url': 'https://github.com/raptorjs/marko.git'
258 'name': 'Patrick Steele-Idem',
259 'email': 'pnidem@gmail.com'
262 'url': 'https://github.com/raptorjs/marko/issues'
264 'license': 'Apache License v2.0',
265 'readmeFilename': 'README.md',
270 'modified': '2014-10-03T02:27:31.775Z'