-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74d70f8
commit fc641fa
Showing
17 changed files
with
625 additions
and
21 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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const assert = require('assert'); | ||
const customErrors = require('../src/utils/customErrors'); | ||
|
||
const port = 1234; | ||
|
||
const EACCES = new Error(); | ||
EACCES.code = 'EACCES'; | ||
const EADDRINUSE = new Error(); | ||
EADDRINUSE.code = 'EADDRINUSE'; | ||
|
||
describe('customErrors', () => { | ||
it('should include port in server errors', () => { | ||
const msg = customErrors.serverErrors(EACCES, port); | ||
assert(msg.includes(port)); | ||
}); | ||
|
||
it('should handle known server errors', () => { | ||
let msg = customErrors.serverErrors(EACCES, port); | ||
assert(msg.includes(`don't have access`)); | ||
|
||
msg = customErrors.serverErrors(EADDRINUSE, port); | ||
assert(msg.includes('already')); | ||
}); | ||
|
||
it('should handled unknown server errors', () => { | ||
let msg = customErrors.serverErrors(new Error(), port); | ||
assert(msg.includes(port)); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
const fs = require('../src/utils/fs'); | ||
const promisify = require('../src/utils/promisify'); | ||
const {sleep} = require('./utils'); | ||
const ncp = promisify(require('ncp')); | ||
const FSCache = require('../src/FSCache'); | ||
|
||
const cachePath = path.join(__dirname, '.cache'); | ||
const inputPath = path.join(__dirname, '/input'); | ||
|
||
const getMTime = async file => { | ||
const stat = await fs.stat(file); | ||
const mtime = stat.mtime.getTime(); | ||
return mtime; | ||
}; | ||
|
||
describe('FSCache', () => { | ||
beforeEach(() => { | ||
rimraf.sync(cachePath); | ||
rimraf.sync(inputPath); | ||
}); | ||
|
||
it('should create directory on ensureDirExists', async () => { | ||
let exists = await fs.exists(cachePath); | ||
assert(!exists); | ||
|
||
const cache = new FSCache({cacheDir: cachePath}); | ||
await cache.ensureDirExists(); | ||
|
||
exists = await fs.exists(cachePath); | ||
assert(exists); | ||
}); | ||
|
||
it('should cache resources', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
await cache.write(__filename, {a: 'test', b: 1, dependencies: []}); | ||
|
||
let cached = await cache.read(__filename); | ||
assert.equal(cached.a, 'test'); | ||
assert.equal(cached.b, 1); | ||
}); | ||
|
||
it('should return null for invalidated resources', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
cache.invalidate(__filename); | ||
|
||
let cached = await cache.read(__filename); | ||
assert.equal(cached, null); | ||
}); | ||
|
||
it('should remove file on delete', async () => { | ||
let cache = new FSCache({cacheDir: cachePath}); | ||
await cache.write(__filename, {a: 'test', b: 1, dependencies: []}); | ||
await cache.delete(__filename); | ||
|
||
let cached = await cache.read(__filename); | ||
assert.equal(cached, null); | ||
}); | ||
|
||
it('should remove from invalidated on write', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
cache.invalidate(__filename); | ||
|
||
assert(cache.invalidated.has(__filename)); | ||
|
||
await cache.write(__filename, {a: 'test', b: 1, dependencies: []}); | ||
|
||
assert(!cache.invalidated.has(__filename)); | ||
}); | ||
|
||
it('should include mtime for dependencies included in parent', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
const mtime = await getMTime(__filename); | ||
|
||
await cache.write(__filename, { | ||
a: 'test', | ||
b: 1, | ||
dependencies: [ | ||
{ | ||
includedInParent: true, | ||
name: __filename | ||
}, | ||
{ | ||
name: __filename | ||
} | ||
] | ||
}); | ||
|
||
const cached = await cache.read(__filename); | ||
assert.equal(cached.dependencies[0].mtime, mtime); | ||
assert.equal(cached.dependencies[1].mtime, undefined); | ||
}); | ||
|
||
it('should invalidate when dependency included in parent changes', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
await ncp(__dirname + '/integration/fs', inputPath); | ||
const filePath = path.join(inputPath, 'test.txt'); | ||
|
||
await cache.write(__filename, { | ||
dependencies: [ | ||
{ | ||
includedInParent: true, | ||
name: filePath | ||
} | ||
] | ||
}); | ||
|
||
// delay and update dependency | ||
await sleep(50); | ||
await fs.writeFile(filePath, 'world'); | ||
|
||
const cached = await cache.read(__filename); | ||
assert.equal(cached, null); | ||
}); | ||
|
||
it('should return null on read error', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
const cached = await cache.read( | ||
path.join(__dirname, '/does/not/exist.txt') | ||
); | ||
|
||
assert.equal(cached, null); | ||
}); | ||
|
||
it('should continue without throwing on write error', async () => { | ||
const cache = new FSCache({cacheDir: cachePath}); | ||
const filePath = path.join(__dirname, '/does/not/exist.txt'); | ||
|
||
assert.doesNotThrow(async () => { | ||
await cache.write(__filename, { | ||
dependencies: [ | ||
{ | ||
includedInParent: true, | ||
name: filePath | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.