refactor(aio): add `assertNotMissingOrEmpty()` helper

This commit is contained in:
Georgios Kalpakas 2017-02-27 21:04:43 +02:00 committed by Chuck Jazdzewski
parent c8d87a936b
commit c5644e5a0d
8 changed files with 39 additions and 20 deletions

View File

@ -3,16 +3,14 @@ import * as fs from 'fs';
import * as path from 'path';
import * as shell from 'shelljs';
import {GithubPullRequests} from '../common/github-pull-requests';
import {assertNotMissingOrEmpty} from '../common/utils';
// Classes
export class BuildCleaner {
// Constructor
constructor(protected buildsDir: string, protected repoSlug: string, protected githubToken?: string) {
if (!buildsDir) {
throw new Error('Missing required parameter \'buildsDir\'!');
} else if (!repoSlug) {
throw new Error('Missing required parameter \'repoSlug\'!');
}
assertNotMissingOrEmpty('buildsDir', buildsDir);
assertNotMissingOrEmpty('repoSlug', repoSlug);
}
// Methods - Public

View File

@ -1,6 +1,7 @@
// Imports
import {IncomingMessage} from 'http';
import * as https from 'https';
import {assertNotMissingOrEmpty} from './utils';
// Constants
const GITHUB_HOSTNAME = 'api.github.com';
@ -18,9 +19,7 @@ export class GithubApi {
// Constructor
constructor(protected repoSlug: string, githubToken?: string) {
if (!repoSlug) {
throw new Error('Missing required parameter \'repoSlug\'!');
}
assertNotMissingOrEmpty('repoSlug', repoSlug);
if (!githubToken) {
console.warn('No GitHub access-token specified. Requests will be unauthenticated.');
}

View File

@ -1,4 +1,10 @@
// Functions
export const assertNotMissingOrEmpty = (name: string, value: string | null | undefined) => {
if (!value) {
throw new Error(`Missing or empty required parameter '${name}'!`);
}
};
export const getEnvVar = (name: string, isOptional = false): string => {
const value = process.env[name];

View File

@ -4,6 +4,7 @@ import {EventEmitter} from 'events';
import * as fs from 'fs';
import * as path from 'path';
import * as shell from 'shelljs';
import {assertNotMissingOrEmpty} from '../common/utils';
import {CreatedBuildEvent} from './build-events';
import {UploadError} from './upload-error';
@ -12,10 +13,7 @@ export class BuildCreator extends EventEmitter {
// Constructor
constructor(protected buildsDir: string) {
super();
if (!buildsDir) {
throw new Error('Missing or empty required parameter \'buildsDir\'!');
}
assertNotMissingOrEmpty('buildsDir', buildsDir);
}
// Methods - Public

View File

@ -1,6 +1,7 @@
// Imports
import * as express from 'express';
import * as http from 'http';
import {assertNotMissingOrEmpty} from '../common/utils';
import {BuildCreator} from './build-creator';
import {CreatedBuildEvent} from './build-events';
import {UploadError} from './upload-error';
@ -12,9 +13,7 @@ const X_FILE_HEADER = 'X-FILE';
class UploadServerFactory {
// Methods - Public
public create(buildsDir: string): http.Server {
if (!buildsDir) {
throw new Error('Missing or empty required parameter \'buildsDir\'!');
}
assertNotMissingOrEmpty('buildsDir', buildsDir);
const buildCreator = new BuildCreator(buildsDir);
const middleware = this.createMiddleware(buildCreator);

View File

@ -14,12 +14,12 @@ describe('BuildCleaner', () => {
describe('constructor()', () => {
it('should throw if \'buildsDir\' is empty', () => {
expect(() => new BuildCleaner('', '/baz/qux')).toThrowError('Missing required parameter \'buildsDir\'!');
expect(() => new BuildCleaner('', '/baz/qux')).toThrowError('Missing or empty required parameter \'buildsDir\'!');
});
it('should throw if \'repoSlug\' is empty', () => {
expect(() => new BuildCleaner('/foo/bar', '')).toThrowError('Missing required parameter \'repoSlug\'!');
expect(() => new BuildCleaner('/foo/bar', '')).toThrowError('Missing or empty required parameter \'repoSlug\'!');
});
});

View File

@ -13,8 +13,8 @@ describe('GithubApi', () => {
describe('constructor()', () => {
it('should throw if \'repoSlug\' is not defined', () => {
expect(() => new GithubApi('', '12345')).toThrowError('Missing required parameter \'repoSlug\'!');
it('should throw if \'repoSlug\' is missing or empty', () => {
expect(() => new GithubApi('', '12345')).toThrowError('Missing or empty required parameter \'repoSlug\'!');
});

View File

@ -1,9 +1,28 @@
// Imports
import {getEnvVar} from '../../lib/common/utils';
import {assertNotMissingOrEmpty, getEnvVar} from '../../lib/common/utils';
// Tests
describe('utils', () => {
describe('assertNotMissingOrEmpty()', () => {
it('should throw if passed an empty value', () => {
expect(() => assertNotMissingOrEmpty('foo', undefined)).
toThrowError('Missing or empty required parameter \'foo\'!');
expect(() => assertNotMissingOrEmpty('bar', null)).toThrowError('Missing or empty required parameter \'bar\'!');
expect(() => assertNotMissingOrEmpty('baz', '')).toThrowError('Missing or empty required parameter \'baz\'!');
});
it('should not throw if passed a non-empty value', () => {
expect(() => assertNotMissingOrEmpty('foo', ' ')).not.toThrow();
expect(() => assertNotMissingOrEmpty('bar', 'bar')).not.toThrow();
expect(() => assertNotMissingOrEmpty('baz', 'b a z')).not.toThrow();
});
});
describe('getEnvVar()', () => {
const emptyVar = '$$test_utils_getEnvVar_empty$$';
const nonEmptyVar = '$$test_utils_getEnvVar_nonEmpty$$';