Skip to content

Commit

Permalink
test: don't use exec to test preloaded modules (#696)
Browse files Browse the repository at this point in the history
PR-URL: #696
  • Loading branch information
kjin authored Mar 19, 2018
1 parent 5338a93 commit 770ab08
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
62 changes: 62 additions & 0 deletions test/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {Logger, logger} from '@google-cloud/common';

const PASS_THROUGH_LOG_LEVEL = Number(process.env.GCLOUD_TEST_LOG_LEVEL || 0);

// tslint:disable-next-line:no-any
type LoggerFunction = (message: any, ...args: any[]) => void;

export class TestLogger implements Logger {
private logs:
{[k in keyof Logger]:
string[]} = {error: [], warn: [], info: [], debug: [], silly: []};
private innerLogger = logger({level: logger.LEVELS[PASS_THROUGH_LOG_LEVEL]});

private makeLoggerFn(logLevel: keyof Logger): LoggerFunction {
// TODO(kjin): When we drop support for Node 4, use spread args.
const that = this;
return function(this: null) {
const args = Array.prototype.slice.call(arguments, 0);
that.logs[logLevel].push(args.join(' '));
that.innerLogger[logLevel].apply(this, args);
};
}

error = this.makeLoggerFn('error');
warn = this.makeLoggerFn('warn');
info = this.makeLoggerFn('info');
debug = this.makeLoggerFn('debug');
silly = this.makeLoggerFn('silly');

getLogs(logLevel: keyof Logger): string[] {
return this.logs[logLevel];
}

getNumLogsWith(logLevel: keyof Logger, strOrReg: string|RegExp): number {
if (typeof strOrReg === 'string') {
return this.logs[logLevel].filter(line => line.includes(strOrReg)).length;
} else {
return this.logs[logLevel].filter(line => line.match(strOrReg)).length;
}
}

clearLogs(): void {
(Object.keys(this.logs) as Array<keyof Logger>)
.forEach(logLevel => this.logs[logLevel].length = 0);
}
}
36 changes: 25 additions & 11 deletions test/test-modules-loaded-before-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@
* limitations under the License.
*/

'use strict';
import * as common from '@google-cloud/common';
import * as assert from 'assert';
import * as shimmer from 'shimmer';

var assert = require('assert');
var cp = require('child_process');
import {TestLogger} from './logger';
import * as trace from './trace';

describe('modules loaded before agent', function() {
it('should log if modules were loaded before agent', function() {
var output =
cp.execSync('node test/fixtures/start-agent.js');
console.log(output.toString());
assert(output.toString().match(/Tracing might not work.*"glob".*/));
describe('modules loaded before agent', () => {
const logger = new TestLogger();

before(() => {
const LEVELS = common.logger.LEVELS;
shimmer.wrap(common, 'logger', () => {
return Object.assign(() => logger, {LEVELS});
});
});

after(() => {
shimmer.unwrap(common, 'logger');
});
});

export default {};
it('should log if modules were loaded before agent', () => {
trace.start();
assert.strictEqual(
logger.getNumLogsWith(
'error', /modules.*loaded.*before.*trace agent.*: .*"shimmer"/),
1);
});
});
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"src/plugins/plugin-koa.ts",
"src/plugins/plugin-restify.ts",
"test/plugins/test-trace-http2.ts",
"test/logger.ts",
"test/nocks.ts",
"test/test-config-credentials.ts",
"test/test-modules-loaded-before-agent.ts",
"test/test-trace-cluster.ts",
"test/test-trace-web-frameworks.ts",
"test/trace.ts",
Expand Down

0 comments on commit 770ab08

Please sign in to comment.