-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Code Coverage #562
Changes from all commits
3f39030
86a480c
e26df6a
fbe1f57
a41f438
a09d779
b118ed3
86ac3ea
da5aa0e
d3be577
b5acc31
16d796d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
}); | ||
}); |
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 () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Exposed by tests, fixed. |
||
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); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests for #514. |
||
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 | ||
} | ||
] | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code was previously built to handle unknown errors
if (!desc) ...
, but an exception on the.replace
call would prevent this code path from ever executing.Exposed by tests, fixed.