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 path from 'path';
import * as shell from 'shelljs'; import * as shell from 'shelljs';
import {GithubPullRequests} from '../common/github-pull-requests'; import {GithubPullRequests} from '../common/github-pull-requests';
import {assertNotMissingOrEmpty} from '../common/utils';
// Classes // Classes
export class BuildCleaner { export class BuildCleaner {
// Constructor // Constructor
constructor(protected buildsDir: string, protected repoSlug: string, protected githubToken?: string) { constructor(protected buildsDir: string, protected repoSlug: string, protected githubToken?: string) {
if (!buildsDir) { assertNotMissingOrEmpty('buildsDir', buildsDir);
throw new Error('Missing required parameter \'buildsDir\'!'); assertNotMissingOrEmpty('repoSlug', repoSlug);
} else if (!repoSlug) {
throw new Error('Missing required parameter \'repoSlug\'!');
}
} }
// Methods - Public // Methods - Public

View File

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

View File

@ -1,4 +1,10 @@
// Functions // 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 => { export const getEnvVar = (name: string, isOptional = false): string => {
const value = process.env[name]; const value = process.env[name];

View File

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

View File

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

View File

@ -14,12 +14,12 @@ describe('BuildCleaner', () => {
describe('constructor()', () => { describe('constructor()', () => {
it('should throw if \'buildsDir\' is empty', () => { 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', () => { 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()', () => { describe('constructor()', () => {
it('should throw if \'repoSlug\' is not defined', () => { it('should throw if \'repoSlug\' is missing or empty', () => {
expect(() => new GithubApi('', '12345')).toThrowError('Missing required parameter \'repoSlug\'!'); expect(() => new GithubApi('', '12345')).toThrowError('Missing or empty required parameter \'repoSlug\'!');
}); });

View File

@ -1,9 +1,28 @@
// Imports // Imports
import {getEnvVar} from '../../lib/common/utils'; import {assertNotMissingOrEmpty, getEnvVar} from '../../lib/common/utils';
// Tests // Tests
describe('utils', () => { 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()', () => { describe('getEnvVar()', () => {
const emptyVar = '$$test_utils_getEnvVar_empty$$'; const emptyVar = '$$test_utils_getEnvVar_empty$$';
const nonEmptyVar = '$$test_utils_getEnvVar_nonEmpty$$'; const nonEmptyVar = '$$test_utils_getEnvVar_nonEmpty$$';