|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import * as sinon from 'sinon'; |
| 19 | +import { InstrumentationBase, InstrumentationModuleDefinition } from '../../src'; |
| 20 | + |
| 21 | +const MODULE_NAME = 'test-module'; |
| 22 | +const MODULE_FILE_NAME = 'test-module-file'; |
| 23 | +const MODULE_VERSION = '0.1.0'; |
| 24 | +const WILDCARD_VERSION = '*'; |
| 25 | +const MODULE_DIR = '/random/dir'; |
| 26 | + |
| 27 | +class TestInstrumentation extends InstrumentationBase { |
| 28 | + constructor() { |
| 29 | + super(MODULE_NAME, MODULE_VERSION); |
| 30 | + } |
| 31 | + |
| 32 | + init() {} |
| 33 | +} |
| 34 | + |
| 35 | +describe('InstrumentationBase', () => { |
| 36 | + describe('_onRequire - module version is not available', () => { |
| 37 | + // For all of these cases, there is no indication of the actual module version, |
| 38 | + // so we require there to be a wildcard supported version. |
| 39 | + |
| 40 | + let instrumentation: TestInstrumentation; |
| 41 | + let modulePatchSpy: sinon.SinonSpy; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + instrumentation = new TestInstrumentation(); |
| 45 | + // @ts-expect-error access internal property for testing |
| 46 | + instrumentation._enabled = true; |
| 47 | + modulePatchSpy = sinon.spy(); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('when patching a module', () => { |
| 51 | + describe('AND there is no wildcard supported version', () => { |
| 52 | + it('should not patch module', () => { |
| 53 | + const moduleExports = {}; |
| 54 | + const instrumentationModule = { |
| 55 | + supportedVersions: [`^${MODULE_VERSION}`], |
| 56 | + name: MODULE_NAME, |
| 57 | + patch: modulePatchSpy as unknown, |
| 58 | + } as InstrumentationModuleDefinition<unknown>; |
| 59 | + |
| 60 | + // @ts-expect-error access internal property for testing |
| 61 | + instrumentation._onRequire<unknown>( |
| 62 | + instrumentationModule, |
| 63 | + moduleExports, |
| 64 | + MODULE_NAME, |
| 65 | + MODULE_DIR |
| 66 | + ); |
| 67 | + |
| 68 | + assert.strictEqual(instrumentationModule.moduleVersion, undefined); |
| 69 | + assert.strictEqual(instrumentationModule.moduleExports, undefined); |
| 70 | + sinon.assert.notCalled(modulePatchSpy); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('AND there is a wildcard supported version', () => { |
| 75 | + it('should patch module', () => { |
| 76 | + const moduleExports = {}; |
| 77 | + const instrumentationModule = { |
| 78 | + supportedVersions: [`^${MODULE_VERSION}`, WILDCARD_VERSION], |
| 79 | + name: MODULE_NAME, |
| 80 | + patch: modulePatchSpy as unknown, |
| 81 | + } as InstrumentationModuleDefinition<unknown>; |
| 82 | + |
| 83 | + // @ts-expect-error access internal property for testing |
| 84 | + instrumentation._onRequire<unknown>( |
| 85 | + instrumentationModule, |
| 86 | + moduleExports, |
| 87 | + MODULE_NAME, |
| 88 | + MODULE_DIR |
| 89 | + ); |
| 90 | + |
| 91 | + assert.strictEqual(instrumentationModule.moduleVersion, undefined); |
| 92 | + assert.strictEqual(instrumentationModule.moduleExports, moduleExports); |
| 93 | + sinon.assert.calledOnceWithExactly(modulePatchSpy, moduleExports, undefined); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('when patching module files', () => { |
| 99 | + let filePatchSpy: sinon.SinonSpy; |
| 100 | + |
| 101 | + beforeEach(() => { |
| 102 | + filePatchSpy = sinon.spy(); |
| 103 | + }) |
| 104 | + |
| 105 | + describe('AND there is no wildcard supported version', () => { |
| 106 | + it('should not patch module file', () => { |
| 107 | + const moduleExports = {}; |
| 108 | + const supportedVersions = [`^${MODULE_VERSION}`]; |
| 109 | + const instrumentationModule = { |
| 110 | + supportedVersions, |
| 111 | + name: MODULE_NAME, |
| 112 | + patch: modulePatchSpy as unknown, |
| 113 | + files: [{ |
| 114 | + name: MODULE_FILE_NAME, |
| 115 | + supportedVersions, |
| 116 | + patch: filePatchSpy as unknown |
| 117 | + }] |
| 118 | + } as InstrumentationModuleDefinition<unknown>; |
| 119 | + |
| 120 | + // @ts-expect-error access internal property for testing |
| 121 | + instrumentation._onRequire<unknown>( |
| 122 | + instrumentationModule, |
| 123 | + moduleExports, |
| 124 | + MODULE_FILE_NAME, |
| 125 | + MODULE_DIR |
| 126 | + ); |
| 127 | + |
| 128 | + assert.strictEqual(instrumentationModule.moduleVersion, undefined); |
| 129 | + assert.strictEqual(instrumentationModule.moduleExports, undefined); |
| 130 | + sinon.assert.notCalled(modulePatchSpy); |
| 131 | + sinon.assert.notCalled(filePatchSpy); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('AND there is a wildcard supported version', () => { |
| 136 | + it('should patch module file', () => { |
| 137 | + const moduleExports = {}; |
| 138 | + const supportedVersions = [`^${MODULE_VERSION}`, WILDCARD_VERSION]; |
| 139 | + const instrumentationModule = { |
| 140 | + supportedVersions, |
| 141 | + name: MODULE_NAME, |
| 142 | + patch: modulePatchSpy as unknown, |
| 143 | + files: [{ |
| 144 | + name: MODULE_FILE_NAME, |
| 145 | + supportedVersions, |
| 146 | + patch: filePatchSpy as unknown |
| 147 | + }] |
| 148 | + } as InstrumentationModuleDefinition<unknown>; |
| 149 | + |
| 150 | + // @ts-expect-error access internal property for testing |
| 151 | + instrumentation._onRequire<unknown>( |
| 152 | + instrumentationModule, |
| 153 | + moduleExports, |
| 154 | + MODULE_FILE_NAME, |
| 155 | + MODULE_DIR |
| 156 | + ); |
| 157 | + |
| 158 | + assert.strictEqual(instrumentationModule.moduleVersion, undefined); |
| 159 | + assert.strictEqual(instrumentationModule.files[0].moduleExports, moduleExports); |
| 160 | + sinon.assert.notCalled(modulePatchSpy); |
| 161 | + sinon.assert.calledOnceWithExactly(filePatchSpy, moduleExports, undefined); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments