Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const env = {}
const main = requireWithCurrentEnv(env)
const cache = main.prepareCache()
const result = await main.fromConfig(main.createConfig({
options: require(source), // eslint-disable-line import/no-dynamic-require
source
}), {cache})
const verifier = await result.createVerifier()
t.deepEqual(verifier.cacheKeysForEnv(), {
dependencies: md5Hex([
hashes[fixture('compare', 'node_modules', 'plugin-copy', 'package.json')],
hashes[fixture('compare', 'node_modules', 'plugin', 'package.json')],
hashes[fixture('compare', 'node_modules', 'preset', 'package.json')]
]),
sources: md5Hex([
hashes[fixture('compare', '.babelrc')],
hashes[fixture('compare', 'dir', 'subdir', 'extended-by-babelrc.js')],
hashes[fixture('compare', 'extended-by-virtual.json5')],
hashes[fixture('compare', 'virtual.json')]
])
})
t.deepEqual(verifier.cacheKeysForEnv('foo'), {
dependencies: md5Hex([
hashes[fixture('compare', 'node_modules', 'env-plugin', 'package.json')],
hashes[fixture('compare', 'node_modules', 'plugin-copy', 'package.json')],
hashes[fixture('compare', 'node_modules', 'plugin-default-opts', 'package.json')],
hashes[fixture('compare', 'node_modules', 'plugin', 'package.json')],
hashes[fixture('compare', 'node_modules', 'preset', 'package.json')]
]),
sources: md5Hex([
test('hashes file contents', async t => {
const source = fixture('babelrc', '.babelrc')
const hashed = await hashSources([{source, runtimeHash: null}])
t.deepEqual(hashed, [md5Hex(fs.readFileSync(source))])
})
test('can use a cache for file access', async t => {
const source = fixture('cached-access')
const contents = Buffer.from('cached')
const cache = prepareCache()
cache.files.set(source, Promise.resolve(contents))
const hashed = await hashSources([{source, runtimeHash: null}], null, cache)
t.deepEqual(hashed, [md5Hex(contents)])
})
test('createConfig() can take a fixed hash for the options', async t => {
const result = await fromConfig(createConfig({
options: {
babelrc: false
},
source: 'foo',
hash: 'hash of foo'
}))
const verifier = await result.createVerifier()
t.deepEqual(verifier.cacheKeysForEnv(), {
dependencies: md5Hex([]),
sources: md5Hex(['hash of foo'])
})
})
test('hashes the "babel" value in package.json sources', async t => {
const sources = [
fixture('pkg', 'package.json'),
fixture('bad-pkg', 'array-babel', 'package.json'),
fixture('bad-pkg', 'bool-babel', 'package.json'),
fixture('bad-pkg', 'falsy', 'package.json'),
fixture('bad-pkg', 'null', 'package.json'),
fixture('bad-pkg', 'null-babel', 'package.json'),
fixture('bad-pkg', 'without-babel', 'package.json')
].map(source => ({source, runtimeHash: null}))
const hashed = await hashSources(sources)
t.deepEqual(hashed, [
md5Hex('{"plugins":["pkg"]}'),
md5Hex('[]'),
md5Hex('true'),
md5Hex('{}'),
md5Hex('{}'),
md5Hex('{}'),
md5Hex('{}')
])
})
test('hashes the "babel" value in package.json sources', async t => {
const sources = [
fixture('pkg', 'package.json'),
fixture('bad-pkg', 'array-babel', 'package.json'),
fixture('bad-pkg', 'bool-babel', 'package.json'),
fixture('bad-pkg', 'falsy', 'package.json'),
fixture('bad-pkg', 'null', 'package.json'),
fixture('bad-pkg', 'null-babel', 'package.json'),
fixture('bad-pkg', 'without-babel', 'package.json')
].map(source => ({source, runtimeHash: null}))
const hashed = await hashSources(sources)
t.deepEqual(hashed, [
md5Hex('{"plugins":["pkg"]}'),
md5Hex('[]'),
md5Hex('true'),
md5Hex('{}'),
md5Hex('{}'),
md5Hex('{}'),
md5Hex('{}')
])
})
test('source may contain a dot-prop path', async t => {
const source = fixture('virtual', 'package.json#deeply.nested')
const hashed = await hashSources([{source, runtimeHash: null}])
t.deepEqual(hashed, [md5Hex('{"virtual":true}')])
})
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))])
})
fse.copySync(fixture('verifier', 'babelrc'), dir)
const verifier = await (await fromDirectory(dir)).createVerifier()
const cacheKeys = verifier.cacheKeysForEnv()
t.deepEqual(await verifier.verifyEnv(), {
sourcesChanged: false,
dependenciesChanged: false,
cacheKeys,
verifier
})
{
fs.writeFileSync(path.join(dir, 'plugin.js'), 'foo')
const expectedCacheKeys = {
dependencies: md5Hex([md5Hex('foo')]),
sources: cacheKeys.sources
}
const result = await verifier.verifyEnv()
const {verifier: newVerifier} = result
delete result.verifier
t.deepEqual(result, {
sourcesChanged: false,
dependenciesChanged: true,
cacheKeys: expectedCacheKeys
})
t.true(newVerifier !== verifier)
t.deepEqual(newVerifier.cacheKeysForEnv(), expectedCacheKeys)
}
fse.copySync(fixture('verifier', 'babelrc'), dir)
const verifier = await (await fromDirectory(dir)).createVerifier()
const cacheKeys = verifier.cacheKeysForEnv('foo')
t.deepEqual(await verifier.verifyEnv('foo'), {
sourcesChanged: false,
dependenciesChanged: false,
cacheKeys,
verifier
})
{
fs.writeFileSync(path.join(dir, 'foo.js'), 'foo')
const expectedCacheKeys = {
dependencies: md5Hex([md5Hex('foo'), md5Hex('module.exports = () => {}\n')]),
sources: cacheKeys.sources
}
const result = await verifier.verifyEnv('foo')
const {verifier: newVerifier} = result
delete result.verifier
t.deepEqual(result, {
sourcesChanged: false,
dependenciesChanged: true,
cacheKeys: expectedCacheKeys
})
t.true(newVerifier !== verifier)
t.deepEqual(newVerifier.cacheKeysForEnv('foo'), expectedCacheKeys)
}