1
0
Fork 0

fix(config/include): handle more errors in include

This commit is contained in:
ssube 2019-10-28 22:41:52 -05:00
parent 013b1d7dfb
commit 4a05fcdba2
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
6 changed files with 99 additions and 4 deletions

View File

@ -13,12 +13,17 @@ export const includeSchema = {
export const includeType = new YamlType('!include', { export const includeType = new YamlType('!include', {
kind: 'scalar', kind: 'scalar',
resolve(path: string) { resolve(path: string) {
try {
const canonical = resolvePath(path); const canonical = resolvePath(path);
// throws in node 11+
if (existsSync(canonical)) { if (existsSync(canonical)) {
return true; return true;
} else { } else {
throw new NotFoundError('included file does not exist'); throw new NotFoundError('included file does not exist');
} }
} catch (err) {
throw new NotFoundError('included file does not exist');
}
}, },
construct(path: string): unknown { construct(path: string): unknown {
try { try {

17
test/config/TestConfig.ts Normal file
View File

@ -0,0 +1,17 @@
import { expect } from 'chai';
import { join } from 'path';
import { loadConfig } from '../../src/config';
import { NotFoundError } from '../../src/error/NotFoundError';
import { describeLeaks, itLeaks } from '../helpers/async';
describeLeaks('load config helper', async () => {
itLeaks('should load an existing config', async () => {
const config = await loadConfig('config-stderr.yml', join(__dirname, '..', 'docs'));
expect(config.data.logger.name).to.equal('salty-dog');
});
itLeaks('should throw when config is missing', async () => {
return expect(loadConfig('missing.yml', join(__dirname, '..', 'docs'))).to.eventually.be.rejectedWith(NotFoundError);
});
});

View File

@ -0,0 +1,22 @@
import { expect } from 'chai';
import { envType } from '../../../src/config/type/Env';
import { NotFoundError } from '../../../src/error/NotFoundError';
import { VERSION_INFO } from '../../../src/version';
import { describeLeaks, itLeaks } from '../../helpers/async';
describeLeaks('env config type', async () => {
itLeaks('should throw on missing variables', async () => {
expect(() => {
envType.resolve('DOES_NOT_EXIST_');
}).to.throw(NotFoundError);
});
itLeaks('should resolve existing variables', async () => {
expect(envType.resolve('CI_COMMIT_SHA')).to.equal(true);
});
itLeaks('should construct a value from variables', async () => {
expect(envType.construct('CI_COMMIT_SHA')).to.equal(VERSION_INFO.git.commit);
});
});

View File

@ -0,0 +1,31 @@
import { expect } from 'chai';
import { BaseError } from 'noicejs';
import { join } from 'path';
import { includeType } from '../../../src/config/type/Include';
import { NotFoundError } from '../../../src/error/NotFoundError';
import { describeLeaks, itLeaks } from '../../helpers/async';
const TEST_ROOT = '../test/config/type';
describeLeaks('include config type', async () => {
itLeaks('should resolve existing files', async () => {
expect(includeType.resolve(join(TEST_ROOT, 'include.yml'))).to.equal(true);
});
itLeaks('should throw when resolving missing files', async () => {
expect(() => {
includeType.resolve(join(TEST_ROOT, 'missing.yml'));
}).to.throw(NotFoundError);
});
itLeaks('should construct data from file', async () => {
expect(includeType.construct(join(TEST_ROOT, 'include.yml'))).to.equal('test');
});
itLeaks('should throw when constructing missing files', async () => {
expect(() => {
includeType.construct(join(TEST_ROOT, 'missing.yml'));
}).to.throw(BaseError);
});
});

View File

@ -0,0 +1,19 @@
import { expect } from 'chai';
import { regexpType } from '../../../src/config/type/Regexp';
import { describeLeaks, itLeaks } from '../../helpers/async';
describeLeaks('regexp config type', async () => {
itLeaks('match slashed strings', async () => {
expect(regexpType.resolve('/foo/')).to.equal(true);
});
itLeaks('should match flags', async () => {
const regexp: RegExp = regexpType.construct('/foo/g');
expect(regexp.flags).to.equal('g');
});
itLeaks('should not match bare strings', async () => {
expect(regexpType.resolve('foo')).to.equal(false);
});
});

View File

@ -0,0 +1 @@
test