From c330ff9678e5d0735b4c4b318315bcc1708fc9ca Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Fri, 1 Mar 2024 22:43:04 +0530 Subject: [PATCH] Do not use readSync everytime telemetry is sent (#23011) For https://github.com/microsoft/vscode-python/issues/22991 Introduced by https://github.com/microsoft/vscode-python/pull/21377 cc/ @DonJayamanne --- src/client/telemetry/index.ts | 18 +++++++++++------- src/test/telemetry/index.unit.test.ts | 24 ------------------------ 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/client/telemetry/index.ts b/src/client/telemetry/index.ts index bdb098be520e..3237bafc224b 100644 --- a/src/client/telemetry/index.ts +++ b/src/client/telemetry/index.ts @@ -3,11 +3,9 @@ // Licensed under the MIT License. import TelemetryReporter from '@vscode/extension-telemetry'; - -import * as path from 'path'; -import * as fs from 'fs-extra'; +import type * as vscodeTypes from 'vscode'; import { DiagnosticCodes } from '../application/diagnostics/constants'; -import { AppinsightsKey, EXTENSION_ROOT_DIR, isTestExecution, isUnitTestExecution } from '../common/constants'; +import { AppinsightsKey, isTestExecution, isUnitTestExecution, PVSC_EXTENSION_ID } from '../common/constants'; import type { TerminalShellType } from '../common/terminal/types'; import { StopWatch } from '../common/utils/stopWatch'; import { isPromise } from '../common/utils/async'; @@ -39,14 +37,20 @@ function isTelemetrySupported(): boolean { } } +// eslint-disable-next-line @typescript-eslint/no-explicit-any +let packageJSON: any; + /** * Checks if the telemetry is disabled * @returns {boolean} */ export function isTelemetryDisabled(): boolean { - const packageJsonPath = path.join(EXTENSION_ROOT_DIR, 'package.json'); - const packageJson = fs.readJSONSync(packageJsonPath); - return !packageJson.enableTelemetry; + if (!packageJSON) { + const vscode = require('vscode') as typeof vscodeTypes; + const pythonExtension = vscode.extensions.getExtension(PVSC_EXTENSION_ID)!; + packageJSON = pythonExtension.packageJSON; + } + return !packageJSON.enableTelemetry; } const sharedProperties: Record = {}; diff --git a/src/test/telemetry/index.unit.test.ts b/src/test/telemetry/index.unit.test.ts index d369dce27855..e155ac0a2092 100644 --- a/src/test/telemetry/index.unit.test.ts +++ b/src/test/telemetry/index.unit.test.ts @@ -10,7 +10,6 @@ import * as fs from 'fs-extra'; import { _resetSharedProperties, clearTelemetryReporter, - isTelemetryDisabled, sendTelemetryEvent, setSharedProperty, } from '../../client/telemetry'; @@ -60,29 +59,6 @@ suite('Telemetry', () => { sinon.restore(); }); - const testsForisTelemetryDisabled = [ - { - testName: 'Returns true', - settings: { enableTelemetry: true }, - expectedResult: false, - }, - { - testName: 'Returns false ', - settings: { enableTelemetry: false }, - expectedResult: true, - }, - ]; - - suite('Function isTelemetryDisabled()', () => { - testsForisTelemetryDisabled.forEach((testParams) => { - test(testParams.testName, async () => { - readJSONSyncStub.returns(testParams.settings); - expect(isTelemetryDisabled()).to.equal(testParams.expectedResult); - sinon.assert.calledOnce(readJSONSyncStub); - }); - }); - }); - test('Send Telemetry', () => { rewiremock.enable(); rewiremock('@vscode/extension-telemetry').with({ default: Reporter });