This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(prefetch): Add tests for prefetch (#71)
* test(prefetch): add prefetch test * test(prefetch): update hash algorithm to match
- Loading branch information
Showing
1 changed file
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |