Skip to content

Commit 8f94c15

Browse files
Remove rewiremock
1 parent 05a3079 commit 8f94c15

File tree

7 files changed

+270
-1616
lines changed

7 files changed

+270
-1616
lines changed

package-lock.json

Lines changed: 216 additions & 1579 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,11 @@
517517
}
518518
],
519519
"debugVisualizers": [
520-
{
521-
"id": "inlineHexDecoder",
522-
"when": "debugConfigurationType == 'debugpy' && (variableType == 'float' || variableType == 'int')"
523-
}
524-
]
520+
{
521+
"id": "inlineHexDecoder",
522+
"when": "debugConfigurationType == 'debugpy' && (variableType == 'float' || variableType == 'int')"
523+
}
524+
]
525525
},
526526
"extensionDependencies": [
527527
"ms-python.python"
@@ -561,7 +561,6 @@
561561
"glob": "^8.0.3",
562562
"mocha": "^10.0.0",
563563
"prettier": "^3.0.3",
564-
"rewiremock": "^3.13.0",
565564
"semver": "^7.5.4",
566565
"sinon": "^15.0.2",
567566
"ts-loader": "^9.3.1",

src/extension/telemetry/index.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

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

7-
import { AppinsightsKey, isTestExecution } from '../common/constants';
7+
import { isTestExecution } from '../common/constants';
88
import { StopWatch } from '../common/utils/stopWatch';
99
import { ConsoleType, TriggerType } from '../types';
1010
import { DebugConfigurationType } from '../debugger/types';
1111
import { EventName } from './constants';
1212
import { isPromise } from '../common/utils/async';
13+
import { getTelemetryReporter } from './reporter';
1314

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

3334
let telemetryReporter: TelemetryReporter | undefined;
34-
function getTelemetryReporter() {
35-
if (!isTestExecution() && telemetryReporter) {
36-
return telemetryReporter;
37-
}
38-
39-
// eslint-disable-next-line @typescript-eslint/naming-convention
40-
const Reporter = require('@vscode/extension-telemetry').default as typeof TelemetryReporter;
41-
telemetryReporter = new Reporter(AppinsightsKey, [
42-
{
43-
lookup: /(errorName|errorMessage|errorStack)/g,
44-
},
45-
]);
46-
47-
return telemetryReporter;
48-
}
4935

5036
export function clearTelemetryReporter(): void {
5137
telemetryReporter = undefined;
@@ -60,7 +46,7 @@ export function sendTelemetryEvent<P extends IEventNamePropertyMapping, E extend
6046
if (isTestExecution() || !isTelemetrySupported()) {
6147
return;
6248
}
63-
const reporter = getTelemetryReporter();
49+
const reporter = getTelemetryReporter(telemetryReporter);
6450
const measures =
6551
typeof measuresOrDurationMs === 'number'
6652
? { duration: measuresOrDurationMs }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import TelemetryReporter from "@vscode/extension-telemetry";
5+
import { AppinsightsKey, isTestExecution } from "../common/constants";
6+
7+
export function getTelemetryReporter(telemetryReporter: TelemetryReporter | undefined) {
8+
if (!isTestExecution() && telemetryReporter) {
9+
return telemetryReporter;
10+
}
11+
12+
// eslint-disable-next-line @typescript-eslint/naming-convention
13+
const Reporter = require('@vscode/extension-telemetry').default as typeof TelemetryReporter;
14+
telemetryReporter = new Reporter(AppinsightsKey, [
15+
{
16+
lookup: /(errorName|errorMessage|errorStack)/g,
17+
},
18+
]);
19+
20+
return telemetryReporter;
21+
}

src/test/unittest/adapter/factory.unit.test.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ import { expect, use } from 'chai';
99
import * as chaiAsPromised from 'chai-as-promised';
1010
import * as path from 'path';
1111
import * as sinon from 'sinon';
12-
import rewiremock from 'rewiremock';
12+
// import rewiremock from 'rewiremock';
1313
import { SemVer } from 'semver';
1414
import { instance, mock, when } from 'ts-mockito';
1515
import { DebugAdapterExecutable, DebugAdapterServer, DebugConfiguration, DebugSession, WorkspaceFolder } from 'vscode';
1616
import { IPersistentStateFactory } from '../../../extension/common/types';
1717
import { DebugAdapterDescriptorFactory, debugStateKeys } from '../../../extension/debugger/adapter/factory';
1818
import { IDebugAdapterDescriptorFactory } from '../../../extension/debugger/types';
19-
import { clearTelemetryReporter } from '../../../extension/telemetry';
2019
import { EventName } from '../../../extension/telemetry/constants';
2120
import { PersistentState, PersistentStateFactory } from '../../../extension/common/persistentState';
22-
import * as vscodeApi from '../../../extension/common/vscodeapi';
2321
import { EXTENSION_ROOT_DIR } from '../../../extension/common/constants';
2422
import { Architecture } from '../../../extension/common/platform';
2523
import * as pythonApi from '../../../extension/common/python';
24+
import * as telemetry from '../../../extension/telemetry';
25+
import * as telemetryReporter from '../../../extension/telemetry/reporter';
26+
import * as vscodeApi from '../../../extension/common/vscodeapi';
2627
import { DebugConfigStrings } from '../../../extension/common/utils/localize';
2728

2829
use(chaiAsPromised);
@@ -36,6 +37,9 @@ suite('Debugging - Adapter Factory', () => {
3637
let getInterpretersStub: sinon.SinonStub;
3738
let getInterpreterDetailsStub: sinon.SinonStub;
3839
let hasInterpretersStub: sinon.SinonStub;
40+
// let sendTelemetryEventStub: sinon.SinonStub;
41+
let getTelemetryReporterStub: sinon.SinonStub;
42+
let reporter: any;
3943

4044
const nodeExecutable = undefined;
4145
const debugAdapterPath = path.join(EXTENSION_ROOT_DIR, 'bundled', 'libs', 'debugpy', 'adapter');
@@ -65,33 +69,35 @@ suite('Debugging - Adapter Factory', () => {
6569
setup(() => {
6670
process.env.VSC_PYTHON_UNIT_TEST = undefined;
6771
process.env.VSC_PYTHON_CI_TEST = undefined;
68-
rewiremock.enable();
69-
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
72+
reporter = new Reporter();
73+
7074
stateFactory = mock(PersistentStateFactory);
7175
state = mock(PersistentState) as PersistentState<boolean | undefined>;
7276
showErrorMessageStub = sinon.stub(vscodeApi, 'showErrorMessage');
7377
resolveEnvironmentStub = sinon.stub(pythonApi, 'resolveEnvironment');
7478
getInterpretersStub = sinon.stub(pythonApi, 'getInterpreters');
7579
getInterpreterDetailsStub = sinon.stub(pythonApi, 'getInterpreterDetails');
7680
hasInterpretersStub = sinon.stub(pythonApi, 'hasInterpreters');
81+
// sendTelemetryEventStub = sinon.stub(telemetry, 'sendTelemetryEvent');
82+
getTelemetryReporterStub = sinon.stub(telemetryReporter, 'getTelemetryReporter');
7783

7884
when(
7985
stateFactory.createGlobalPersistentState<boolean | undefined>(debugStateKeys.doNotShowAgain, false),
8086
).thenReturn(instance(state));
81-
8287
getInterpretersStub.returns([interpreter]);
8388
hasInterpretersStub.returns(true);
89+
getTelemetryReporterStub.returns(reporter);
8490
factory = new DebugAdapterDescriptorFactory(instance(stateFactory));
8591
});
8692

8793
teardown(() => {
8894
process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST;
8995
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
90-
Reporter.properties = [];
91-
Reporter.eventNames = [];
92-
Reporter.measures = [];
93-
rewiremock.disable();
94-
clearTelemetryReporter();
96+
reporter.properties = [];
97+
reporter.eventNames = [];
98+
reporter.measures = [];
99+
// rewiremock.disable();
100+
telemetry.clearTelemetryReporter();
95101
sinon.restore();
96102
});
97103

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

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

267273
await factory.createDebugAdapterDescriptor(session, nodeExecutable);
268274

269-
assert.ok(Reporter.eventNames.includes(EventName.DEBUGGER_ATTACH_TO_LOCAL_PROCESS));
275+
assert.ok(reporter.eventNames.includes(EventName.DEBUGGER_ATTACH_TO_LOCAL_PROCESS));
270276
});
271277

272278
test("Don't send any telemetry if not attaching to a local process", async () => {

src/test/unittest/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export function run(): Promise<void> {
66
if ((Reflect as any).metadata === undefined) {
77
require('reflect-metadata');
88
}
9+
10+
process.env.VSC_PYTHON_UNIT_TEST = '1';
911
// Create the mocha test
1012
const mocha = new Mocha({
1113
ui: 'tdd',
@@ -15,7 +17,7 @@ export function run(): Promise<void> {
1517
const testsRoot = path.resolve(__dirname);
1618

1719
return new Promise((c, e) => {
18-
glob('**/*.unit.test.js', { cwd: testsRoot }, (err: any, files: any[]) => {
20+
glob('**/factory.unit.test.js', { cwd: testsRoot }, (err: any, files: any[]) => {
1921
if (err) {
2022
return e(err);
2123
}

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
],
1616
"rootDir": "src",
1717
"strict": true, /* enable all strict type-checking options */
18+
"types": [
19+
"chai-as-promised"
20+
],
1821
"experimentalDecorators": true,
1922
"allowSyntheticDefaultImports": true,
2023
"noImplicitAny": true,

0 commit comments

Comments
 (0)