Skip to content

Commit

Permalink
fix: use bath path when provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jbw committed Oct 6, 2022
1 parent cea80ec commit e9ccae9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
4 changes: 3 additions & 1 deletion packages/core/configuration-builder.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 8,12 @@ import { IConfigurationSource } from './configuration-source.interface';
export interface IConfigurationBuilder {
readonly sources: IConfigurationSource[];

add(configurationSourcec: IConfigurationSource): IConfigurationBuilder;
add(configurationSource: IConfigurationSource): IConfigurationBuilder;

setBasePath(basePath: string): IConfigurationBuilder;

getBasePath(): string;

addEnvironmentVariables(): IConfigurationBuilder;

build(): IConfigurationRoot;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/configuration-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,10 @@ export class ConfigurationBuilder implements IConfigurationBuilder {
return this;
}

public getBasePath(): string {
return this.basePath;
}

/**
* Extension method to add EnvironmentVariablesConfigurationSource
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/file-configuration-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,7 @@ import { IConfigurationProvider } from './configuration-provider.interface';
import { IConfigurationSource } from './configuration-source.interface';

export abstract class FileConfigurationSource implements IConfigurationSource {
constructor(public path?: string) {}
constructor(public path: string) {}

abstract build(builder: IConfigurationBuilder): IConfigurationProvider;
}
5 changes: 4 additions & 1 deletion packages/json/json-configuration-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 2,10 @@ import { IConfigurationProvider } from '../core/configuration-provider.interface
import { IConfigurationBuilder } from '../core/configuration-builder.interface';
import { JsonConfigurationProvider } from './json-configuration-provider';
import { FileConfigurationSource } from '../core/file-configuration-source';
import path from 'path';

export class JsonConfigurationSource extends FileConfigurationSource {
constructor(path?: string) {
constructor(path: string) {
super(path);
}

Expand All @@ -17,6 18,8 @@ export class JsonConfigurationSource extends FileConfigurationSource {
throw new Error('builder cannot be null');
}

this.path = path.join(builder.getBasePath(), this.path);

return new JsonConfigurationProvider(this);
}
}
67 changes: 47 additions & 20 deletions test/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 5,7 @@ import { IConfigurationRoot } from '../packages/core/configuration-root.interfac
import { ConfigurationRoot } from '../packages/core/configuration-root';

function buildConfigurationProvider(path: string): IConfigurationProvider {
const source = new JsonConfigurationSource();
source.path = path;
const source = new JsonConfigurationSource(path);

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -16,11 15,47 @@ function buildConfigurationProvider(path: string): IConfigurationProvider {
return provider;
}

describe('configuration loading', () => {
it('should load configuration using base path', () => {
const config = new ConfigurationBuilder()
.setBasePath(__dirname '/examples/configuration-loading/')
.add(new JsonConfigurationSource('base-configuration.json'))
.build();

expect(config.get('logging.level')).toBe('debug');
});

it('should load configuration with base path as __dirname', () => {
const config = new ConfigurationBuilder()
.setBasePath(__dirname)
.add(new JsonConfigurationSource('/examples/configuration-loading/base-configuration.json'))
.build();

expect(config.get('logging.level')).toBe('debug');
});

it('should load configuration with base path as default', () => {
const config = new ConfigurationBuilder()
.setBasePath('./')
.add(new JsonConfigurationSource('test/examples/configuration-loading/base-configuration.json'))
.build();
expect(config.get('logging.level')).toBe('debug');
});

it('should load configuration with base path not set', () => {
const config = new ConfigurationBuilder()

.add(new JsonConfigurationSource('test/examples/configuration-loading/base-configuration.json'))
.build();

expect(config.get('logging.level')).toBe('debug');
});
});

describe('configuration-root', () => {
it('should load configuration from json file', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/basic/basic-configuration-2.json';
const source = new JsonConfigurationSource('./test/examples/basic/basic-configuration-2.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -36,8 71,7 @@ describe('configuration-root', () => {

it('get values from array', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/array/basic-configuration-array.json';
const source = new JsonConfigurationSource('./test/examples/array/basic-configuration-array.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -53,8 87,7 @@ describe('configuration-root', () => {

it('should get section', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/basic/basic-configuration-1.json';
const source = new JsonConfigurationSource('./test/examples/basic/basic-configuration-1.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -70,8 103,7 @@ describe('configuration-root', () => {

it('should get top level section', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/basic/basic-configuration-1.json';
const source = new JsonConfigurationSource('./test/examples/basic/basic-configuration-1.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -86,8 118,7 @@ it('should get top level section', () => {

it('should get value from nested config', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/basic/basic-configuration-1.json';
const source = new JsonConfigurationSource('./test/examples/basic/basic-configuration-1.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -102,8 133,7 @@ it('should get value from nested config', () => {

it('should get nested value from config using a type', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/nested/heroes.json';
const source = new JsonConfigurationSource('./test/examples/nested/heroes.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -120,8 150,7 @@ it('should get nested value from config using a type', () => {

it('should get nested value from config using a type', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/nested/heroes.json';
const source = new JsonConfigurationSource('./test/examples/nested/heroes.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -136,8 165,7 @@ it('should get nested value from config using a type', () => {

it('should get value from config using a type', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/basic/basic-configuration-1.json';
const source = new JsonConfigurationSource('./test/examples/basic/basic-configuration-1.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand All @@ -152,8 180,7 @@ it('should get value from config using a type', () => {

it('should handle basic types', () => {
// given
const source = new JsonConfigurationSource();
source.path = './test/examples/basic/basic-types.json';
const source = new JsonConfigurationSource('./test/examples/basic/basic-types.json');

const builder = new ConfigurationBuilder();
builder.add(source);
Expand Down
5 changes: 5 additions & 0 deletions test/examples/configuration-loading/base-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
{
"logging": {
"level": "debug"
}
}

0 comments on commit e9ccae9

Please sign in to comment.