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

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

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

const {File} = Tacks

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

const PKG_DIR = path.resolve(__dirname, 'create-cache-entry-stream')
const CACHE_DIR = path.resolve(PKG_DIR, 'cache')

function setup () {
  mkdirp.sync(CACHE_DIR)
}

function cleanup () {
  rimraf.sync(PKG_DIR)
}

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)
  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'
      }])
      cleanup()
    })
  })
})

test('createCacheEntryStream empty cache', t => {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const fixture = new Tacks(File({}))
  fixture.create(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')
      cleanup()
    }
  )
})

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)
  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')
      cleanup()
    })
  })
})

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')
      cleanup()
    }
  )
})