From 770ab0840a9041c719fb1f5567550e6f1374384c Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Mon, 19 Mar 2018 15:21:55 -0700 Subject: [PATCH] test: don't use exec to test preloaded modules (#696) PR-URL: #696 --- test/logger.ts | 62 ++++++++++++++++++++++++ test/test-modules-loaded-before-agent.ts | 36 +++++++++----- tsconfig.json | 2 + 3 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 test/logger.ts diff --git a/test/logger.ts b/test/logger.ts new file mode 100644 index 000000000..51b0c40f3 --- /dev/null +++ b/test/logger.ts @@ -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) + .forEach(logLevel => this.logs[logLevel].length = 0); + } +} diff --git a/test/test-modules-loaded-before-agent.ts b/test/test-modules-loaded-before-agent.ts index 8729b3527..f7b935e80 100644 --- a/test/test-modules-loaded-before-agent.ts +++ b/test/test-modules-loaded-before-agent.ts @@ -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); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 580c4c787..eab7683b6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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",