Skip to content

Commit

Permalink
Merge pull request #3 from wtgtybhertgeghgtwtg/remove-default
Browse files Browse the repository at this point in the history
Remove `defaultValue` options.
  • Loading branch information
wtgtybhertgeghgtwtg authored Feb 27, 2018
2 parents 9a520b9 + 4ef3e4c commit 5732270
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 272 deletions.
260 changes: 4 additions & 256 deletions __tests__/loadConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import loadConfig from '../src/loadConfig';
jest.mock('fs');

describe('loadConfig', () => {
const defaultValue = 'Default property.';

describe('invariants', () => {
it('throws if "configMap" is undefined.', () => {
// $FlowFixMe
Expand Down Expand Up @@ -110,22 +108,14 @@ describe('loadConfig', () => {

expect(config.groupOne.propOne).toEqual(variableValue);
});

it('equals the environmental variable, ignoring the default.', async () => {
const config = await loadConfig({
groupOne: {propOne: {defaultValue, variableName}},
});

expect(config.groupOne.propOne).toEqual(variableValue);
});
});

describe('when the environmental variable is not present', () => {
beforeEach(() => {
delete process.env[variableName];
});

it('equals null if there is no default.', async () => {
it('equals null.', async () => {
const config = await loadConfig({
groupOne: {propOne: {variableName}},
});
Expand All @@ -141,23 +131,7 @@ describe('loadConfig', () => {
expect(config.groupOne.propOne).toBeNull();
});

it('equals the default, if defined.', async () => {
const config = await loadConfig({
groupOne: {propOne: {defaultValue, variableName}},
});

expect(config.groupOne.propOne).toEqual(defaultValue);
});

it('equals the default, if defined, even if it is required.', async () => {
const config = await loadConfig({
groupOne: {propOne: {defaultValue, required: true, variableName}},
});

expect(config.groupOne.propOne).toEqual(defaultValue);
});

it('rejects if required and there is no default.', async () => {
it('rejects if required.', async () => {
const configPromise = loadConfig({
groupOne: {propOne: {required: true, variableName}},
});
Expand Down Expand Up @@ -217,17 +191,6 @@ describe('loadConfig', () => {
expect.anything(),
);
});

it('equals the file content, ignoring the default.', async () => {
// $FlowFixMe
fs.__setFiles({[filePath]: Buffer.from(fileContent, 'utf8')});

const config = await loadConfig({
groupOne: {propOne: {defaultValue, filePath}},
});

expect(config.groupOne.propOne).toEqual(fileContent);
});
});

describe('when the file is not present', () => {
Expand All @@ -236,31 +199,15 @@ describe('loadConfig', () => {
fs.__setFiles({});
});

it('equals null if there is no default.', async () => {
it('equals null.', async () => {
const config = await loadConfig({
groupOne: {propOne: {filePath}},
});

expect(config.groupOne.propOne).toBeNull();
});

it('equals the default, if defined.', async () => {
const config = await loadConfig({
groupOne: {propOne: {defaultValue, filePath}},
});

expect(config.groupOne.propOne).toEqual(defaultValue);
});

it('equals the default, if defined, even if it is required.', async () => {
const config = await loadConfig({
groupOne: {propOne: {defaultValue, filePath, required: true}},
});

expect(config.groupOne.propOne).toEqual(defaultValue);
});

it('rejects if required and there is no default.', async () => {
it('rejects if required.', async () => {
const configPromise = loadConfig({
groupOne: {propOne: {filePath, required: true}},
});
Expand All @@ -280,203 +227,4 @@ describe('loadConfig', () => {
});
});
});

// it('can load empty groups.', async () => {
// const {config, errors} = await loadConfig({groupOne: {}});
//
// expect(config.groupOne).toEqual({});
// expect(errors.length).toBe(0);
// });
//
// describe('loading environmental variables', () => {
// const variableName = 'PROP_ONE';
//
// describe('when the environmental variable is present', () => {
// const variableValue = 'The first property.';
//
// beforeEach(() => {
// process.env[variableName] = variableValue;
// });
//
// it('equals the environmental variable.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {variableName}},
// });
//
// expect(config.groupOne.propOne).toEqual(variableValue);
// expect(errors.length).toBe(0);
// });
//
// it('equals the environmental variable (using shorthand syntax).', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: variableName},
// });
//
// expect(config.groupOne.propOne).toEqual(variableValue);
// expect(errors.length).toBe(0);
// });
//
// it('equals the environmental variable, ignoring the default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {defaultValue, variableName}},
// });
//
// expect(config.groupOne.propOne).toEqual(variableValue);
// expect(errors.length).toBe(0);
// });
// });
//
// describe('when the environmental variable is not present', () => {
// beforeEach(() => {
// delete process.env[variableName];
// });
//
// it('equals null if there is no default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {variableName}},
// });
//
// expect(config.groupOne.propOne).toBeNull();
// expect(errors.length).toBe(0);
// });
//
// it('equals null if using shorthand syntax.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: variableName},
// });
//
// expect(config.groupOne.propOne).toBeNull();
// expect(errors.length).toBe(0);
// });
//
// it('equals the default, if defined.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {defaultValue, variableName}},
// });
//
// expect(config.groupOne.propOne).toEqual(defaultValue);
// expect(errors.length).toBe(0);
// });
//
// it('gives an error if required and there is no default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {required: true, variableName}},
// });
//
// expect(config.groupOne.propOne).toBeNull();
// expect(errors.length).toBe(1);
//
// const [error] = errors;
// expect(error).toBeInstanceOf(Error);
// expect(error.message).toEqual('PROP_ONE is not defined.');
// });
//
// it('does not give an error if required and there is a default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {defaultValue, required: true, variableName}},
// });
//
// expect(config.groupOne.propOne).toEqual(defaultValue);
// expect(errors.length).toBe(0);
// });
// });
// });
//
// describe('loading files', () => {
// const filePath = '/prop.one';
//
// describe('when the file is present', () => {
// const fileContent = 'The first property.';
//
// it('equals the file content decoded as UTF-8 if no encoding is defined.', async () => {
// // $FlowFixMe
// fs.__setFiles({[filePath]: Buffer.from(fileContent, 'utf8')});
//
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {filePath}},
// });
//
// expect(config.groupOne.propOne).toEqual(fileContent);
// expect(fs.readFile).toHaveBeenCalledWith(
// filePath,
// 'utf8',
// expect.anything(),
// );
// expect(errors.length).toBe(0);
// });
//
// it('equals the file content decoded using the given encoding, if defined.', async () => {
// // $FlowFixMe
// fs.__setFiles({[filePath]: Buffer.from(fileContent, 'ascii')});
//
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {encoding: 'ascii', filePath}},
// });
//
// expect(config.groupOne.propOne).toEqual(fileContent);
// expect(fs.readFile).toHaveBeenCalledWith(
// filePath,
// 'ascii',
// expect.anything(),
// );
// expect(errors.length).toBe(0);
// });
//
// it('equals the file content, ignoring the default.', async () => {
// // $FlowFixMe
// fs.__setFiles({[filePath]: Buffer.from(fileContent, 'utf8')});
//
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {defaultValue, filePath}},
// });
//
// expect(config.groupOne.propOne).toEqual(fileContent);
// expect(errors.length).toBe(0);
// });
// });
//
// describe('when the file is not present', () => {
// beforeEach(() => {
// // $FlowFixMe
// fs.__setFiles({});
// });
//
// it('equals null if there is no default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {filePath}},
// });
//
// expect(config.groupOne.propOne).toBeNull();
// expect(errors.length).toBe(0);
// });
//
// it('equals the default, if defined.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {defaultValue, filePath}},
// });
//
// expect(config.groupOne.propOne).toEqual(defaultValue);
// expect(errors.length).toBe(0);
// });
//
// it('gives an error if required and there is no default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {filePath, required: true}},
// });
//
// expect(config.groupOne.propOne).toBeNull();
// expect(errors.length).toBe(1);
// expect(errors[0]).toBeInstanceOf(Error);
// });
//
// it('does not give an error if required and there is a default.', async () => {
// const {config, errors} = await loadConfig({
// groupOne: {propOne: {defaultValue, filePath, required: true}},
// });
//
// expect(config.groupOne.propOne).toEqual(defaultValue);
// expect(errors.length).toBe(0);
// });
// });
// });
});
8 changes: 4 additions & 4 deletions src/loadEnvironmentConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import type {EnvironmentConfig} from './types';

export default function loadEnvironmentConfig(property: EnvironmentConfig) {
const {defaultValue = null, required = false, variableName} = property;
const {required = false, variableName} = property;

// '' is falsey, so `process.env[variableName] || defaultValue` would not behave right when the variable is set to ''.
// '' is falsey, so `process.env[variableName] || null` would not behave right when the variable is set to ''.
let config = process.env[variableName];

// `process.env[variableName] ?? defaultValue`, on the other hand.
// `process.env[variableName] ?? null`, on the other hand.
if (typeof config !== 'string') {
config = defaultValue;
config = null;
}

const error =
Expand Down
12 changes: 2 additions & 10 deletions src/loadFileConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ const readFileAsync: (
) => Promise<?string> = pify(readFile);

export default function loadFileConfig(property: FileConfig) {
const {
defaultValue = null,
encoding = 'utf8',
filePath,
required = false,
} = property;
const {encoding = 'utf8', filePath, required = false} = property;

// $FlowFixMe
return readFileAsync(filePath, encoding).then(
config => ({config, error: null}),
(error: Error) =>
required && defaultValue === null
? {config: null, error}
: {config: defaultValue, error: null},
(error: Error) => ({config: null, error: required ? error : null}),
);
}
2 changes: 0 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ export type ConfigResult<CMap: ConfigMap> = {
};

export type EnvironmentConfig = {
defaultValue?: string,
required?: boolean,
variableName: string,
};

export type FileConfig = {
defaultValue?: string,
encoding?: buffer$Encoding,
filePath: string,
required?: boolean,
Expand Down

0 comments on commit 5732270

Please sign in to comment.