Skip to content

Commit

Permalink
Do not use readSync everytime telemetry is sent (#23011)
Browse files Browse the repository at this point in the history
For #22991

Introduced by #21377

cc/ @DonJayamanne
  • Loading branch information
Kartik Raj authored Mar 1, 2024
1 parent 50f4b7b commit c330ff9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
18 changes: 11 additions & 7 deletions src/client/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, unknown> = {};
Expand Down
24 changes: 0 additions & 24 deletions src/test/telemetry/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import * as fs from 'fs-extra';
import {
_resetSharedProperties,
clearTelemetryReporter,
isTelemetryDisabled,
sendTelemetryEvent,
setSharedProperty,
} from '../../client/telemetry';
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit c330ff9

Please sign in to comment.