1'use strict' 2 3const common = require('../common-tap.js') 4const getStream = require('get-stream') 5const npm = require('../../') 6const test = require('tap').test 7const mkdirp = require('mkdirp') 8const path = require('path') 9const fs = require('fs') 10const ms = require('mississippi') 11 12const _createCacheWriteStream = require('../../lib/search/all-package-metadata.js')._createCacheWriteStream 13 14// this test uses a fresh cache for each test block 15// create them all in common.cache so that we can verify 16// them for root-owned files in sudotest 17let CACHE_DIR 18let cacheCounter = 1 19function setup () { 20 CACHE_DIR = common.cache + '/' + cacheCounter++ 21 mkdirp.sync(CACHE_DIR) 22 fixOwner(CACHE_DIR) 23} 24 25const chownr = require('chownr') 26const fixOwner = ( 27 process.getuid && process.getuid() === 0 && 28 process.env.SUDO_UID && process.env.SUDO_GID 29) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) 30 : () => {} 31 32function fromArray (array) { 33 var idx = 0 34 return ms.from.obj(function (size, next) { 35 next(null, array[idx++] || null) 36 }) 37} 38 39test('setup', function (t) { 40 // This is pretty much only used for `getCacheStat` in the implementation 41 npm.load({ cache: CACHE_DIR, registry: common.registry }, function (err) { 42 t.ifError(err, 'npm successfully loaded') 43 t.done() 44 }) 45}) 46 47test('createCacheEntryStream basic', function (t) { 48 setup() 49 var cachePath = path.join(CACHE_DIR, '.cache.json') 50 var latest = 12345 51 var src = [ 52 { name: 'bar', version: '1.0.0' }, 53 { name: 'foo', version: '1.0.0' } 54 ] 55 var srcStream = fromArray(src) 56 return _createCacheWriteStream(cachePath, latest, { 57 cache: CACHE_DIR 58 }).then(stream => { 59 t.ok(stream, 'returned a stream') 60 stream = ms.pipeline.obj(srcStream, stream) 61 return getStream.array(stream) 62 }).then(results => { 63 t.deepEquals(results, [{ 64 name: 'bar', 65 version: '1.0.0' 66 }, { 67 name: 'foo', 68 version: '1.0.0' 69 }]) 70 var fileData = JSON.parse(fs.readFileSync(cachePath)) 71 t.ok(fileData, 'cache contents written to the right file') 72 t.deepEquals(fileData, { 73 '_updated': latest, 74 bar: { 75 name: 'bar', 76 version: '1.0.0' 77 }, 78 foo: { 79 name: 'foo', 80 version: '1.0.0' 81 } 82 }, 'cache contents based on what was written') 83 }) 84}) 85 86test('createCacheEntryStream no entries', function (t) { 87 setup() 88 const cachePath = path.join(CACHE_DIR, '.cache.json') 89 var latest = 12345 90 const src = [] 91 const srcStream = fromArray(src) 92 return _createCacheWriteStream(cachePath, latest, { 93 cache: CACHE_DIR 94 }).then(stream => { 95 t.ok(stream, 'returned a stream') 96 stream = ms.pipeline.obj(srcStream, stream) 97 stream.resume() 98 return getStream(stream) 99 }).then(() => { 100 const fileData = JSON.parse(fs.readFileSync(cachePath)) 101 t.ok(fileData, 'cache file exists and has stuff in it') 102 }) 103}) 104 105test('createCacheEntryStream missing cache dir', function (t) { 106 setup() 107 var cachePath = path.join(CACHE_DIR, '.cache.json') 108 var latest = 12345 109 var src = [] 110 var srcStream = fromArray(src) 111 return _createCacheWriteStream(cachePath, latest, { 112 cache: CACHE_DIR 113 }).then(stream => { 114 t.ok(stream, 'returned a stream') 115 stream = ms.pipeline.obj(srcStream, stream) 116 return getStream.array(stream) 117 }).then(res => { 118 t.deepEqual(res, [], 'no data returned') 119 var fileData = JSON.parse(fs.readFileSync(cachePath)) 120 t.ok(fileData, 'cache contents written to the right file') 121 t.deepEquals(fileData, { 122 '_updated': latest 123 }, 'cache still contains `_updated`') 124 }) 125}) 126