diff --git a/build/azure-pipeline.pre-release.yml b/build/azure-pipeline.pre-release.yml index 5bc61b2fd559..7b13978b8db2 100644 --- a/build/azure-pipeline.pre-release.yml +++ b/build/azure-pipeline.pre-release.yml @@ -61,6 +61,10 @@ extends: python ./build/update_ext_version.py --for-publishing displayName: Update build number + - script: | + python ./build/update_package_file.py + displayName: Update telemetry in package.json + - script: npm run addExtensionPackDependencies displayName: Update optional extension dependencies diff --git a/build/azure-pipeline.stable.yml b/build/azure-pipeline.stable.yml index 159d856b6c3e..50ccbb9fff7a 100644 --- a/build/azure-pipeline.stable.yml +++ b/build/azure-pipeline.stable.yml @@ -63,6 +63,10 @@ extends: - script: | python ./build/update_ext_version.py --release --for-publishing displayName: Update build number + + - script: | + python ./build/update_package_file.py + displayName: Update telemetry in package.json - script: npm run addExtensionPackDependencies displayName: Update optional extension dependencies diff --git a/build/update_package_file.py b/build/update_package_file.py new file mode 100644 index 000000000000..b61460b4cc21 --- /dev/null +++ b/build/update_package_file.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +import pathlib +import sys + +EXT_ROOT = pathlib.Path(__file__).parent.parent +PACKAGE_JSON_PATH = EXT_ROOT / "package.json" + +def main(package_json: pathlib.Path) -> None: + package = json.loads(package_json.read_text(encoding="utf-8")) + package['enableTelemetry'] = True + + # Overwrite package.json with new data add a new-line at the end of the file. + package_json.write_text( + json.dumps(package, indent=4, ensure_ascii=False) + "\n", encoding="utf-8" + ) + +if __name__ == "__main__": + main(PACKAGE_JSON_PATH, sys.argv[1:]) diff --git a/package.json b/package.json index d76ba16ee16d..8d9c14724438 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "engines": { "vscode": "^1.79.0-20230526" }, + "enableTelemetry": false, "keywords": [ "python", "django", diff --git a/src/client/telemetry/index.ts b/src/client/telemetry/index.ts index d0b9d463c070..20df3a21eb37 100644 --- a/src/client/telemetry/index.ts +++ b/src/client/telemetry/index.ts @@ -4,9 +4,10 @@ import TelemetryReporter from '@vscode/extension-telemetry'; +import * as path from 'path'; +import * as fs from 'fs-extra'; import { DiagnosticCodes } from '../application/diagnostics/constants'; -import { IWorkspaceService } from '../common/application/types'; -import { AppinsightsKey, isTestExecution, isUnitTestExecution } from '../common/constants'; +import { AppinsightsKey, EXTENSION_ROOT_DIR, isTestExecution, isUnitTestExecution } from '../common/constants'; import type { TerminalShellType } from '../common/terminal/types'; import { StopWatch } from '../common/utils/stopWatch'; import { isPromise } from '../common/utils/async'; @@ -41,12 +42,13 @@ function isTelemetrySupported(): boolean { } /** - * Checks if the telemetry is disabled in user settings + * Checks if the telemetry is disabled * @returns {boolean} */ -export function isTelemetryDisabled(workspaceService: IWorkspaceService): boolean { - const settings = workspaceService.getConfiguration('telemetry').inspect('enableTelemetry')!; - return settings.globalValue === false; +export function isTelemetryDisabled(): boolean { + const packageJsonPath = path.join(EXTENSION_ROOT_DIR, 'package.json'); + const packageJson = fs.readJSONSync(packageJsonPath); + return !packageJson.enableTelemetry; } const sharedProperties: Record = {}; @@ -101,7 +103,7 @@ export function sendTelemetryEvent

{ let stateFactory: IPersistentStateFactory; let state: PersistentState; let showErrorMessageStub: sinon.SinonStub; + let readJSONSyncStub: sinon.SinonStub; let commandManager: ICommandManager; const nodeExecutable = undefined; @@ -66,6 +68,8 @@ suite('Debugging - Adapter Factory', () => { setup(() => { process.env.VSC_PYTHON_UNIT_TEST = undefined; process.env.VSC_PYTHON_CI_TEST = undefined; + readJSONSyncStub = sinon.stub(fs, 'readJSONSync'); + readJSONSyncStub.returns({ enableTelemetry: true }); rewiremock.enable(); rewiremock('@vscode/extension-telemetry').with({ default: Reporter }); stateFactory = mock(PersistentStateFactory); diff --git a/src/test/telemetry/index.unit.test.ts b/src/test/telemetry/index.unit.test.ts index 00272aad1f64..d369dce27855 100644 --- a/src/test/telemetry/index.unit.test.ts +++ b/src/test/telemetry/index.unit.test.ts @@ -4,12 +4,9 @@ import { expect } from 'chai'; import rewiremock from 'rewiremock'; -import * as TypeMoq from 'typemoq'; +import * as sinon from 'sinon'; +import * as fs from 'fs-extra'; -import { instance, mock, verify, when } from 'ts-mockito'; -import { WorkspaceConfiguration } from 'vscode'; -import { IWorkspaceService } from '../../client/common/application/types'; -import { WorkspaceService } from '../../client/common/application/workspace'; import { _resetSharedProperties, clearTelemetryReporter, @@ -19,9 +16,9 @@ import { } from '../../client/telemetry'; suite('Telemetry', () => { - let workspaceService: IWorkspaceService; const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST; const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST; + let readJSONSyncStub: sinon.SinonStub; class Reporter { public static eventName: string[] = []; @@ -48,9 +45,10 @@ suite('Telemetry', () => { } setup(() => { - workspaceService = mock(WorkspaceService); process.env.VSC_PYTHON_UNIT_TEST = undefined; process.env.VSC_PYTHON_CI_TEST = undefined; + readJSONSyncStub = sinon.stub(fs, 'readJSONSync'); + readJSONSyncStub.returns({ enableTelemetry: true }); clearTelemetryReporter(); Reporter.clear(); }); @@ -59,35 +57,28 @@ suite('Telemetry', () => { process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST; rewiremock.disable(); _resetSharedProperties(); + sinon.restore(); }); const testsForisTelemetryDisabled = [ { - testName: 'Returns true when globalValue is set to false', - settings: { globalValue: false }, - expectedResult: true, + testName: 'Returns true', + settings: { enableTelemetry: true }, + expectedResult: false, }, { - testName: 'Returns false otherwise', - settings: {}, - expectedResult: false, + testName: 'Returns false ', + settings: { enableTelemetry: false }, + expectedResult: true, }, ]; suite('Function isTelemetryDisabled()', () => { testsForisTelemetryDisabled.forEach((testParams) => { test(testParams.testName, async () => { - const workspaceConfig = TypeMoq.Mock.ofType(); - when(workspaceService.getConfiguration('telemetry')).thenReturn(workspaceConfig.object); - workspaceConfig - .setup((c) => c.inspect('enableTelemetry')) - .returns(() => testParams.settings as any) - .verifiable(TypeMoq.Times.once()); - - expect(isTelemetryDisabled(instance(workspaceService))).to.equal(testParams.expectedResult); - - verify(workspaceService.getConfiguration('telemetry')).once(); - workspaceConfig.verifyAll(); + readJSONSyncStub.returns(testParams.settings); + expect(isTelemetryDisabled()).to.equal(testParams.expectedResult); + sinon.assert.calledOnce(readJSONSyncStub); }); }); });