Skip to content

Commit

Permalink
fix(config/include): handle more errors in include
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 29, 2019
1 parent 013b1d7 commit 4a05fcd
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/config/type/Include.ts
Expand Up @@ -13,10 +13,15 @@ export const includeSchema = {
export const includeType = new YamlType('!include', {
kind: 'scalar',
resolve(path: string) {
const canonical = resolvePath(path);
if (existsSync(canonical)) {
return true;
} else {
try {
const canonical = resolvePath(path);
// throws in node 11+
if (existsSync(canonical)) {
return true;
} else {
throw new NotFoundError('included file does not exist');
}
} catch (err) {
throw new NotFoundError('included file does not exist');
}
},
Expand Down
17 changes: 17 additions & 0 deletions test/config/TestConfig.ts
@@ -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);
});
});
22 changes: 22 additions & 0 deletions test/config/type/TestEnv.ts
@@ -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);
});
});
31 changes: 31 additions & 0 deletions test/config/type/TestInclude.ts
@@ -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);
});
});
19 changes: 19 additions & 0 deletions test/config/type/TestRegexp.ts
@@ -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);
});
});
1 change: 1 addition & 0 deletions test/config/type/include.yml
@@ -0,0 +1 @@
test

0 comments on commit 4a05fcd

Please sign in to comment.