Skip to content

Commit

Permalink
feat: compatible read config.prod and config.unittest (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Mar 12, 2021
1 parent ea2edfe commit f90cfe3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/service/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class MidwayConfigService implements IConfigService {
isReady = false;
container: IMidwayContainer;
externalObject: Record<string, unknown>[] = [];
aliasMap = {
prod: 'production',
unittest: 'test',
};

constructor(container) {
this.container = container;
Expand All @@ -27,6 +31,9 @@ export class MidwayConfigService implements IConfigService {
const env = this.getConfigEnv(dir);
const envSet = this.getEnvSet(env);
envSet.add(dir);
if (this.aliasMap[env]) {
this.getEnvSet(this.aliasMap[env]).add(dir);
}
} else {
// directory
const fileStat = statSync(dir);
Expand Down
57 changes: 57 additions & 0 deletions packages/core/test/services/configService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert = require('assert');
import { resolve, join } from 'path';
import { MidwayContainer } from '../../src';
import { MidwayConfigService } from '../../src/service/configService';
import * as mm from 'mm';

describe('/test/services/configService.test.ts', () => {
it('configService should be ok', () => {
Expand Down Expand Up @@ -68,4 +69,60 @@ describe('/test/services/configService.test.ts', () => {
assert.ok(cfg.configuration.bb === 222);
assert.ok(cfg.configuration.aa === 1);
});

it('should test default', async () => {
const container = new MidwayContainer();
const cfg = new MidwayConfigService(container);

const configFile = resolve(join(__dirname, './fixtures/default_case', 'config.default'));
const result = await cfg.loadConfig(configFile);
expect(result.parent).toEqual({a: 1, b:2});
const configFileLocal = resolve(join(__dirname, './fixtures/default_case', 'config.local'));
const resultLocal = await cfg.loadConfig(configFileLocal);
expect(resultLocal.parent).toEqual({a: 1});
});

it('should compatible old production', async () => {
mm(process.env, 'MIDWAY_SERVER_ENV', 'production');
const container = new MidwayContainer();
const cfg = new MidwayConfigService(container);

cfg.add([
join(__dirname, './fixtures/compatible_production'),
]);

await cfg.load();

expect(cfg.configuration).toEqual({
key: {
data: 123,
},
bbb: {
data: 123,
}
})
mm.restore()
});

it('should compatible old test', async () => {
mm(process.env, 'MIDWAY_SERVER_ENV', 'test');
const container = new MidwayContainer();
const cfg = new MidwayConfigService(container);

cfg.add([
join(__dirname, './fixtures/compatible_production'),
]);

await cfg.load();

expect(cfg.configuration).toEqual({
key: {
data: 123,
},
bbb: {
data: 321,
}
})
mm.restore()
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const key = {
data: 123,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const bbb = {
data: 123,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const bbb = {
data: 321,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
parent: {
a: 1,
b: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
parent: {
a: 1
}
};

0 comments on commit f90cfe3

Please sign in to comment.