Skip to content

Commit

Permalink
Issue #63: split contest as per upstream #61424.
Browse files Browse the repository at this point in the history
  • Loading branch information
davehenton authored and fgm committed Sep 23, 2018
1 parent 3aef64f commit 6132a20
Show file tree
Hide file tree
Showing 28 changed files with 925 additions and 254 deletions.
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
},

"globals": {
"console": true,

// Jest.
"afterAll": false,
"afterEach": false,
Expand Down
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ node_js:
- 10

before_script:
# Codecov preparation.
- npm install -g codecov

# CodeClimate preparation.
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter -d before-build
Expand All @@ -21,4 +25,8 @@ script:
yarn test-ci

after_script:
# CodeCode report.
- codecov

# CodeClimate report.
- ./cc-test-reporter after-build -d --exit-code $TRAVIS_TEST_RESULT
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FiLog: a Meteor 1.4-1.6 logging package
FiLog: a Meteor 1.4-1.7 logging package
=======================================

[![Build Status](https://travis-ci.org/fgm/filog.svg?branch=master)](https://travis-ci.org/fgm/filog)
[![Test Coverage](https://api.codeclimate.com/v1/badges/f380bfd73a491c472221/test_coverage)](https://codeclimate.com/github/fgm/filog/test_coverage)
[![CodeCov Test Coverage](https://codecov.io/gh/fgm/filog/branch/master/graph/badge.svg)](https://codecov.io/gh/fgm/filog)
[![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)
[![Known Vulnerabilities](https://snyk.io/test/github/fgm/filog/badge.svg?targetFile=package.json)](https://snyk.io/test/github/fgm/filog?targetFile=package.json)

Expand Down Expand Up @@ -202,3 +202,5 @@ page, failing the integration tests.
$ meteor npm run test-integration
$ meteor npm run test
$ meteor npm run cover

**TIP** Reading the `.travis.yml` file can be useful too.
35 changes: 26 additions & 9 deletions __tests__/unit/browserProcessorTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BrowserProcessor from "../../src/Processors/BrowserProcessor";
import NullFn from "../../src/NullFn";

/* This test hand-builds the MemoryInfo object in window.Performance, because
* that class is not defined outside Chrome.
Expand All @@ -10,10 +11,6 @@ function testBrowserProcessor() {
this[key] = values[key];
}
}

toJSON() {
return { memory: {} };
}
}

let initialContext;
Expand All @@ -27,20 +24,23 @@ function testBrowserProcessor() {
});

test("should fail outside the browser", () => {
let processor;
expect(() => {
const processor = new BrowserProcessor(navigator, window);
processor = new BrowserProcessor(navigator, window);
processor.process(initialContext);
}).not.toThrow(ReferenceError);

processor = null;
expect(() => {
const processor = new BrowserProcessor();
processor.process(initialContext);
processor = new BrowserProcessor();
}).toThrow(ReferenceError);
expect(processor).toBeNull();

processor = null;
expect(() => {
const processor = new BrowserProcessor(null, null);
processor.process(initialContext);
processor = new BrowserProcessor(null, null);
}).toThrow(ReferenceError);
expect(processor).toBeNull();
});

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

test("Should ignore extra defaults on browser", () => {
const MAGIC = "xyzzy";
const win = Object.assign({}, { performance: new Performance({ memory: {
jsHeapSizeLimit: 1,
totalJSHeapSize: 2,
usedJSHeapSize: 3,
} }) });

const processor = new BrowserProcessor(navigator, win);
Object.prototype[MAGIC] = NullFn;
const processed = processor.process(initialContext);
expect(processed).toHaveProperty('browser');
delete Object.prototype[MAGIC];
const browser = processed.browser;
expect(browser).not.toHaveProperty(MAGIC);
});

test("should serialize performance.memory correctly", () => {
const keys = [
"jsHeapSizeLimit",
Expand Down
140 changes: 94 additions & 46 deletions __tests__/unit/logContextTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function testImmutableContext() {
selectSenders: () => [],
};
test("should not modify context in log() calls", () => {
const logger = new Logger(strategy);
const logger = new Logger(strategy );
logger.side = 'test';
const originalContext = {};
const context = { ...originalContext };

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

function testMessageContext() {
let result;
const referenceContext = { a: "A" };
const referenceContext = () => ({ a: "A" });
const sender = new class {
send(level, message, context) {
result = { level, message, context };
Expand All @@ -41,73 +42,117 @@ function testMessageContext() {
selectSenders: () => [sender],
};

const DETAILS_KEY = "message_details";

test(`should add the message argument to ${DETAILS_KEY}`, () => {
/**
* log(..., { a: 1 }) should ...
*
* - log { message_details: { a: 1} }.
*/
test(`should add the message argument to ${Logger.KEY_DETAILS}`, () => {
const logger = new Logger(strategy);
result = null;
logger.log(LogLevel.DEBUG, "some message", referenceContext);
logger.log(LogLevel.DEBUG, "some message", referenceContext());

const actual = result.context[DETAILS_KEY].a;
const actual = result.context[Logger.KEY_DETAILS].a;
const expected = "A";
// Message details is set
expect(actual).toBe(expected);
});

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

const actual = result.context[DETAILS_KEY];

// Original top-level key should be in top [DETAILS_KEY].
const expectedReference = "A";
expect(actual).toHaveProperty("a");
expect(actual.a).toBe(expectedReference);

// Key nested in original message_detail should also in top [DETAILS_KEY].
const expectedNested = "bar";
expect(actual).toHaveProperty("foo");
expect(actual.foo).toBe(expectedNested);
const actual = result.context.hasOwnProperty("a");
const expected = false;
// Message details is set
expect(actual).toBe(expected);
});

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

// Message+_details should not contain a nested [DETAILS_KEY].
const actual = result.context[DETAILS_KEY];
expect(actual).not.toHaveProperty(DETAILS_KEY);
const actual = result.context;
expect(actual).not.toHaveProperty("a");
expect(actual).not.toHaveProperty("foo");
expect(actual).toHaveProperty(Logger.KEY_DETAILS);

// Original top-level keys should still be in top [KEY_DETAILS].
const actualDetails = actual[Logger.KEY_DETAILS];
expect(actualDetails).toHaveProperty("a", "A");
expect(actualDetails).toHaveProperty(Logger.KEY_DETAILS);
expect(actualDetails).not.toHaveProperty("foo");

// Key nested in original message_detail should remain in place.
const actualNested = actualDetails[Logger.KEY_DETAILS];
expect(actualNested).not.toHaveProperty("a", "A");
expect(actualNested).not.toHaveProperty(Logger.KEY_DETAILS);
expect(actualNested).toHaveProperty("foo", 'bar');
});

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

// [DETAILS_KEY] should contain the newly added value for key "a", not the
// one present in the initial [DETAILS_KEY].
const actual = result.context[DETAILS_KEY];
const expected = "A";
// Message details is set
expect(actual).toHaveProperty("a");
expect(actual.a).toBe(expected);
// Message_details should only contain a nested [KEY_DETAILS].
const actual = result.context;
const keys = Object.keys(actual).sort();
expect(keys.length).toBe(3);
expect(keys).toEqual([Logger.KEY_DETAILS, Logger.KEY_SOURCE, Logger.KEY_TS]);
expect(actual).toHaveProperty(Logger.KEY_DETAILS);

// Original top-level keys should still be in top [KEY_DETAILS].
const actualDetails = actual[Logger.KEY_DETAILS];
expect(Object.keys(actualDetails).length).toBe(2);
expect(actualDetails).toHaveProperty("a", "A");
expect(actualDetails).toHaveProperty(Logger.KEY_DETAILS);

// Key nested in original message_detail should remain in place.
const actualNested = actualDetails[Logger.KEY_DETAILS];
expect(Object.keys(actualNested).length).toBe(1);
expect(actualNested).toHaveProperty("a", "A");
});

test("should not add the message arguments to context root", () => {
/**
* log(..., { a: "A", message_details: { a: "B" } }) should ...
*
* - log { message_details: { a: "A", message_details: { a: "B" } } }.
*/
test(`should not merge keys within ${Logger.KEY_DETAILS}`, () => {
const logger = new Logger(strategy);
result = null;
logger.log(LogLevel.DEBUG, "some message", referenceContext);
const originalContext = Object.assign({ [Logger.KEY_DETAILS]: { a: "B" } }, referenceContext());
logger.log(LogLevel.DEBUG, "some message", originalContext);

const actual = result.context.hasOwnProperty("a");
const expected = false;
// Message details is set
expect(actual).toBe(expected);
// [KEY_DETAILS] should contain the newly added value for key "a", not the
// one present in the initial [KEY_DETAILS].
const actual = result.context[Logger.KEY_DETAILS];
const expected = "A";
// Message details is set.
expect(actual).toHaveProperty("a");
expect(actual.a).toBe(expected);
});
}

Expand Down Expand Up @@ -303,7 +348,9 @@ function testProcessors() {
class TimeWarp extends ProcessorBase {
// Let's do the time warp again.
process(context) {
context.timestamp = { log: +new Date("1978-11-19 05:00:00") };
context[Logger.KEY_TS] = {
test: { log: +new Date("1978-11-19 05:00:00") },
};
context.hostname = "remote";
return context;
}
Expand All @@ -317,6 +364,7 @@ function testProcessors() {
selectSenders: () => [this.sender],
};
this.logger = new Logger(this.strategy);
this.logger.side = 'test';
});

test("processors should be able to modify the content of ordinary existing keys", () => {
Expand Down Expand Up @@ -368,8 +416,8 @@ function testProcessors() {
expect(this.sender.logs.length).toBe(1);
const [,, context] = this.sender.logs.pop();
expect(context).toHaveProperty("hostname", "local");
expect(context).toHaveProperty("timestamp.log");
const lag = ts - context.timestamp.log;
expect(context).toHaveProperty(`${Logger.KEY_TS}.${this.logger.side}.log`);
const lag = ts - context[Logger.KEY_TS][this.logger.side].log;
expect(lag).toBeGreaterThanOrEqual(0);
// No sane machine should take more than 100 msec to return from log() with
// such a fast sending configuration.
Expand All @@ -384,8 +432,8 @@ function testProcessors() {
expect(this.sender.logs.length).toBe(1);
const [,, context] = this.sender.logs.pop();
expect(context).toHaveProperty("hostname", "remote");
expect(context).toHaveProperty("timestamp.log");
const lag = ts - context.timestamp.log;
expect(context).toHaveProperty(`${Logger.KEY_TS}.${this.logger.side}.log`);
const lag = ts - context[Logger.KEY_TS][this.logger.side].log;
expect(lag).toBeGreaterThanOrEqual(0);
// No sane machine should take more than 100 msec to return from log() with
// such a fast sending configuration. The TimeWarp processor attempts to
Expand Down
20 changes: 14 additions & 6 deletions __tests__/unit/meteorUserProcessorTest.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import MeteorUserProcessor from "../../src/Processors/MeteorUserProcessor";
import ServerLogger from "../../src/ServerLogger";

function testMeteorUserProcessor() {
const mockMeteor = { isServer: true };
const mockAccountsPackage = { "accounts-base": { AccountsServer: true } };
const SIDE = ServerLogger.side;

const postProcessorDelete = data => {
let result = Object.assign({}, data);
delete result.meteor.user.services;
delete result.server.user.services;
return result;
};

const forcedUserId = 42;
const postProcessorUpdate = data => {
let result = Object.assign({}, data);
result.meteor.user = forcedUserId;
result.server.user = forcedUserId;
return result;
};

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

expect(processorRaw).toBeInstanceOf(MeteorUserProcessor);
const resultRaw = processorRaw.process(data);
expect(resultRaw.meteor.user.services).toBeDefined();
expect(resultRaw).toHaveProperty(SIDE);
expect(resultRaw[SIDE].user.services).toBeDefined();

expect(processorDeletor).toBeInstanceOf(MeteorUserProcessor);
const resultDeleted = processorDeletor.process(data);
expect(resultDeleted.meteor.user.services).toBeUndefined();
expect(resultDeleted[SIDE].user.services).toBeUndefined();

expect(processorUpdater).toBeInstanceOf(MeteorUserProcessor);
const resultUpdated = processorUpdater.process(data);
expect(resultUpdated.meteor.user).toBe(forcedUserId);
expect(resultUpdated[SIDE].user).toBe(forcedUserId);
});
}

Expand Down
Loading

0 comments on commit 6132a20

Please sign in to comment.