summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/all-package-metadata-cache-stream-unit.js
blob: 51be836769050ff2d6d2907d259729dab576acf8 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'

require('../common-tap.js')
var test = require('tap').test
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var path = require('path')
var ms = require('mississippi')
var Tacks = require('tacks')
var File = Tacks.File

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

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

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

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

test('createCacheEntryStream basic', function (t) {
  setup()
  var cachePath = path.join(CACHE_DIR, '.cache.json')
  var fixture = new Tacks(File({
    '_updated': 1234,
    bar: {
      name: 'bar',
      version: '1.0.0'
    },
    foo: {
      name: 'foo',
      version: '1.0.0'
    }
  }))
  fixture.create(cachePath)
  _createCacheEntryStream(cachePath, function (err, stream, latest) {
    if (err) throw err
    t.equals(latest, 1234, '`latest` correctly extracted')
    t.ok(stream, 'returned a stream')
    var results = []
    stream.on('data', function (pkg) {
      results.push(pkg)
    })
    ms.finished(stream, function (err) {
      if (err) throw err
      t.deepEquals(results, [{
        name: 'bar',
        version: '1.0.0'
      }, {
        name: 'foo',
        version: '1.0.0'
      }])
      cleanup()
      t.done()
    })
  })
})

test('createCacheEntryStream empty cache', function (t) {
  setup()
  var cachePath = path.join(CACHE_DIR, '.cache.json')
  var fixture = new Tacks(File({}))
  fixture.create(cachePath)
  _createCacheEntryStream(cachePath, function (err, stream, latest) {
    t.ok(err, 'returned an error because there was no _updated')
    t.match(err.message, /Empty or invalid stream/, 'useful error message')
    t.notOk(stream, 'no stream returned')
    t.notOk(latest, 'no latest returned')
    cleanup()
    t.done()
  })
})

test('createCacheEntryStream no entry cache', function (t) {
  setup()
  var cachePath = path.join(CACHE_DIR, '.cache.json')
  var fixture = new Tacks(File({
    '_updated': 1234
  }))
  fixture.create(cachePath)
  _createCacheEntryStream(cachePath, function (err, stream, latest) {
    if (err) throw err
    t.equals(latest, 1234, '`latest` correctly extracted')
    t.ok(stream, 'returned a stream')
    var results = []
    stream.on('data', function (pkg) {
      results.push(pkg)
    })
    ms.finished(stream, function (err) {
      if (err) throw err
      t.deepEquals(results, [], 'no results')
      cleanup()
      t.done()
    })
  })
})

test('createCacheEntryStream missing cache', function (t) {
  setup()
  var cachePath = path.join(CACHE_DIR, '.cache.json')
  _createCacheEntryStream(cachePath, function (err, stream, latest) {
    t.ok(err, 'returned an error because there was no cache')
    t.equals(err.code, 'ENOENT', 'useful error message')
    t.notOk(stream, 'no stream returned')
    t.notOk(latest, 'no latest returned')
    cleanup()
    t.done()
  })
})

test('createCacheEntryStream bad syntax', function (t) {
  setup()
  var cachePath = path.join(CACHE_DIR, '.cache.json')
  var fixture = new Tacks(File('{"_updated": 1234, uh oh'))
  fixture.create(cachePath)
  _createCacheEntryStream(cachePath, function (err, stream, latest) {
    if (err) throw err
    t.equals(latest, 1234, '`latest` correctly extracted')
    t.ok(stream, 'returned a stream')
    var results = []
    stream.on('data', function (pkg) {
      results.push(pkg)
    })
    ms.finished(stream, function (err) {
      t.ok(err, 'stream errored')
      t.match(err.message, /Invalid JSON/i, 'explains there\'s a syntax error')
      t.deepEquals(results, [], 'no results')
      cleanup()
      t.done()
    })
  })
})