summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/all-package-metadata-cache-stream-unit.js
blob: 66b24c5e0c710c5ebb2becc14c8ca2e99b03d656 (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
'use strict'

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

const getStream = require('get-stream')
const mkdirp = require('mkdirp')
const path = require('path')
const Tacks = require('tacks')
const {test} = require('tap')

const {File} = Tacks

const _createCacheEntryStream = require('../../lib/search/all-package-metadata.js')._createCacheEntryStream

// this test uses a fresh cache for each test block
// create them all in common.cache so that we can verify
// them for root-owned files in sudotest
let CACHE_DIR
let cacheCounter = 1
const chownr = require('chownr')
function setup () {
  CACHE_DIR = common.cache + '/' + cacheCounter++
  mkdirp.sync(CACHE_DIR)
  fixOwner(CACHE_DIR)
}

const fixOwner = (
  process.getuid && process.getuid() === 0 &&
  process.env.SUDO_UID && process.env.SUDO_GID
) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID)
  : () => {}

test('createCacheEntryStream basic', t => {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const fixture = new Tacks(File({
    '_updated': 1234,
    bar: {
      name: 'bar',
      version: '1.0.0'
    },
    foo: {
      name: 'foo',
      version: '1.0.0'
    }
  }))
  fixture.create(cachePath)
  fixOwner(cachePath)
  return _createCacheEntryStream(cachePath, {}).then(({
    updateStream: stream,
    updatedLatest: latest
  }) => {
    t.equals(latest, 1234, '`latest` correctly extracted')
    t.ok(stream, 'returned a stream')
    return getStream.array(stream).then(results => {
      t.deepEquals(results, [{
        name: 'bar',
        version: '1.0.0'
      }, {
        name: 'foo',
        version: '1.0.0'
      }])
    })
  })
})

test('createCacheEntryStream empty cache', t => {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const fixture = new Tacks(File({}))
  fixture.create(cachePath)
  fixOwner(cachePath)
  return _createCacheEntryStream(cachePath, {}).then(
    () => { throw new Error('should not succeed') },
    err => {
      t.ok(err, 'returned an error because there was no _updated')
      t.match(err.message, /Empty or invalid stream/, 'useful error message')
    }
  )
})

test('createCacheEntryStream no entry cache', t => {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const fixture = new Tacks(File({
    '_updated': 1234
  }))
  fixture.create(cachePath)
  fixOwner(cachePath)
  return _createCacheEntryStream(cachePath, {}).then(({
    updateStream: stream,
    updatedLatest: latest
  }) => {
    t.equals(latest, 1234, '`latest` correctly extracted')
    t.ok(stream, 'returned a stream')
    return getStream.array(stream).then(results => {
      t.deepEquals(results, [], 'no results')
    })
  })
})

test('createCacheEntryStream missing cache', t => {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  return _createCacheEntryStream(cachePath, {}).then(
    () => { throw new Error('should not succeed') },
    err => {
      t.ok(err, 'returned an error because there was no cache')
      t.equals(err.code, 'ENOENT', 'useful error message')
    }
  )
})