This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashDependencies.js
85 lines (71 loc) · 2.52 KB
/
hashDependencies.js
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
import fs from 'fs'
import path from 'path'
import test from 'ava'
import md5Hex from 'md5-hex'
import packageHash from 'package-hash'
import {prepareCache} from '..'
import hashDependencies from '../build/hashDependencies'
import fixture from './helpers/fixture'
test('hashes packages', async t => {
const fromPackage = fixture('compare', 'node_modules', 'plugin')
const filename = path.join(fromPackage, 'index.js')
const hashed = await hashDependencies([
{filename, fromPackage}
])
t.deepEqual(hashed, [await packageHash(path.join(fromPackage, 'package.json'))])
})
test('hashes files', async t => {
const filename = fixture('compare', 'node_modules', 'plugin', 'index.js')
const hashed = await hashDependencies([
{filename, fromPackage: null}
])
t.deepEqual(hashed, [md5Hex(fs.readFileSync(filename))])
})
test('fails when dependency package does not exist', async t => {
const fromPackage = fixture('node_modules', 'non-existent')
const filename = path.join(fromPackage, 'index.js')
const err = await t.throws(hashDependencies([
{filename, fromPackage}
]))
t.is(err.name, 'BadDependencyError')
t.is(err.source, filename)
t.is(err.parent.code, 'ENOENT')
})
test('fails when dependency file does not exist', async t => {
const filename = path.join('non-existent', 'index.js')
const err = await t.throws(hashDependencies([
{filename, fromPackage: null}
]))
t.is(err.name, 'BadDependencyError')
t.is(err.source, filename)
t.true(err.parent === null)
})
test('can use a cache of computed hashes', async t => {
const filename = fixture('precomputed.js')
const cache = prepareCache()
cache.dependencyHashes.set(filename, Promise.resolve('hash'))
const hashed = await hashDependencies([
{filename, fromPackage: null}
], cache)
t.deepEqual(hashed, ['hash'])
})
test('caches new hashes', async t => {
const fromPackage = fixture('compare', 'node_modules', 'plugin')
const filename = path.join(fromPackage, 'index.js')
const cache = prepareCache()
const hashed = await hashDependencies([
{filename, fromPackage}
], cache)
const fromCache = await cache.dependencyHashes.get(filename)
t.true(hashed[0] === fromCache)
})
test('can use a cache for file access', async t => {
const filename = fixture('cached-access')
const contents = Buffer.from('cached')
const cache = prepareCache()
cache.files.set(filename, Promise.resolve(contents))
const hashed = await hashDependencies([
{filename, fromPackage: null}
], cache)
t.deepEqual(hashed, [md5Hex(contents)])
})