-
Notifications
You must be signed in to change notification settings - Fork 31
#2 Implement options parsing. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a090170
0b470ea
93fbac6
8be08da
5302683
263fccd
831b964
3b1ece5
cec9858
954a135
1a0dffc
a141aeb
40d27e3
f429571
5792122
b93ed50
82599e2
ef67115
cd8357a
7ea76b5
82788fd
9740584
b573151
4283204
9080329
5e0d9b1
79dd5d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| module.exports = { | ||
| transform: {'^.+\\.ts?$': 'ts-jest'}, | ||
| testMatch: ["**/__tests__/**/*test.ts?(x)"], | ||
| testEnvironment: 'node', | ||
| moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] | ||
| moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
| collectCoverageFrom: [ | ||
| "platform-node/src/**/*.ts", | ||
| "sdk-common/src/**/*.ts", | ||
| "server-sdk-common/src/**/*.ts" | ||
| ] | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,6 @@ | |
| "declaration": true, | ||
| "declarationMap": true, // enables importers to jump to source | ||
| "resolveJsonModule": true, | ||
| "stripInternal": true | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to support |
||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { LDLogger } from '../src'; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a logger implementation for tests. Helps to validate some common scenarios. |
||
|
|
||
| // TODO: Move this to sdk-common when implementing logging. | ||
| export enum LogLevel { | ||
| Debug, | ||
| Info, | ||
| Warn, | ||
| Error, | ||
| } | ||
|
|
||
| type ExpectedMessage = { level: LogLevel, matches: RegExp }; | ||
|
|
||
| export default class TestLogger implements LDLogger { | ||
| private readonly messages: Record<LogLevel, string[]> = { | ||
| [LogLevel.Debug]: [], | ||
| [LogLevel.Info]: [], | ||
| [LogLevel.Warn]: [], | ||
| [LogLevel.Error]: [], | ||
| }; | ||
|
|
||
| private callCount = 0; | ||
|
|
||
| private waiters: Array<() => void> = []; | ||
|
|
||
| timeout(timeoutMs: number): Promise<number> { | ||
| return new Promise<number>((resolve) => { | ||
| setTimeout(() => resolve(this.callCount), timeoutMs); | ||
| }); | ||
| } | ||
|
|
||
| async waitForMessages(count: number, timeoutMs: number = 1000): Promise<number> { | ||
| return Promise.race([ | ||
| new Promise<number>((resolve) => { | ||
| const waiter = () => { | ||
| if (this.callCount >= count) { | ||
| resolve(this.callCount); | ||
| } | ||
| }; | ||
| waiter(); | ||
| this.waiters.push(waiter); | ||
| }), this.timeout(timeoutMs)]); | ||
| } | ||
|
|
||
| /** | ||
| * Check received messages for expected messages. | ||
| * | ||
| * @param expectedMessages List of expected messages. If a message is expected | ||
| * more than once, then it should be included multiple times. | ||
| * @returns A list of messages that were not received. | ||
| */ | ||
| expectMessages( | ||
| expectedMessages: ExpectedMessage[], | ||
| ): void { | ||
| const matched: Record<LogLevel, number[]> = { | ||
| [LogLevel.Debug]: [], | ||
| [LogLevel.Info]: [], | ||
| [LogLevel.Warn]: [], | ||
| [LogLevel.Error]: [], | ||
| }; | ||
|
|
||
| expectedMessages.forEach((expectedMessage) => { | ||
| const received = this.messages[expectedMessage.level]; | ||
| const index = received.findIndex( | ||
| (receivedMessage) => receivedMessage.match(expectedMessage.matches), | ||
| ); | ||
| if (index < 0) { | ||
| throw new Error(`Did not find expected message: ${expectedMessage}`); | ||
| } else if (matched[expectedMessage.level].indexOf(index) >= 0) { | ||
| throw new Error(`Did not find expected message: ${expectedMessage}`); | ||
| } else { | ||
| matched[expectedMessage.level].push(index); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| getCount() { | ||
| return this.callCount; | ||
| } | ||
|
|
||
| private checkResolves() { | ||
| this.waiters.forEach((waiter) => waiter()); | ||
| } | ||
|
|
||
| private log(level: LogLevel, ...args: any[]) { | ||
| this.messages[level].push(args.join(' ')); | ||
| this.callCount += 1; | ||
| this.checkResolves(); | ||
| } | ||
|
|
||
| error(...args: any[]): void { | ||
| this.log(LogLevel.Error, args); | ||
| } | ||
|
|
||
| warn(...args: any[]): void { | ||
| this.log(LogLevel.Warn, args); | ||
| } | ||
|
|
||
| info(...args: any[]): void { | ||
| this.log(LogLevel.Info, args); | ||
| } | ||
|
|
||
| debug(...args: any[]): void { | ||
| this.log(LogLevel.Debug, args); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import ApplicationTags from '../../src/options/ApplicationTags'; | ||
| import { ValidatedOptions } from '../../src/options/ValidatedOptions'; | ||
| import TestLogger, { LogLevel } from '../Logger'; | ||
|
|
||
| describe.each([ | ||
| [ | ||
| { application: { id: 'is-valid', version: 'also-valid' }, logger: new TestLogger() }, | ||
| 'application-id/is-valid application-version/also-valid', [], | ||
| ], | ||
| [{ application: { id: 'is-valid' }, logger: new TestLogger() }, 'application-id/is-valid', []], | ||
| [{ application: { version: 'also-valid' }, logger: new TestLogger() }, 'application-version/also-valid', []], | ||
| [{ application: {}, logger: new TestLogger() }, undefined, []], | ||
| [{ logger: new TestLogger() }, undefined, []], | ||
| [undefined, undefined, undefined], | ||
|
|
||
| // Above ones are 'valid' cases. Below are invalid. | ||
| [ | ||
| { application: { id: 'bad tag' }, logger: new TestLogger() }, | ||
| undefined, [ | ||
| { level: LogLevel.Warn, matches: /Config option "application.id" must/ }, | ||
| ], | ||
| ], | ||
| [ | ||
| { application: { id: 'bad tag', version: 'good-tag' }, logger: new TestLogger() }, | ||
| 'application-version/good-tag', [ | ||
| { level: LogLevel.Warn, matches: /Config option "application.id" must/ }, | ||
| ], | ||
| ], | ||
| [ | ||
| { application: { id: 'bad tag', version: 'also bad' }, logger: new TestLogger() }, | ||
| undefined, [ | ||
| { level: LogLevel.Warn, matches: /Config option "application.id" must/ }, | ||
| { level: LogLevel.Warn, matches: /Config option "application.version" must/ }, | ||
| ], | ||
| ], | ||
| // Bad tags and no logger. | ||
| [ | ||
| { application: { id: 'bad tag', version: 'also bad' }, logger: undefined }, | ||
| undefined, undefined, | ||
| ], | ||
| ])('given application tags configurations', (config, result, logs) => { | ||
| it('produces the correct tag values', () => { | ||
| const tags = new ApplicationTags(config as unknown as ValidatedOptions); | ||
| expect(tags.value).toEqual(result); | ||
| }); | ||
|
|
||
| it('logs issues it encounters', () => { | ||
| expect(config?.logger?.getCount()).toEqual(logs?.length); | ||
| if (logs) { | ||
| config?.logger?.expectMessages(logs); | ||
| } | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some updates to make coverage more useful.