Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
test(prefetch): Add tests for prefetch (#71)
Browse files Browse the repository at this point in the history
* test(prefetch): add prefetch test

* test(prefetch): update hash algorithm to match
  • Loading branch information
nlkluth authored and zkat committed Mar 24, 2017
1 parent 4ddd2f5 commit 1c5c1d9
Showing 1 changed file with 100 additions and 5 deletions.
105 changes: 100 additions & 5 deletions test/prefetch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,104 @@
'use strict'

const npmlog = require('npmlog')
const tar = require('tar-stream')
const crypto = require('crypto')
const test = require('tap').test
const tnock = require('./util/tnock')
const cache = require('../lib/cache')
const testDir = require('./util/test-dir')
const CACHE = testDir(__filename)
const prefetch = require('../prefetch')
const mockTar = require('./util/mock-tarball')

test('parses string specs into specifiers')
test('accepts realized package specifiers')
test('dispatches a different tarball handler based on spec type')
test('exits early if not given `opts.cache`')
test('exits early if `opts.digest` is already in cache')
npmlog.level = process.env.LOGLEVEL || 'silent'
const BASE = {
name: 'foo',
version: '1.0.0',
_hasShrinkwrap: false,
_resolved: 'https://foo.bar/x.tgz',
dist: {
tarball: 'https://foo.bar/x.tgz'
}
}

const OPTS = {
registry: 'https://mock.reg',
log: npmlog,
cache: CACHE
}

const META = {
name: 'foo',
'dist-tags': { latest: '1.2.3', lts: '1.0.0' },
versions: {
'1.0.0': BASE,
}
}

const PKG = {
'package.json': JSON.stringify({
name: 'foo',
version: '1.2.3'
}),
'index.js': 'console.log("hello world!")'
}

const SHRINKWRAP = {
name: 'foo',
version: '1.0.0'
}

test('prefetch by manifest if no digest', t => {
return mockTar(PKG).then(tarData => {
const srv = tnock(t, OPTS.registry)
srv.get('/foo').reply(200, META)
tnock(t, 'https://foo.bar').get('/x.tgz').reply(200, tarData)

return prefetch('foo@1.0.0', OPTS).then(() => {
t.equal(srv.isDone(), true)
return cache.ls(CACHE)
}).then(result => {
t.equal(Object.keys(result).length, 2)
})
})
})

test('skip if no cache is provided', t => {
return prefetch('foo@1.0.0', {}).then(() => {
return cache.ls(CACHE)
}).then(result => {
t.equal(Object.keys(result).length, 0)
})
})

test('use cache content if found', t => {
const key = cache.key('registry-request', OPTS.registry + '/foo')
return cache.put(CACHE, key, JSON.stringify(META), OPTS).then(digest => {
OPTS.digest = digest
OPTS.hashAlgorithm = 'sha512'
return prefetch('foo@1.0.0', OPTS).then(() => {
return cache.ls(CACHE)
}).then(result => {
t.equal(Object.keys(result).length, 1)
})
})
})

test('prefetch by manifest if digest provided but no cache content found', t => {
return mockTar(PKG).then(tarData => {
const srv = tnock(t, OPTS.registry)
srv.get('/foo').reply(200, META)

tnock(t, 'https://foo.bar').get('/x.tgz').reply(200, tarData)
const sha = crypto.createHash('sha1').update(tarData).digest('hex')
OPTS.digest = sha

return prefetch('foo@1.0.0', OPTS).then(() => {
t.equal(srv.isDone(), true)
return cache.ls(CACHE)
}).then(result => {
t.equal(Object.keys(result).length, 2)
})
})
})

0 comments on commit 1c5c1d9

Please sign in to comment.