Skip to content

Commit 6132a20

Browse files
davehentonfgm
authored andcommitted
Issue #63: split contest as per upstream #61424.
1 parent 3aef64f commit 6132a20

28 files changed

+925
-254
lines changed

.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
},
88

99
"globals": {
10+
"console": true,
11+
1012
// Jest.
1113
"afterAll": false,
1214
"afterEach": false,

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ node_js:
1313
- 10
1414

1515
before_script:
16+
# Codecov preparation.
17+
- npm install -g codecov
18+
19+
# CodeClimate preparation.
1620
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
1721
- chmod +x ./cc-test-reporter
1822
- ./cc-test-reporter -d before-build
@@ -21,4 +25,8 @@ script:
2125
yarn test-ci
2226

2327
after_script:
28+
# CodeCode report.
29+
- codecov
30+
31+
# CodeClimate report.
2432
- ./cc-test-reporter after-build -d --exit-code $TRAVIS_TEST_RESULT

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FiLog: a Meteor 1.4-1.6 logging package
1+
FiLog: a Meteor 1.4-1.7 logging package
22
=======================================
33

44
[![Build Status](https://travis-ci.org/fgm/filog.svg?branch=master)](https://travis-ci.org/fgm/filog)
5-
[![Test Coverage](https://api.codeclimate.com/v1/badges/f380bfd73a491c472221/test_coverage)](https://codeclimate.com/github/fgm/filog/test_coverage)
5+
[![CodeCov Test Coverage](https://codecov.io/gh/fgm/filog/branch/master/graph/badge.svg)](https://codecov.io/gh/fgm/filog)
66
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fgm/filog/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fgm/filog/?branch=master)
77
[![Known Vulnerabilities](https://snyk.io/test/github/fgm/filog/badge.svg?targetFile=package.json)](https://snyk.io/test/github/fgm/filog?targetFile=package.json)
88

@@ -202,3 +202,5 @@ page, failing the integration tests.
202202
$ meteor npm run test-integration
203203
$ meteor npm run test
204204
$ meteor npm run cover
205+
206+
**TIP** Reading the `.travis.yml` file can be useful too.

__tests__/unit/browserProcessorTest.ts

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import BrowserProcessor from "../../src/Processors/BrowserProcessor";
2+
import NullFn from "../../src/NullFn";
23

34
/* This test hand-builds the MemoryInfo object in window.Performance, because
45
* that class is not defined outside Chrome.
@@ -10,10 +11,6 @@ function testBrowserProcessor() {
1011
this[key] = values[key];
1112
}
1213
}
13-
14-
toJSON() {
15-
return { memory: {} };
16-
}
1714
}
1815

1916
let initialContext;
@@ -27,20 +24,23 @@ function testBrowserProcessor() {
2724
});
2825

2926
test("should fail outside the browser", () => {
27+
let processor;
3028
expect(() => {
31-
const processor = new BrowserProcessor(navigator, window);
29+
processor = new BrowserProcessor(navigator, window);
3230
processor.process(initialContext);
3331
}).not.toThrow(ReferenceError);
3432

33+
processor = null;
3534
expect(() => {
36-
const processor = new BrowserProcessor();
37-
processor.process(initialContext);
35+
processor = new BrowserProcessor();
3836
}).toThrow(ReferenceError);
37+
expect(processor).toBeNull();
3938

39+
processor = null;
4040
expect(() => {
41-
const processor = new BrowserProcessor(null, null);
42-
processor.process(initialContext);
41+
processor = new BrowserProcessor(null, null);
4342
}).toThrow(ReferenceError);
43+
expect(processor).toBeNull();
4444
});
4545

4646
test("should at least return defaults", () => {
@@ -54,6 +54,23 @@ function testBrowserProcessor() {
5454
expect(actual).not.toHaveProperty("browser.product");
5555
});
5656

57+
test("Should ignore extra defaults on browser", () => {
58+
const MAGIC = "xyzzy";
59+
const win = Object.assign({}, { performance: new Performance({ memory: {
60+
jsHeapSizeLimit: 1,
61+
totalJSHeapSize: 2,
62+
usedJSHeapSize: 3,
63+
} }) });
64+
65+
const processor = new BrowserProcessor(navigator, win);
66+
Object.prototype[MAGIC] = NullFn;
67+
const processed = processor.process(initialContext);
68+
expect(processed).toHaveProperty('browser');
69+
delete Object.prototype[MAGIC];
70+
const browser = processed.browser;
71+
expect(browser).not.toHaveProperty(MAGIC);
72+
});
73+
5774
test("should serialize performance.memory correctly", () => {
5875
const keys = [
5976
"jsHeapSizeLimit",

__tests__/unit/logContextTest.ts

+94-46
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ function testImmutableContext() {
99
selectSenders: () => [],
1010
};
1111
test("should not modify context in log() calls", () => {
12-
const logger = new Logger(strategy);
12+
const logger = new Logger(strategy );
13+
logger.side = 'test';
1314
const originalContext = {};
1415
const context = { ...originalContext };
1516

@@ -29,7 +30,7 @@ function testImmutableContext() {
2930

3031
function testMessageContext() {
3132
let result;
32-
const referenceContext = { a: "A" };
33+
const referenceContext = () => ({ a: "A" });
3334
const sender = new class {
3435
send(level, message, context) {
3536
result = { level, message, context };
@@ -41,73 +42,117 @@ function testMessageContext() {
4142
selectSenders: () => [sender],
4243
};
4344

44-
const DETAILS_KEY = "message_details";
45-
46-
test(`should add the message argument to ${DETAILS_KEY}`, () => {
45+
/**
46+
* log(..., { a: 1 }) should ...
47+
*
48+
* - log { message_details: { a: 1} }.
49+
*/
50+
test(`should add the message argument to ${Logger.KEY_DETAILS}`, () => {
4751
const logger = new Logger(strategy);
4852
result = null;
49-
logger.log(LogLevel.DEBUG, "some message", referenceContext);
53+
logger.log(LogLevel.DEBUG, "some message", referenceContext());
5054

51-
const actual = result.context[DETAILS_KEY].a;
55+
const actual = result.context[Logger.KEY_DETAILS].a;
5256
const expected = "A";
5357
// Message details is set
5458
expect(actual).toBe(expected);
5559
});
5660

57-
test(`should merge contents of existing ${DETAILS_KEY} context key`, () => {
61+
/**
62+
* log(..., { a: 1 }) should ...
63+
*
64+
* - NOT log { a: 1 }.
65+
*/
66+
test("should not add the message arguments to context root", () => {
5867
const logger = new Logger(strategy);
5968
result = null;
60-
const originalContext = Object.assign({ [DETAILS_KEY]: { foo: "bar" } }, referenceContext);
61-
logger.log(LogLevel.DEBUG, "some message", originalContext);
69+
logger.log(LogLevel.DEBUG, "some message", referenceContext());
6270

63-
const actual = result.context[DETAILS_KEY];
64-
65-
// Original top-level key should be in top [DETAILS_KEY].
66-
const expectedReference = "A";
67-
expect(actual).toHaveProperty("a");
68-
expect(actual.a).toBe(expectedReference);
69-
70-
// Key nested in original message_detail should also in top [DETAILS_KEY].
71-
const expectedNested = "bar";
72-
expect(actual).toHaveProperty("foo");
73-
expect(actual.foo).toBe(expectedNested);
71+
const actual = result.context.hasOwnProperty("a");
72+
const expected = false;
73+
// Message details is set
74+
expect(actual).toBe(expected);
7475
});
7576

76-
test(`should not merge existing ${DETAILS_KEY} context key itself`, () => {
77+
/**
78+
* log(..., { a: "A", message_details: { foo: "bar" } }) should...
79+
*
80+
* - log { message_details: { a: "A", message_details: { foo: "bar" } } },
81+
* unlike the message_details merging it did until 0.1.18 included.
82+
*/
83+
test(`should not merge contents of existing ${Logger.KEY_DETAILS} context key`, () => {
7784
const logger = new Logger(strategy);
7885
result = null;
79-
const originalContext = Object.assign({ [DETAILS_KEY]: { foo: "bar" } }, referenceContext);
86+
const originalContext = Object.assign({ [Logger.KEY_DETAILS]: { foo: "bar" } }, referenceContext());
8087
logger.log(LogLevel.DEBUG, "some message", originalContext);
8188

82-
// Message+_details should not contain a nested [DETAILS_KEY].
83-
const actual = result.context[DETAILS_KEY];
84-
expect(actual).not.toHaveProperty(DETAILS_KEY);
89+
const actual = result.context;
90+
expect(actual).not.toHaveProperty("a");
91+
expect(actual).not.toHaveProperty("foo");
92+
expect(actual).toHaveProperty(Logger.KEY_DETAILS);
93+
94+
// Original top-level keys should still be in top [KEY_DETAILS].
95+
const actualDetails = actual[Logger.KEY_DETAILS];
96+
expect(actualDetails).toHaveProperty("a", "A");
97+
expect(actualDetails).toHaveProperty(Logger.KEY_DETAILS);
98+
expect(actualDetails).not.toHaveProperty("foo");
99+
100+
// Key nested in original message_detail should remain in place.
101+
const actualNested = actualDetails[Logger.KEY_DETAILS];
102+
expect(actualNested).not.toHaveProperty("a", "A");
103+
expect(actualNested).not.toHaveProperty(Logger.KEY_DETAILS);
104+
expect(actualNested).toHaveProperty("foo", 'bar');
85105
});
86106

87-
test(`should keep the latest keys when merging existing ${DETAILS_KEY}`, () => {
107+
/**
108+
* log(..., { a: "A", message_details: { a: "A" } }) should...
109+
*
110+
* - log { message_details: { a: "A", message_details: { a: "A" } } },
111+
* unlike the message_details merging it did until 0.1.18 included.
112+
*/
113+
test(`should not merge existing ${Logger.KEY_DETAILS} context key itself`, () => {
88114
const logger = new Logger(strategy);
89115
result = null;
90-
const originalContext = Object.assign(referenceContext, { [DETAILS_KEY]: { a: "B" } });
116+
const originalContext = Object.assign({ [Logger.KEY_DETAILS]: { a: "A" } }, referenceContext());
91117
logger.log(LogLevel.DEBUG, "some message", originalContext);
92118

93-
// [DETAILS_KEY] should contain the newly added value for key "a", not the
94-
// one present in the initial [DETAILS_KEY].
95-
const actual = result.context[DETAILS_KEY];
96-
const expected = "A";
97-
// Message details is set
98-
expect(actual).toHaveProperty("a");
99-
expect(actual.a).toBe(expected);
119+
// Message_details should only contain a nested [KEY_DETAILS].
120+
const actual = result.context;
121+
const keys = Object.keys(actual).sort();
122+
expect(keys.length).toBe(3);
123+
expect(keys).toEqual([Logger.KEY_DETAILS, Logger.KEY_SOURCE, Logger.KEY_TS]);
124+
expect(actual).toHaveProperty(Logger.KEY_DETAILS);
125+
126+
// Original top-level keys should still be in top [KEY_DETAILS].
127+
const actualDetails = actual[Logger.KEY_DETAILS];
128+
expect(Object.keys(actualDetails).length).toBe(2);
129+
expect(actualDetails).toHaveProperty("a", "A");
130+
expect(actualDetails).toHaveProperty(Logger.KEY_DETAILS);
131+
132+
// Key nested in original message_detail should remain in place.
133+
const actualNested = actualDetails[Logger.KEY_DETAILS];
134+
expect(Object.keys(actualNested).length).toBe(1);
135+
expect(actualNested).toHaveProperty("a", "A");
100136
});
101137

102-
test("should not add the message arguments to context root", () => {
138+
/**
139+
* log(..., { a: "A", message_details: { a: "B" } }) should ...
140+
*
141+
* - log { message_details: { a: "A", message_details: { a: "B" } } }.
142+
*/
143+
test(`should not merge keys within ${Logger.KEY_DETAILS}`, () => {
103144
const logger = new Logger(strategy);
104145
result = null;
105-
logger.log(LogLevel.DEBUG, "some message", referenceContext);
146+
const originalContext = Object.assign({ [Logger.KEY_DETAILS]: { a: "B" } }, referenceContext());
147+
logger.log(LogLevel.DEBUG, "some message", originalContext);
106148

107-
const actual = result.context.hasOwnProperty("a");
108-
const expected = false;
109-
// Message details is set
110-
expect(actual).toBe(expected);
149+
// [KEY_DETAILS] should contain the newly added value for key "a", not the
150+
// one present in the initial [KEY_DETAILS].
151+
const actual = result.context[Logger.KEY_DETAILS];
152+
const expected = "A";
153+
// Message details is set.
154+
expect(actual).toHaveProperty("a");
155+
expect(actual.a).toBe(expected);
111156
});
112157
}
113158

@@ -303,7 +348,9 @@ function testProcessors() {
303348
class TimeWarp extends ProcessorBase {
304349
// Let's do the time warp again.
305350
process(context) {
306-
context.timestamp = { log: +new Date("1978-11-19 05:00:00") };
351+
context[Logger.KEY_TS] = {
352+
test: { log: +new Date("1978-11-19 05:00:00") },
353+
};
307354
context.hostname = "remote";
308355
return context;
309356
}
@@ -317,6 +364,7 @@ function testProcessors() {
317364
selectSenders: () => [this.sender],
318365
};
319366
this.logger = new Logger(this.strategy);
367+
this.logger.side = 'test';
320368
});
321369

322370
test("processors should be able to modify the content of ordinary existing keys", () => {
@@ -368,8 +416,8 @@ function testProcessors() {
368416
expect(this.sender.logs.length).toBe(1);
369417
const [,, context] = this.sender.logs.pop();
370418
expect(context).toHaveProperty("hostname", "local");
371-
expect(context).toHaveProperty("timestamp.log");
372-
const lag = ts - context.timestamp.log;
419+
expect(context).toHaveProperty(`${Logger.KEY_TS}.${this.logger.side}.log`);
420+
const lag = ts - context[Logger.KEY_TS][this.logger.side].log;
373421
expect(lag).toBeGreaterThanOrEqual(0);
374422
// No sane machine should take more than 100 msec to return from log() with
375423
// such a fast sending configuration.
@@ -384,8 +432,8 @@ function testProcessors() {
384432
expect(this.sender.logs.length).toBe(1);
385433
const [,, context] = this.sender.logs.pop();
386434
expect(context).toHaveProperty("hostname", "remote");
387-
expect(context).toHaveProperty("timestamp.log");
388-
const lag = ts - context.timestamp.log;
435+
expect(context).toHaveProperty(`${Logger.KEY_TS}.${this.logger.side}.log`);
436+
const lag = ts - context[Logger.KEY_TS][this.logger.side].log;
389437
expect(lag).toBeGreaterThanOrEqual(0);
390438
// No sane machine should take more than 100 msec to return from log() with
391439
// such a fast sending configuration. The TimeWarp processor attempts to

__tests__/unit/meteorUserProcessorTest.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import MeteorUserProcessor from "../../src/Processors/MeteorUserProcessor";
2+
import ServerLogger from "../../src/ServerLogger";
23

34
function testMeteorUserProcessor() {
45
const mockMeteor = { isServer: true };
56
const mockAccountsPackage = { "accounts-base": { AccountsServer: true } };
7+
const SIDE = ServerLogger.side;
68

79
const postProcessorDelete = data => {
810
let result = Object.assign({}, data);
9-
delete result.meteor.user.services;
11+
delete result.server.user.services;
1012
return result;
1113
};
1214

1315
const forcedUserId = 42;
1416
const postProcessorUpdate = data => {
1517
let result = Object.assign({}, data);
16-
result.meteor.user = forcedUserId;
18+
result.server.user = forcedUserId;
1719
return result;
1820
};
1921

2022
test("should accept a collection name", () => {
21-
const data = { anything: "goes" };
23+
const data = {
24+
anything: "goes",
25+
// Actual contexts always have a "source" top-level key, added by
26+
// Logger.log() before invoking buildContext().
27+
"source": SIDE,
28+
};
2229
global.Package = mockAccountsPackage;
2330
const processorRaw = new MeteorUserProcessor(mockMeteor);
2431
const processorDeletor = new MeteorUserProcessor(mockMeteor, postProcessorDelete);
@@ -27,15 +34,16 @@ function testMeteorUserProcessor() {
2734

2835
expect(processorRaw).toBeInstanceOf(MeteorUserProcessor);
2936
const resultRaw = processorRaw.process(data);
30-
expect(resultRaw.meteor.user.services).toBeDefined();
37+
expect(resultRaw).toHaveProperty(SIDE);
38+
expect(resultRaw[SIDE].user.services).toBeDefined();
3139

3240
expect(processorDeletor).toBeInstanceOf(MeteorUserProcessor);
3341
const resultDeleted = processorDeletor.process(data);
34-
expect(resultDeleted.meteor.user.services).toBeUndefined();
42+
expect(resultDeleted[SIDE].user.services).toBeUndefined();
3543

3644
expect(processorUpdater).toBeInstanceOf(MeteorUserProcessor);
3745
const resultUpdated = processorUpdater.process(data);
38-
expect(resultUpdated.meteor.user).toBe(forcedUserId);
46+
expect(resultUpdated[SIDE].user).toBe(forcedUserId);
3947
});
4048
}
4149

0 commit comments

Comments
 (0)