1'use strict' 2 3const common = require('../common-tap.js') 4 5const getStream = require('get-stream') 6const mkdirp = require('mkdirp') 7const path = require('path') 8const Tacks = require('tacks') 9const {test} = require('tap') 10 11const {File} = Tacks 12 13const _createCacheEntryStream = require('../../lib/search/all-package-metadata.js')._createCacheEntryStream 14 15// this test uses a fresh cache for each test block 16// create them all in common.cache so that we can verify 17// them for root-owned files in sudotest 18let CACHE_DIR 19let cacheCounter = 1 20const chownr = require('chownr') 21function setup () { 22 CACHE_DIR = common.cache + '/' + cacheCounter++ 23 mkdirp.sync(CACHE_DIR) 24 fixOwner(CACHE_DIR) 25} 26 27const fixOwner = ( 28 process.getuid && process.getuid() === 0 && 29 process.env.SUDO_UID && process.env.SUDO_GID 30) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) 31 : () => {} 32 33test('createCacheEntryStream basic', t => { 34 setup() 35 const cachePath = path.join(CACHE_DIR, '.cache.json') 36 const fixture = new Tacks(File({ 37 '_updated': 1234, 38 bar: { 39 name: 'bar', 40 version: '1.0.0' 41 }, 42 foo: { 43 name: 'foo', 44 version: '1.0.0' 45 } 46 })) 47 fixture.create(cachePath) 48 fixOwner(cachePath) 49 return _createCacheEntryStream(cachePath, {}).then(({ 50 updateStream: stream, 51 updatedLatest: latest 52 }) => { 53 t.equals(latest, 1234, '`latest` correctly extracted') 54 t.ok(stream, 'returned a stream') 55 return getStream.array(stream).then(results => { 56 t.deepEquals(results, [{ 57 name: 'bar', 58 version: '1.0.0' 59 }, { 60 name: 'foo', 61 version: '1.0.0' 62 }]) 63 }) 64 }) 65}) 66 67test('createCacheEntryStream empty cache', t => { 68 setup() 69 const cachePath = path.join(CACHE_DIR, '.cache.json') 70 const fixture = new Tacks(File({})) 71 fixture.create(cachePath) 72 fixOwner(cachePath) 73 return _createCacheEntryStream(cachePath, {}).then( 74 () => { throw new Error('should not succeed') }, 75 err => { 76 t.ok(err, 'returned an error because there was no _updated') 77 t.match(err.message, /Empty or invalid stream/, 'useful error message') 78 } 79 ) 80}) 81 82test('createCacheEntryStream no entry cache', t => { 83 setup() 84 const cachePath = path.join(CACHE_DIR, '.cache.json') 85 const fixture = new Tacks(File({ 86 '_updated': 1234 87 })) 88 fixture.create(cachePath) 89 fixOwner(cachePath) 90 return _createCacheEntryStream(cachePath, {}).then(({ 91 updateStream: stream, 92 updatedLatest: latest 93 }) => { 94 t.equals(latest, 1234, '`latest` correctly extracted') 95 t.ok(stream, 'returned a stream') 96 return getStream.array(stream).then(results => { 97 t.deepEquals(results, [], 'no results') 98 }) 99 }) 100}) 101 102test('createCacheEntryStream missing cache', t => { 103 setup() 104 const cachePath = path.join(CACHE_DIR, '.cache.json') 105 return _createCacheEntryStream(cachePath, {}).then( 106 () => { throw new Error('should not succeed') }, 107 err => { 108 t.ok(err, 'returned an error because there was no cache') 109 t.equals(err.code, 'ENOENT', 'useful error message') 110 } 111 ) 112}) 113