提交 e48dd61e 编写于 作者: B Benjamin Pasero

more config tests

上级 35f1a8e7
......@@ -27,6 +27,11 @@ export interface IConfigOptions<T> {
/**
* A simple helper to watch a configured file for changes and process its contents as JSON object.
* Supports:
* - comments in JSON files and errors
* - symlinks for the config file itself
* - delayed processing of changes to accomodate for lots of changes
* - configurable defaults
*/
export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
private cache: T;
......@@ -84,13 +89,14 @@ export class ConfigWatcher<T> implements IConfigWatcher<T>, IDisposable {
}
private parse(raw: string): T {
let res: T;
try {
return json.parse(raw) || this.options.defaultConfig;
res = json.parse(raw);
} catch (error) {
// Ignore loading and parsing errors
}
return this.options.defaultConfig;
return res || this.options.defaultConfig;
}
private registerWatcher(): void {
......
......@@ -63,6 +63,27 @@ suite('Config', () => {
});
});
test('getConfig / getValue - broken JSON', function (done: () => void) {
const id = uuid.generateUuid();
const parentDir = path.join(os.tmpdir(), 'vsctests', id);
const newDir = path.join(parentDir, 'config', id);
const testFile = path.join(newDir, 'config.json');
extfs.mkdirp(newDir, 493, (error) => {
fs.writeFileSync(testFile, '// my comment\n "foo": "bar ... ');
let watcher = new ConfigWatcher<{ foo: string; }>(testFile);
let config = watcher.getConfig();
assert.ok(config);
assert.ok(!config.foo);
watcher.dispose();
extfs.del(parentDir, os.tmpdir(), () => { }, done);
});
});
test('watching', function (done: () => void) {
const id = uuid.generateUuid();
const parentDir = path.join(os.tmpdir(), 'vsctests', id);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册