Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cache.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c27386ed8fe191c59b1b6519e1e326d590828b0 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const t = require('tap')
const requireInject = require('require-inject')
const path = require('path')

const usageUtil = () => 'usage instructions'

const flatOptions = {
  force: false
}

const npm = {
  flatOptions,
  cache: '/fake/path'
}

let rimrafPath = ''
const rimraf = (path, cb) => {
  rimrafPath = path
  return cb()
}

let logOutput = []
const npmlog = {
  silly: (...args) => {
    logOutput.push(['silly', ...args])
  }
}

let tarballStreamSpec = ''
let tarballStreamOpts = {}
const pacote = {
  tarball: {
    stream: (spec, cb, opts) => {
      tarballStreamSpec = spec
      tarballStreamOpts = opts
      return cb({
        resume: () => {},
        promise: () => Promise.resolve()
      })
    }
  }
}

let outputOutput = []
const output = (msg) => {
  outputOutput.push(msg)
}

let cacacheVerifyPath = ''
const cacacheVerifyStats = {
  keptSize: 100,
  verifiedContent: 1,
  totalEntries: 1,
  runTime: { total: 2000 }
}
const cacache = {
  verify: (path) => {
    cacacheVerifyPath = path
    return cacacheVerifyStats
  }
}

const mocks = {
  cacache,
  npmlog,
  pacote,
  rimraf,
  '../../lib/npm.js': npm,
  '../../lib/utils/output.js': output,
  '../../lib/utils/usage.js': usageUtil
}

const cache = requireInject('../../lib/cache.js', mocks)

t.test('cache no args', t => {
  cache([], err => {
    t.equal(err.message, 'usage instructions', 'should throw usage instructions')
    t.end()
  })
})

t.test('cache clean', t => {
  cache(['clean'], err => {
    t.match(err.message, 'the npm cache self-heals', 'should throw warning')
    t.end()
  })
})

t.test('cache clean (force)', t => {
  flatOptions.force = true
  t.teardown(() => {
    rimrafPath = ''
    flatOptions.force = false
  })

  cache(['clear'], err => {
    t.ifError(err)
    t.equal(rimrafPath, path.join(npm.cache, '_cacache'))
    t.end()
  })
})

t.test('cache clean with arg', t => {
  cache(['rm', 'pkg'], err => {
    t.match(err.message, 'does not accept arguments', 'should throw error')
    t.end()
  })
})

t.test('cache add no arg', t => {
  t.teardown(() => {
    logOutput = []
  })

  cache(['add'], err => {
    t.strictSame(logOutput, [
      ['silly', 'cache add', 'args', []],
    ], 'logs correctly')
    t.equal(err.code, 'EUSAGE', 'throws usage error')
    t.end()
  })
})

t.test('cache add pkg only', t => {
  t.teardown(() => {
    logOutput = []
    tarballStreamSpec = ''
    tarballStreamOpts = {}
  })

  cache(['add', 'mypkg'], err => {
    t.ifError(err)
    t.strictSame(logOutput, [
      ['silly', 'cache add', 'args', ['mypkg']],
      ['silly', 'cache add', 'spec', 'mypkg']
    ], 'logs correctly')
    t.equal(tarballStreamSpec, 'mypkg', 'passes the correct spec to pacote')
    t.same(tarballStreamOpts, flatOptions, 'passes the correct options to pacote')
    t.end()
  })
})

t.test('cache add pkg w/ spec modifier', t => {
  t.teardown(() => {
    logOutput = []
    tarballStreamSpec = ''
    tarballStreamOpts = {}
  })

  cache(['add', 'mypkg', 'latest'], err => {
    t.ifError(err)
    t.strictSame(logOutput, [
      ['silly', 'cache add', 'args', ['mypkg', 'latest']],
      ['silly', 'cache add', 'spec', 'mypkg@latest']
    ], 'logs correctly')
    t.equal(tarballStreamSpec, 'mypkg@latest', 'passes the correct spec to pacote')
    t.same(tarballStreamOpts, flatOptions, 'passes the correct options to pacote')
    t.end()
  })
})

t.test('cache verify', t => {
  t.teardown(() => {
    outputOutput = []
    cacacheVerifyPath = ''
  })

  cache(['verify'], err => {
    t.ifError(err)
    t.match(outputOutput, [
      `Cache verified and compressed (${path.join(npm.cache, '_cacache')})`,
      'Content verified: 1 (100 bytes)',
      'Index entries: 1',
      'Finished in 2s'
    ], 'prints correct output')
    t.end()
  })
})

t.test('cache verify w/ extra output', t => {
  npm.cache = `${process.env.HOME}/fake/path`
  cacacheVerifyStats.badContentCount = 1
  cacacheVerifyStats.reclaimedCount = 2
  cacacheVerifyStats.reclaimedSize = 200
  cacacheVerifyStats.missingContent = 3
  t.teardown(() => {
    npm.cache = '/fake/path'
    outputOutput = []
    cacacheVerifyPath = ''
    delete cacacheVerifyStats.badContentCount
    delete cacacheVerifyStats.reclaimedCount
    delete cacacheVerifyStats.reclaimedSize
    delete cacacheVerifyStats.missingContent
  })

  cache(['check'], err => {
    t.ifError(err)
    t.match(outputOutput, [
      `Cache verified and compressed (~${path.join('/fake/path', '_cacache')})`,
      'Content verified: 1 (100 bytes)',
      'Corrupted content removed: 1',
      'Content garbage-collected: 2 (200 bytes)',
      'Missing content: 3',
      'Index entries: 1',
      'Finished in 2s'
    ], 'prints correct output')
    t.end()
  })
})

t.test('cache completion', t => {
  const { completion } = cache

  const testComp = (argv, expect) => {
    completion({ conf: { argv: { remain: argv } } }, (err, res) => {
      t.ifError(err)
      t.strictSame(res, expect, argv.join(' '))
    })
  }

  testComp(['npm', 'cache'], [
    'add',
    'clean',
    'verify'
  ])

  testComp(['npm', 'cache', 'add'], [])
  testComp(['npm', 'cache', 'clean'], [])
  testComp(['npm', 'cache', 'verify'], [])

  t.end()
})