|
| 1 | +const assert = require('assert'); |
| 2 | +const path = require('path'); |
| 3 | +const rimraf = require('rimraf'); |
| 4 | +const fs = require('../src/utils/fs'); |
| 5 | +const promisify = require('../src/utils/promisify'); |
| 6 | +const {sleep} = require('./utils'); |
| 7 | +const ncp = promisify(require('ncp')); |
| 8 | +const FSCache = require('../src/FSCache'); |
| 9 | + |
| 10 | +const cachePath = path.join(__dirname, '.cache'); |
| 11 | +const inputPath = path.join(__dirname, '/input'); |
| 12 | + |
| 13 | +const getMTime = async file => { |
| 14 | + const stat = await fs.stat(file); |
| 15 | + const mtime = stat.mtime.getTime(); |
| 16 | + return mtime; |
| 17 | +}; |
| 18 | + |
| 19 | +describe('FSCache', () => { |
| 20 | + beforeEach(() => { |
| 21 | + rimraf.sync(cachePath); |
| 22 | + rimraf.sync(inputPath); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should create directory on ensureDirExists', async () => { |
| 26 | + let exists = await fs.exists(cachePath); |
| 27 | + assert(!exists); |
| 28 | + |
| 29 | + const cache = new FSCache({cacheDir: cachePath}); |
| 30 | + await cache.ensureDirExists(); |
| 31 | + |
| 32 | + exists = await fs.exists(cachePath); |
| 33 | + assert(exists); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should cache resources', async () => { |
| 37 | + const cache = new FSCache({cacheDir: cachePath}); |
| 38 | + await cache.write(__filename, {a: 'test', b: 1, dependencies: []}); |
| 39 | + |
| 40 | + let cached = await cache.read(__filename); |
| 41 | + assert.equal(cached.a, 'test'); |
| 42 | + assert.equal(cached.b, 1); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return null for invalidated resources', async () => { |
| 46 | + const cache = new FSCache({cacheDir: cachePath}); |
| 47 | + cache.invalidate(__filename); |
| 48 | + |
| 49 | + let cached = await cache.read(__filename); |
| 50 | + assert.equal(cached, null); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should remove file on delete', async () => { |
| 54 | + let cache = new FSCache({cacheDir: cachePath}); |
| 55 | + await cache.write(__filename, {a: 'test', b: 1, dependencies: []}); |
| 56 | + await cache.delete(__filename); |
| 57 | + |
| 58 | + let cached = await cache.read(__filename); |
| 59 | + assert.equal(cached, null); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should remove from invalidated on write', async () => { |
| 63 | + const cache = new FSCache({cacheDir: cachePath}); |
| 64 | + cache.invalidate(__filename); |
| 65 | + |
| 66 | + assert(cache.invalidated.has(__filename)); |
| 67 | + |
| 68 | + await cache.write(__filename, {a: 'test', b: 1, dependencies: []}); |
| 69 | + |
| 70 | + assert(!cache.invalidated.has(__filename)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should include mtime for dependencies included in parent', async () => { |
| 74 | + const cache = new FSCache({cacheDir: cachePath}); |
| 75 | + const mtime = await getMTime(__filename); |
| 76 | + |
| 77 | + await cache.write(__filename, { |
| 78 | + a: 'test', |
| 79 | + b: 1, |
| 80 | + dependencies: [ |
| 81 | + { |
| 82 | + includedInParent: true, |
| 83 | + name: __filename |
| 84 | + }, |
| 85 | + { |
| 86 | + name: __filename |
| 87 | + } |
| 88 | + ] |
| 89 | + }); |
| 90 | + |
| 91 | + const cached = await cache.read(__filename); |
| 92 | + assert.equal(cached.dependencies[0].mtime, mtime); |
| 93 | + assert.equal(cached.dependencies[1].mtime, undefined); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should invalidate when dependency included in parent changes', async () => { |
| 97 | + const cache = new FSCache({cacheDir: cachePath}); |
| 98 | + await ncp(__dirname + '/integration/fs', inputPath); |
| 99 | + const filePath = path.join(inputPath, 'test.txt'); |
| 100 | + |
| 101 | + await cache.write(__filename, { |
| 102 | + dependencies: [ |
| 103 | + { |
| 104 | + includedInParent: true, |
| 105 | + name: filePath |
| 106 | + } |
| 107 | + ] |
| 108 | + }); |
| 109 | + |
| 110 | + // delay and update dependency |
| 111 | + await sleep(50); |
| 112 | + await fs.writeFile(filePath, 'world'); |
| 113 | + |
| 114 | + const cached = await cache.read(__filename); |
| 115 | + assert.equal(cached, null); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should return null on read error', async () => { |
| 119 | + const cache = new FSCache({cacheDir: cachePath}); |
| 120 | + const cached = await cache.read( |
| 121 | + path.join(__dirname, '/does/not/exist.txt') |
| 122 | + ); |
| 123 | + |
| 124 | + assert.equal(cached, null); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should continue without throwing on write error', async () => { |
| 128 | + const cache = new FSCache({cacheDir: cachePath}); |
| 129 | + const filePath = path.join(__dirname, '/does/not/exist.txt'); |
| 130 | + |
| 131 | + assert.doesNotThrow(async () => { |
| 132 | + await cache.write(__filename, { |
| 133 | + dependencies: [ |
| 134 | + { |
| 135 | + includedInParent: true, |
| 136 | + name: filePath |
| 137 | + } |
| 138 | + ] |
| 139 | + }); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments