Skip to content

Commit

Permalink
Remove rewiremock
Browse files Browse the repository at this point in the history
  • Loading branch information
paulacamargo25 committed May 14, 2024
1 parent 7b2177d commit 17f3dfe
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 1,616 deletions.
1,795 changes: 216 additions & 1,579 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@
}
],
"debugVisualizers": [
{
"id": "inlineHexDecoder",
"when": "debugConfigurationType == 'debugpy' && (variableType == 'float' || variableType == 'int')"
}
]
{
"id": "inlineHexDecoder",
"when": "debugConfigurationType == 'debugpy' && (variableType == 'float' || variableType == 'int')"
}
]
},
"extensionDependencies": [
"ms-python.python"
Expand Down Expand Up @@ -561,7 +561,6 @@
"glob": "^8.0.3",
"mocha": "^10.0.0",
"prettier": "^3.0.3",
"rewiremock": "^3.13.0",
"semver": "^7.5.4",
"sinon": "^15.0.2",
"ts-loader": "^9.3.1",
Expand Down
20 changes: 3 additions & 17 deletions src/extension/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import TelemetryReporter from '@vscode/extension-telemetry';

import { AppinsightsKey, isTestExecution } from '../common/constants';
import { isTestExecution } from '../common/constants';
import { StopWatch } from '../common/utils/stopWatch';
import { ConsoleType, TriggerType } from '../types';
import { DebugConfigurationType } from '../debugger/types';
import { EventName } from './constants';
import { isPromise } from '../common/utils/async';
import { getTelemetryReporter } from './reporter';

/**
* Checks whether telemetry is supported.
Expand All @@ -31,21 +32,6 @@ function isTelemetrySupported(): boolean {
const sharedProperties: Record<string, unknown> = {};

let telemetryReporter: TelemetryReporter | undefined;
function getTelemetryReporter() {
if (!isTestExecution() && telemetryReporter) {
return telemetryReporter;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const Reporter = require('@vscode/extension-telemetry').default as typeof TelemetryReporter;
telemetryReporter = new Reporter(AppinsightsKey, [
{
lookup: /(errorName|errorMessage|errorStack)/g,
},
]);

return telemetryReporter;
}

export function clearTelemetryReporter(): void {
telemetryReporter = undefined;
Expand All @@ -60,7 +46,7 @@ export function sendTelemetryEvent<P extends IEventNamePropertyMapping, E extend
if (isTestExecution() || !isTelemetrySupported()) {
return;
}
const reporter = getTelemetryReporter();
const reporter = getTelemetryReporter(telemetryReporter);
const measures =
typeof measuresOrDurationMs === 'number'
? { duration: measuresOrDurationMs }
Expand Down
21 changes: 21 additions & 0 deletions src/extension/telemetry/reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import TelemetryReporter from "@vscode/extension-telemetry";
import { AppinsightsKey, isTestExecution } from "../common/constants";

export function getTelemetryReporter(telemetryReporter: TelemetryReporter | undefined) {
if (!isTestExecution() && telemetryReporter) {
return telemetryReporter;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const Reporter = require('@vscode/extension-telemetry').default as typeof TelemetryReporter;
telemetryReporter = new Reporter(AppinsightsKey, [
{
lookup: /(errorName|errorMessage|errorStack)/g,
},
]);

return telemetryReporter;
}
32 changes: 19 additions & 13 deletions src/test/unittest/adapter/factory.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import * as sinon from 'sinon';
import rewiremock from 'rewiremock';
// import rewiremock from 'rewiremock';
import { SemVer } from 'semver';
import { instance, mock, when } from 'ts-mockito';
import { DebugAdapterExecutable, DebugAdapterServer, DebugConfiguration, DebugSession, WorkspaceFolder } from 'vscode';
import { IPersistentStateFactory } from '../../../extension/common/types';
import { DebugAdapterDescriptorFactory, debugStateKeys } from '../../../extension/debugger/adapter/factory';
import { IDebugAdapterDescriptorFactory } from '../../../extension/debugger/types';
import { clearTelemetryReporter } from '../../../extension/telemetry';
import { EventName } from '../../../extension/telemetry/constants';
import { PersistentState, PersistentStateFactory } from '../../../extension/common/persistentState';
import * as vscodeApi from '../../../extension/common/vscodeapi';
import { EXTENSION_ROOT_DIR } from '../../../extension/common/constants';
import { Architecture } from '../../../extension/common/platform';
import * as pythonApi from '../../../extension/common/python';
import * as telemetry from '../../../extension/telemetry';
import * as telemetryReporter from '../../../extension/telemetry/reporter';
import * as vscodeApi from '../../../extension/common/vscodeapi';
import { DebugConfigStrings } from '../../../extension/common/utils/localize';

use(chaiAsPromised);
Expand All @@ -36,6 +37,9 @@ suite('Debugging - Adapter Factory', () => {
let getInterpretersStub: sinon.SinonStub;
let getInterpreterDetailsStub: sinon.SinonStub;
let hasInterpretersStub: sinon.SinonStub;
// let sendTelemetryEventStub: sinon.SinonStub;
let getTelemetryReporterStub: sinon.SinonStub;
let reporter: any;

const nodeExecutable = undefined;
const debugAdapterPath = path.join(EXTENSION_ROOT_DIR, 'bundled', 'libs', 'debugpy', 'adapter');
Expand Down Expand Up @@ -65,33 +69,35 @@ suite('Debugging - Adapter Factory', () => {
setup(() => {
process.env.VSC_PYTHON_UNIT_TEST = undefined;
process.env.VSC_PYTHON_CI_TEST = undefined;
rewiremock.enable();
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
reporter = new Reporter();

stateFactory = mock(PersistentStateFactory);
state = mock(PersistentState) as PersistentState<boolean | undefined>;
showErrorMessageStub = sinon.stub(vscodeApi, 'showErrorMessage');
resolveEnvironmentStub = sinon.stub(pythonApi, 'resolveEnvironment');
getInterpretersStub = sinon.stub(pythonApi, 'getInterpreters');
getInterpreterDetailsStub = sinon.stub(pythonApi, 'getInterpreterDetails');
hasInterpretersStub = sinon.stub(pythonApi, 'hasInterpreters');
// sendTelemetryEventStub = sinon.stub(telemetry, 'sendTelemetryEvent');
getTelemetryReporterStub = sinon.stub(telemetryReporter, 'getTelemetryReporter');

when(
stateFactory.createGlobalPersistentState<boolean | undefined>(debugStateKeys.doNotShowAgain, false),
).thenReturn(instance(state));

getInterpretersStub.returns([interpreter]);
hasInterpretersStub.returns(true);
getTelemetryReporterStub.returns(reporter);
factory = new DebugAdapterDescriptorFactory(instance(stateFactory));
});

teardown(() => {
process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST;
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
Reporter.properties = [];
Reporter.eventNames = [];
Reporter.measures = [];
rewiremock.disable();
clearTelemetryReporter();
reporter.properties = [];
reporter.eventNames = [];
reporter.measures = [];
// rewiremock.disable();
telemetry.clearTelemetryReporter();
sinon.restore();
});

Expand Down Expand Up @@ -259,14 +265,14 @@ suite('Debugging - Adapter Factory', () => {
assert.deepStrictEqual(descriptor, debugExecutable);
});

test('Send attach to local process telemetry if attaching to a local process', async () => {
test.only('Send attach to local process telemetry if attaching to a local process', async () => {
const session = createSession({ request: 'attach', processId: 1234 });
getInterpreterDetailsStub.resolves({ path: [interpreter.path] });
resolveEnvironmentStub.withArgs(interpreter.path).resolves(interpreter);

await factory.createDebugAdapterDescriptor(session, nodeExecutable);

assert.ok(Reporter.eventNames.includes(EventName.DEBUGGER_ATTACH_TO_LOCAL_PROCESS));
assert.ok(reporter.eventNames.includes(EventName.DEBUGGER_ATTACH_TO_LOCAL_PROCESS));
});

test("Don't send any telemetry if not attaching to a local process", async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/test/unittest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export function run(): Promise<void> {
if ((Reflect as any).metadata === undefined) {
require('reflect-metadata');
}

process.env.VSC_PYTHON_UNIT_TEST = '1';
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
Expand All @@ -15,7 +17,7 @@ export function run(): Promise<void> {
const testsRoot = path.resolve(__dirname);

return new Promise((c, e) => {
glob('**/*.unit.test.js', { cwd: testsRoot }, (err: any, files: any[]) => {
glob('**/factory.unit.test.js', { cwd: testsRoot }, (err: any, files: any[]) => {
if (err) {
return e(err);
}
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
],
"rootDir": "src",
"strict": true, /* enable all strict type-checking options */
"types": [
"chai-as-promised"
],
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
Expand Down

0 comments on commit 17f3dfe

Please sign in to comment.